2020-12-07 23:56:33 +08:00
|
|
|
|
import {Message} from 'element-ui'
|
|
|
|
|
|
2020-07-03 14:43:14 +08:00
|
|
|
|
export default function ({app, $axios, store, redirect}) {
|
2022-10-30 16:56:15 +08:00
|
|
|
|
|
|
|
|
|
function generateReqKey(config) {
|
|
|
|
|
const { method, url, params, data } = config;
|
|
|
|
|
return [method, url, JSON.stringify(params), JSON.stringify(data)].join("&");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pendingRequest = new Map();
|
|
|
|
|
function addPendingRequest(config) {
|
|
|
|
|
const requestKey = generateReqKey(config);
|
|
|
|
|
config.cancelToken = config.cancelToken || new $axios.CancelToken((cancel) => {
|
|
|
|
|
if (!pendingRequest.has(requestKey)) {
|
|
|
|
|
pendingRequest.set(requestKey, cancel);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function removePendingRequest(config) {
|
|
|
|
|
const requestKey = generateReqKey(config);
|
|
|
|
|
if (pendingRequest.has(requestKey)) {
|
|
|
|
|
const cancelToken = pendingRequest.get(requestKey);
|
|
|
|
|
cancelToken(requestKey);
|
|
|
|
|
pendingRequest.delete(requestKey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-03 14:43:14 +08:00
|
|
|
|
$axios.onRequest(config => {
|
2022-01-14 10:38:55 +08:00
|
|
|
|
let fingerprint = store.state.fingerprint;
|
|
|
|
|
if (fingerprint) {
|
|
|
|
|
config.headers['fingerprint'] = fingerprint
|
|
|
|
|
}
|
2020-07-03 14:43:14 +08:00
|
|
|
|
})
|
2022-10-30 16:56:15 +08:00
|
|
|
|
|
|
|
|
|
$axios.interceptors.request.use(
|
|
|
|
|
function (config) {
|
|
|
|
|
// 检查是否存在重复请求,若存在则取消已发的请求
|
|
|
|
|
removePendingRequest(config);
|
|
|
|
|
// 把当前请求信息添加到pendingRequest对象中
|
|
|
|
|
addPendingRequest(config);
|
|
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2020-07-03 14:43:14 +08:00
|
|
|
|
$axios.onResponse(response => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-10-30 16:56:15 +08:00
|
|
|
|
removePendingRequest(response.config);
|
2020-07-03 14:43:14 +08:00
|
|
|
|
//返回数据逻辑处理 比如:error_code错误处理
|
|
|
|
|
let message;
|
|
|
|
|
if (typeof (response.data.data) !== 'undefined') {
|
|
|
|
|
message = response.data.data.message
|
|
|
|
|
} else if (typeof (response.data) !== 'undefined') {
|
|
|
|
|
message = response.data.message
|
|
|
|
|
}
|
|
|
|
|
if (response.data.success) {
|
|
|
|
|
resolve(response.data);
|
|
|
|
|
} else {
|
2021-12-17 19:53:42 +08:00
|
|
|
|
if (response.data.code === 0) {
|
2020-12-07 23:56:33 +08:00
|
|
|
|
Message.error(message ? message : '服务异常')
|
2021-12-17 19:53:42 +08:00
|
|
|
|
} else if (response.data.code === 401) {
|
2022-10-27 23:22:46 +08:00
|
|
|
|
app.$auth.logout()
|
2021-12-17 19:53:42 +08:00
|
|
|
|
} else if (response.data.code === 404) {
|
2020-12-07 23:56:33 +08:00
|
|
|
|
Message.error('操作失败,请稍后再试......')
|
2021-12-17 19:53:42 +08:00
|
|
|
|
} else if (response.data.code === 500) {
|
2020-12-07 23:56:33 +08:00
|
|
|
|
Message.error('服务器正在开小差,请稍后再试......')
|
2021-02-01 18:12:35 +08:00
|
|
|
|
} else {
|
|
|
|
|
Message.error(response.data.message);
|
2020-07-03 14:43:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
reject(response);
|
|
|
|
|
})
|
|
|
|
|
});
|
2022-10-27 23:22:46 +08:00
|
|
|
|
|
2020-07-03 14:43:14 +08:00
|
|
|
|
$axios.onError(error => {
|
2022-10-30 16:56:15 +08:00
|
|
|
|
removePendingRequest(error.config || {}); // 从pendingRequest对象中移除请求
|
|
|
|
|
if ($axios.isCancel(error)) {
|
|
|
|
|
// console.log("已取消的重复请求:" + error.message);
|
2020-07-03 14:43:14 +08:00
|
|
|
|
} else {
|
2022-10-30 16:56:15 +08:00
|
|
|
|
// 添加异常处理
|
|
|
|
|
const code = parseInt(error.response && error.response.status)
|
|
|
|
|
if (code === 400) {
|
|
|
|
|
redirect('/400');
|
|
|
|
|
} else {
|
|
|
|
|
console.log(error.data);
|
|
|
|
|
}
|
2020-07-03 14:43:14 +08:00
|
|
|
|
}
|
2022-10-30 16:56:15 +08:00
|
|
|
|
|
2020-07-03 14:43:14 +08:00
|
|
|
|
})
|
|
|
|
|
}
|