BaseUtil.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. Ext.define('saas.util.BaseUtil', {
  2. statics: {
  3. /**
  4. * 打开/切换到新页签
  5. * @param xtype: view xtype
  6. * @param title: 标题
  7. * @param id: id
  8. * @param config: 绑定到view的其他配置
  9. */
  10. openTab: function (xtype, title, id, config) {
  11. var mainTab = Ext.getCmp('main-tab-panel');
  12. var panel = mainTab.query('[tabId="' + id + '"]')[0];
  13. if (!panel) {
  14. panel = Ext.create('saas.view.core.tab.Panel', {
  15. tabId: id,
  16. title: title,
  17. viewType: xtype,
  18. viewConfig: config
  19. });
  20. Ext.suspendLayouts();
  21. mainTab.setActiveTab(mainTab.add(panel));
  22. Ext.resumeLayouts(true);
  23. } else {
  24. panel.viewConfig = config;
  25. mainTab.setActiveTab(panel);
  26. }
  27. },
  28. /**
  29. * 重设tab标题
  30. */
  31. refreshTabTitle: function (id, title) {
  32. var currentTab = this.getCurrentTab();
  33. currentTab.tabId = id;
  34. currentTab.setTitle(title);
  35. },
  36. /**
  37. * 获得当前Tab
  38. */
  39. getCurrentTab: function () {
  40. var mainTab = Ext.getCmp('main-tab-panel');
  41. var currentTab = mainTab.getActiveTab();
  42. return currentTab;
  43. },
  44. /**
  45. * 显示toast提示(无需用户操作)
  46. * @param content: 内容
  47. * @param title: 标题
  48. *
  49. */
  50. showToast: function (content, title) {
  51. Ext.toast({
  52. html: content,
  53. title: title,
  54. closable: false,
  55. align: 't',
  56. slideDUration: 400,
  57. maxWidth: 400
  58. });
  59. },
  60. /**
  61. * 显示警告(需要选择是、否)
  62. * @param title: 标题
  63. * @param message: 内容
  64. * @return : Promise
  65. */
  66. showConfirm: function (title, message) {
  67. return new Ext.Promise(function (resolve, reject) {
  68. Ext.MessageBox.confirm(title, message, function (buttonId) {
  69. return resolve(buttonId);
  70. });
  71. })
  72. },
  73. warnMsg: function (msg, fn) {
  74. Ext.MessageBox.show({
  75. title: '提示',
  76. msg: msg,
  77. buttons: Ext.Msg.YESNO,
  78. icon: Ext.Msg.WARNING,
  79. fn: fn
  80. });
  81. },
  82. deleteWarn: function (msg, fn) {
  83. Ext.MessageBox.show({
  84. title: '提示',
  85. msg: msg || '确定要删除当前表单?',
  86. buttons: Ext.Msg.YESNO,
  87. icon: Ext.Msg.WARNING,
  88. fn: fn,
  89. renderTo: Ext.getCmp('main-tab-panel').getActiveTab().getEl()
  90. });
  91. },
  92. /**
  93. * 判断字符串是否日期字符串
  94. * 需要满足格式 yyyy-MM-dd hh:mm:ss
  95. * 或者yyyy-M-d h:m:s
  96. * @param str: 字符串
  97. * @returns Boolean
  98. */
  99. isDateString: function (str) {
  100. return (/^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/).test(str);
  101. },
  102. getCurrentUse: function () {
  103. return saas.util.State.get('session').account;
  104. },
  105. /**
  106. * 发起Ajax请求
  107. * @param config: 请求参数
  108. */
  109. request: function (config) {
  110. var url = config.url,
  111. params = config.params,
  112. async = config.async || true,
  113. method = config.method || 'GET',
  114. timeout = config.timeout || 8000,
  115. defaultHeaders = {
  116. 'Access-Control-Allow-Origin': '*',
  117. "Content-Type": 'application/json;charset=UTF-8'
  118. };
  119. return new Ext.Promise(function (resolve, reject) {
  120. Ext.Ajax.request({
  121. url: url,
  122. params: params,
  123. async: async,
  124. method: method,
  125. timeout: timeout,
  126. headers: Ext.apply(defaultHeaders, config.headers),
  127. success: function (response, opts) {
  128. var res = Ext.decode(response.responseText);
  129. if (res.success) {
  130. return resolve(res);
  131. } else {
  132. console.error('server request failure with code ' + res.code + '.');
  133. console.error('failure message: ' + res.message);
  134. return reject(res);
  135. }
  136. },
  137. failure: function (response, opts) {
  138. console.error('server-side failure with status code ' + response);
  139. return reject(response);
  140. }
  141. });
  142. })
  143. },
  144. }
  145. });