2021-06-11 10:00:22 +08:00
|
|
|
|
import axios from 'axios'
|
2021-08-13 09:39:02 +08:00
|
|
|
|
import { storageKey } from '@/utils/constants'
|
2022-10-19 15:27:26 +08:00
|
|
|
|
import store from '@/store'
|
|
|
|
|
|
|
|
|
|
|
|
const CancelToken = axios.CancelToken
|
2021-06-11 10:00:22 +08:00
|
|
|
|
|
|
|
|
|
|
axios.interceptors.request.use(config => {
|
2022-04-14 15:52:07 +08:00
|
|
|
|
const token = localStorage.getItem(storageKey.token)
|
2022-10-19 15:27:26 +08:00
|
|
|
|
// 添加http请求终止方法
|
|
|
|
|
|
const arr = []
|
|
|
|
|
|
const cancelToken = new CancelToken(function executor (c) {
|
|
|
|
|
|
arr.push(c)
|
|
|
|
|
|
store.commit('setHttpCancel', arr)
|
|
|
|
|
|
})
|
2023-08-02 16:04:03 +08:00
|
|
|
|
// 登录超时,将参数q的+变为空格
|
|
|
|
|
|
if (config.params && config.params.q && config.params.q.length > 0) {
|
|
|
|
|
|
let q = config.params.q
|
|
|
|
|
|
if (q.indexOf('+') > -1) {
|
2023-08-07 11:40:11 +08:00
|
|
|
|
q = q.replace(/\+/g, ' ')
|
2023-08-02 16:04:03 +08:00
|
|
|
|
}
|
2023-08-04 10:15:34 +08:00
|
|
|
|
config.params.q = q
|
2023-08-02 16:04:03 +08:00
|
|
|
|
}
|
2022-10-19 15:27:26 +08:00
|
|
|
|
|
|
|
|
|
|
config.cancelToken = cancelToken
|
2021-06-11 10:00:22 +08:00
|
|
|
|
if (token) {
|
2022-06-24 15:09:20 +08:00
|
|
|
|
config.headers['Cn-Authorization'] = token // 请求头token
|
2021-06-11 10:00:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
return config
|
|
|
|
|
|
},
|
|
|
|
|
|
err => Promise.reject(err)
|
|
|
|
|
|
)
|
|
|
|
|
|
const accountErrorCode = [518003, 518004, 518005, 518006, 518007, 518008] // 账号锁定等
|
|
|
|
|
|
const licenceErrorCode = [711001]
|
|
|
|
|
|
|
|
|
|
|
|
// 若get请求的url中带问号,则将url上的参数截取,改为对象形式传参
|
|
|
|
|
|
axios.interceptors.request.use(
|
|
|
|
|
|
config => {
|
|
|
|
|
|
if (config.method === 'get') {
|
|
|
|
|
|
const url = config.url
|
|
|
|
|
|
const index = url.indexOf('?')
|
|
|
|
|
|
if (index > -1) {
|
|
|
|
|
|
const pre = url.substring(0, index)
|
|
|
|
|
|
const suf = url.substring(index + 1, url.length)
|
|
|
|
|
|
const paramsArr = suf.split('&')
|
|
|
|
|
|
const params = {}
|
|
|
|
|
|
paramsArr.forEach(p => {
|
|
|
|
|
|
const i = p.indexOf('=')
|
|
|
|
|
|
if (i > -1) {
|
|
|
|
|
|
params[p.substring(0, i)] = p.substring(i + 1, p.length)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
params[p] = ''
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
config = { ...config, url: pre, params: params }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return config
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
axios.interceptors.response.use(
|
|
|
|
|
|
response => {
|
|
|
|
|
|
if (licenceErrorCode.indexOf(response.data.code) !== -1) {
|
2022-01-03 22:46:22 +08:00
|
|
|
|
localStorage.removeItem(storageKey.token)
|
2021-08-13 18:57:49 +08:00
|
|
|
|
window.location.href = '/'
|
2023-08-25 14:56:57 +08:00
|
|
|
|
} else if (accountErrorCode.indexOf(response.data.code) !== -1) {
|
|
|
|
|
|
localStorage.removeItem(storageKey.token)
|
|
|
|
|
|
window.location.href = '/'
|
2021-06-11 10:00:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
return response
|
|
|
|
|
|
},
|
|
|
|
|
|
error => {
|
2023-08-30 15:27:19 +08:00
|
|
|
|
try {
|
2023-09-12 14:27:26 +08:00
|
|
|
|
if (error.response && licenceErrorCode.indexOf(error.response.data.code) !== -1) {
|
2023-08-30 15:27:19 +08:00
|
|
|
|
localStorage.removeItem(storageKey.token)
|
|
|
|
|
|
window.location.href = '/'
|
2023-09-12 14:27:26 +08:00
|
|
|
|
} else if (error.response && accountErrorCode.indexOf(error.response.data.code) !== -1) {
|
2023-08-30 15:27:19 +08:00
|
|
|
|
localStorage.removeItem(storageKey.token)
|
|
|
|
|
|
window.location.href = '/'
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error(e)
|
2023-08-25 16:23:18 +08:00
|
|
|
|
}
|
2021-06-11 10:00:22 +08:00
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
2021-11-11 23:28:52 +08:00
|
|
|
|
export function getForDebug (url, params) {
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
|
axios.get(url, {
|
|
|
|
|
|
params: params
|
|
|
|
|
|
}).then(response => {
|
|
|
|
|
|
resolve(response)
|
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
|
if (err.response) {
|
|
|
|
|
|
resolve(err.response)
|
|
|
|
|
|
} else if (err.message) {
|
|
|
|
|
|
resolve(err.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function postForDebug (url, params, headers) {
|
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
|
axios.post(url, params, { headers: headers }).then(response => {
|
|
|
|
|
|
resolve(response, response)
|
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
|
if (err.response) {
|
|
|
|
|
|
resolve(err.response)
|
|
|
|
|
|
} else if (err.message) {
|
|
|
|
|
|
resolve(err.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|