This commit is contained in:
北枳 2025-09-07 06:40:08 +08:00
parent 766f447be3
commit 79a622375e
2 changed files with 22 additions and 14 deletions

View File

@ -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);
}

View File

@ -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: (
<div data-alt="insufficient-points-notification" style={{ minWidth: '280px' }}>