2025-10-10 17:26:52 +08:00
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
|
|
// 创建axios实例
|
|
|
|
|
const api = axios.create({
|
|
|
|
|
baseURL: process.env.REACT_APP_API_BASE_URL || 'http://localhost:5000',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
}
|
2025-10-10 17:33:07 +08:00
|
|
|
|
|
|
|
|
//测试
|
2025-10-10 17:26:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 请求拦截器(可选)
|
|
|
|
|
api.interceptors.request.use(
|
|
|
|
|
(config) => {
|
|
|
|
|
// 可以在这里添加token等通用信息
|
|
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
|
if (token) {
|
|
|
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 响应拦截器
|
|
|
|
|
api.interceptors.response.use(
|
|
|
|
|
(response) => {
|
|
|
|
|
return response;
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
if (error.response) {
|
|
|
|
|
// 处理401未授权错误
|
|
|
|
|
if (error.response.status === 401) {
|
|
|
|
|
localStorage.removeItem('token');
|
|
|
|
|
window.location.href = '/login';
|
|
|
|
|
}
|
|
|
|
|
// 处理403权限不足错误
|
|
|
|
|
if (error.response.status === 403) {
|
|
|
|
|
console.error('权限不足:', error.response.data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export default api;
|