Ext.define('saas.util.BaseUtil', { statics: { /** * 打开/切换到新页签 * @param xtype: view xtype * @param title: 标题 * @param id: id * @param config: 绑定到view的其他配置 */ openTab: function (xtype, title, id, config) { var mainTab = Ext.getCmp('main-tab-panel'); var panel = mainTab.query('[tabId="' + id + '"]')[0]; if (!panel) { panel = Ext.create('saas.view.core.tab.Panel', { tabId: id, title: title, viewType: xtype, viewConfig: config }); Ext.suspendLayouts(); mainTab.setActiveTab(mainTab.add(panel)); Ext.resumeLayouts(true); } else { panel.viewConfig = config; mainTab.setActiveTab(panel); } }, openWin: function(xtype, title, id, config) { var mainTab = Ext.getCmp('main-tab-panel'); var win = mainTab.query('[winId="' + id + '"]')[0]; if (!win) { win = Ext.create('saas.view.core.window.Window', { tabId: id, title: title, viewType: xtype, viewConfig: config }); } else { win.viewConfig = config; } win.show(); }, /** * 重设tab标题 */ refreshTabTitle: function (id, title) { var currentTab = this.getCurrentTab(); currentTab.tabId = id; currentTab.setTitle(title); }, /** * 获得当前Tab */ getCurrentTab: function () { var mainTab = Ext.getCmp('main-tab-panel'); var currentTab = mainTab.getActiveTab(); return currentTab; }, /** * 显示toast提示(无需用户操作) * @param content: 内容 * @param title: 标题 * */ showSuccessToast: function (content, title, configs) { Ext.toast(Ext.Object.merge({ cls: 'x-toast-success', html: content, title: title, closable: false, align: 't', autoCloseDelay: 3000, autoClose: true, maxWidth: 400 }, configs)); }, showErrorToast: function (content, title, configs) { Ext.toast(Ext.Object.merge({ cls: 'x-toast-error', html: content, title: title, closable: false, align: 't', autoCloseDelay: 3000, autoClose: true, maxWidth: 400 }, configs)); }, /** * 显示警告(需要选择是、否) * @param title: 标题 * @param message: 内容 * @return : Promise */ showConfirm: function (title, message, config) { return new Ext.Promise(function (resolve, reject) { Ext.MessageBox.show(Ext.Object.merge({ title: title, msg: message, buttons: Ext.Msg.YESNO, icon: Ext.Msg.QUESTION, fn: function (buttonId) { return resolve(buttonId); } }, config)); }) }, warnMsg: function (msg, fn) { Ext.MessageBox.show({ title: '提示', msg: msg, buttons: Ext.Msg.YESNO, icon: Ext.Msg.WARNING, fn: fn }); }, deleteWarn: function (msg, fn) { Ext.MessageBox.show({ title: '提示', msg: msg || '确定要删除当前表单?', buttons: Ext.Msg.YESNO, icon: Ext.Msg.WARNING, fn: fn, renderTo: Ext.getCmp('main-tab-panel').getActiveTab().getEl() }); }, /** * 判断字符串是否日期字符串 * 需要满足格式 yyyy-MM-dd hh:mm:ss * 或者yyyy-M-d h:m:s * @param str: 字符串 * @returns Boolean */ isDateString: function (str) { return (/^(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/).test(str); }, getCurrentUser: function () { return saas.util.State.get('session') ? (saas.util.State.get('session').account || {}) : {}; }, /** * 发起Ajax请求 * @param config: 请求参数 */ request: function (config) { var url = config.url, params = config.params, async = (Ext.isBoolean(config.async) ? config.async : true), method = config.method || 'GET', timeout = config.timeout || 8000, defaultHeaders = { 'Access-Control-Allow-Origin': '*', "Content-Type": 'application/json;charset=UTF-8' }; return new Ext.Promise(function (resolve, reject) { Ext.Ajax.request({ url: url, params: params, async: async, method: method, timeout: timeout, headers: Ext.apply(defaultHeaders, config.headers), success: function (response, opts) { var res = Ext.decode(response.responseText); if (res.success) { return resolve(res); } else { res.message = res.message || '未知错误'; return reject(res); } }, failure: function (response, opts) { var res; try{ res = JSON.parse(response.responseText); }catch(e) { res = response.responseJson || {}; } console.error('ajax request failure: ', res); res.message = res.message || '未知错误'; throw new Error(res.message); } }); }) }, /** * 格式化数字 * @param value: 需要格式化的数字 * @param decimalPrecision: 最大小数位数 * @param thousandSeparator: 显示千分位分隔符 */ numberFormat: function (value, decimalPrecision, thousandSeparator) { value = Number(value) || 0; decimalPrecision = Ext.isNumber(decimalPrecision) ? decimalPrecision : 2; thousandSeparator = !!thousandSeparator; var f1 = thousandSeparator ? '0,000' : '0'; var arr = (value + '.').split('.'); var xr = (new Array(arr[1].length > decimalPrecision ? decimalPrecision : arr[1].length)).fill('0'); var format = f1 + '.' + xr.join(''); return Ext.util.Format.number(value, format); }, showLoginWin: function() { var main = Ext.getCmp('rootView'), win = Ext.getCmp('relogin'); if(!win) { win = main.add({ xtype: 'relogin' }); } win.show(); }, hashcode: function(obj) { let str = JSON.stringify(obj); let hash = 0, i, chr, len; if (str.length === 0) return hash; for (i = 0, len = str.length; i < len; i++) { chr = str.charCodeAt(i); hash = ((hash << 5) - hash) + chr; hash |= 0; } return hash; } } });