perf: message hooks

pull/2/head
xingyu4j 2022-11-11 10:21:43 +08:00
parent 775083e152
commit 583549ed98
1 changed files with 15 additions and 19 deletions

View File

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