BaseUtil.js 8.1 KB

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