|
@@ -26,43 +26,46 @@ const codeMessage = {
|
26
|
26
|
};
|
27
|
27
|
|
28
|
28
|
const replaceURLParams = (url, params = {}) => {
|
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
|
|
-};
|
|
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
|
+}
|
35
|
34
|
|
36
|
35
|
request.interceptors.request.use((url, options) => {
|
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'));
|
|
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'))
|
40
|
39
|
|
41
|
40
|
if (login || logout) {
|
42
|
|
- window.localStorage.removeItem('test-foobar');
|
|
41
|
+ window.localStorage.removeItem('test-foobar')
|
43
|
42
|
}
|
44
|
43
|
|
45
|
|
- const authHeader = !login ? { Authorization: `Bearer ${token}` } : {};
|
46
|
|
- const actionHeader = action ? { 'X-ACTION': action } : {};
|
|
44
|
+ const authHeader = !login ? { Authorization: `Bearer ${token}` } : {}
|
|
45
|
+ const actionHeader = action ? { 'X-ACTION': action }: {}
|
47
|
46
|
|
48
|
|
- return {
|
49
|
|
- url: apiURL,
|
50
|
|
- options: {
|
51
|
|
- ...opts,
|
52
|
|
- headers: {
|
53
|
|
- ...authHeader,
|
54
|
|
- ...actionHeader,
|
55
|
|
- ...headers,
|
|
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,
|
|
61
|
+ getResponse: true,
|
56
|
62
|
},
|
57
|
|
- data,
|
58
|
|
- requestType: data instanceof FormData ? 'form' : 'json',
|
59
|
|
- credentials: 'include', // 带 cookie
|
60
|
|
- interceptors: true,
|
61
|
|
- },
|
62
|
|
- };
|
|
63
|
+ }
|
|
64
|
+ );
|
63
|
65
|
});
|
64
|
66
|
|
65
|
67
|
request.interceptors.response.use(async (response, options) => {
|
|
68
|
+
|
66
|
69
|
if (response && response.status) {
|
67
|
70
|
if (response.status != 200) {
|
68
|
71
|
const errorText = codeMessage[response.status] || response.statusText;
|
|
@@ -73,6 +76,12 @@ request.interceptors.response.use(async (response, options) => {
|
73
|
76
|
});
|
74
|
77
|
throw new Error(response.statusText);
|
75
|
78
|
} else {
|
|
79
|
+ console.log('response.headers: ', response.headers)
|
|
80
|
+ console.log('response.headers.Content-Type: ', response.headers.get('Content-Type'))
|
|
81
|
+ if (response.headers.get('Content-Type') === 'application/octet-stream;charset=utf-8') {
|
|
82
|
+ return await response.clone().blob();
|
|
83
|
+ }
|
|
84
|
+
|
76
|
85
|
const { code, data, message } = await response.clone().json();
|
77
|
86
|
if (code != 1000) {
|
78
|
87
|
if (code === 1001) {
|
|
@@ -91,7 +100,7 @@ request.interceptors.response.use(async (response, options) => {
|
91
|
100
|
}
|
92
|
101
|
|
93
|
102
|
if (data && data.token) {
|
94
|
|
- window.localStorage.setItem('test-foobar', mixStr(data.token));
|
|
103
|
+ window.localStorage.setItem('test-foobar', mixStr(data.token))
|
95
|
104
|
}
|
96
|
105
|
|
97
|
106
|
return data;
|
|
@@ -104,15 +113,16 @@ request.interceptors.response.use(async (response, options) => {
|
104
|
113
|
}
|
105
|
114
|
});
|
106
|
115
|
|
107
|
|
-const fetch = api => options => request(api.url, { ...api, ...(options || {}) });
|
|
116
|
+
|
|
117
|
+const fetch = api => options => request(api.url, {...api, ...options || {}})
|
108
|
118
|
|
109
|
119
|
export default config => {
|
110
|
120
|
if (typeof config === 'string') {
|
111
|
121
|
return request(config);
|
112
|
122
|
} else {
|
113
|
|
- const { url, ...options } = config;
|
|
123
|
+ const {url, ...options} = config;
|
114
|
124
|
return request(url, options);
|
115
|
125
|
}
|
116
|
126
|
};
|
117
|
127
|
|
118
|
|
-export { fetch, apis };
|
|
128
|
+export { fetch, apis }
|