|
@@ -1,88 +1,99 @@
|
1
|
1
|
import axios from "axios";
|
2
|
|
-import React from 'react';
|
3
|
|
-import { message } from 'antd';
|
|
2
|
+import React from "react";
|
|
3
|
+import { message } from "antd";
|
4
|
4
|
|
5
|
5
|
const instance = axios.create({
|
6
|
|
- baseURL: import.meta.env.VITE_SERVER_BASE || '',
|
|
6
|
+ baseURL: import.meta.env.VITE_SERVER_BASE || "",
|
7
|
7
|
timeout: 10000,
|
8
|
8
|
withCredentials: false, // 跨域
|
9
|
9
|
});
|
10
|
10
|
|
11
|
|
-
|
12
|
11
|
// 添加请求拦截器
|
13
|
|
-instance.interceptors.request.use(function (config) {
|
14
|
|
- const { headers = {}, method = 'get', responseType = 'json', download = false, silent } = config;
|
15
|
|
- const token = localStorage.getItem('token') || '';
|
|
12
|
+instance.interceptors.request.use(
|
|
13
|
+ function (config) {
|
|
14
|
+ const {
|
|
15
|
+ headers = {},
|
|
16
|
+ method = "get",
|
|
17
|
+ responseType = "json",
|
|
18
|
+ download = false,
|
|
19
|
+ silent,
|
|
20
|
+ } = config;
|
|
21
|
+ const token = localStorage.getItem("token") || "";
|
|
22
|
+
|
|
23
|
+ let noTip = silent;
|
|
24
|
+ if (noTip === undefined) {
|
|
25
|
+ noTip = method.toLocaleLowerCase() === "get" ? true : false;
|
|
26
|
+ }
|
16
|
27
|
|
17
|
|
- let noTip = silent;
|
18
|
|
- if (noTip === undefined) {
|
19
|
|
- noTip = method.toLocaleLowerCase() === 'get' ? true : false;
|
|
28
|
+ // 在发送请求之前做些什么
|
|
29
|
+ return {
|
|
30
|
+ ...config,
|
|
31
|
+ silent: noTip,
|
|
32
|
+ timeout: download ? 600000 : 10000,
|
|
33
|
+ headers: {
|
|
34
|
+ "Content-Type": "application/json;charset=utf-8", // charset 防止乱码
|
|
35
|
+ ...headers,
|
|
36
|
+ Authorization: token,
|
|
37
|
+ },
|
|
38
|
+ responseType: download ? "blob" : responseType,
|
|
39
|
+ };
|
|
40
|
+ },
|
|
41
|
+ function (error) {
|
|
42
|
+ // 对请求错误做些什么
|
|
43
|
+ return Promise.reject(error);
|
20
|
44
|
}
|
21
|
|
-
|
22
|
|
- // 在发送请求之前做些什么
|
23
|
|
- return {
|
24
|
|
- ...config,
|
25
|
|
- silent: noTip,
|
26
|
|
- timeout: download ? 600000 : 10000,
|
27
|
|
- headers: {
|
28
|
|
- 'Content-Type': 'application/json;charset=utf-8', // charset 防止乱码
|
29
|
|
- ...headers,
|
30
|
|
- Authorization: token,
|
31
|
|
- },
|
32
|
|
- responseType: download ? 'blob' : responseType,
|
33
|
|
- };
|
34
|
|
-}, function (error) {
|
35
|
|
- // 对请求错误做些什么
|
36
|
|
- return Promise.reject(error);
|
37
|
|
-});
|
|
45
|
+);
|
38
|
46
|
|
39
|
47
|
// 添加响应拦截器
|
40
|
|
-instance.interceptors.response.use(function (response) {
|
41
|
|
- // 2xx 范围内的状态码都会触发该函数。
|
42
|
|
- // 对响应数据做点什么
|
43
|
|
-
|
44
|
|
- const { data, config } = response;
|
|
48
|
+instance.interceptors.response.use(
|
|
49
|
+ function (response) {
|
|
50
|
+ // 2xx 范围内的状态码都会触发该函数。
|
|
51
|
+ // 对响应数据做点什么
|
45
|
52
|
|
46
|
|
- if (config.download && !data.code) {
|
47
|
|
- return downloadBlob(response, '下载文件');
|
48
|
|
- }
|
|
53
|
+ const { data, config } = response;
|
49
|
54
|
|
50
|
|
- if (data.code === 1000) {
|
51
|
|
- if (data.data?.token) {
|
52
|
|
- localStorage.setItem('token', data.data.token);
|
|
55
|
+ if (config.download && !data.code) {
|
|
56
|
+ return downloadBlob(response, "下载文件");
|
53
|
57
|
}
|
54
|
58
|
|
55
|
|
- if (!config.silent) {
|
56
|
|
- message.success('操作成功');
|
|
59
|
+ if (data.code === 1000) {
|
|
60
|
+ if (data.data?.token) {
|
|
61
|
+ localStorage.setItem("token", data.data.token);
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ if (!config.silent) {
|
|
65
|
+ message.success("操作成功");
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ return data.data;
|
|
69
|
+ } else if (data.code === 1001) {
|
|
70
|
+ if (!config.silent) {
|
|
71
|
+ message.error("未登录或者超时, 请重新登录");
|
|
72
|
+ }
|
|
73
|
+ } else {
|
|
74
|
+ console.log(config);
|
|
75
|
+ if (!config.silent) {
|
|
76
|
+ const errMsg = data.message || "系统错误";
|
|
77
|
+ message.error(errMsg.indexOf("exception") > -1 ? "服务异常" : errMsg);
|
|
78
|
+ }
|
57
|
79
|
}
|
58
|
80
|
|
59
|
|
- return data.data;
|
60
|
|
- } else if (data.code === 1001) {
|
61
|
|
- if (!config.silent) {
|
62
|
|
- message.error('未登录或者超时, 请重新登录');
|
63
|
|
- }
|
64
|
|
- } else {
|
65
|
|
- console.log(config)
|
66
|
|
- if (!config.silent) {
|
67
|
|
- const errMsg = data.message || '系统错误';
|
68
|
|
- message.error(errMsg.indexOf('exception') > -1 ? '服务异常' : errMsg);
|
69
|
|
- }
|
70
|
|
- }
|
|
81
|
+ return Promise.reject(response);
|
|
82
|
+ },
|
|
83
|
+ function (error) {
|
|
84
|
+ // 超出 2xx 范围的状态码都会触发该函数。
|
|
85
|
+ // 对响应错误做点什么
|
71
|
86
|
|
72
|
|
- return Promise.reject(response);
|
73
|
|
-}, function (error) {
|
74
|
|
- // 超出 2xx 范围的状态码都会触发该函数。
|
75
|
|
- // 对响应错误做点什么
|
|
87
|
+ console.error(error);
|
76
|
88
|
|
77
|
|
- console.error(error);
|
78
|
|
-
|
79
|
|
- message.error('网络超时或异常...')
|
80
|
|
- return Promise.reject("网络异常, 请重试...");
|
81
|
|
-});
|
|
89
|
+ message.error("网络超时或异常...");
|
|
90
|
+ return Promise.reject("网络异常, 请重试...");
|
|
91
|
+ }
|
|
92
|
+);
|
82
|
93
|
|
83
|
94
|
export default instance;
|
84
|
95
|
|
85
|
|
-export function queryTable (apiRequest) {
|
|
96
|
+export function queryTable(apiRequest) {
|
86
|
97
|
return function (params) {
|
87
|
98
|
const { pageSize } = params;
|
88
|
99
|
return apiRequest({
|
|
@@ -105,7 +116,10 @@ export function queryTable (apiRequest) {
|
105
|
116
|
};
|
106
|
117
|
}
|
107
|
118
|
|
108
|
|
-export function queryDict (apiRequest, {labelKey = 'name', valueKey = 'id'} = {}) {
|
|
119
|
+export function queryDict(
|
|
120
|
+ apiRequest,
|
|
121
|
+ { labelKey = "name", valueKey = "id" } = {}
|
|
122
|
+) {
|
109
|
123
|
return function (params) {
|
110
|
124
|
const { current, pageSize, ...leftParams } = params || {};
|
111
|
125
|
return apiRequest({
|
|
@@ -128,48 +142,53 @@ export function queryDict (apiRequest, {labelKey = 'name', valueKey = 'id'} = {}
|
128
|
142
|
};
|
129
|
143
|
}
|
130
|
144
|
|
131
|
|
-export function restful (url) {
|
132
|
|
- const list = params => instance.get(url, { params });
|
133
|
|
- const get = id => instance.get(`${url}/${id}`);
|
134
|
|
- const add = data => instance.post(url, data);
|
|
145
|
+export function restful(url) {
|
|
146
|
+ const list = (params) => instance.get(url, { params });
|
|
147
|
+ const get = (id) => instance.get(`${url}/${id}`);
|
|
148
|
+ const add = (data) => instance.post(url, data);
|
135
|
149
|
const update = (id, data) => instance.put(`${url}/${id}`, data);
|
136
|
|
- const del = id => instance.delete(`${url}/${id}`);
|
|
150
|
+ const del = (id) => instance.delete(`${url}/${id}`);
|
137
|
151
|
|
138
|
152
|
return [list, get, add, update, del];
|
139
|
153
|
}
|
140
|
154
|
|
141
|
|
-export function useRequest (fn) {
|
|
155
|
+export function useRequest(fn) {
|
142
|
156
|
const [loading, setLoading] = React.useState(false);
|
143
|
157
|
|
144
|
|
- const p = (...args) => new Promise((resolve, reject) => {
|
145
|
|
- setLoading(true);
|
146
|
|
- fn(...args).then(res => {
|
147
|
|
- setLoading(false);
|
148
|
|
- resolve(res);
|
149
|
|
- }).catch(e => {
|
150
|
|
- setLoading(false);
|
151
|
|
- reject(e);
|
|
158
|
+ const p = (...args) =>
|
|
159
|
+ new Promise((resolve, reject) => {
|
|
160
|
+ setLoading(true);
|
|
161
|
+ fn(...args)
|
|
162
|
+ .then((res) => {
|
|
163
|
+ setLoading(false);
|
|
164
|
+ resolve(res);
|
|
165
|
+ })
|
|
166
|
+ .catch((e) => {
|
|
167
|
+ setLoading(false);
|
|
168
|
+ reject(e);
|
|
169
|
+ });
|
152
|
170
|
});
|
153
|
|
- });
|
154
|
171
|
|
155
|
|
- return [loading, p]
|
|
172
|
+ return [loading, p];
|
156
|
173
|
}
|
157
|
174
|
|
158
|
|
-function downloadBlob (response) {
|
159
|
|
- let fileName = '未知文件';
|
160
|
|
- const contentType = response.headers['content-type'];
|
161
|
|
- const contentDisposition = response.headers['content-disposition'];
|
|
175
|
+function downloadBlob(response) {
|
|
176
|
+ let fileName = "未知文件";
|
|
177
|
+ const contentType = response.headers["content-type"];
|
|
178
|
+ const contentDisposition = response.headers["content-disposition"];
|
162
|
179
|
if (contentDisposition) {
|
163
|
|
- const parts = contentDisposition.split(';filename=');
|
|
180
|
+ const parts = contentDisposition.split(";filename=");
|
164
|
181
|
if (parts[1]) {
|
165
|
182
|
fileName = decodeURIComponent(parts[1]);
|
166
|
183
|
}
|
167
|
184
|
}
|
168
|
185
|
|
169
|
|
- const url = window.URL.createObjectURL(new Blob([response.data], { type: contentType }));
|
170
|
|
- const link = document.createElement('a');
|
|
186
|
+ const url = window.URL.createObjectURL(
|
|
187
|
+ new Blob([response.data], { type: contentType })
|
|
188
|
+ );
|
|
189
|
+ const link = document.createElement("a");
|
171
|
190
|
link.href = url;
|
172
|
|
- link.setAttribute('download', fileName);
|
|
191
|
+ link.setAttribute("download", fileName);
|
173
|
192
|
link.click();
|
174
|
193
|
window.URL.revokeObjectURL(url);
|
175
|
194
|
}
|