2026-04-21 10:13:31 +08:00
|
|
|
|
import axios, { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
|
|
|
|
|
import envConfig from '../../env';
|
|
|
|
|
|
import Cookies from 'js-cookie';
|
2026-04-17 17:20:41 +08:00
|
|
|
|
import { message } from 'antd';
|
|
|
|
|
|
|
2026-04-21 10:33:48 +08:00
|
|
|
|
const API_BASE_URL = envConfig.baseURL;
|
2026-04-21 10:13:31 +08:00
|
|
|
|
|
|
|
|
|
|
const api = axios.create({
|
2026-04-21 10:33:48 +08:00
|
|
|
|
baseURL: API_BASE_URL,
|
2026-04-30 15:02:08 +08:00
|
|
|
|
timeout: 50000,
|
2026-04-21 10:33:48 +08:00
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
},
|
|
|
|
|
|
withCredentials: true,
|
2026-04-17 17:20:41 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-04-30 10:49:44 +08:00
|
|
|
|
// 请求拦截器
|
2026-04-21 10:13:31 +08:00
|
|
|
|
api.interceptors.request.use(
|
2026-04-21 10:33:48 +08:00
|
|
|
|
(config: InternalAxiosRequestConfig) => {
|
|
|
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
|
|
if (token && config.headers) {
|
|
|
|
|
|
config.headers['Authorization'] = `Bearer ${token}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error: AxiosError) => {
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
2026-04-17 17:20:41 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
2026-05-11 16:34:44 +08:00
|
|
|
|
// 统一登出跳转方法
|
|
|
|
|
|
const handleLogoutAndRedirect = () => {
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
|
Cookies.remove('userId');
|
|
|
|
|
|
Cookies.remove('Admin-Token');
|
|
|
|
|
|
|
|
|
|
|
|
// 动态导入避免循环依赖
|
|
|
|
|
|
import('../store').then(store => {
|
|
|
|
|
|
import('../store/userSlice').then(({ logout }) => {
|
|
|
|
|
|
store.default.dispatch(logout());
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
message.error('登录已过期,请重新登录');
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
window.location.href = '/login';
|
|
|
|
|
|
}, 800);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 响应拦截器
|
2026-04-21 10:13:31 +08:00
|
|
|
|
api.interceptors.response.use(
|
2026-05-11 16:34:44 +08:00
|
|
|
|
(response: AxiosResponse) => {
|
|
|
|
|
|
const data = response.data;
|
|
|
|
|
|
|
2026-04-30 10:49:44 +08:00
|
|
|
|
// ==============================================
|
2026-05-11 16:34:44 +08:00
|
|
|
|
// 👇 重点:如果接口返回 code = 401,直接跳转登录
|
2026-04-30 10:49:44 +08:00
|
|
|
|
// ==============================================
|
2026-05-11 16:34:44 +08:00
|
|
|
|
if (data?.code == 401) {
|
|
|
|
|
|
handleLogoutAndRedirect();
|
|
|
|
|
|
return Promise.reject(new Error('登录已过期'));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
},
|
|
|
|
|
|
async (error: AxiosError) => {
|
|
|
|
|
|
// 网络异常 / 服务器挂了
|
2026-04-30 10:49:44 +08:00
|
|
|
|
if (!error.response) {
|
|
|
|
|
|
message.error('服务异常或网络断开,即将重新登录');
|
2026-05-11 16:34:44 +08:00
|
|
|
|
handleLogoutAndRedirect();
|
2026-04-30 10:49:44 +08:00
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
2026-04-21 14:57:52 +08:00
|
|
|
|
|
2026-05-11 16:34:44 +08:00
|
|
|
|
// HTTP 401
|
2026-04-30 10:49:44 +08:00
|
|
|
|
if (error.response.status === 401) {
|
2026-05-11 16:34:44 +08:00
|
|
|
|
handleLogoutAndRedirect();
|
|
|
|
|
|
return Promise.reject(error);
|
2026-04-21 10:33:48 +08:00
|
|
|
|
}
|
2026-04-21 14:57:52 +08:00
|
|
|
|
|
2026-04-21 10:33:48 +08:00
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
2026-04-17 17:20:41 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
2026-04-21 10:13:31 +08:00
|
|
|
|
export default api;
|