2026-04-22 17:53:20 +08:00
|
|
|
import axios from 'axios';
|
2026-03-27 14:50:35 +08:00
|
|
|
import { message, Modal } from 'ant-design-vue';
|
2026-03-25 10:02:19 +08:00
|
|
|
import { getToken } from '@/utils/auth';
|
|
|
|
|
import { useUserStoreHook } from '@/store/modules/user';
|
2026-05-08 14:15:44 +08:00
|
|
|
import router from '@/router';
|
2026-03-25 10:02:19 +08:00
|
|
|
|
|
|
|
|
// 创建 axios 实例
|
|
|
|
|
const service = axios.create({
|
|
|
|
|
baseURL: import.meta.env.VITE_APP_BASE_API,
|
2026-05-11 17:45:09 +08:00
|
|
|
timeout: 15000, // 15秒
|
2026-03-25 10:02:19 +08:00
|
|
|
headers: { 'Content-Type': 'application/json;charset=utf-8' }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 请求拦截器
|
|
|
|
|
service.interceptors.request.use(
|
2026-04-22 17:53:20 +08:00
|
|
|
(config: any) => {
|
2026-03-25 10:02:19 +08:00
|
|
|
if (!config.headers) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Expected 'config' and 'config.headers' not to be undefined`
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-05-19 18:56:33 +08:00
|
|
|
if (
|
|
|
|
|
config.url.includes('/dec-lygk-base-server') ||
|
2026-05-20 17:55:48 +08:00
|
|
|
config.url.includes('/wmp-env-server') ||
|
|
|
|
|
config.url.includes('/wmp-sys-server')
|
2026-05-19 18:56:33 +08:00
|
|
|
) {
|
2026-05-15 17:38:03 +08:00
|
|
|
config.headers._appid = '974975A6-47FD-4C04-9ACD-68938D2992BD';
|
|
|
|
|
config.headers._isolateid = '5b34aecb-adfb-4dfc-ad95-21505a9eb388';
|
|
|
|
|
config.headers._platformid = '31e6d13f-c359-4a19-8e67-b6f868f2401b';
|
|
|
|
|
config.headers._sysid = '10EC2E0B-AEA9-4757-83A2-201BA1BC54E9';
|
|
|
|
|
|
|
|
|
|
config.headers.authorization =
|
2026-05-25 08:54:17 +08:00
|
|
|
'bearer a385ef90-a17d-429f-817f-7d961594020c';
|
2026-05-15 17:38:03 +08:00
|
|
|
config.baseURL = '/';
|
|
|
|
|
} else {
|
|
|
|
|
const user = useUserStoreHook();
|
|
|
|
|
if (user.Token) {
|
|
|
|
|
config.headers.token = getToken();
|
|
|
|
|
}
|
2026-03-25 10:02:19 +08:00
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
(error: any) => {
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 响应拦截器
|
|
|
|
|
service.interceptors.response.use(
|
2026-04-22 17:53:20 +08:00
|
|
|
(response: any) => {
|
2026-03-25 10:02:19 +08:00
|
|
|
const { status, msg } = response;
|
|
|
|
|
if (status === 200) {
|
|
|
|
|
if (response.data.code == 401) {
|
2026-05-11 17:45:09 +08:00
|
|
|
message.error(response.data.msg || '请求失败');
|
2026-03-25 10:02:19 +08:00
|
|
|
return;
|
2026-05-11 17:45:09 +08:00
|
|
|
} else if (response.data.code == 1) {
|
2026-03-27 14:50:35 +08:00
|
|
|
message.error(response.data.msg);
|
2026-03-25 10:02:19 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
|
|
|
|
// 响应数据为二进制流处理(Excel导出)
|
|
|
|
|
if (response.data instanceof ArrayBuffer) {
|
|
|
|
|
return response;
|
|
|
|
|
}
|
2026-05-11 17:45:09 +08:00
|
|
|
message.error(msg || '系统出错');
|
2026-03-25 10:02:19 +08:00
|
|
|
return Promise.reject(new Error(msg || 'Error'));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
(error: any) => {
|
|
|
|
|
if (error.response.data) {
|
|
|
|
|
const { status, msg } = error.response.data;
|
|
|
|
|
// token 过期,重新登录
|
|
|
|
|
if (status === '403') {
|
2026-03-27 14:50:35 +08:00
|
|
|
Modal.confirm({
|
2026-05-11 17:45:09 +08:00
|
|
|
title: '提示',
|
|
|
|
|
content: '当前页面已失效,请重新登录',
|
|
|
|
|
okText: '确定',
|
2026-04-22 17:53:20 +08:00
|
|
|
cancelButtonProps: { disabled: false },
|
2026-03-27 14:50:35 +08:00
|
|
|
onOk: () => {
|
|
|
|
|
localStorage.clear();
|
2026-05-08 14:15:44 +08:00
|
|
|
router.push('/login');
|
2026-03-27 14:50:35 +08:00
|
|
|
window.location.href = '/';
|
2026-05-11 17:45:09 +08:00
|
|
|
}
|
2026-03-25 10:02:19 +08:00
|
|
|
});
|
|
|
|
|
} else {
|
2026-03-27 14:50:35 +08:00
|
|
|
message.error(msg || '当前页面已失效');
|
2026-03-25 10:02:19 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Promise.reject(error.message);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 导出 axios 实例
|
|
|
|
|
export default service;
|