|
@@ -10,8 +10,23 @@ const Axios = axios.create({
|
10
|
10
|
})
|
11
|
11
|
|
12
|
12
|
Axios.interceptors.request.use((config) => {
|
13
|
|
- console.log(config)
|
14
|
|
- config.data = qs.stringify(config.data)
|
|
13
|
+ // 处理请求data,若为get请求,拼到url后面,若为post请求,直接添加到body中
|
|
14
|
+ let data = qs.stringify(config.data)
|
|
15
|
+ if (config.method === 'get' || config.method === 'GET') {
|
|
16
|
+ // 判断是通过斜杠传参还是通过query传参
|
|
17
|
+ if (config.url.indexOf(':') > -1) {
|
|
18
|
+ if (typeof config.data === 'object') {
|
|
19
|
+ config.url = Object.keys(config.data).reduce((url, k) => { // 此方法对每个元素进行处理
|
|
20
|
+ const re = new RegExp(`:${k}(?!w)`, 'i')
|
|
21
|
+ return url.replace(re, config.data[k])
|
|
22
|
+ }, config.url)
|
|
23
|
+ } else {
|
|
24
|
+ config.url = config.url.slice(0, config.url.indexOf(':')) + data
|
|
25
|
+ }
|
|
26
|
+ } else {
|
|
27
|
+ config.url += '?' + data
|
|
28
|
+ }
|
|
29
|
+ }
|
15
|
30
|
return config
|
16
|
31
|
}, (error) => {
|
17
|
32
|
console.log(error)
|