/** * 在 ajax request 前修改url,增加服务端`basePath` * `basePath`在`app.json`配置 */ Ext.define('saas.override.data.Connection', { override: 'Ext.data.Connection', urlRegexp: /(http|ftp|https):\/\//, config: { /** * @cfg {Object} defaultServerHeaders * 与defaultHeaders有区别,只在调用server api的时候才添加的headers */ defaultServerHeaders: null }, privates: { parseBasePath: function (basePath) { if (Ext.isObject(basePath)) { var protocol = window.location.protocol.split(":")[0]; if (protocol === 'file') { protocol = 'https'; } return basePath[protocol]; } return basePath; }, setupServerOptions: function (options) { var serverOptions = Ext.manifest.server, originUrl = options.url; if (serverOptions && serverOptions.basePath && !this.urlRegexp.test(originUrl) && (!serverOptions.urlPattern || new RegExp(serverOptions.urlPattern).test(originUrl))) { Ext.Object.merge(options, { url: this.parseBasePath(serverOptions.basePath) + (originUrl.indexOf('/') == 0 ? '' : '/') + originUrl, headers: this.getDefaultServerHeaders() || {} }); } }, parseResponseStatus: function (status) { switch (status) { case 0: return "无法连接服务"; } } }, request: function (options) { if (options && options.url) { this.setupServerOptions(options); if (options.headers && !options.headers['Authorization']) { // 没有身份信息的情况下,尝试使用cookie信息 options.withCredentials = true; } } return this.callParent([options]); }, onRequestComplete: function (request) { let headers = request.result.getAllResponseHeaders() || {}, Authorization = headers['authorization'], result = request.result, res; try { res = JSON.parse(result.responseText); } catch (e) { res = result.responseJson // console.error(e); } if (res && res.code == 40001) { // 如果token超时则显示重新登陆弹窗 saas.util.BaseUtil.showLoginWin(); throw new Error('会话已过期'); } if (Authorization) { // 如果返回了token则刷新token let headers = Ext.Ajax.getDefaultHeaders() || {}; let session = saas.util.State.get('session'); headers['Authorization'] = Authorization; Ext.Ajax.setDefaultHeaders(headers); session.token = Authorization; session.timestamp = Ext.now(); saas.util.State.set('session', session); } }, promise: function (config) { var me = this, 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, jsonData: config.jsonData, async: async, method: method, timeout: timeout, headers: Ext.apply(defaultHeaders, config.headers), success: function (response) { var res = Ext.decode(response.responseText); if (res.success) { return resolve(res); } else { res.message = res.message || '未知错误'; return reject(res); } }, failure: function (response) { var res, status = response.status; try { res = JSON.parse(response.responseText); } catch (e) { res = response.responseJson || { message: me.parseResponseStatus(status) }; } res.message = res.message || '未知错误'; reject(res); } }); }) }, /** * form post * * @param {*} config */ form: function (config) { config.headers = config.headers || {}; Ext.apply(config.headers, { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }); return this.post(config); }, /** * post * * @param {*} config */ post: function (config) { config.method = 'POST'; return this.promise(config); }, /** * payload * * @param {*} config */ payload: function (config) { if (Ext.isObject(config.params)) { config.params = JSON.stringify(config.params); } return this.post(config); }, getServerBasePath: function () { const basePath = Ext.manifest.server.basePath; if (Ext.isObject(basePath)) { let protocol = window.location.protocol.split(":")[0]; if (protocol === 'file') { protocol = 'https'; } return basePath[protocol]; } return basePath; } });