fix(frontend): 给全局 axios 实例添加 Token 拦截器修复多页面列表加载失败
- 16 个页面文件 100+ 处直接使用 axios 而非封装的 api 实例 - 导致请求不携带 Token,后端返回 401 列表加载失败 - 给全局 axios 默认实例添加请求/响应拦截器自动附加 Token
This commit is contained in:
@@ -2,6 +2,31 @@ import axios from 'axios';
|
|||||||
import { API_CONFIG } from '../config/api';
|
import { API_CONFIG } from '../config/api';
|
||||||
import secureStorage, { TOKEN_KEY } from '../utils/secureStorage';
|
import secureStorage, { TOKEN_KEY } from '../utils/secureStorage';
|
||||||
|
|
||||||
|
// 给全局 axios 默认实例添加 Token 拦截器
|
||||||
|
// 确保所有页面中直接使用 axios.get/post 的请求也能自动携带 Token
|
||||||
|
axios.interceptors.request.use(config => {
|
||||||
|
const token = secureStorage.get(TOKEN_KEY);
|
||||||
|
if (token) {
|
||||||
|
config.headers.Authorization = `Bearer ${token}`;
|
||||||
|
}
|
||||||
|
return config;
|
||||||
|
});
|
||||||
|
|
||||||
|
axios.interceptors.response.use(
|
||||||
|
response => response,
|
||||||
|
error => {
|
||||||
|
if (error.response?.status === 401) {
|
||||||
|
const currentPath = window.location.pathname;
|
||||||
|
if (!currentPath.startsWith('/login')) {
|
||||||
|
secureStorage.remove(TOKEN_KEY);
|
||||||
|
secureStorage.remove('user');
|
||||||
|
window.location.href = '/login';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const api = axios.create({
|
const api = axios.create({
|
||||||
baseURL: API_CONFIG.baseURL,
|
baseURL: API_CONFIG.baseURL,
|
||||||
timeout: API_CONFIG.timeout,
|
timeout: API_CONFIG.timeout,
|
||||||
|
|||||||
Reference in New Issue
Block a user