|
@@ -3,7 +3,16 @@ import qs from 'qs'
|
3
|
3
|
import router from '../router'
|
4
|
4
|
import { Message } from 'element-ui'
|
5
|
5
|
|
6
|
|
-var JWT = localStorage.getItem('JWT')
|
|
6
|
+const token = function (headers) {
|
|
7
|
+ let JWT
|
|
8
|
+ if (headers){
|
|
9
|
+ localStorage.setItem('JWT', headers.authorization)
|
|
10
|
+ JWT = headers.authorization
|
|
11
|
+ }
|
|
12
|
+ console.log(JWT)
|
|
13
|
+ return JWT ? JWT : localStorage.getItem('JWT')
|
|
14
|
+}
|
|
15
|
+
|
7
|
16
|
const Axios = axios.create({
|
8
|
17
|
timeout: 60000,
|
9
|
18
|
responseType: 'json',
|
|
@@ -12,11 +21,12 @@ const Axios = axios.create({
|
12
|
21
|
urlData: {},
|
13
|
22
|
headers: {
|
14
|
23
|
'Content-Type': 'multipart/form-data',
|
15
|
|
- 'authorization': JWT
|
|
24
|
+ 'authorization': token()
|
16
|
25
|
}
|
17
|
26
|
})
|
18
|
27
|
|
19
|
28
|
Axios.interceptors.request.use((config) => {
|
|
29
|
+ config.headers.authorization = token()
|
20
|
30
|
config.urlData = {...config.urlData, org: 'MQ'}
|
21
|
31
|
// 处理请求data,若为get请求,拼到url后面,若为post请求,直接添加到body中
|
22
|
32
|
let urlData = qs.stringify(config.urlData)
|
|
@@ -72,8 +82,7 @@ const ajax = (...args) => {
|
72
|
82
|
return new Promise((resolve, reject) => {
|
73
|
83
|
Axios(...args).then(({ data, headers }) => {
|
74
|
84
|
if (headers.authorization) {
|
75
|
|
- localStorage.setItem('JWT', headers.authorization)
|
76
|
|
- JWT = headers.authorization
|
|
85
|
+ token(headers)
|
77
|
86
|
}
|
78
|
87
|
const { code, message, result } = data
|
79
|
88
|
if (code === 200) {
|