request.js
3.64 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
import axios from 'axios'
import {
MessageBox,
Message
} from 'element-ui'
import store from '@/store'
import {
getToken
} from '@/utils/auth'
// create an axios instance
// 创建axios实例
const service = axios.create({
// baseURL: '', // url = base url + request url
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
// withCredentials: true, // send cookies when cross-domain requests
timeout: 5000, // request timeout
// headers: {
// 'Content-Type': 'application/x-www-form-urlencoded'
// }
})
// request interceptor
// request拦截器
service.interceptors.request.use(
config => {
// do something before request is sent
// console.log('do something before request is sent')
if (store.getters.token) {
// console.log('[X-Token] is a custom headers key')
// let each request carry token
// ['X-Token'] is a custom headers key
// please modify it according to the actual situation
config.headers['X-Token'] = getToken();
// config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
// var token = getToken()
// Object.assign(config.headers, { 'token': token })
}
return config
},
error => {
// console.log('do something with request error')
// do something with request error
console.error('出现错误。在use里。======>', error) // for debug
return Promise.reject(error)
}
)
// response interceptor
// respone拦截器
service.interceptors.response.use(
/**
* If you want to get http information such as headers or status
* Please return response => response
*/
/**
* Determine the request status by custom code
* Here is just an example
* You can also judge the status by HTTP Status Code
*/
response => {
const res = response.data
console.log('返回的数据-- step 1------>', res);
/**
* 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页
* 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中
*/
// console.log('if the custom code is not 20000, it is judged as an error.')
if (res.code !== 20000) {
Message({
message: res.message || 'Error',
type: 'error',
duration: 5 * 1000
})
// 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了;
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
// to re-login
// MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', {
// confirmButtonText: 'Re-Login',
confirmButtonText: '重新登录',
// cancelButtonText: 'Cancel',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
store.dispatch('user/resetToken').then(() => {
location.reload() // 为了重新实例化vue-router对象 避免bug
})
})
}
return Promise.reject(new Error(res.message || 'Error'))
} else {
// const token = res.data.token;
console.log('进入20000号判断- step 2------->', res.data);
const data = res.data;
return data;
}
},
error => {
console.error('===============发生错误!!!!!===============' + error.message); // for debug
// Message({
// message: error.message,
// type: 'error',
// duration: 5 * 1000
// })
return Promise.reject(error)
}
)
export default service