index.js
3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import axios from 'axios'
import { message } from 'antd'
import SYS_CONFIG from '@src/config/constant.js'
import Storage from '@src/utils/localStorage'
export default class Axios {
static ajax(options) {
const userInfo = Storage.get('userInfo')
const isLogin = window.location.href.includes('/login')
return new Promise((resolve, reject) => {
if (!isLogin) {
if (!userInfo || !userInfo.token) {
window.location.href = '#/login'
return
}
}
axios({
...options,
url: options.url,
method: options.method ? options.method : 'POST',
baseURL: SYS_CONFIG.baseApi,
headers: options.headers ? options.headers : {
'Content-Type': 'application/json; charset=UTF-8',
'X-Requested-with': 'XMLHttpRequest',
'token': !isLogin ? userInfo.token : '',
},
params: options.params ? options.params : '',
data: JSON.stringify(options.data) === "{}" ? null : JSON.stringify(options.data),
}).then(res => {
if (res.status === 200) {
let resp = res.data;
if (res.data.code == 0) {
resolve(resp.data)
} else {
if ((res.data.msg && res.data.msg.includes('token')) || (res.data.msg && res.data.msg.includes('在别处登录')) ) {
Storage.clearToken()
window.location.href = '#/login'
return
}
if(res.data.msg && res.data.msg.length < 30){
message.error(res.data.msg)
return
}
// message.error('请求超时,请重试');
}
} else {
reject(res.data)
}
}).catch(err => {
message.error('请求超时,请重试');
reject(err)
})
})
}
static optAjax(options) {
const userInfo = Storage.get('userInfo')
const isLogin = window.location.href.includes('/login')
return new Promise((resolve, reject) => {
if (!isLogin) {
if (!userInfo || !userInfo.token) {
// message.warn('token失效,请重新登录')
window.location.href = '#/login'
return
}
}
axios({
...options,
url: options.url,
method: options.method ? options.method : 'POST',
baseURL: SYS_CONFIG.baseApi,
headers: options.headers ? options.headers : {
'Content-Type': 'application/json; charset=UTF-8',
'X-Requested-with': 'XMLHttpRequest',
'token': !isLogin ? userInfo.token : '',
},
params: options.params ? options.params : '',
data: JSON.stringify(options.data) === "{}" ? null : JSON.stringify(options.data),
}).then(res => {
if (res.status === 200) {
let resp = res.data;
if (res.data.code == 0) {
resolve(resp.data)
}else if(res.data.code === -2){
window.location.href = '#/login'
return
}else {
if ((res.data.msg && res.data.msg.includes('token')) || (res.data.msg && res.data.msg.includes('在别处登录')) ) {
Storage.clearToken()
window.location.href = '#/login'
return
}
if(res.data.msg && res.data.msg.length < 30){
message.error(res.data.msg)
resolve(resp)
return
}
// message.error('请求超时,请重试');
}
} else {
reject(res.data)
}
}).catch(err => {
message.error('请求超时,请重试');
reject(err)
})
})
}
}