fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import axios from 'axios'
import { Message, MessageBox, Loading } from 'element-ui'
import store from '../store'
import router from '../router'
import { codeHeaders, encryptBody } from './sm'
import errorCode from '@/utils/errorCode'
import Cookie from 'js-cookie'
// 跨域认证信息 header 名
const xsrfHeaderName = 'Authorization'
// 是否显示重新登录
let isReloginShow
// 创建axios实例
const service = axios.create({
  baseURL: process.env.VUE_APP_CURRENTMODE === 'development'
    ? '/api' : process.env.VUE_APP_API_BASE_URL,
  timeout: 300000,
  withCredentials: true
})
 
let loading = null
const noReadMsgArr = ['/awl-basecommon-service/msgSite/findComMsgInfoNumByNoReadAll', '/lbcloud-oauth/oauth/logout', '/wly-customization-service/user/self/changePsCode']
const indepArr = ['/wly-qrcode-service/orderInfo/order/compensationPush', '/wly-customization-service/user/self/checkVerificationCodeAndGetToken', '/wly-customization-service/user/self/accountToFindPsCode']
const noPermissionArr = ['/lbcloud-oauth/oauth/token']
/**
 * loading开始时
 * @param {*} config
 */
function startLoading (config) {
  loading = Loading.service({
    background: 'rgba(242,242,242,0.8)',
    target: '.loading-mask',
    lock: true,
    text: '',
    customClass: 'customLoading'
    // background: 'rgba(0, 0, 0, 0.7)'
  })
}
/**
 * loading结束时
 */
function endLoading () {
  loading.close()
}
 
let needLoadingRequestCount = 0
export function showFullScreenLoading (config) {
  if (!config.showLoading && !config[0]) {
    return
  }
  if (needLoadingRequestCount === 0) {
    startLoading(config)
  }
  needLoadingRequestCount++
}
 
export function tryHideFullScreenLoading () {
  if (needLoadingRequestCount <= 0) return
  needLoadingRequestCount--
  if (needLoadingRequestCount === 0) {
    endLoading()
  }
}
export function noReadMsgCountQuery (res) {
  if (Cookie.get(xsrfHeaderName) && noReadMsgArr.indexOf(res.config.url) === -1) {
    async function getNoReadMsgCount () {
      await store.dispatch('getNoReadMsgCount')
    }
    getNoReadMsgCount()
  }
}
// request拦截器
service.interceptors.request.use(
  (config) => {
    // 如果没有设置Content-Type,默认application/json
    if (!config.headers['Content-Type']) {
      config.headers['Content-Type'] = 'application/json'
    }
    // 方法做防重返和防篡改
    if (process.env.VUE_APP_ENCRYPTION === '1') {
      config = codeHeaders(config)
      config = encryptBody(config)
    }
    // 每次发送请求之前判断vuex中是否存在token
    // 如果存在,则统一在http请求的header都加上token
    // const token = '9fcd0cbf5a92c318c913685fbf5a1b44'
    const token = Cookie.get(xsrfHeaderName)
    if (token) {
      config.headers.Authorization = 'Bearer ' + token
      config.headers.appid = 'wly_ydjs'
      config.headers.appsecret = process.env.VUE_APP_HEADER_APP_SECRET
    }
    config.headers.bizId = 'B02'
    showFullScreenLoading(config)
    return config
  },
  (error) => {
    Promise.reject(error)
    tryHideFullScreenLoading()
  }
)
 
// respone拦截器
service.interceptors.response.use(res => {
  const code = res.data.code ? String(res.data.code) : '0'
  // 获取错误信息
  const msg = errorCode[code] || res.data.msg || res.data.message || errorCode.default
  tryHideFullScreenLoading()
  // 二进制数据则直接返回
  if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') {
    noReadMsgCountQuery(res)
    return res.data
  }
  if (code === '401' || code === '4001' || code === '4000') {
    if (!isReloginShow) {
      isReloginShow = true
      MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
        confirmButtonText: '重新登录',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        isReloginShow = false
        store.commit('logout')
        router.replace('/')
      }).catch(() => {
        isReloginShow = false
      })
    }
    const msg = '无效的会话,或者会话已过期,请重新登录。'
    return Promise.reject(msg)
  } else if (code === '50003') {
    if (noPermissionArr.indexOf(res.config.url) === -1) {
      Message({
        message: msg,
        type: 'error'
      })
      return Promise.reject(msg)
    } else {
      return Promise.reject(res)
    }
  } else if (code === '500') {
    if (indepArr.indexOf(res.config.url) === -1) {
      Message({
        message: msg,
        type: 'error'
      })
      return Promise.reject(msg)
    } else {
      return Promise.reject(res)
    }
  } else if (code === '50006' || code === '50020') {
    if (noPermissionArr.indexOf(res.config.url) != -1) {
      return Promise.reject(res)
    } else {
      Message.warning(msg)
      return Promise.reject(msg)
    }
  } else if (code !== '0' && code != '200') {
    Message.warning(msg)
    return Promise.reject(msg)
  } else {
    noReadMsgCountQuery(res)
    return res.data
  }
}, (error) => {
  let { message } = error
  tryHideFullScreenLoading()
  if (message === 'Network Error') {
    message = '后端接口连接异常'
  } else if (message.includes('timeout')) {
    message = '系统接口请求超时'
  } else {
    message = error.response.data.error_description || error.response.data.message
  }
  if (error.response.data.code === '999') {
    Message.warning('登录已失效,请重新登录')
    store.dispatch('clearLoginInfo')
    router.replace('/')
    return
  } else {
    message && Message({
      message: message,
      type: 'error',
      duration: 3 * 1000
    })
  }
  switch (String(error.response.data.status)) {
    case '401':
      store.dispatch('clearLoginInfo')
      router.replace('/')
      break
    case '403':
      router.push({ name: '404' })
      break
  }
  return Promise.reject(error)
})
 
export default service