2024-08-14 10:38:23 +00:00
|
|
|
import axios, { InternalAxiosRequestConfig, AxiosResponse } from 'axios';
|
|
|
|
|
import { ElMessage, ElMessageBox } from 'element-plus';
|
2024-08-12 02:18:11 +00:00
|
|
|
// 创建 axios 实例
|
|
|
|
|
const service = axios.create({
|
2024-08-16 06:46:22 +00:00
|
|
|
baseURL: import.meta.env.VITE_APP_BASE_API == '/dev-api' ? import.meta.env.VITE_APP_BASE_API : window.location.origin,
|
2024-08-12 02:18:11 +00:00
|
|
|
timeout: 50000,
|
2024-08-14 10:38:23 +00:00
|
|
|
headers: { 'Content-Type': 'application/json;charset=utf-8' },
|
|
|
|
|
});
|
2024-08-12 02:18:11 +00:00
|
|
|
|
|
|
|
|
// 请求拦截器
|
|
|
|
|
service.interceptors.request.use(
|
|
|
|
|
(config: InternalAxiosRequestConfig) => {
|
|
|
|
|
// const userStore = useUserStoreHook();
|
2024-08-14 10:38:23 +00:00
|
|
|
// const accessToken = localStorage.getItem(TOKEN_KEY);
|
2024-08-15 11:06:56 +00:00
|
|
|
const accessToken = { token: 'b84c6cac0dacd1bc624393cd522dec37' };
|
2024-08-14 10:38:23 +00:00
|
|
|
if (accessToken.token) {
|
|
|
|
|
// config.headers.Authorization = accessToken.token;
|
|
|
|
|
// config.data = { ...config.data, token: accessToken.token };
|
|
|
|
|
config.url = config.url + '?token=' + accessToken.token;
|
2024-08-12 02:18:11 +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-08-14 10:38:23 +00:00
|
|
|
const { code, msg } = response.data;
|
2024-08-12 02:18:11 +00:00
|
|
|
// 登录成功
|
2024-08-14 10:38:23 +00:00
|
|
|
if (code == '0') {
|
|
|
|
|
return response.data;
|
2024-08-12 02:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-14 10:38:23 +00:00
|
|
|
ElMessage.error(msg || '系统出错');
|
|
|
|
|
return Promise.reject(new Error(msg || 'Error'));
|
2024-08-12 02:18:11 +00:00
|
|
|
},
|
|
|
|
|
(error: any) => {
|
|
|
|
|
if (error.response.data) {
|
2024-08-14 10:38:23 +00:00
|
|
|
const { code, msg } = error.response.data;
|
2024-08-12 02:18:11 +00:00
|
|
|
// token 过期,跳转登录页
|
2024-08-14 10:38:23 +00:00
|
|
|
if (code === '-400') {
|
2024-08-12 02:18:11 +00:00
|
|
|
ElMessageBox.confirm('当前页面已失效,请重新登录', '提示', {
|
|
|
|
|
confirmButtonText: '确定',
|
2024-08-14 10:38:23 +00:00
|
|
|
type: 'warning',
|
2024-08-12 02:18:11 +00:00
|
|
|
}).then(() => {
|
2024-08-14 10:38:23 +00:00
|
|
|
localStorage.clear(); // @vueuse/core 自动导入
|
|
|
|
|
window.location.href = '/';
|
|
|
|
|
});
|
2024-08-12 02:18:11 +00:00
|
|
|
} else {
|
2024-08-14 10:38:23 +00:00
|
|
|
ElMessage.error(msg || '系统出错');
|
2024-08-12 02:18:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-14 10:38:23 +00:00
|
|
|
return Promise.reject(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;
|