request.ts
4.21 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Axios 请求封装
*/
import type {
AxiosError,
AxiosInstance,
AxiosRequestConfig,
AxiosResponse,
InternalAxiosRequestConfig,
} from "axios";
import axios from "axios";
import { clearAuth, getToken } from "./auth";
// 创建 axios 实例
const service: AxiosInstance = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL || "/api",
timeout: 30_000,
headers: {
"Content-Type": "application/json",
},
});
// 请求拦截器
service.interceptors.request.use(
(config: InternalAxiosRequestConfig) => {
// 添加 token
const token = getToken();
if (token && config.headers) {
config.headers.Authorization = `${token}`;
}
// 添加时间戳防止缓存
if (config.method === "get" || config.method === "GET") {
config.params = {
...config.params,
_t: Date.now(),
};
}
return config;
},
(error: AxiosError) => {
console.error("请求错误:", error);
return Promise.reject(error);
},
);
// 响应拦截器
service.interceptors.response.use(
(response: AxiosResponse) => {
const res = response.data;
// 根据实际后端接口规范调整
// 假设后端返回格式: { code: number, data: any, message: string }
if (res && typeof res === "object" && "code" in res) {
const code = res.code as number;
if (code !== undefined && code !== 200 && code !== 0) {
const message = (res.message as string) || "未知错误";
console.error("接口错误:", message);
// 401: 未授权
if (code === 401) {
handleUnauthorized();
}
// 403: 无权限
if (code === 403) {
console.error("无权限访问");
}
return Promise.reject(new Error(message || "接口请求失败"));
}
}
return res;
},
(error: AxiosError) => {
console.error("响应错误:", error);
if (error.response) {
const { status, data } = error.response;
switch (status) {
case 401: {
handleUnauthorized();
break;
}
case 403: {
console.error("无权限访问");
break;
}
case 404: {
console.error("请求的资源不存在");
break;
}
case 500: {
console.error("服务器内部错误");
break;
}
default: {
console.error("请求失败:", data);
}
}
} else if (error.request) {
console.error("网络错误,请检查网络连接");
} else {
console.error("请求配置错误:", error.message);
}
return Promise.reject(error);
},
);
/**
* 处理未授权
*/
const handleUnauthorized = () => {
console.warn("登录已过期,请重新登录");
clearAuth();
// 跳转到登录页
if (window.location.pathname !== "/login") {
window.location.href = "/login";
}
};
/**
* 通用请求方法
* 注意:响应拦截器已处理数据提取,直接返回 data
*/
export const request = <T = any>(config: AxiosRequestConfig): Promise<T> => {
return service.request<T, T>(config) as Promise<T>;
};
/**
* GET 请求
* 注意:响应拦截器已处理数据提取,直接返回 data
*/
export const get = <T = any>(
url: string,
params?: Record<string, any>,
config?: AxiosRequestConfig,
): Promise<T> => {
return service.get<T, T>(url, { params, ...config }) as Promise<T>;
};
/**
* POST 请求
* 注意:响应拦截器已处理数据提取,直接返回 data
*/
export const post = <T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig,
): Promise<T> => {
return service.post<T, T>(url, data, config) as Promise<T>;
};
/**
* PUT 请求
* 注意:响应拦截器已处理数据提取,直接返回 data
*/
export const put = <T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig,
): Promise<T> => {
return service.put<T, T>(url, data, config) as Promise<T>;
};
/**
* DELETE 请求
* 注意:响应拦截器已处理数据提取,直接返回 data
*/
export const del = <T = any>(
url: string,
params?: Record<string, any>,
config?: AxiosRequestConfig,
): Promise<T> => {
return service.delete<T, T>(url, { params, ...config }) as Promise<T>;
};
/**
* PATCH 请求
* 注意:响应拦截器已处理数据提取,直接返回 data
*/
export const patch = <T = any>(
url: string,
data?: any,
config?: AxiosRequestConfig,
): Promise<T> => {
return service.patch<T, T>(url, data, config) as Promise<T>;
};
export default service;