http.js
1.23 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
import axios from 'axios'
import router from './router'
// 请求拦截器
axios.interceptors.request.use(
config => {
// 发送请求前
let hash = location.hash.split('?')[0];
config.headers['Step-Url'] = location.pathname + hash;
if (config.params) {
config.params = _.merge(config.params, { version: 'v.1.1.0 ' })
} else {
config.params = {
version: 'v.1.1.0'
}
}
return config;
},
error => {
// 请求错误处理
return Promise.reject(error);
})
// 响应拦截器
axios.interceptors.response.use(
response => {
return response;
},
error => {
if (error.response) {
switch (error.response.status) {
case 401:
if (window.location.hostname === 'localhost') {
window.location.href = '../login.html';
} else {
let h = window.location.origin + '/' + _.split(window.location.pathname, '/', 2)[1] + '/login.html';
window.location.href = error.response.data.hasOwnProperty('content') ? error.response.data.content : h;
}
break;
case 404:
router.replace({
path: '/'
})
break;
}
}
return Promise.reject(error.response.data);
})
export default axios;