From 583549ed9879cb116be27454cc5502a1df626197 Mon Sep 17 00:00:00 2001 From: xingyu4j Date: Fri, 11 Nov 2022 10:21:43 +0800 Subject: [PATCH] perf: message hooks --- .../src/config/axios/service.ts | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/yudao-ui-admin-vue3/src/config/axios/service.ts b/yudao-ui-admin-vue3/src/config/axios/service.ts index 14cf68b0d..cd1235e58 100644 --- a/yudao-ui-admin-vue3/src/config/axios/service.ts +++ b/yudao-ui-admin-vue3/src/config/axios/service.ts @@ -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( '
' + t('sys.api.errMsg901') + '
' + @@ -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() // 重置静态路由表