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
|
|
|
|
import store from '../store';
|
|
|
|
|
|
import { logout } from '../store/userSlice';
|
2026-04-17 17:20:41 +08:00
|
|
|
|
|
2026-04-21 10:13:31 +08:00
|
|
|
|
|
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,
|
|
|
|
|
|
timeout: 20000,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
2026-04-21 10:13:31 +08:00
|
|
|
|
|
2026-04-21 10:33:48 +08:00
|
|
|
|
},
|
|
|
|
|
|
withCredentials: true,
|
2026-04-17 17:20:41 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2026-04-21 10:13:31 +08:00
|
|
|
|
// 请求拦截器:自动添加token
|
2026-04-17 17:20:41 +08:00
|
|
|
|
// 请求拦截器
|
2026-04-21 10:13:31 +08:00
|
|
|
|
api.interceptors.request.use(
|
2026-04-21 10:33:48 +08:00
|
|
|
|
(config: InternalAxiosRequestConfig) => {
|
|
|
|
|
|
// 从 localStorage 获取 token
|
|
|
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
|
|
if (token && config.headers) {
|
|
|
|
|
|
// 将 token 添加到请求头
|
|
|
|
|
|
config.headers['Authorization'] = `Bearer ${token}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
return config;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error: AxiosError) => {
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
2026-04-17 17:20:41 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 响应拦截器
|
2026-04-21 10:13:31 +08:00
|
|
|
|
api.interceptors.response.use(
|
2026-04-21 10:33:48 +08:00
|
|
|
|
(response: AxiosResponse) => {
|
|
|
|
|
|
// 处理成功响应
|
|
|
|
|
|
const { data } = response;
|
|
|
|
|
|
// 如果后端规范定义了 code,可以在这里统一处理
|
|
|
|
|
|
// if (data.code !== 200) {
|
|
|
|
|
|
// message.error(data.message || '请求失败');
|
|
|
|
|
|
// return Promise.reject(new Error(data.message || 'Error'));
|
|
|
|
|
|
// }
|
|
|
|
|
|
return data;
|
|
|
|
|
|
},
|
|
|
|
|
|
(error: AxiosError) => {
|
|
|
|
|
|
// 处理错误响应
|
|
|
|
|
|
if (error.response) {
|
|
|
|
|
|
switch (error.response.status) {
|
|
|
|
|
|
case 401:
|
|
|
|
|
|
// 未授权,清除本地信息并跳转到登录页
|
|
|
|
|
|
localStorage.removeItem('token');
|
|
|
|
|
|
localStorage.removeItem('isAuthenticated');
|
|
|
|
|
|
localStorage.removeItem('user');
|
|
|
|
|
|
store.dispatch(logout());
|
|
|
|
|
|
window.location.href = '/login';
|
|
|
|
|
|
message.error('会话已过期,请重新登录');
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 403:
|
|
|
|
|
|
message.error('拒绝访问:权限不足');
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 404:
|
|
|
|
|
|
message.error('资源不存在');
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 500:
|
|
|
|
|
|
message.error('服务器内部错误');
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
message.error(error.message || '网络错误');
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
message.error('连接服务器失败');
|
|
|
|
|
|
}
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
2026-04-17 17:20:41 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
2026-04-21 10:13:31 +08:00
|
|
|
|
export default api;
|