|
@@ -26,45 +26,43 @@ const codeMessage = {
|
26
|
26
|
};
|
27
|
27
|
|
28
|
28
|
const replaceURLParams = (url, params = {}) => {
|
29
|
|
- return Object.keys(params).reduce((acc, k) => { // 此方法对每个元素进行处理
|
30
|
|
- const re = new RegExp(`:${k}(?!w)`, 'i')
|
31
|
|
- return acc.replace(re, params[k])
|
32
|
|
- }, url)
|
33
|
|
-}
|
|
29
|
+ return Object.keys(params).reduce((acc, k) => {
|
|
30
|
+ // 此方法对每个元素进行处理
|
|
31
|
+ const re = new RegExp(`:${k}(?!w)`, 'i');
|
|
32
|
+ return acc.replace(re, params[k]);
|
|
33
|
+ }, url);
|
|
34
|
+};
|
34
|
35
|
|
35
|
36
|
request.interceptors.request.use((url, options) => {
|
36
|
|
- const { urlData, headers = {}, logout = false, login = false, action, data, ...opts } = options
|
37
|
|
- const apiURL = urlData ? replaceURLParams(url, urlData) : url
|
38
|
|
- const token = mixStr(window.localStorage.getItem('test-foobar'))
|
|
37
|
+ const { urlData, headers = {}, logout = false, login = false, action, data, ...opts } = options;
|
|
38
|
+ const apiURL = urlData ? replaceURLParams(url, urlData) : url;
|
|
39
|
+ const token = mixStr(window.localStorage.getItem('test-foobar'));
|
39
|
40
|
|
40
|
41
|
if (login || logout) {
|
41
|
|
- window.localStorage.removeItem('test-foobar')
|
|
42
|
+ window.localStorage.removeItem('test-foobar');
|
42
|
43
|
}
|
43
|
44
|
|
44
|
|
- const authHeader = !login ? { Authorization: `Bearer ${token}` } : {}
|
45
|
|
- const actionHeader = action ? { 'X-ACTION': action }: {}
|
|
45
|
+ const authHeader = !login ? { Authorization: `Bearer ${token}` } : {};
|
|
46
|
+ const actionHeader = action ? { 'X-ACTION': action } : {};
|
46
|
47
|
|
47
|
|
- return (
|
48
|
|
- {
|
49
|
|
- url: apiURL,
|
50
|
|
- options: {
|
51
|
|
- ...opts,
|
52
|
|
- headers: {
|
53
|
|
- ...authHeader,
|
54
|
|
- ...actionHeader,
|
55
|
|
- ...headers,
|
56
|
|
- },
|
57
|
|
- data,
|
58
|
|
- requestType: data instanceof FormData ? 'form' : 'json',
|
59
|
|
- credentials: 'include', // 带 cookie
|
60
|
|
- interceptors: true
|
|
48
|
+ return {
|
|
49
|
+ url: apiURL,
|
|
50
|
+ options: {
|
|
51
|
+ ...opts,
|
|
52
|
+ headers: {
|
|
53
|
+ ...authHeader,
|
|
54
|
+ ...actionHeader,
|
|
55
|
+ ...headers,
|
61
|
56
|
},
|
62
|
|
- }
|
63
|
|
- );
|
|
57
|
+ data,
|
|
58
|
+ requestType: data instanceof FormData ? 'form' : 'json',
|
|
59
|
+ credentials: 'include', // 带 cookie
|
|
60
|
+ interceptors: true,
|
|
61
|
+ },
|
|
62
|
+ };
|
64
|
63
|
});
|
65
|
64
|
|
66
|
65
|
request.interceptors.response.use(async (response, options) => {
|
67
|
|
-
|
68
|
66
|
if (response && response.status) {
|
69
|
67
|
if (response.status != 200) {
|
70
|
68
|
const errorText = codeMessage[response.status] || response.statusText;
|
|
@@ -77,15 +75,23 @@ request.interceptors.response.use(async (response, options) => {
|
77
|
75
|
} else {
|
78
|
76
|
const { code, data, message } = await response.clone().json();
|
79
|
77
|
if (code != 1000) {
|
80
|
|
- notification.error({
|
81
|
|
- message: `请求错误`,
|
82
|
|
- description: message,
|
83
|
|
- });
|
84
|
|
- throw new Error(message);
|
|
78
|
+ if (code === 1001) {
|
|
79
|
+ notification.error({
|
|
80
|
+ message: `请求错误`,
|
|
81
|
+ description: '请登录系统',
|
|
82
|
+ });
|
|
83
|
+ throw new Error('请登录系统');
|
|
84
|
+ } else {
|
|
85
|
+ notification.error({
|
|
86
|
+ message: `请求错误`,
|
|
87
|
+ description: message,
|
|
88
|
+ });
|
|
89
|
+ throw new Error(message);
|
|
90
|
+ }
|
85
|
91
|
}
|
86
|
92
|
|
87
|
93
|
if (data && data.token) {
|
88
|
|
- window.localStorage.setItem('test-foobar', mixStr(data.token))
|
|
94
|
+ window.localStorage.setItem('test-foobar', mixStr(data.token));
|
89
|
95
|
}
|
90
|
96
|
|
91
|
97
|
return data;
|
|
@@ -98,16 +104,15 @@ request.interceptors.response.use(async (response, options) => {
|
98
|
104
|
}
|
99
|
105
|
});
|
100
|
106
|
|
101
|
|
-
|
102
|
|
-const fetch = api => options => request(api.url, {...api, ...options || {}})
|
|
107
|
+const fetch = api => options => request(api.url, { ...api, ...(options || {}) });
|
103
|
108
|
|
104
|
109
|
export default config => {
|
105
|
110
|
if (typeof config === 'string') {
|
106
|
111
|
return request(config);
|
107
|
112
|
} else {
|
108
|
|
- const {url, ...options} = config;
|
|
113
|
+ const { url, ...options } = config;
|
109
|
114
|
return request(url, options);
|
110
|
115
|
}
|
111
|
116
|
};
|
112
|
117
|
|
113
|
|
-export { fetch, apis }
|
|
118
|
+export { fetch, apis };
|