inleft
2022-01-21 f9c38ed1b72fc96b96c1d3ffdcc1a66b03bd3b73
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
24         const data = error.response.data
25         // const token = Vue.ls.get(ACCESS_TOKEN)
26         if (error.response.status === 404) {
27             notification.error({
28                 message: '404',
29                 description: "找不到请求地址"
30             })
31         }
32
33         if (error.response.status === 403) {
34             console.log('服务器403啦,要重新登录!')
35             notification.error({
36                 message: 'Forbidden',
37                 description: data.message
38             })
39         }
40         if (error.response.status === 500) {
41             if (data.message.length > 0) {
42                 message.error(data.message)
43             }
44         }
45         if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
46             notification.error({
47                 message: 'Unauthorized',
48                 description: 'Authorization verification failed'
49             })
50             // if (token) {
51             //     store.dispatch('Logout').then(() => {
52             //         setTimeout(() => {
53             //             window.location.reload()
54             //         }, 1500)
55             //     })
56             // }
57         }
58     }
59     return Promise.reject(error)
60 }
61
62 // request interceptor
63 service.interceptors.request.use(config => {
64     // const token = Vue.ls.get(ACCESS_TOKEN)
65     // if (token) {
66     //   config.headers['Authorization'] = 'Bearer ' + token
67     // }
68     config.headers['Authorization'] = 'Bearer ';
69     return config
70 }, err)
71
72 /**
73  * response interceptor
74  * 所有请求统一返回
75  */
76 service.interceptors.response.use((response) => {
77     if (response.request.responseType === 'blob') {
78         return response
79     }
80     const code = response.data.code
81     if (code === 1011006 || code === 1011007 || code === 1011008 || code === 1011009) {
82         Modal.error({
83             title: '提示:',
84             content: response.data.message,
85             okText: '重新登录',
86             onOk: () => {
87                 Vue.ls.remove(ACCESS_TOKEN)
88                 window.location.reload()
89             }
90         })
91     } else if (code === 1013002 || code === 1016002 || code === 1015002) {
92         message.error(response.data.message)
93         return response.data
94     } else {
95         return response.data
96     }
97 }, err)
98
99 const installer = {
100     vm: {},
101     install(Vue) {
102         Vue.use(VueAxios, service)
103     }
104 }
105
106 export {
107     installer as VueAxios,
108     service as axios
109 }