tony.cheng
2026-02-05 c17c80a5d4b4ceb8f4347e43da14c0c31c0615f7
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
 * axios 封装
 * 包含请求/响应拦截器、统一错误处理、Loading状态管理
 */
 
import axios from 'axios';
import { message } from 'antd';
import config from '../config/env';
 
// 创建 axios 实例
const service = axios.create({
  baseURL: config.baseURL,
  timeout: config.timeout,
  withCredentials: config.withCredentials
});
 
// 请求计数器,用于Loading状态控制
let requestCount = 0;
// let loadingInstance = null; // 预留:全局Loading实例
 
// 显示Loading
const showLoading = () => {
  if (requestCount === 0) {
    // 这里可以集成全局Loading组件
    // loadingInstance = Loading.service({ fullscreen: true, text: '加载中...' });
  }
  requestCount++;
};
 
// 隐藏Loading
const hideLoading = () => {
  requestCount--;
  if (requestCount <= 0) {
    requestCount = 0;
    // if (loadingInstance) {
    //   loadingInstance.close();
    //   loadingInstance = null;
    // }
  }
};
 
// 请求拦截器
service.interceptors.request.use(
  (config) => {
    // 显示Loading
    showLoading();
    
    // 添加认证token(如果有的话)
    const token = localStorage.getItem('access_token');
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    
 
    
    // 打印请求日志(开发环境)
    if (process.env.NODE_ENV === 'development') {
      console.log('🚀 Request:', {
        url: config.url,
        method: config.method,
        params: config.params,
        data: config.data
      });
    }
    
    return config;
  },
  (error) => {
    hideLoading();
    console.error('Request Error:', error);
    return Promise.reject(error);
  }
);
 
// 响应拦截器
service.interceptors.response.use(
  (response) => {
    hideLoading();
    
    const res = response.data;
    
    // 打印响应日志(开发环境)
    if (process.env.NODE_ENV === 'development') {
      console.log('✅ Response:', res);
    }
    
    // 根据后端约定判断请求是否成功
    if (res.code !== 200 && res.code !== 201) {
      // 统一错误处理
      const errorMsg = res.message || '请求失败';
      
      // 特殊状态码处理
      switch (res.code) {
        case 401:
          message.error('登录已过期,请重新登录');
          // 可以跳转到登录页
          // window.location.href = '/login';
          break;
        case 403:
          message.error('没有权限访问该资源');
          break;
        case 404:
          message.error('请求的资源不存在');
          break;
        case 500:
          message.error('服务器内部错误');
          break;
        default:
          message.error(errorMsg);
      }
      
      return Promise.reject(new Error(errorMsg));
    }
    
    // 返回成功的数据
    return res;
  },
  (error) => {
    hideLoading();
    
    console.error('Response Error:', error);
    
    // 网络错误处理
    if (error.code === 'ECONNABORTED') {
      message.error('请求超时,请稍后重试');
    } else if (error.message.includes('Network Error')) {
      message.error('网络连接异常,请检查网络');
    } else {
      message.error(error.message || '请求失败');
    }
    
    return Promise.reject(error);
  }
);
 
// 封装通用请求方法
export const request = {
  // GET 请求
  get(url, params = {}, config = {}) {
    return service.get(url, {
      params,
      ...config
    });
  },
  
  // POST 请求
  post(url, data = {}, config = {}) {
    return service.post(url, data, config);
  },
  
  // PUT 请求
  put(url, data = {}, config = {}) {
    return service.put(url, data, config);
  },
  
  // DELETE 请求
  delete(url, config = {}) {
    return service.delete(url, config);
  },
  
  // PATCH 请求
  patch(url, data = {}, config = {}) {
    return service.patch(url, data, config);
  }
};
 
// 导出 axios 实例(供特殊场景使用)
export default service;