zj
2024-06-03 5a166725de1adeb7ac2fa5b037abbfc28fedf047
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
import axios from 'axios'
import store from '@/store'
import storage from 'store'
import notification from 'ant-design-vue/es/notification'
import { VueAxios } from './axios'
import { ACCESS_TOKEN } from '@/store/mutation-types'
 
// 创建 axios 实例
const request = axios.create({
  // API 请求的默认前缀
  baseURL: process.env.VUE_APP_API_BASE_URL,
  // content-type: application/x-www-form-urlencoded
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  timeout: 6000 // 请求超时时间
})
 
// 异常拦截处理器
const errorHandler = (error) => {
  if (error.response) {
    const data = error.response.data
    // 从 localstorage 获取 token
    const token = storage.get(ACCESS_TOKEN)
    if (error.response.status === 403) {
      notification.error({
        message: 'Forbidden',
        description: data.msg
      })
    }
    if (error.response.status === 401 && !(data.result && data.result.isLogin)) {
      notification.error({
        message: 'Unauthorized',
        description: 'Authorization verification failed'
      })
      if (token) {
        store.dispatch('Logout').then(() => {
          setTimeout(() => {
            window.location.reload()
          }, 1500)
        })
      }
    }
  }
  return Promise.reject(error)
}
 
// request interceptor
request.interceptors.request.use(config => {
  const token = storage.get(ACCESS_TOKEN)
  // 如果 token 存在
  // 让每个请求携带自定义 token 请根据实际情况自行修改
  if (token) {
    config.headers['agenttoken'] = token
  }
  return config
}, errorHandler)
 
// response interceptor
// request.interceptors.response.use((response) => {
//   return response.data
// }, errorHandler)
request.interceptors.response.use(async (response) => {
  // 克隆响应对象做解析处理
  // 这里的res就是我们请求到的数据
  const res = await response
  const { code, msg } = res.data
  if (msg == '請先登錄,無權限訪問agent' || msg == '請先登錄') {
    // messageUtils.error('未登录或登录过期,请重新登录', 10)
    notification.error({
      message: '重新登陆',
      description: '未登录或登录过期,请重新登录'
    })
    store.dispatch('Logout').then(() => {
      setTimeout(() => {
        window.localStorage.clear()
        window.location.reload()
      }, 1500)
    })
  } else if (!res.data) {
    notification.error({
      message: '网络错误',
      description: '网络错误,请稍后刷新页面重试!'
    })
  }
  return res.data
})
 
const installer = {
  vm: {},
  install (Vue) {
    Vue.use(VueAxios, request)
  }
}
 
export default request
 
export {
  installer as VueAxios,
  request as axios
}