Files
web-Iot/src/api/request.ts

111 lines
2.5 KiB
TypeScript
Raw Normal View History

import axios, { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
2026-04-21 10:13:31 +08:00
import envConfig from '../../env';
import Cookies from 'js-cookie';
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;
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,
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');
2026-10-12 13:49:44 +08:00
const lang = localStorage.getItem('i18nextLng') || 'zh';
if (config.headers) {
if (token) config.headers['Authorization'] = `Bearer ${token}`;
config.headers['language'] = lang;
2026-04-21 10:33:48 +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
);
// 🔥 全局唯一锁:保证并发多个 401 也只执行一次
let isLoggingOut = false;
2026-05-11 16:34:44 +08:00
const handleLogoutAndRedirect = () => {
// 已经在登出流程中,直接阻止
if (isLoggingOut) return;
// 如果当前已经在登录页,不再跳转,防止死循环
if (window.location.pathname === '/login') {
return;
}
// 立即上锁
isLoggingOut = true;
2026-05-11 16:34:44 +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 }) => {
storeModule.default.dispatch(logout());
2026-05-11 16:34:44 +08:00
});
});
// 只提示一次
2026-08-12 11:29:44 +08:00
message?.error(i18n.t('api.loginExpired'));
// 只跳转一次
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;
// 业务 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;
},
(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
// 网络异常
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;