commit | author | age
|
f9c38e
|
1 |
import Vue from 'vue' |
I |
2 |
import axios from 'axios' |
|
3 |
// import store from '@/store' |
|
4 |
import { |
|
5 |
message, |
|
6 |
Modal, |
|
7 |
notification |
|
8 |
} from 'ant-design-vue' /// es/notification |
|
9 |
import { |
|
10 |
VueAxios |
|
11 |
} from './axios' |
|
12 |
// import { ACCESS_TOKEN } from '@/store/mutation-types' |
|
13 |
|
|
14 |
// 创建 axios 实例 |
|
15 |
const service = axios.create({ |
|
16 |
baseURL: '/api', // api base_url |
|
17 |
timeout: 6000 // 请求超时时间 |
|
18 |
}) |
|
19 |
|
|
20 |
const err = (error) => { |
|
21 |
if (error.response) { |
|
22 |
|
|
23 |
const data = error.response.data |
|
24 |
// const token = Vue.ls.get(ACCESS_TOKEN) |
|
25 |
if (error.response.status === 404) { |
|
26 |
notification.error({ |
|
27 |
message: '404', |
|
28 |
description: "找不到请求地址" |
|
29 |
}) |
|
30 |
} |
|
31 |
|
|
32 |
if (error.response.status === 403) { |
|
33 |
console.log('服务器403啦,要重新登录!') |
|
34 |
notification.error({ |
|
35 |
message: 'Forbidden', |
|
36 |
description: data.message |
|
37 |
}) |
|
38 |
} |
|
39 |
if (error.response.status === 500) { |
279785
|
40 |
if (data.message == undefined) { |
I |
41 |
message.error(error.response.data) |
|
42 |
} else { |
|
43 |
if (data.message.length > 0) { |
|
44 |
message.error(data.message) |
|
45 |
} |
f9c38e
|
46 |
} |
I |
47 |
} |
|
48 |
if (error.response.status === 401 && !(data.result && data.result.isLogin)) { |
|
49 |
notification.error({ |
|
50 |
message: 'Unauthorized', |
|
51 |
description: 'Authorization verification failed' |
|
52 |
}) |
|
53 |
// if (token) { |
|
54 |
// store.dispatch('Logout').then(() => { |
|
55 |
// setTimeout(() => { |
|
56 |
// window.location.reload() |
|
57 |
// }, 1500) |
|
58 |
// }) |
|
59 |
// } |
|
60 |
} |
|
61 |
} |
|
62 |
return Promise.reject(error) |
|
63 |
} |
|
64 |
|
|
65 |
// request interceptor |
|
66 |
service.interceptors.request.use(config => { |
|
67 |
// const token = Vue.ls.get(ACCESS_TOKEN) |
|
68 |
// if (token) { |
|
69 |
// config.headers['Authorization'] = 'Bearer ' + token |
|
70 |
// } |
63af45
|
71 |
// config.headers['Authorization'] = 'Bearer '; |
f9c38e
|
72 |
return config |
I |
73 |
}, err) |
|
74 |
|
|
75 |
/** |
|
76 |
* response interceptor |
|
77 |
* 所有请求统一返回 |
|
78 |
*/ |
|
79 |
service.interceptors.response.use((response) => { |
|
80 |
if (response.request.responseType === 'blob') { |
|
81 |
return response |
|
82 |
} |
|
83 |
const code = response.data.code |
|
84 |
if (code === 1011006 || code === 1011007 || code === 1011008 || code === 1011009) { |
|
85 |
Modal.error({ |
|
86 |
title: '提示:', |
|
87 |
content: response.data.message, |
|
88 |
okText: '重新登录', |
|
89 |
onOk: () => { |
|
90 |
Vue.ls.remove(ACCESS_TOKEN) |
|
91 |
window.location.reload() |
|
92 |
} |
|
93 |
}) |
|
94 |
} else if (code === 1013002 || code === 1016002 || code === 1015002) { |
|
95 |
message.error(response.data.message) |
|
96 |
return response.data |
|
97 |
} else { |
|
98 |
return response.data |
|
99 |
} |
|
100 |
}, err) |
|
101 |
|
|
102 |
const installer = { |
|
103 |
vm: {}, |
|
104 |
install(Vue) { |
|
105 |
Vue.use(VueAxios, service) |
|
106 |
} |
|
107 |
} |
|
108 |
|
|
109 |
export { |
|
110 |
installer as VueAxios, |
|
111 |
service as axios |
|
112 |
} |