/** * 请求工具类,临时方案,待完善 * @author cls * @date 2021-01-30 */ import { baseURL, projectName, requestUrl } from '@birt/config' import Message from '../components/vue-m-message/index.js' import axios from 'axios'; import { getToken } from '@birt/utils/auth' var pn = projectName || 'birt' //请求状态码属性 const STATUS_KEY = "status"; //请求成功状态码 const STATUS_SUCCESS = 200; //超时 const STATUS_TIMEOUT = 418; // 检测登录超时 function checkTimeout(res) { if (res[STATUS_KEY] == STATUS_TIMEOUT) { // to re-login MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { confirmButtonText: 'Re-Login', cancelButtonText: 'Cancel', type: 'warning' }).then(() => { console.log("这里处理超时登录"); }) return true; } return false } debugger axios.baseURL = baseURL axios.interceptors.request.use( config => { // "3d3fe30c-3d99-41e8-9aea-addd8528aa07" // config.headers["Authorization"] = "bearer " + "b9e6a365-c551-4551-b258-c56e3acd0eed"; if (getToken()) { config.headers["Authorization"] = "bearer " + getToken(); } // else { // //没有token,跳转登录页 // if (window.HIVUI_SETTING.loginUrl) { // location = window.HIVUI_SETTING.loginUrl; // } else { // location = "/" + projectName + "/hivuiLogin/index.html#/"; // } // } if (!config.url.startsWith("/api") && config.url.indexOf(requestUrl) == -1) { config.url = requestUrl + config.url; } if (config.url.indexOf(pn) == -1) { if (config.url.indexOf('pn=birt') != -1) { var url = config.url.replace('birt', pn) console.log('url', url) config.url = url } else if (config.url.indexOf('pn=false') != -1) { var url = config.url.replace('false', pn) console.log('url', url) config.url = url } else { if (config.url.indexOf('?') == -1) { config.url += "?pn=" + pn; } else { config.url += "&pn=" + pn } } } return config }, error => { console.log(error) return Promise.reject(error) } ) axios.interceptors.response.use(function (response) { // 因返回mock数据包格式不统一,做兼容处理,枚举接口未返回status:200 var res = (response[STATUS_KEY] && response.dataPack) ? response : response.data; res = (res || response.dataPack) || response; let status = res[STATUS_KEY]; // 流程特殊添加options,流程批处理返回 let flowData = response.config && response.config.data; if (flowData && (typeof flowData == "string" && flowData.indexOf("__isIntercept") != -1) && JSON.parse(flowData).__isIntercept === false) { try { res.options = JSON.parse(response.config.data) } catch (e) { } } if (status != STATUS_SUCCESS && status != 207) { if (!checkTimeout(res)) { let msgType = "error"; if (res == 700) { msgType = "info"; } Message({ message: res.msg || 'error', type: msgType, duration: 5 * 1000 }) } return Promise.reject(res.msg || 'error') } else if (res[STATUS_KEY] == 401) { // Message({ // message: 'token失效', // type: 'error', // duration: 5 * 1000 // }) MessageBox.alert('你已被登出,请重新登录', '登录超时', { confirmButtonText: '重新登录', type: 'warning' }).then(() => { let loginUrl = window.HIVUI_SETTING.loginUrl if (window.HIVUI_SETTING.isSingleLogin) { loginUrl = window.HIVUI_SETTING.singleLoginUrl; } if (loginUrl.endsWith("=")) { let params = window.location.search.slice(1, -1).split("&").filter(item => { if (item.indexOf("ticket") == -1) { return item } }); loginUrl = loginUrl + window.location.origin + window.location.pathname + (params.length > 0 ? '?' : '') + params.join("&"); } window.location.href = loginUrl }) } else { return res } }, function (error) { const res = error.response.data; if (res[STATUS_KEY] == 401) { if(top&&top.window.SysPage&&top.window.SysPage.openMiniLogin){//小窗口 top.window.SysPage.openMiniLogin(); }else{ Message({ message: 'token失效', type: 'error', duration: 5 * 1000 }) } } else { if (!checkTimeout(res)) { Message({ showClose: true, message: res.msg || '未知错误', type: 'error', duration: 5 * 1000 }) return Promise.reject(error) } } }); export default axios;