|
|
@@ -1,11 +1,11 @@
|
|
1
|
|
-import axios from 'axios'
|
|
2
|
|
-import qs from 'qs' // 引入axios时同时引入qs即可。
|
|
3
|
|
-import { Message, MessageBox } from 'element-ui'
|
|
4
|
|
-import store from '../store'
|
|
5
|
|
-import { getToken } from '@/utils/auth'
|
|
|
1
|
+import axios from "axios";
|
|
|
2
|
+import qs from "qs"; // 引入axios时同时引入qs即可。
|
|
|
3
|
+import { Message, MessageBox } from "element-ui";
|
|
|
4
|
+import store from "../store";
|
|
|
5
|
+import { getToken } from "@/utils/auth";
|
|
6
|
6
|
|
|
7
|
7
|
// 正在进行中的请求列表
|
|
8
|
|
-const reqList = []
|
|
|
8
|
+const reqList = [];
|
|
9
|
9
|
/**
|
|
10
|
10
|
* 阻止重复请求
|
|
11
|
11
|
* @param {array} reqList - 请求缓存列表
|
|
|
@@ -14,15 +14,15 @@ const reqList = []
|
|
14
|
14
|
* @param {string} errorMessage - 请求中断时需要显示的错误信息
|
|
15
|
15
|
*/
|
|
16
|
16
|
const stopRepeatRequest = function(reqList, url, cancel, errorMessage) {
|
|
17
|
|
- const errorMsg = errorMessage || ''
|
|
|
17
|
+ const errorMsg = errorMessage || "";
|
|
18
|
18
|
for (let i = 0; i < reqList.length; i++) {
|
|
19
|
19
|
if (reqList[i] === url) {
|
|
20
|
|
- cancel(errorMsg)
|
|
21
|
|
- return
|
|
|
20
|
+ cancel(errorMsg);
|
|
|
21
|
+ return;
|
|
22
|
22
|
}
|
|
23
|
23
|
}
|
|
24
|
|
- reqList.push(url)
|
|
25
|
|
-}
|
|
|
24
|
+ reqList.push(url);
|
|
|
25
|
+};
|
|
26
|
26
|
|
|
27
|
27
|
/**
|
|
28
|
28
|
* 允许某个请求可以继续进行
|
|
|
@@ -32,20 +32,32 @@ const stopRepeatRequest = function(reqList, url, cancel, errorMessage) {
|
|
32
|
32
|
const allowRequest = function(reqList, url) {
|
|
33
|
33
|
for (let i = 0; i < reqList.length; i++) {
|
|
34
|
34
|
if (reqList[i] === url) {
|
|
35
|
|
- reqList.splice(i, 1)
|
|
36
|
|
- break
|
|
|
35
|
+ reqList.splice(i, 1);
|
|
|
36
|
+ break;
|
|
37
|
37
|
}
|
|
38
|
38
|
}
|
|
39
|
|
- console.log(reqList)
|
|
40
|
|
-}
|
|
|
39
|
+ console.log(reqList);
|
|
|
40
|
+};
|
|
|
41
|
+axios.interceptors.request.use(
|
|
|
42
|
+ config => {
|
|
|
43
|
+ if (config.method === "post") {
|
|
|
44
|
+ config.data = qs.stringify(config.data);
|
|
|
45
|
+ }
|
|
41
|
46
|
|
|
|
47
|
+ return config;
|
|
|
48
|
+ },
|
|
|
49
|
+ error => {
|
|
|
50
|
+ return Promise.reject(error);
|
|
|
51
|
+ }
|
|
|
52
|
+);
|
|
42
|
53
|
// const baseURL = process.env.BASE_API
|
|
43
|
54
|
// 创建axios实例
|
|
44
|
55
|
const service = axios.create({
|
|
45
|
|
- // baseURL: baseURL, // api的base_url
|
|
|
56
|
+ // baseURL: baseURL, // api的base_url axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
|
|
|
57
|
+
|
|
46
|
58
|
timeout: 30000, // 请求超时时间
|
|
47
|
59
|
headers: {
|
|
48
|
|
- 'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
60
|
+ "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
|
|
49
|
61
|
},
|
|
50
|
62
|
// `transformRequest` 允许在向服务器发送前,修改请求数据
|
|
51
|
63
|
// 只能用在 'PUT', 'POST' 和 'PATCH' 这几个请求方法
|
|
|
@@ -54,7 +66,7 @@ const service = axios.create({
|
|
54
|
66
|
transformRequest: [
|
|
55
|
67
|
function(data) {
|
|
56
|
68
|
// 对 data 进行任意转换处理
|
|
57
|
|
- return qs.stringify(data)
|
|
|
69
|
+ return qs.stringify(data);
|
|
58
|
70
|
}
|
|
59
|
71
|
],
|
|
60
|
72
|
|
|
|
@@ -65,38 +77,40 @@ const service = axios.create({
|
|
65
|
77
|
// return qs.stringify({ids: [1, 2, 3]}, {arrayFormat: 'brackets'}) // 形式:ids[]=1&ids[]=2&ids[]=3
|
|
66
|
78
|
// return qs.stringify({ids: [1, 2, 3]}, {arrayFormat: 'repeat'}) // 形式: ids=1&ids=2&id=3
|
|
67
|
79
|
paramsSerializer: function(params) {
|
|
68
|
|
- return qs.stringify(params, { arrayFormat: 'indices' }) // 形式:ids=1&ids=2&id=3
|
|
|
80
|
+ return qs.stringify(params, { arrayFormat: "indices" }); // 形式:ids=1&ids=2&id=3
|
|
69
|
81
|
}
|
|
70
|
|
-})
|
|
|
82
|
+});
|
|
71
|
83
|
// request拦截器
|
|
72
|
84
|
service.interceptors.request.use(
|
|
73
|
85
|
config => {
|
|
74
|
86
|
if (store.getters.token) {
|
|
75
|
|
- config.headers['Authorization'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
|
|
|
87
|
+ // config.headers["Authorization"] = getToken(); // 让每个请求携带自定义token 请根据实际情况自行修改 Bearer
|
|
76
|
88
|
}
|
|
77
|
|
- let cancel
|
|
78
|
|
- if (config.method === 'post') {
|
|
|
89
|
+ config.url = config.url + "?token=" + getToken();
|
|
|
90
|
+ let cancel;
|
|
|
91
|
+ if (config.method === "post") {
|
|
79
|
92
|
// 设置cancelToken对象
|
|
80
|
93
|
config.cancelToken = new axios.CancelToken(function(c) {
|
|
81
|
|
- cancel = c
|
|
82
|
|
- })
|
|
|
94
|
+ cancel = c;
|
|
|
95
|
+ });
|
|
83
|
96
|
// 阻止重复请求。当上个请求未完成时,相同的请求不会进行
|
|
84
|
97
|
stopRepeatRequest(
|
|
85
|
98
|
reqList,
|
|
86
|
99
|
config.url,
|
|
87
|
100
|
cancel,
|
|
88
|
101
|
`${config.url} 请求被中断`
|
|
89
|
|
- )
|
|
|
102
|
+ );
|
|
90
|
103
|
// stopRepeatRequest(reqList, config.url, cancel, `请求被中断`)
|
|
|
104
|
+ } else {
|
|
91
|
105
|
}
|
|
92
|
106
|
|
|
93
|
|
- return config
|
|
|
107
|
+ return config;
|
|
94
|
108
|
},
|
|
95
|
109
|
error => {
|
|
96
|
110
|
// Do something with request error
|
|
97
|
|
- Promise.reject(error)
|
|
|
111
|
+ Promise.reject(error);
|
|
98
|
112
|
}
|
|
99
|
|
-)
|
|
|
113
|
+);
|
|
100
|
114
|
|
|
101
|
115
|
// respone拦截器
|
|
102
|
116
|
service.interceptors.response.use(
|
|
|
@@ -105,130 +119,130 @@ service.interceptors.response.use(
|
|
105
|
119
|
* state为非"success"时抛错
|
|
106
|
120
|
*/
|
|
107
|
121
|
// 增加延迟,相同请求不得在短时间内重复发送
|
|
108
|
|
- if (response.config.method === 'post') {
|
|
|
122
|
+ if (response.config.method === "post") {
|
|
109
|
123
|
setTimeout(() => {
|
|
110
|
|
- const replaceList = response.config.url.split('/')
|
|
|
124
|
+ const replaceList = response.config.url.split("/");
|
|
111
|
125
|
const newUrl =
|
|
112
|
126
|
replaceList[replaceList.length - 2] +
|
|
113
|
|
- '/' +
|
|
114
|
|
- replaceList[replaceList.length - 1]
|
|
115
|
|
- allowRequest(reqList, newUrl)
|
|
116
|
|
- }, 1000)
|
|
|
127
|
+ "/" +
|
|
|
128
|
+ replaceList[replaceList.length - 1];
|
|
|
129
|
+ allowRequest(reqList, newUrl);
|
|
|
130
|
+ }, 1000);
|
|
117
|
131
|
}
|
|
118
|
|
- const res = response.data
|
|
119
|
|
- if (res.state && res.state.toLowerCase() !== 'success') {
|
|
|
132
|
+ const res = response.data;
|
|
|
133
|
+ if (res.state && res.state.toLowerCase() !== "success") {
|
|
120
|
134
|
Message({
|
|
121
|
135
|
message: res.message,
|
|
122
|
|
- type: 'error',
|
|
|
136
|
+ type: "error",
|
|
123
|
137
|
duration: 5 * 1000
|
|
124
|
|
- })
|
|
|
138
|
+ });
|
|
125
|
139
|
// 50008:非法的token; 50012:其他客户端登录了; "notoken":Token 过期了
|
|
126
|
140
|
if (
|
|
127
|
141
|
res.state.toLowerCase() === 50008 ||
|
|
128
|
142
|
res.state.toLowerCase() === 50012 ||
|
|
129
|
|
- res.state.toLowerCase() === 'notoken'
|
|
|
143
|
+ res.state.toLowerCase() === "notoken"
|
|
130
|
144
|
) {
|
|
131
|
145
|
MessageBox.confirm(
|
|
132
|
|
- '你已被登出,可以取消继续留在该页面,或者重新登录',
|
|
133
|
|
- '确定登出',
|
|
|
146
|
+ "你已被登出,可以取消继续留在该页面,或者重新登录",
|
|
|
147
|
+ "确定登出",
|
|
134
|
148
|
{
|
|
135
|
|
- confirmButtonText: '重新登录',
|
|
136
|
|
- cancelButtonText: '取消',
|
|
137
|
|
- type: 'warning'
|
|
|
149
|
+ confirmButtonText: "重新登录",
|
|
|
150
|
+ cancelButtonText: "取消",
|
|
|
151
|
+ type: "warning"
|
|
138
|
152
|
}
|
|
139
|
153
|
).then(() => {
|
|
140
|
|
- store.dispatch('FedLogOut').then(() => {
|
|
141
|
|
- location.reload() // 为了重新实例化vue-router对象 避免bug
|
|
142
|
|
- })
|
|
143
|
|
- })
|
|
|
154
|
+ store.dispatch("FedLogOut").then(() => {
|
|
|
155
|
+ location.reload(); // 为了重新实例化vue-router对象 避免bug
|
|
|
156
|
+ });
|
|
|
157
|
+ });
|
|
144
|
158
|
}
|
|
145
|
|
- return Promise.reject('error')
|
|
|
159
|
+ return Promise.reject("error");
|
|
146
|
160
|
} else {
|
|
147
|
|
- if (response.config.responseType === 'blob') {
|
|
148
|
|
- return response
|
|
|
161
|
+ if (response.config.responseType === "blob") {
|
|
|
162
|
+ return response;
|
|
149
|
163
|
}
|
|
150
|
|
- return response.data
|
|
|
164
|
+ return response.data;
|
|
151
|
165
|
}
|
|
152
|
166
|
},
|
|
153
|
167
|
error => {
|
|
154
|
|
- if (error && !error.message && error.response.config.method === 'post') {
|
|
|
168
|
+ if (error && !error.message && error.response.config.method === "post") {
|
|
155
|
169
|
setTimeout(() => {
|
|
156
|
|
- const replaceList = error.response.config.url.split('/')
|
|
|
170
|
+ const replaceList = error.response.config.url.split("/");
|
|
157
|
171
|
const newUrl =
|
|
158
|
172
|
replaceList[replaceList.length - 2] +
|
|
159
|
|
- '/' +
|
|
160
|
|
- replaceList[replaceList.length - 1]
|
|
161
|
|
- allowRequest(reqList, newUrl)
|
|
162
|
|
- }, 500)
|
|
|
173
|
+ "/" +
|
|
|
174
|
+ replaceList[replaceList.length - 1];
|
|
|
175
|
+ allowRequest(reqList, newUrl);
|
|
|
176
|
+ }, 500);
|
|
163
|
177
|
}
|
|
164
|
|
- console.log('err:' + error) // for debug
|
|
|
178
|
+ console.log("err:" + error); // for debug
|
|
165
|
179
|
if (error && error.response) {
|
|
166
|
180
|
switch (
|
|
167
|
181
|
error.response.status // 跨域存在获取不到状态码的情况
|
|
168
|
182
|
) {
|
|
169
|
183
|
case 400:
|
|
170
|
|
- error.message = `请求错误(400):${error.response.config.url}`
|
|
171
|
|
- break
|
|
|
184
|
+ error.message = `请求错误(400):${error.response.config.url}`;
|
|
|
185
|
+ break;
|
|
172
|
186
|
|
|
173
|
187
|
case 401:
|
|
174
|
|
- error.message = `未授权,请登录(401):${error.response.config.url}`
|
|
175
|
|
- store.dispatch('FedLogOut').then(() => {
|
|
176
|
|
- location.reload() // 为了重新实例化vue-router对象 避免bug
|
|
177
|
|
- })
|
|
178
|
|
- break
|
|
|
188
|
+ error.message = `未授权,请登录(401):${error.response.config.url}`;
|
|
|
189
|
+ store.dispatch("FedLogOut").then(() => {
|
|
|
190
|
+ location.reload(); // 为了重新实例化vue-router对象 避免bug
|
|
|
191
|
+ });
|
|
|
192
|
+ break;
|
|
179
|
193
|
|
|
180
|
194
|
case 403:
|
|
181
|
|
- error.message = `拒绝访问(403):${error.response.config.url}`
|
|
182
|
|
- break
|
|
|
195
|
+ error.message = `拒绝访问(403):${error.response.config.url}`;
|
|
|
196
|
+ break;
|
|
183
|
197
|
|
|
184
|
198
|
case 404:
|
|
185
|
|
- error.message = `请求地址出错(404): ${error.response.config.url}`
|
|
186
|
|
- break
|
|
|
199
|
+ error.message = `请求地址出错(404): ${error.response.config.url}`;
|
|
|
200
|
+ break;
|
|
187
|
201
|
|
|
188
|
202
|
case 408:
|
|
189
|
|
- error.message = `请求超时(408):${error.response.config.url}`
|
|
190
|
|
- break
|
|
|
203
|
+ error.message = `请求超时(408):${error.response.config.url}`;
|
|
|
204
|
+ break;
|
|
191
|
205
|
|
|
192
|
206
|
case 500:
|
|
193
|
|
- error.message = `服务器内部错误(500):${error.response.config.url}`
|
|
194
|
|
- break
|
|
|
207
|
+ error.message = `服务器内部错误(500):${error.response.config.url}`;
|
|
|
208
|
+ break;
|
|
195
|
209
|
|
|
196
|
210
|
case 501:
|
|
197
|
|
- error.message = `服务未实现(501):${error.response.config.url}`
|
|
198
|
|
- break
|
|
|
211
|
+ error.message = `服务未实现(501):${error.response.config.url}`;
|
|
|
212
|
+ break;
|
|
199
|
213
|
|
|
200
|
214
|
case 502:
|
|
201
|
|
- error.message = `网关错误(502):${error.response.config.url}`
|
|
202
|
|
- break
|
|
|
215
|
+ error.message = `网关错误(502):${error.response.config.url}`;
|
|
|
216
|
+ break;
|
|
203
|
217
|
|
|
204
|
218
|
case 503:
|
|
205
|
|
- error.message = `服务不可用(503):${error.response.config.url}`
|
|
206
|
|
- break
|
|
|
219
|
+ error.message = `服务不可用(503):${error.response.config.url}`;
|
|
|
220
|
+ break;
|
|
207
|
221
|
|
|
208
|
222
|
case 504:
|
|
209
|
|
- error.message = `网关超时(504):${error.response.config.url}`
|
|
210
|
|
- break
|
|
|
223
|
+ error.message = `网关超时(504):${error.response.config.url}`;
|
|
|
224
|
+ break;
|
|
211
|
225
|
|
|
212
|
226
|
case 505:
|
|
213
|
|
- error.message = `HTTP版本不受支持(505):${error.response.config.url}`
|
|
214
|
|
- break
|
|
|
227
|
+ error.message = `HTTP版本不受支持(505):${error.response.config.url}`;
|
|
|
228
|
+ break;
|
|
215
|
229
|
|
|
216
|
230
|
default:
|
|
217
|
|
- error.message = `连接错误:${error.response.status}`
|
|
218
|
|
- break
|
|
|
231
|
+ error.message = `连接错误:${error.response.status}`;
|
|
|
232
|
+ break;
|
|
219
|
233
|
}
|
|
220
|
|
- } else if (error.message.indexOf('请求被中断') !== -1) {
|
|
221
|
|
- error.message = '请勿重复请求!'
|
|
|
234
|
+ } else if (error.message.indexOf("请求被中断") !== -1) {
|
|
|
235
|
+ error.message = "请勿重复请求!";
|
|
222
|
236
|
} else {
|
|
223
|
|
- error.message = '网络出现问题,请稍后重试!'
|
|
|
237
|
+ error.message = "网络出现问题,请稍后重试!";
|
|
224
|
238
|
}
|
|
225
|
239
|
Message({
|
|
226
|
240
|
message: error.message,
|
|
227
|
|
- type: 'error',
|
|
|
241
|
+ type: "error",
|
|
228
|
242
|
duration: 5 * 1000
|
|
229
|
|
- })
|
|
230
|
|
- return Promise.reject(error)
|
|
|
243
|
+ });
|
|
|
244
|
+ return Promise.reject(error);
|
|
231
|
245
|
}
|
|
232
|
|
-)
|
|
|
246
|
+);
|
|
233
|
247
|
|
|
234
|
|
-export default service
|
|
|
248
|
+export default service;
|