From 99f3002ff6f90e98c542cc653ea73cab938a19f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=97=E6=9E=B3?= <7854742+wang_rumeng@user.noreply.gitee.com> Date: Sun, 7 Sep 2025 05:06:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=AF=E5=88=86=E4=B8=8D=E8=B6=B3=E6=8F=90?= =?UTF-8?q?=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/errorHandle.ts | 7 +------ api/request.ts | 17 +++++++++++++++-- utils/notifications.tsx | 18 ++++++++++++++++-- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/api/errorHandle.ts b/api/errorHandle.ts index 2486b2d..fba0ba5 100644 --- a/api/errorHandle.ts +++ b/api/errorHandle.ts @@ -7,6 +7,7 @@ const HTTP_ERROR_MESSAGES: Record = { 0: "Please try again if the network is abnormal. If it happens again, please contact us.", 400: "Request parameter error, please check your input.", 401: "Login expired, please log in again.", + 402: "Insufficient points, please recharge.", 403: "Insufficient permissions to access this resource.", 404: "Requested resource does not exist.", 408: "Request timeout, please try again.", @@ -31,12 +32,6 @@ const ERROR_HANDLERS: Record void> = { localStorage.removeItem('token'); // 跳转到登录页面 window.location.href = '/login'; - }, - 4001: () => { - // 显示积分不足通知 - import('../utils/notifications').then(({ showInsufficientPointsNotification }) => { - showInsufficientPointsNotification(); - }); } }; diff --git a/api/request.ts b/api/request.ts index c30cc5a..f5f123f 100644 --- a/api/request.ts +++ b/api/request.ts @@ -10,11 +10,24 @@ import { errorHandle } from './errorHandle'; const handleRequestError = (error: any, defaultMessage: string = '请求失败') => { if (error.response) { const { status, data } = error.response; - const errorMessage = data?.message || defaultMessage; + const errorMessage = data?.message || data?.detail?.message || defaultMessage; + + // 处理 402 状态码的特殊情况 + if (status === 402 && data?.detail) { + import('./errorHandle').then(({ errorHandle }) => { + errorHandle(status, errorMessage); + // 调用积分不足通知,传入详细信息 + import('../utils/notifications').then(({ showInsufficientPointsNotification }) => { + showInsufficientPointsNotification(data.detail); + }); + }); + return; + } + errorHandle(status, errorMessage); } else if (error.request) { // 请求已发出但没有收到响应 - errorHandle(0 ); + errorHandle(0); } else { // 请求配置出错 errorHandle(0, error.message || defaultMessage); diff --git a/utils/notifications.tsx b/utils/notifications.tsx index a516df2..d7dda08 100644 --- a/utils/notifications.tsx +++ b/utils/notifications.tsx @@ -58,7 +58,11 @@ const btnStyle = { * 显示积分不足通知 * @description 在右上角显示一个带有充值链接的积分不足提醒 */ -export const showInsufficientPointsNotification = () => { +export const showInsufficientPointsNotification = (detail?: { + current_balance?: number; + required_tokens?: number; + message?: string; +}) => { notification.warning({ message: null, description: ( @@ -66,7 +70,17 @@ export const showInsufficientPointsNotification = () => {

Insufficient credits reminder

-

Your credits are insufficient, please upgrade to continue.

+

+ {detail?.message || 'Your credits are insufficient, please upgrade to continue.'} + {detail?.current_balance !== undefined && detail?.required_tokens !== undefined && ( + <> +
+ + Current balance: {detail.current_balance} / Required: {detail.required_tokens} + + + )} +