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-05-18 13:27:29 +08:00
|
|
|
|
import utils from '../lib/utils';
|
2026-08-12 11:29:44 +08:00
|
|
|
|
import i18n from '../i18n';
|
2026-04-17 17:20:41 +08:00
|
|
|
|
|
2026-04-21 10:33:48 +08:00
|
|
|
|
const API_BASE_URL = envConfig.baseURL;
|
2026-05-18 13:27:29 +08:00
|
|
|
|
const message = utils.message; // 使用全局 message
|
|
|
|
|
|
|
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-05-29 14:14:28 +08:00
|
|
|
|
timeout: 100000,
|
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}`;
|
|
|
|
|
|
}
|
2026-06-16 13:28:21 +08:00
|
|
|
|
|
|
|
|
|
|
if (!(config.data instanceof FormData)) {
|
|
|
|
|
|
config.headers['Content-Type'] = 'application/json';
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// FormData 删除json头,交给axios自动生成multipart
|
|
|
|
|
|
delete config.headers['Content-Type'];
|
|
|
|
|
|
}
|
2026-04-21 10:33:48 +08:00
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error: AxiosError) => {
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
2026-04-17 17:20:41 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
2026-05-18 13:27:29 +08:00
|
|
|
|
// 🔥 全局唯一锁:保证并发多个 401 也只执行一次
|
|
|
|
|
|
let isLoggingOut = false;
|
|
|
|
|
|
|
2026-05-11 16:34:44 +08:00
|
|
|
|
const handleLogoutAndRedirect = () => {
|
2026-05-18 13:27:29 +08:00
|
|
|
|
// 已经在登出流程中,直接阻止
|
|
|
|
|
|
if (isLoggingOut) return;
|
|
|
|
|
|
|
|
|
|
|
|
// 立即上锁
|
|
|
|
|
|
isLoggingOut = true;
|
2026-05-11 16:34:44 +08:00
|
|
|
|
|
2026-05-18 13:27:29 +08:00
|
|
|
|
// 清除登录信息
|
|
|
|
|
|
try {
|
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
|
Cookies.remove('userId');
|
|
|
|
|
|
Cookies.remove('Admin-Token');
|
|
|
|
|
|
Cookies.remove('token');
|
|
|
|
|
|
} catch { }
|
|
|
|
|
|
|
|
|
|
|
|
// redux 登出
|
|
|
|
|
|
import('../store').then(storeModule => {
|
2026-05-11 16:34:44 +08:00
|
|
|
|
import('../store/userSlice').then(({ logout }) => {
|
2026-05-18 13:27:29 +08:00
|
|
|
|
storeModule.default.dispatch(logout());
|
2026-05-11 16:34:44 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-05-18 13:27:29 +08:00
|
|
|
|
// 只提示一次
|
2026-08-12 11:29:44 +08:00
|
|
|
|
message?.error(i18n.t('api.loginExpired'));
|
2026-05-18 13:27:29 +08:00
|
|
|
|
|
|
|
|
|
|
// 只跳转一次
|
2026-05-11 16:34:44 +08:00
|
|
|
|
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-05-18 13:27:29 +08:00
|
|
|
|
// 业务 401 → 统一走登出
|
|
|
|
|
|
if (data?.code === 401) {
|
2026-05-11 16:34:44 +08:00
|
|
|
|
handleLogoutAndRedirect();
|
2026-08-12 11:29:44 +08:00
|
|
|
|
return Promise.reject(new Error(i18n.t('api.loginExpired')));
|
2026-05-11 16:34:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
return data;
|
|
|
|
|
|
},
|
2026-05-18 13:27:29 +08:00
|
|
|
|
(error: AxiosError) => {
|
|
|
|
|
|
// HTTP 401 → 统一走登出
|
|
|
|
|
|
if (error.response?.status == 401) {
|
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-18 13:27:29 +08:00
|
|
|
|
// 网络异常
|
|
|
|
|
|
if (!error.response) {
|
2026-08-12 11:29:44 +08:00
|
|
|
|
message?.error(i18n.t('api.networkError'));
|
2026-05-11 16:34:44 +08:00
|
|
|
|
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;
|