Skip to content

Commit

Permalink
🐛 (i18n) 修复 axios 请求错误在关闭国际化时提示为 code 的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Hccake committed Jan 20, 2024
1 parent e460064 commit 3ff358e
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/utils/axios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { loginPath } from '@/config'
import router from '@/router'
import { HttpClient } from '@/utils/axios/http-client'
import { useI18nStore } from '@/stores/i18n-store'
import { i18n } from '@/locales'
import { useAdminI18n } from '@/hooks/i18n'

const { rawI18nText } = useAdminI18n()

const onRequestFulfilled = (requestConfig: InternalAxiosRequestConfig) => {
const headers = requestConfig.headers || {}
Expand Down Expand Up @@ -50,8 +52,6 @@ const onResponseFulfilled = (response: AxiosResponse) => {

// 响应失败处理函数
const onResponseRejected = (error: AxiosError) => {
const { t } = i18n.global

if (error.response) {
const data = error.response.data as unknown as ApiResult
const errorStatus = error.response.status
Expand All @@ -70,9 +70,9 @@ const onResponseRejected = (error: AxiosError) => {
// 防止重复弹出 TODO 这里拦截所有其他的 axios 的请求
Modal.destroyAll()
Modal.info({
title: t('system.tip.title'),
content: t('user.login.expired'),
okText: t('user.login.submit.retry'),
title: rawI18nText('system.tip.title', '系统提示'),
content: rawI18nText('user.login.expired', '登录状态已过期!'),
okText: rawI18nText('user.login.submit.retry', '重新登陆'),
onOk: () => {
router.push({
path: loginPath,
Expand All @@ -85,27 +85,28 @@ const onResponseRejected = (error: AxiosError) => {
case 403:
error.resolved = true
notification.error({
message: t('user.pemission.reject'),
message: rawI18nText('user.pemission.reject', '没有权限访问!'),
description: data.message
})
break
default:
error.resolved = true
notification.error({
message: t('system.tip.request.error'),
message: rawI18nText('system.tip.request.error', '网络请求异常!'),
description:
data?.message ||
errorStatusText ||
error.message ||
t('system.tip.request.error.message', { code: errorStatus })
rawI18nText('system.tip.request.error.message', '错误状态码:' + errorStatus)
})
break
}
} else {
error.resolved = true
notification.error({
message: t('system.tip.network.error'),
description: error.message || t('system.tip.network.error.message')
message: rawI18nText('system.tip.network.error', '网络异常, 无法访问到服务器!'),
description:
error.message || rawI18nText('system.tip.network.error.message', '请检测您的网络是否通畅!')
})
}
return Promise.reject(error)
Expand Down

0 comments on commit 3ff358e

Please sign in to comment.