BaseUtil.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. showSuccessToast: function (content, title, configs) {
  51. Ext.toast(Ext.Object.merge({
  52. cls: 'x-toast-success',
  53. html: content,
  54. title: title,
  55. closable: false,
  56. align: 't',
  57. autoCloseDelay: 3000,
  58. autoClose: true,
  59. maxWidth: 400
  60. }, configs));
  61. },
  62. showErrorToast: function (content, title, configs) {
  63. Ext.toast(Ext.Object.merge({
  64. cls: 'x-toast-error',
  65. html: content,
  66. title: title,
  67. closable: false,
  68. align: 't',
  69. autoCloseDelay: 3000,
  70. autoClose: true,
  71. maxWidth: 400
  72. }, configs));
  73. },
  74. /**
  75. * 显示警告(需要选择是、否)
  76. * @param title: 标题
  77. * @param message: 内容
  78. * @return : Promise
  79. */
  80. showConfirm: function (title, message, config) {
  81. return new Ext.Promise(function (resolve, reject) {
  82. Ext.MessageBox.show(Ext.Object.merge({
  83. title: title,
  84. msg: message,
  85. buttons: Ext.Msg.YESNO,
  86. icon: Ext.Msg.QUESTION,
  87. fn: function (buttonId) {
  88. return resolve(buttonId);
  89. }
  90. }, config));
  91. })
  92. },
  93. warnMsg: function (msg, fn) {
  94. Ext.MessageBox.show({
  95. title: '提示',
  96. msg: msg,
  97. buttons: Ext.Msg.YESNO,
  98. icon: Ext.Msg.WARNING,
  99. fn: fn
  100. });
  101. },
  102. deleteWarn: function (msg, fn) {
  103. Ext.MessageBox.show({
  104. title: '提示',
  105. msg: msg || '确定要删除当前表单?',
  106. buttons: Ext.Msg.YESNO,
  107. icon: Ext.Msg.WARNING,
  108. fn: fn,
  109. renderTo: Ext.getCmp('main-tab-panel').getActiveTab().getEl()
  110. });
  111. },
  112. /**
  113. * 判断字符串是否日期字符串
  114. * 需要满足格式 yyyy-MM-dd hh:mm:ss
  115. * 或者yyyy-M-d h:m:s
  116. * @param str: 字符串
  117. * @returns Boolean
  118. */
  119. isDateString: function (str) {
  120. return (/^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/).test(str);
  121. },
  122. getCurrentUser: function () {
  123. return saas.util.State.get('session') ? (saas.util.State.get('session').account || {}) : {};
  124. },
  125. /**
  126. * 发起Ajax请求
  127. * @param config: 请求参数
  128. */
  129. request: function (config) {
  130. var url = config.url,
  131. params = config.params,
  132. async = (Ext.isBoolean(config.async) ? config.async : true),
  133. method = config.method || 'GET',
  134. timeout = config.timeout || 8000,
  135. defaultHeaders = {
  136. 'Access-Control-Allow-Origin': '*',
  137. "Content-Type": 'application/json;charset=UTF-8'
  138. };
  139. return new Ext.Promise(function (resolve, reject) {
  140. Ext.Ajax.request({
  141. url: url,
  142. params: params,
  143. async: async,
  144. method: method,
  145. timeout: timeout,
  146. headers: Ext.apply(defaultHeaders, config.headers),
  147. success: function (response, opts) {
  148. var res = Ext.decode(response.responseText);
  149. if (res.success) {
  150. return resolve(res);
  151. } else {
  152. res.message = res.message || '未知错误';
  153. return reject(res);
  154. }
  155. },
  156. failure: function (response, opts) {
  157. var res;
  158. try{
  159. res = JSON.parse(response.responseText);
  160. }catch(e) {
  161. res = response.responseJson || {};
  162. }
  163. console.error('ajax request failure: ', res);
  164. res.message = res.message || '未知错误';
  165. throw new Error(res.message);
  166. }
  167. });
  168. })
  169. },
  170. /**
  171. * 格式化数字
  172. * @param value: 需要格式化的数字
  173. * @param decimalPrecision: 最大小数位数
  174. * @param thousandSeparator: 显示千分位分隔符
  175. */
  176. numberFormat: function (value, decimalPrecision, thousandSeparator) {
  177. value = Number(value) || 0;
  178. decimalPrecision = Ext.isNumber(decimalPrecision) ? decimalPrecision : 2;
  179. thousandSeparator = !!thousandSeparator;
  180. var f1 = thousandSeparator ? '0,000' : '0';
  181. var arr = (value + '.').split('.');
  182. var xr = (new Array(arr[1].length > decimalPrecision ? decimalPrecision : arr[1].length)).fill('0');
  183. var format = f1 + '.' + xr.join('');
  184. return Ext.util.Format.number(value, format);
  185. },
  186. showLoginWin: function() {
  187. var main = Ext.getCmp('rootView'),
  188. win = Ext.getCmp('relogin');
  189. if(!win) {
  190. win = main.add({
  191. xtype: 'relogin'
  192. });
  193. }
  194. win.show();
  195. },
  196. hashcode: function(obj) {
  197. let str = JSON.stringify(obj);
  198. let hash = 0,
  199. i, chr, len;
  200. if (str.length === 0) return hash;
  201. for (i = 0, len = str.length; i < len; i++) {
  202. chr = str.charCodeAt(i);
  203. hash = ((hash << 5) - hash) + chr;
  204. hash |= 0;
  205. }
  206. return hash;
  207. }
  208. }
  209. });