export.js
1.36 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
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) {
//message.warn('token失效,请重新登录')
window.location.href = '#/login'
return
}
}
axios({
...options,
url: options.url,
method: options.method ? options.method : 'POST',
baseURL: SYS_CONFIG.baseApi,
responseType: 'blob',
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) {
resolve(res)
} else {
reject(res.data)
}
}).catch(err => {
message.error('请求超时,请重试');
reject(err)
})
})
}
}