BaseUtil.js 8.0 KB

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