zouyu
2023-11-17 1cf81a64af5bac57f2af8c419db0b22b3d5ba7c8
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
113
import { serialize } from '@/util/util'
import { getStore } from '../util/store'
import NProgress from 'nprogress' // progress bar
import errorCode from '@/const/errorCode'
import router from '@/router/router'
import { Message } from 'element-ui'
import 'nprogress/nprogress.css'
import qs from 'qs'
import store from '@/store' // progress bar style
import { webVersion } from '@/config/sysParam'
axios.defaults.timeout = 60000
// 返回其他状态吗
axios.defaults.validateStatus = function(status) {
  return status >= 200 && status <= 500 // 默认的
}
// 跨域请求,允许保存cookie
axios.defaults.withCredentials = true
// NProgress Configuration
NProgress.configure({
  showSpinner: false
})
 
// HTTPrequest拦截
// eslint-disable-next-line no-undef
axios.interceptors.request.use(
  (config) => {
    config.headers['VERSION'] = '10.88.201.83'
    // config.headers.VERSION = '192.168.18.50'
    // config.headers.VERSION = '10.88.18.114'
    // config.headers.VERSION = '10.19.77.84'
    NProgress.start() // start progress bar
    const TENANT_ID = getStore({ name: 'tenantId' })
    const isToken = (config.headers || {}).isToken === false
    const token = store.getters.access_token
    if (token && !isToken) {
      config.headers.Authorization = 'Bearer ' + token // token
    }
    if (TENANT_ID) {
      config.headers['TENANT-ID'] = TENANT_ID // 租户ID
    }
    // 前端版本
    // if (webVersion) {
    //   const webVersionVal = webVersion.VERSION
    //   if (webVersionVal) {
    //     config.headers['WEB-VERSION'] = webVersionVal // web版本
    //   }
    // }
 
    // // 自定义访问地址
    // config.headers['VERSION'] = '10.19.77.84'
    // headers中配置serialize为true开启序列化
    if (config.method === 'post' && config.headers.serialize) {
      config.data = serialize(config.data)
      delete config.data.serialize
    }
    if (config.method === 'get') {
      config.paramsSerializer = function(params) {
        return qs.stringify(params, { arrayFormat: 'repeat' })
      }
    }
 
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)
 
// HTTPresponse拦截
axios.interceptors.response.use(
  (res) => {
    NProgress.done()
    const status = Number(res.status) || 200
    const message = res.data.msg || errorCode[status] || errorCode.default
    if (status === 401) {
      Message({
        message: message,
        type: 'error'
      })
      store.dispatch('FedLogOut').then(() => {
        router.push({ path: '/login' })
      })
      return
    }
 
    if ((status == 200 || res.data.code === 1) && message == '版本过低') {
      Message({
        message: '客户端已发布升级,请先F5刷新后,再进行操作!',
        type: 'error'
      })
      store.dispatch('FedLogOut').then(() => {
        router.push({ path: '/login' })
      })
      return
    }
 
    if (status !== 200 || res.data.code === 1) {
      Message({
        message: message,
        type: 'error'
      })
      return Promise.reject(new Error(message))
    }
 
    return res
  },
  (error) => {
    NProgress.done()
    return Promise.reject(new Error(error))
  }
)
 
export default axios