fix: axios bug

This commit is contained in:
xingyu 2022-08-02 10:52:08 +08:00
parent 93a97cd526
commit 02315804ac

View File

@ -1,8 +1,8 @@
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios' import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'
import { ElMessage, ElMessageBox, ElNotification } from 'element-plus' import { ElMessage, ElNotification } from 'element-plus'
import qs from 'qs' import qs from 'qs'
import { config } from '@/config/axios/config' import { config } from '@/config/axios/config'
import { getAccessToken, getRefreshToken, getTenantId } from '@/utils/auth' import { getAccessToken, getTenantId, removeToken } from '@/utils/auth'
import errorCode from './errorCode' import errorCode from './errorCode'
import { useI18n } from '@/hooks/web/useI18n' import { useI18n } from '@/hooks/web/useI18n'
@ -22,7 +22,7 @@ export const isRelogin = { show: false }
// 请求队列 // 请求队列
// const requestList = [] // const requestList = []
// 是否正在刷新中 // 是否正在刷新中
let isRefreshToken = false // const isRefreshToken = false
export const PATH_URL = base_url[import.meta.env.VITE_API_BASEPATH] export const PATH_URL = base_url[import.meta.env.VITE_API_BASEPATH]
@ -61,8 +61,7 @@ service.interceptors.request.use(
// const now = new Date().getTime() // const now = new Date().getTime()
// params = params.substring(0, url.length - 1) + `?_t=${now}` // params = params.substring(0, url.length - 1) + `?_t=${now}`
url += '?' url += '?'
const keys = Object.keys(params) for (const key of Object.keys(params)) {
for (const key of keys) {
if (params[key] !== void 0 && params[key] !== null) { if (params[key] !== void 0 && params[key] !== null) {
url += `${key}=${encodeURIComponent(params[key])}&` url += `${key}=${encodeURIComponent(params[key])}&`
} }
@ -82,6 +81,7 @@ service.interceptors.request.use(
// response 拦截器 // response 拦截器
service.interceptors.response.use( service.interceptors.response.use(
async (response: AxiosResponse<Recordable>) => { async (response: AxiosResponse<Recordable>) => {
console.info(response)
const { data } = response const { data } = response
if (!data) { if (!data) {
// 返回“[HTTP]请求没有返回值”; // 返回“[HTTP]请求没有返回值”;
@ -90,6 +90,13 @@ service.interceptors.response.use(
const { t } = useI18n() const { t } = useI18n()
// 未设置状态码则默认成功状态 // 未设置状态码则默认成功状态
const code = data.code || result_code const code = data.code || result_code
// 二进制数据则直接返回
if (
response.request.responseType === 'blob' ||
response.request.responseType === 'arraybuffer'
) {
return response.data
}
// 获取错误信息 // 获取错误信息
const msg = data.msg || errorCode[code] || errorCode['default'] const msg = data.msg || errorCode[code] || errorCode['default']
if (ignoreMsgs.indexOf(msg) !== -1) { if (ignoreMsgs.indexOf(msg) !== -1) {
@ -97,15 +104,16 @@ service.interceptors.response.use(
return Promise.reject(msg) return Promise.reject(msg)
} else if (code === 401) { } else if (code === 401) {
// 如果未认证,并且未进行刷新令牌,说明可能是访问令牌过期了 // 如果未认证,并且未进行刷新令牌,说明可能是访问令牌过期了
if (!isRefreshToken) { return handleAuthorized()
isRefreshToken = true // if (!isRefreshToken) {
// 1. 如果获取不到刷新令牌,则只能执行登出操作 // isRefreshToken = true
if (!getRefreshToken()) { // // 1. 如果获取不到刷新令牌,则只能执行登出操作
return handleAuthorized() // if (!getRefreshToken()) {
} // return handleAuthorized()
// 2. 进行刷新访问令牌 // }
// TODO: 引入refreshToken会循环依赖报错 // // 2. 进行刷新访问令牌
} // // TODO: 引入refreshToken会循环依赖报错
// }
} else if (code === 500) { } else if (code === 500) {
ElMessage.error(t('sys.api.errMsg500')) ElMessage.error(t('sys.api.errMsg500'))
return Promise.reject(new Error(msg)) return Promise.reject(new Error(msg))
@ -149,21 +157,12 @@ service.interceptors.response.use(
return Promise.reject(error) return Promise.reject(error)
} }
) )
function handleAuthorized() { const handleAuthorized = () => {
const { t } = useI18n() const { t } = useI18n()
if (!isRelogin.show) { if (!isRelogin.show) {
removeToken()
isRelogin.show = true isRelogin.show = true
ElMessageBox.confirm(t('sys.api.timeoutMessage'), t('common.confirmTitle'), { ElNotification.error(t('sys.api.timeoutMessage'))
confirmButtonText: t('login.relogin'),
cancelButtonText: t('common.cancel'),
type: 'warning'
})
.then(() => {
isRelogin.show = false
})
.catch(() => {
isRelogin.show = false
})
} }
return Promise.reject(t('sys.api.timeoutMessage')) return Promise.reject(t('sys.api.timeoutMessage'))
} }