From 79a622375e531e08ce8eb89f8fb3568eb3553386 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 06:40:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=B9=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/request.ts | 25 ++++++++++++++----------- utils/notifications.tsx | 11 ++++++++--- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/api/request.ts b/api/request.ts index 4bc2b41..f5187eb 100644 --- a/api/request.ts +++ b/api/request.ts @@ -1,6 +1,7 @@ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig, AxiosHeaders } from 'axios'; import { BASE_URL } from './constants' import { errorHandle } from './errorHandle'; +import { showInsufficientPointsNotification } from '../utils/notifications'; /** * 统一的错误处理函数 @@ -11,23 +12,12 @@ const handleRequestError = (error: any, defaultMessage: string = '请求失败') if (error.response) { const { status, data } = error.response; const errorMessage = data?.message || data?.detail?.message || defaultMessage; - - // 处理 402 状态码的特殊情况 - if (status === 402 && data?.detail) { - // 只显示通知,不调用 errorHandle - import('../utils/notifications').then(({ showInsufficientPointsNotification }) => { - showInsufficientPointsNotification(data.detail); - }); - return; // 直接返回,不再抛出错误 - } - errorHandle(status, errorMessage); } else if (error.request) { errorHandle(0); } else { errorHandle(0, error.message || defaultMessage); } - return Promise.reject(error); // 将 reject 移到这里,避免 402 时重复处理 }; // 创建 axios 实例 const request: AxiosInstance = axios.create({ @@ -81,6 +71,19 @@ request.interceptors.response.use( return response.data; }, (error) => { + // 处理 402 错误 + if (error.response?.status === 402 && error.response?.data?.detail) { + // 使用动态导入并确保在下一个事件循环中执行 + setTimeout(() => { + import('../utils/notifications').then(({ showInsufficientPointsNotification }) => { + showInsufficientPointsNotification(error.response.data.detail); + }); + }, 0); + + return Promise.reject(new Error('Insufficient points')); + } + + // 其他错误走通用处理 handleRequestError(error); return Promise.reject(error); } diff --git a/utils/notifications.tsx b/utils/notifications.tsx index a44bbcd..f19b0a3 100644 --- a/utils/notifications.tsx +++ b/utils/notifications.tsx @@ -58,16 +58,21 @@ const btnStyle = { * 显示积分不足通知 * @description 在右上角显示一个带有充值链接的积分不足提醒 */ +const INSUFFICIENT_POINTS_KEY = 'insufficient-points-notification'; + export const showInsufficientPointsNotification = (detail?: { current_balance?: number; required_tokens?: number; message?: string; }) => { - // 生成唯一的 key - const key = `insufficient-points-${Date.now()}`; + // 先关闭现有的通知 + notification.destroy(INSUFFICIENT_POINTS_KEY); + + // 每次都生成新的 key + const uniqueKey = `${INSUFFICIENT_POINTS_KEY}-${Date.now()}`; notification.warning({ - key, // 添加唯一的 key + key: uniqueKey, message: null, description: (