From 1a7c74b9da2d60ff9020c3b516c31b79ef803996 Mon Sep 17 00:00:00 2001 From: changDongFang <2628162102@qq.com> Date: Mon, 19 May 2025 11:34:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=BB=E5=AE=B6=E6=B5=B7=E5=A4=96=E6=94=AF?= =?UTF-8?q?=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api-collect-code/abroad/index.js | 9 ++ app/api-collect-code/http-abroad.js | 134 +++++++++++++++++++++++++++ env/.env.prod | 1 + 3 files changed, 144 insertions(+) create mode 100644 app/api-collect-code/abroad/index.js create mode 100644 app/api-collect-code/http-abroad.js diff --git a/app/api-collect-code/abroad/index.js b/app/api-collect-code/abroad/index.js new file mode 100644 index 0000000..67d7579 --- /dev/null +++ b/app/api-collect-code/abroad/index.js @@ -0,0 +1,9 @@ +import { request } from '@/api-collect-code/http-abroad.js' +export async function abroadCreateOrder(data) { + return await request({ + url:"/artshow/overseas/show-pay/createOrder", + method:"POST", + data + }) + +} \ No newline at end of file diff --git a/app/api-collect-code/http-abroad.js b/app/api-collect-code/http-abroad.js new file mode 100644 index 0000000..d893933 --- /dev/null +++ b/app/api-collect-code/http-abroad.js @@ -0,0 +1,134 @@ +/** + * 本文件目前仅用于画家海外支付 + */ + +import {useRuntimeConfig} from '#app' +import {ofetch} from 'ofetch' +import {message} from '@/components/x-message/useMessage.js' +import {codeAuthStore} from "@/stores-collect-code/auth/index.js" + +let httpStatusErrorHandler +let http + +// HTTP 状态码映射 - 使用i18n国际化 +export function setupHttp() { + if (http) return http + const {codeToken}= codeAuthStore() + const config = useRuntimeConfig() + const baseURL = config.public.NUXT_PUBLIC_ABROAD_API_BASE // 海外支付接口地址 + 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: codeToken.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')) + } + console.log('拦截响应',data) + // 处理登录失效 + if (data.status === 401) { + message.error(i18n.t('http.error.loginExpired')) + codeToken.value = '' // 清除 token + router.replace('/collectCode/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 + } +} \ No newline at end of file diff --git a/env/.env.prod b/env/.env.prod index 7dd8711..c532d9f 100644 --- a/env/.env.prod +++ b/env/.env.prod @@ -4,3 +4,4 @@ NUXT_PUBLIC_SOCKET_URL=wss://auction.szjixun.cn NUXT_API_SECRET=prod-secret NUXT_PUBLIC_PKEY=pk_live_51QfbSAAB1Vm8VfJqEVY2uFHPn9N4sDbOaCzht8IVKoylYBrYvdUsmsnCzGxIoN9skBCvI5PsxLJcf4PlytXIr1aX00mFJBXSB8 +NUXT_PUBLIC_ABROAD_API_BASE \ No newline at end of file