import axios from 'axios';
// cancelToken.js
// 声明一个 Map 用于存储每个请求的标识 和 取消函数
const pending = new Map();
/**
* 添加请求
* @param {Object} config
*/
export const addPending = (config: any) => {
const url = [config.method, config.url].join('&');
config.cancelToken =
config.cancelToken ||
new axios.CancelToken((cancel: any) => {
if (!pending.has(url)) {
// 如果 pending 中不存在当前请求,则添加进去
pending.set(url, cancel);
}
});
};
/**
* 移除请求
* @param {Object} config
*/
export const removePending = (config: any) => {
const url = [config.method, config.url].join('&');
if (pending.has(url)) {
// 如果在 pending 中存在当前请求标识,需要取消当前请求,并且移除
const cancel = pending.get(url);
cancel(url);
pending.delete(url);
}
};
/**
* 清空 pending 中的请求(在路由跳转时调用)
*/
export const clearPending = () => {
// eslint-disable-next-line no-restricted-syntax
for (const [url, cancel] of pending) {
cancel(url);
}
pending.clear();
};
/* eslint-disable */
import axios from 'axios';
import {h} from 'vue'
// import qs from 'qs';
import { ElMessageBox } from 'element-plus';
import globalData from './contant';
import warningIcon from '@/assets/imgs/status_warning.png';
import {addPending,removePending} from "./cancelToken";
let showedMsgBox = false; // 默认messagebox未打开
axios.defaults.withCredentials = true;
// const isDemoPage = window.location.href.includes('/resource-center')
// 请求拦截(配置发送请求的信息)
axios.interceptors.request.use(
config => {
const {appName,versionName,versionCode, cuid, channel, version} = globalData.appInfo;
// if(isDemoPage){
// config.headers['X-SMART-DLGIN'] = '!ab@testemo'
// }
if (
config.method === 'post' &&
config.headers['content-type'] !== 'multipart/form-data'
) {
config.data = config.data;
}
removePending(config) // 在请求开始前,对之前的请求做检查取消操作
addPending(config) // 将当前请求添加到 pending 中
return config;
},
error => {
return Promise.reject(error);
}
);
const showMessageBox = (message: string, fn?: Function) => {
return (
ElMessageBox({
customClass: 'globalMsg',
showClose: false,
message: h('div',{ class: 'message-body-wrapper' }, [
h('div', { class: 'message-title'}, [
h('img', { class: 'icon', src: warningIcon }),
h('span',{ class:'title' }, '提示')
]),
h('div', { class: 'message-content' }, message),
]),
confirmButtonText: '确定',
closeOnClickModal: true,
callback:(action:any) =>{
fn && fn();
}
})
)
}
/**
* define error msg
*/
const msgConst = new Map([
[5002, '用户未登录'],
[5006, '密码已修改,请重新登录'],
[4000, '服务器异常, 请稍后重试'],
])
// 响应拦截(配置请求回来的信息)
axios.interceptors.response.use(
res => {
removePending(res) // 在请求结束后,移除本次请求
if (res.headers['content-type'] === 'text/html') {
return Promise.reject({
errMsg: "Not Found",
errNo: 404,
data: null
});
}
const resData: any = res.data;
let errCode: number | undefined = Number(resData.errNo);
if (errCode === 0) {
return Promise.resolve(resData.data);
}
if (msgConst.has(errCode) && !showedMsgBox) {
showedMsgBox = true;
showMessageBox(msgConst.get(errCode) || '系统发现未知异常', () => showedMsgBox = false);
}
return Promise.reject(resData);
},
err => {
console.log(err)
if (axios.isCancel(err)) {//处理手动cancel
console.log('这是手动cancel的')
}
return Promise.reject(err);
}
);
export default axios;