diff --git a/src/assets/js/utils/request.js b/src/assets/js/utils/request.js index da84c8b4d..c122bd95a 100644 --- a/src/assets/js/utils/request.js +++ b/src/assets/js/utils/request.js @@ -118,92 +118,100 @@ const service = axios.create({ // request拦截器 service.interceptors.request.use( - async config => { - let relativePath = config.url && config.url.split(configApp.API_VERSION)[1] - if (whiteList.includes(relativePath)) { - Toast.loading({ - duration: 0, // 持续展示 toast - forbidClick: true, // 禁用背景点击 - loadingType: 'spinner', - message: '加载中……' - }) - } - /** - * 请求拦截处理(待添加 判断走统一网关处理) - */ - if(config.url && /api\/$/.test(config.url.split(configApp.API_VERSION)[0]) && configApp.API_VERSION == 'v2'){ - if(!config.data || config.data == null){ - config.data = {} - } - if(!!config.data && config.data != null){ - let encrypt = AESTools.AESEncrypt(JSON.stringify(config.data),configApp.REQ_PWD) - config.data = {"data": encrypt } - } - } - let token = CacheUtils.getLocItem('token') - /** - * 在HomeProduct页面获取代理人信息时, 客户端可能还未将token存储在本地 - * 此时如果token为空, 调用客户端方法获取token并放入localstorage中 - */ - if (relativePath === '/customer/agent/info' && !token) { - // 设备类型 - const target = device() - // 安卓or苹果 - if (target.isAndroid || target.isIphone) { - // eslint-disable-next-line no-undef - const res = await EWebBridge.webCallAppInJs('getToken') - const token = JSON.parse(res).token - CacheUtils.setLocItem('token', token) - } - } - config.headers['token'] = token - // 添加请时间戳 - let timeStr = new Date().getTime() + '' - config.headers['timeStr'] = timeStr - config.headers['signature'] = MD5(timeStr + CacheUtils.getLocItem('token')) - return config - }, - error => { - // Do something with request error - Promise.reject(error) + async config => { + let relativePath = config.url && config.url.split(configApp.API_VERSION)[1] + if (whiteList.includes(relativePath)) { + Toast.loading({ + duration: 0, // 持续展示 toast + forbidClick: true, // 禁用背景点击 + loadingType: 'spinner', + message: '加载中……' + }) } + /** + * 请求拦截处理(待添加 判断走统一网关处理) + */ + if (config.url && /api\/$/.test(config.url.split(configApp.API_VERSION)[0]) && configApp.API_VERSION == 'v2') { + if (!config.data || config.data == null) { + config.data = {} + } + if (!!config.data && config.data != null) { + let encrypt = AESTools.AESEncrypt(JSON.stringify(config.data), configApp.REQ_PWD) + config.data = { data: encrypt } + } + } + let token = CacheUtils.getLocItem('token') + console.log('localstorage token: ', token) + /** + * 在HomeProduct页面获取代理人信息时, 客户端可能还未将token存储在本地 + * 此时如果token为空, 调用客户端方法获取token并放入localstorage中 + */ + if (relativePath === '/customer/agent/info' && !token) { + // 设备类型 + const target = device() + // 安卓or苹果 + if (target.isAndroid || target.isIphone) { + // eslint-disable-next-line no-undef + const res = await EWebBridge.webCallAppInJs('getToken') + const token = JSON.parse(res).token + console.log('调用客户端获取token: ', res) + CacheUtils.setLocItem('token', token) + } + } + config.headers['token'] = token + // 添加请时间戳 + let timeStr = new Date().getTime() + '' + config.headers['timeStr'] = timeStr + config.headers['signature'] = MD5(timeStr + CacheUtils.getLocItem('token')) + return config + }, + error => { + // Do something with request error + Promise.reject(error) + } ) // respone拦截器 service.interceptors.response.use( - response => { - let res = response.data - if( configApp.API_VERSION == 'v2' && response.config.url && response.headers['content-type'].match(/application\/json/) && /api\/$/.test(response.config.url.split(configApp.API_VERSION)[0])){ - if(res.response){// 正常情況返回必有response 节点 - res = JSON.parse(AESTools.AESDecrypt(res.response,configApp.REQ_PWD)) - } + response => { + let res = response.data + if ( + configApp.API_VERSION == 'v2' && + response.config.url && + response.headers['content-type'].match(/application\/json/) && + /api\/$/.test(response.config.url.split(configApp.API_VERSION)[0]) + ) { + if (res.response) { + // 正常情況返回必有response 节点 + res = JSON.parse(AESTools.AESDecrypt(res.response, configApp.REQ_PWD)) } - Toast.clear() - if (res.code != 0) { - if (res.code == 10001 || res.code == 10002) { - Dialog.confirm({ - confirmButtonText: '重新登录', - message: '你已被登出,可以取消继续留在该页面,或者重新登录' - }).then(() => { - //eslint-disable-next-line - EWebBridge.webCallAppInJs('bridge', { - flag: 'login' - }) - }) - } else { - //Toast.fail(res.msg) - } - return Promise.reject(res) - } else { - return res.content - } - }, - error => { - Toast.clear() - console.log('err' + error) // for debug - //Toast.fail(error.message) - return Promise.reject(error) } + Toast.clear() + if (res.code != 0) { + if (res.code == 10001 || res.code == 10002) { + Dialog.confirm({ + confirmButtonText: '重新登录', + message: '你已被登出,可以取消继续留在该页面,或者重新登录' + }).then(() => { + //eslint-disable-next-line + EWebBridge.webCallAppInJs('bridge', { + flag: 'login' + }) + }) + } else { + //Toast.fail(res.msg) + } + return Promise.reject(res) + } else { + return res.content + } + }, + error => { + Toast.clear() + console.log('err' + error) // for debug + //Toast.fail(error.message) + return Promise.reject(error) + } ) export default service