33 lines
738 B
TypeScript
33 lines
738 B
TypeScript
|
|
import axios from "axios";
|
||
|
|
|
||
|
|
const api = axios.create({
|
||
|
|
baseURL: import.meta.env.VITE_API_BASE_URL,
|
||
|
|
timeout: 10000,
|
||
|
|
headers: {
|
||
|
|
"Content-Type": "application/json",
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
// 请求拦截器 — 可在此注入 token
|
||
|
|
api.interceptors.request.use(
|
||
|
|
(config) => {
|
||
|
|
// const token = localStorage.getItem("access_token");
|
||
|
|
// if (token) config.headers.Authorization = `Bearer ${token}`;
|
||
|
|
return config;
|
||
|
|
},
|
||
|
|
(error) => Promise.reject(error),
|
||
|
|
);
|
||
|
|
|
||
|
|
// 响应拦截器 — 统一错误处理
|
||
|
|
api.interceptors.response.use(
|
||
|
|
(response) => response,
|
||
|
|
(error) => {
|
||
|
|
if (error.response?.status === 401) {
|
||
|
|
// 未授权:跳转登录 / 刷新 token
|
||
|
|
}
|
||
|
|
return Promise.reject(error);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
export default api;
|