wanshenmean
6 小时以前 f288ccc545f8cc32bc922c96dfb3cab9a1f92ec6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
 * axios 客户端封装
 * 统一配置:baseURL、timeout、请求/响应拦截器
 */
import axios from 'axios';
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
 
const BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:9291';
 
/** 创建 axios 实例 */
const client: AxiosInstance = axios.create({
  baseURL: BASE_URL,
  timeout: 30000,
  headers: { 'Content-Type': 'application/json' },
});
 
let isRefreshing = false;
let refreshQueue: Array<(token?: string) => void> = [];
 
/** 请求拦截器:注入 Bearer Token */
client.interceptors.request.use(
  (config: InternalAxiosRequestConfig) => {
    const userStr = localStorage.getItem('user');
    if (userStr) {
      try {
        const userData = JSON.parse(userStr);
        if (userData.token) {
          config.headers.Authorization = `Bearer ${userData.token}`;
        }
      } catch {}
    }
    return config;
  },
  (error) => Promise.reject(error)
);
 
/** 响应拦截器:统一错误处理 + 401 Token 过期跳转登录 */
client.interceptors.response.use(
  (response: AxiosResponse) => response.data,
  async (error) => {
    const status = error.response?.status;
    const data = error.response?.data;
 
    // 401 未授权
    if (status === 401 || data?.code === 401) {
      localStorage.removeItem('user');
      window.location.href = '/#/login';
      return Promise.reject(error);
    }
 
    return Promise.reject(error);
  }
);
 
export { client };
export type { AxiosRequestConfig };