|
@@ -1,30 +1,32 @@
|
1
|
1
|
import axios from "axios";
|
|
2
|
+import React from 'react';
|
2
|
3
|
import { message } from 'antd';
|
3
|
4
|
|
4
|
5
|
const instance = axios.create({
|
5
|
|
- baseURL: import.meta.env.PROD ? SERVER_BASE : import.meta.env.VITE_SERVER_BASE,
|
|
6
|
+ baseURL: import.meta.env.VITE_SERVER_BASE,
|
6
|
7
|
timeout: 10000,
|
|
8
|
+ withCredentials: true, // 跨域
|
7
|
9
|
});
|
8
|
10
|
|
9
|
11
|
|
10
|
12
|
// 添加请求拦截器
|
11
|
13
|
instance.interceptors.request.use(function (config) {
|
12
|
|
- const { headers = {}, method = 'get', responseType = 'json', download = false, successTip } = config;
|
|
14
|
+ const { headers = {}, method = 'get', responseType = 'json', download = false, silent } = config;
|
13
|
15
|
const token = localStorage.getItem('token') || '';
|
14
|
16
|
|
15
|
|
- let tip = successTip;
|
16
|
|
- if (tip === undefined) {
|
17
|
|
- tip = method === 'get' ? false : true;
|
|
17
|
+ let noTip = silent;
|
|
18
|
+ if (noTip === undefined) {
|
|
19
|
+ noTip = method.toLocaleLowerCase() === 'get' ? true : false;
|
18
|
20
|
}
|
19
|
21
|
|
20
|
22
|
// 在发送请求之前做些什么
|
21
|
23
|
return {
|
22
|
24
|
...config,
|
23
|
|
- successTip : tip,
|
|
25
|
+ silent: noTip,
|
24
|
26
|
headers: {
|
25
|
27
|
...headers,
|
26
|
28
|
Authorization: token,
|
27
|
|
- },
|
|
29
|
+ },
|
28
|
30
|
responseType: download ? 'blob' : responseType,
|
29
|
31
|
};
|
30
|
32
|
}, function (error) {
|
|
@@ -38,17 +40,18 @@ instance.interceptors.response.use(function (response) {
|
38
|
40
|
// 对响应数据做点什么
|
39
|
41
|
|
40
|
42
|
const { data, config } = response;
|
|
43
|
+
|
41
|
44
|
if (config.download && !data.code) {
|
42
|
45
|
return downloadBlob(response, '下载文件');
|
43
|
46
|
}
|
44
|
|
-
|
|
47
|
+
|
45
|
48
|
if (data.code === 1000) {
|
46
|
49
|
if (data.data.token) {
|
47
|
50
|
localStorage.setItem('token', data.data.token);
|
48
|
51
|
}
|
49
|
52
|
|
50
|
|
- if (config.successTip && !config.silent) {
|
51
|
|
- message.success(typeof success === 'string' ? successTip : '操作成功');
|
|
53
|
+ if (!config.silent) {
|
|
54
|
+ message.success('操作成功');
|
52
|
55
|
}
|
53
|
56
|
|
54
|
57
|
return data.data;
|
|
@@ -57,6 +60,7 @@ instance.interceptors.response.use(function (response) {
|
57
|
60
|
message.error('未登录或者超时, 请重新登录');
|
58
|
61
|
}
|
59
|
62
|
} else {
|
|
63
|
+ console.log(config)
|
60
|
64
|
if (!config.silent) {
|
61
|
65
|
const errMsg = data.message || '系统错误';
|
62
|
66
|
message.error(errMsg.indexOf('exception') > -1 ? '服务异常' : errMsg);
|
|
@@ -75,13 +79,13 @@ instance.interceptors.response.use(function (response) {
|
75
|
79
|
|
76
|
80
|
export default instance;
|
77
|
81
|
|
78
|
|
-export function queryTable(apiRequest) {
|
|
82
|
+export function queryTable (apiRequest) {
|
79
|
83
|
return function (params) {
|
80
|
|
- const { current, pageSize, ...leftParams } = params;
|
|
84
|
+ const { pageSize } = params;
|
81
|
85
|
return apiRequest({
|
|
86
|
+ ...params,
|
82
|
87
|
pageSize,
|
83
|
88
|
pageNum: params.current,
|
84
|
|
- ...(leftParams || {}),
|
85
|
89
|
})
|
86
|
90
|
.then((res) => {
|
87
|
91
|
return {
|
|
@@ -98,7 +102,7 @@ export function queryTable(apiRequest) {
|
98
|
102
|
};
|
99
|
103
|
}
|
100
|
104
|
|
101
|
|
-export function queryDict(apiRequest) {
|
|
105
|
+export function queryDict (apiRequest) {
|
102
|
106
|
return function (params, labelKey = 'name', valueKey = 'id') {
|
103
|
107
|
const { current, pageSize, ...leftParams } = params || {};
|
104
|
108
|
return apiRequest({
|
|
@@ -121,9 +125,9 @@ export function queryDict(apiRequest) {
|
121
|
125
|
};
|
122
|
126
|
}
|
123
|
127
|
|
124
|
|
-export function restful(url) {
|
125
|
|
- const list = params => instance.get(url, { params, successTip: false });
|
126
|
|
- const get = id => instance.get(`${url}/${id}`,{ successTip: false });
|
|
128
|
+export function restful (url) {
|
|
129
|
+ const list = params => instance.get(url, { params });
|
|
130
|
+ const get = id => instance.get(`${url}/${id}`);
|
127
|
131
|
const add = data => instance.post(url, data);
|
128
|
132
|
const update = (id, data) => instance.put(`${url}/${id}`, data);
|
129
|
133
|
const del = id => instance.delete(`${url}/${id}`);
|
|
@@ -131,7 +135,24 @@ export function restful(url) {
|
131
|
135
|
return [list, get, add, update, del];
|
132
|
136
|
}
|
133
|
137
|
|
134
|
|
-function downloadBlob(response) {
|
|
138
|
+export function useRequest (fn) {
|
|
139
|
+ const [loading, setLoading] = React.useState(false);
|
|
140
|
+
|
|
141
|
+ const p = (...args) => new Promise((resolve, reject) => {
|
|
142
|
+ setLoading(true);
|
|
143
|
+ fn(...args).then(res => {
|
|
144
|
+ setLoading(false);
|
|
145
|
+ resolve(res);
|
|
146
|
+ }).catch(e => {
|
|
147
|
+ setLoading(false);
|
|
148
|
+ reject(e);
|
|
149
|
+ });
|
|
150
|
+ });
|
|
151
|
+
|
|
152
|
+ return [loading, p]
|
|
153
|
+}
|
|
154
|
+
|
|
155
|
+function downloadBlob (response) {
|
135
|
156
|
let fileName = '未知文件';
|
136
|
157
|
const contentType = response.headers['content-type'];
|
137
|
158
|
const contentDisposition = response.headers['content-disposition'];
|