- 在 en-US、ja-JP、zh-CN 和 zh-TW 语言文件中添加了 HTTP 错误相关翻译 - 更新了 http.js 文件,使用国际化消息替换硬编码的错误提示 - 优化了错误处理逻辑,提高了代码的可维护性和用户体验
130 lines
3.3 KiB
JavaScript
130 lines
3.3 KiB
JavaScript
import {useRuntimeConfig} from '#app'
|
|
import {ofetch} from 'ofetch'
|
|
import {message} from '@/components/x-message/useMessage.js'
|
|
import {authStore} from "@/stores/auth/index.js"
|
|
|
|
let httpStatusErrorHandler
|
|
let http
|
|
|
|
// HTTP 状态码映射 - 使用i18n国际化
|
|
export function setupHttp() {
|
|
if (http) return http
|
|
const config = useRuntimeConfig()
|
|
const baseURL = config.public.NUXT_PUBLIC_API_BASE
|
|
const { token } = authStore()
|
|
const router = useRouter()
|
|
const i18n = useNuxtApp().$i18n
|
|
|
|
// 国际化的HTTP状态码映射
|
|
const HTTP_STATUS_MAP = {
|
|
400: i18n.t('http.error.badRequest'),
|
|
401: i18n.t('http.error.unauthorized'),
|
|
403: i18n.t('http.error.forbidden'),
|
|
404: i18n.t('http.error.notFound'),
|
|
500: i18n.t('http.error.serverError'),
|
|
502: i18n.t('http.error.badGateway'),
|
|
503: i18n.t('http.error.serviceUnavailable'),
|
|
504: i18n.t('http.error.gatewayTimeout')
|
|
}
|
|
|
|
const defaultOptions = {
|
|
baseURL,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
timeout: 15000, // 15秒超时
|
|
retry: 3,
|
|
retryDelay: 1000,
|
|
}
|
|
http = ofetch.create({
|
|
...defaultOptions,
|
|
|
|
// 请求拦截
|
|
async onRequest({ options, request }) {
|
|
// 添加 token
|
|
options.headers = {
|
|
...options.headers,
|
|
Authorization: token.value,
|
|
'accept-language':i18n.locale.value
|
|
|
|
}
|
|
|
|
// GET 请求添加时间戳防止缓存
|
|
if (request.toLowerCase().includes('get')) {
|
|
options.params = {
|
|
...options.params,
|
|
_t: Date.now()
|
|
}
|
|
}
|
|
},
|
|
|
|
// 响应拦截
|
|
async onResponse({ response }) {
|
|
const data = response._data
|
|
|
|
// 处理业务错误
|
|
if (data.status === 1) {
|
|
message.error(data.msg || i18n.t('http.error.operationFailed'))
|
|
}
|
|
|
|
// 处理登录失效
|
|
if (data.status === 401) {
|
|
message.error(i18n.t('http.error.loginExpired'))
|
|
token.value = '' // 清除 token
|
|
router.replace('/login')
|
|
}
|
|
|
|
return response
|
|
},
|
|
|
|
// 响应错误处理
|
|
async onResponseError({ response, request }) {
|
|
// 网络错误
|
|
if (!response) {
|
|
message.error(i18n.t('http.error.networkError'))
|
|
return Promise.reject(new Error(i18n.t('http.error.networkError')))
|
|
}
|
|
const status = response.status
|
|
const data = response._data
|
|
|
|
// 处理 HTTP 状态错误
|
|
const errorMessage = data.msg || HTTP_STATUS_MAP[status] || i18n.t('http.error.requestFailed')
|
|
|
|
if (Array.isArray(data.msg)) {
|
|
data.msg.forEach(item => {
|
|
httpStatusErrorHandler?.(item, status)
|
|
})
|
|
} else {
|
|
httpStatusErrorHandler?.(errorMessage, status)
|
|
}
|
|
|
|
message.error(errorMessage)
|
|
return Promise.reject(data)
|
|
},
|
|
})
|
|
|
|
return http
|
|
}
|
|
|
|
export function createAbortController() {
|
|
return new AbortController()
|
|
}
|
|
|
|
export function injectHttpStatusErrorHandler(handler) {
|
|
httpStatusErrorHandler = handler
|
|
}
|
|
|
|
export function getHttp() {
|
|
if (!http) {
|
|
throw new Error(useNuxtApp().$i18n.t('http.error.httpNotInitialized'))
|
|
}
|
|
return http
|
|
}
|
|
|
|
// 导出请求工具函数
|
|
export async function request({url,...options}) {
|
|
const http = getHttp()
|
|
try {
|
|
return await http(url, {...options,body:options.data})
|
|
} catch (error) {
|
|
throw error
|
|
}
|
|
} |