perf: message hooks

This commit is contained in:
xingyu4j 2022-11-11 10:21:43 +08:00
parent 775083e152
commit 583549ed98

View File

@ -5,7 +5,7 @@ import axios, {
AxiosResponse,
AxiosError
} from 'axios'
import { ElMessage, ElMessageBox, ElNotification } from 'element-plus'
import { useMessage } from '@/hooks/web/useMessage'
import qs from 'qs'
import { config } from '@/config/axios/config'
import { getAccessToken, getRefreshToken, getTenantId, removeToken, setToken } from '@/utils/auth'
@ -17,6 +17,7 @@ import { useCache } from '@/hooks/web/useCache'
const tenantEnable = import.meta.env.VITE_APP_TENANT_ENABLE
const { result_code, base_url, request_timeout } = config
const message = useMessage()
// 需要忽略的提示。忽略后,自动 Promise.reject('error')
const ignoreMsgs = [
'无效的刷新令牌', // 刷新令牌被删除时,不用提示
@ -156,10 +157,10 @@ service.interceptors.response.use(
})
}
} else if (code === 500) {
ElMessage.error(t('sys.api.errMsg500'))
message.error(t('sys.api.errMsg500'))
return Promise.reject(new Error(msg))
} else if (code === 901) {
ElMessage.error(
message.error(
'<div>' +
t('sys.api.errMsg901') +
'</div>' +
@ -174,9 +175,7 @@ service.interceptors.response.use(
// hard coding忽略这个提示直接登出
console.log(msg)
} else {
ElNotification.error({
title: msg
})
message.notifyError(msg)
}
return Promise.reject('error')
} else {
@ -185,16 +184,16 @@ service.interceptors.response.use(
},
(error: AxiosError) => {
console.log('err' + error) // for debug
let { message } = error
let { message: msg } = error
const { t } = useI18n()
if (message === 'Network Error') {
message = t('sys.api.errorMessage')
} else if (message.includes('timeout')) {
message = t('sys.api.apiTimeoutMessage')
} else if (message.includes('Request failed with status code')) {
message = t('sys.api.apiRequestFailed') + message.substr(message.length - 3)
if (msg === 'Network Error') {
msg = t('sys.api.errorMessage')
} else if (msg.includes('timeout')) {
msg = t('sys.api.apiTimeoutMessage')
} else if (msg.includes('Request failed with status code')) {
msg = t('sys.api.apiRequestFailed') + msg.substr(msg.length - 3)
}
ElMessage.error(message)
message.error(msg)
return Promise.reject(error)
}
)
@ -206,11 +205,8 @@ const handleAuthorized = () => {
const { t } = useI18n()
if (!isRelogin.show) {
isRelogin.show = true
ElMessageBox.confirm(t('sys.api.timeoutMessage'), t('common.confirmTitle'), {
confirmButtonText: t('login.relogin'),
cancelButtonText: t('common.cancel'),
type: 'warning'
})
message
.confirm(t('sys.api.timeoutMessage'))
.then(() => {
const { wsCache } = useCache()
resetRouter() // 重置静态路由表