perf: axios

pull/2/head
xingyu 2022-07-20 00:27:13 +08:00
parent c8823752d0
commit 6eddbffd8c
2 changed files with 18 additions and 54 deletions

View File

@ -4,6 +4,8 @@ import qs from 'qs'
import { config } from '@/config/axios/config'
import { getAccessToken, getRefreshToken, getTenantId } from '@/utils/auth'
import errorCode from './errorCode'
import { useI18n } from '@/hooks/web/useI18n'
const { t } = useI18n()
const tenantEnable = import.meta.env.VITE_APP_TENANT_ENABLE
const BASE_URL = import.meta.env.VITE_BASE_URL
@ -18,8 +20,11 @@ const ignoreMsgs = [
// 是否显示重新登录
export const isRelogin = { show: false }
// Axios 无感知刷新令牌,参考 https://www.dashingdog.cn/article/11 与 https://segmentfault.com/a/1190000020210980 实现
// 请求队列
// const requestList = []
// 是否正在刷新中
let isRefreshToken = false
export const PATH_URL = base_url[import.meta.env.VITE_API_BASEPATH]
// 创建axios实例
@ -95,13 +100,17 @@ service.interceptors.response.use(
if (!getRefreshToken()) {
return handleAuthorized()
}
// 2. 进行刷新访问令牌
// TODO: 引入refreshToken会循环依赖报错
}
} else if (code === 500) {
ElMessage.error(msg)
ElMessage.error(t('sys.api.errMsg500'))
return Promise.reject(new Error(msg))
} else if (code === 901) {
ElMessage.error(
'<div>演示模式,无法进行写操作</div>' +
'<div>' +
t('sys.api.errMsg901') +
'</div>' +
'<div> &nbsp; </div>' +
'<div>参考 https://doc.iocoder.cn/ 教程</div>' +
'<div> &nbsp; </div>' +
@ -126,11 +135,11 @@ service.interceptors.response.use(
console.log('err' + error) // for debug
let { message } = error
if (message === 'Network Error') {
message = '后端接口连接异常'
message = t('sys.api.errorMessage')
} else if (message.includes('timeout')) {
message = '系统接口请求超时'
message = t('sys.api.apiTimeoutMessage')
} else if (message.includes('Request failed with status code')) {
message = '系统接口' + message.substr(message.length - 3) + '异常'
message = t('sys.api.apiRequestFailed') + message.substr(message.length - 3)
}
ElMessage.error(message)
return Promise.reject(error)
@ -139,9 +148,9 @@ service.interceptors.response.use(
function handleAuthorized() {
if (!isRelogin.show) {
isRelogin.show = true
ElMessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
confirmButtonText: '重新登录',
cancelButtonText: '取消',
ElMessageBox.confirm(t('sys.api.timeoutMessage'), t('common.confirmTitle'), {
confirmButtonText: t('login.relogin'),
cancelButtonText: t('common.cancel'),
type: 'warning'
})
.then(() => {
@ -151,6 +160,6 @@ function handleAuthorized() {
isRelogin.show = false
})
}
return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
return Promise.reject(t('sys.api.timeoutMessage'))
}
export { service }

View File

@ -1,45 +0,0 @@
import { Layout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'
const { t } = useI18n()
const errorRouter = [
{
path: '/error',
component: Layout,
redirect: '/error/404',
name: 'Error',
meta: {
title: t('router.errorPage'),
icon: 'ci:error',
alwaysShow: true
},
children: [
{
path: '404-demo',
component: () => import('@/views/Error/404.vue'),
name: '404Demo',
meta: {
title: '404'
}
},
{
path: '403-demo',
component: () => import('@/views/Error/403.vue'),
name: '403Demo',
meta: {
title: '403'
}
},
{
path: '500-demo',
component: () => import('@/views/Error/500.vue'),
name: '500Demo',
meta: {
title: '500'
}
}
]
}
]
export default errorRouter