2024-08-14 10:38:23 +00:00
|
|
|
|
import axios, { InternalAxiosRequestConfig, AxiosResponse } from 'axios';
|
2024-09-04 07:48:59 +00:00
|
|
|
|
import { ElMessage, ElMessageBox, type MessageHandler } from 'element-plus';
|
2024-08-16 09:16:25 +00:00
|
|
|
|
import { get_cookie } from './index';
|
2024-09-04 07:48:59 +00:00
|
|
|
|
|
|
|
|
|
|
// 提示拦截
|
|
|
|
|
|
|
|
|
|
|
|
let messageInstance: MessageHandler;
|
|
|
|
|
|
const message_error = (info: string) => {
|
|
|
|
|
|
if (messageInstance) {
|
|
|
|
|
|
messageInstance.close();
|
|
|
|
|
|
}
|
2024-10-11 08:37:43 +00:00
|
|
|
|
messageInstance = ElMessage.error({
|
|
|
|
|
|
type: 'error',
|
|
|
|
|
|
message: info,
|
2024-10-16 07:36:51 +00:00
|
|
|
|
duration: 30000,
|
2024-10-11 08:37:43 +00:00
|
|
|
|
showClose: true,
|
|
|
|
|
|
});
|
2024-09-04 07:48:59 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 创建一个状态变量来跟踪是否已经弹出了退出登录的弹窗
|
|
|
|
|
|
const isLogoutModalShown = ref(true);
|
|
|
|
|
|
|
2024-12-02 08:42:55 +00:00
|
|
|
|
// 用于存储每个请求的CancelToken
|
|
|
|
|
|
const pendingRequests = new Map();
|
2025-01-17 10:48:40 +00:00
|
|
|
|
// 不需要认证的接口
|
|
|
|
|
|
const release_url = ['diyapi/attachmentupload'];
|
2024-08-12 02:18:11 +00:00
|
|
|
|
// 创建 axios 实例
|
2024-08-19 01:51:15 +00:00
|
|
|
|
const index = window.location.href.lastIndexOf('?s=');
|
2024-08-16 09:16:25 +00:00
|
|
|
|
const pro_url = window.location.href.substring(0, index);
|
2024-08-12 02:18:11 +00:00
|
|
|
|
const service = axios.create({
|
2024-08-19 01:51:15 +00:00
|
|
|
|
baseURL: import.meta.env.VITE_APP_BASE_API == '/dev-api' ? import.meta.env.VITE_APP_BASE_API : pro_url + '?s=',
|
2024-10-16 07:36:51 +00:00
|
|
|
|
timeout: 60000,
|
2024-10-21 06:09:46 +00:00
|
|
|
|
headers: { 'Content-Type': 'application/json;charset=utf-8', 'X-Requested-With': 'XMLHttpRequest' },
|
2024-08-14 10:38:23 +00:00
|
|
|
|
});
|
2024-08-27 10:24:07 +00:00
|
|
|
|
/** @ts-ignore */
|
2024-08-12 02:18:11 +00:00
|
|
|
|
// 请求拦截器
|
|
|
|
|
|
service.interceptors.request.use(
|
2024-08-26 08:42:11 +00:00
|
|
|
|
async (config: InternalAxiosRequestConfig) => {
|
2024-08-16 09:16:25 +00:00
|
|
|
|
// 如果是本地则使用静态tonken如果是线上则使用cookie的token
|
2024-10-23 02:52:19 +00:00
|
|
|
|
const cookie = get_cookie('admin_info') || '';
|
2024-09-03 09:15:00 +00:00
|
|
|
|
const symbol = config.url?.includes('?') ? '&' : '?';
|
2024-08-16 09:47:31 +00:00
|
|
|
|
if (import.meta.env.VITE_APP_BASE_API == '/dev-api') {
|
2024-08-27 10:24:07 +00:00
|
|
|
|
let temp_data = await import(import.meta.env.VITE_APP_BASE_API == '/dev-api' ? '../../temp.d' : '../../temp_pro.d');
|
2024-09-03 09:15:00 +00:00
|
|
|
|
config.url = config.url + symbol + 'token=' + temp_data.default.temp_token;
|
2024-08-16 09:47:31 +00:00
|
|
|
|
} else {
|
2024-10-23 02:52:19 +00:00
|
|
|
|
if (cookie && cookie !== null && cookie !== 'null') {
|
2024-10-23 05:34:41 +00:00
|
|
|
|
config.url = config.url + '&token=' + (JSON.parse(cookie) !== 'null' ? JSON.parse(cookie)?.token : '');
|
2024-08-16 09:47:31 +00:00
|
|
|
|
}
|
2024-08-12 02:18:11 +00:00
|
|
|
|
}
|
2025-01-17 10:48:40 +00:00
|
|
|
|
// 判断是否是包含不需要认证的接口
|
|
|
|
|
|
const release_list = release_url.filter(item => config.url?.includes(item));
|
|
|
|
|
|
if (release_list.length === 0) {
|
|
|
|
|
|
// 检查是否有相同请求正在进行,如果有则取消, 防止重复请求导致返回数据有误
|
|
|
|
|
|
if (pendingRequests.has(config.url)) {
|
|
|
|
|
|
const cancelToken = pendingRequests.get(config.url);
|
|
|
|
|
|
cancelToken.cancel('canceled');
|
|
|
|
|
|
pendingRequests.delete(config.url);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 创建一个新的 CancelToken
|
|
|
|
|
|
const source = axios.CancelToken.source();
|
|
|
|
|
|
config.cancelToken = source.token;
|
|
|
|
|
|
pendingRequests.set(config.url, source);
|
2024-12-02 08:42:55 +00:00
|
|
|
|
}
|
2024-08-14 10:38:23 +00:00
|
|
|
|
return config;
|
2024-08-12 02:18:11 +00:00
|
|
|
|
},
|
|
|
|
|
|
(error: any) => {
|
2024-08-14 10:38:23 +00:00
|
|
|
|
return Promise.reject(error);
|
2024-08-12 02:18:11 +00:00
|
|
|
|
}
|
2024-08-14 10:38:23 +00:00
|
|
|
|
);
|
2024-08-12 02:18:11 +00:00
|
|
|
|
// 响应拦截器
|
|
|
|
|
|
service.interceptors.response.use(
|
|
|
|
|
|
(response: AxiosResponse) => {
|
2024-12-02 08:42:55 +00:00
|
|
|
|
// 请求完成后,从pendingRequests中移除
|
|
|
|
|
|
pendingRequests.delete(response.config.url);
|
|
|
|
|
|
|
2024-08-30 06:05:07 +00:00
|
|
|
|
const { code, msg, message, data } = response.data;
|
2024-08-19 07:01:08 +00:00
|
|
|
|
if (code == 0) {
|
2024-08-14 10:38:23 +00:00
|
|
|
|
return response.data;
|
2024-08-19 07:11:44 +00:00
|
|
|
|
} else if (code == -400) {
|
2024-09-04 07:48:59 +00:00
|
|
|
|
if (isLogoutModalShown.value) {
|
|
|
|
|
|
isLogoutModalShown.value = false;
|
|
|
|
|
|
ElMessageBox.alert(msg, '温馨提示', {
|
|
|
|
|
|
confirmButtonText: '确定',
|
|
|
|
|
|
showClose: false,
|
|
|
|
|
|
type: 'warning',
|
|
|
|
|
|
}).then(() => {
|
|
|
|
|
|
localStorage.clear(); // @vueuse/core 自动导入
|
2024-09-09 06:00:31 +00:00
|
|
|
|
window.location.href = data.logout;
|
2024-09-04 07:48:59 +00:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-08-19 07:01:08 +00:00
|
|
|
|
} else {
|
2024-09-04 07:48:59 +00:00
|
|
|
|
message_error(msg || message || '系统出错');
|
2025-04-27 03:16:54 +00:00
|
|
|
|
return Promise.reject(msg || message || '系统出错');
|
2024-10-18 08:50:44 +00:00
|
|
|
|
// return Promise.reject(new Error(msg || 'Error'));
|
2024-08-12 02:18:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
(error: any) => {
|
2024-10-11 08:18:28 +00:00
|
|
|
|
if (error.response && error.response.data) {
|
2024-08-30 06:05:07 +00:00
|
|
|
|
const { msg, message } = error.response.data;
|
2024-09-04 07:48:59 +00:00
|
|
|
|
message_error(msg || message || '系统出错');
|
2024-12-02 08:42:55 +00:00
|
|
|
|
} else if (error.message == 'canceled') {
|
|
|
|
|
|
console.log('请求已取消');
|
2024-10-11 08:37:43 +00:00
|
|
|
|
} else {
|
|
|
|
|
|
message_error(error.message);
|
2024-08-12 02:18:11 +00:00
|
|
|
|
}
|
2025-04-27 03:16:54 +00:00
|
|
|
|
return Promise.reject(error.msg || error.message);
|
2024-08-12 02:18:11 +00:00
|
|
|
|
}
|
2024-08-14 10:38:23 +00:00
|
|
|
|
);
|
2024-08-12 02:18:11 +00:00
|
|
|
|
|
|
|
|
|
|
// 导出 axios 实例
|
2024-08-14 10:38:23 +00:00
|
|
|
|
export default service;
|