积分不足提醒

This commit is contained in:
北枳 2025-09-07 05:06:25 +08:00
parent e6f849e0af
commit 99f3002ff6
3 changed files with 32 additions and 10 deletions

View File

@ -7,6 +7,7 @@ const HTTP_ERROR_MESSAGES: Record<number, string> = {
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<number, () => void> = {
localStorage.removeItem('token');
// 跳转到登录页面
window.location.href = '/login';
},
4001: () => {
// 显示积分不足通知
import('../utils/notifications').then(({ showInsufficientPointsNotification }) => {
showInsufficientPointsNotification();
});
}
};

View File

@ -10,7 +10,20 @@ 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) {
// 请求已发出但没有收到响应

View File

@ -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 = () => {
<h3 style={messageStyle}>
Insufficient credits reminder
</h3>
<p style={descriptionStyle}>Your credits are insufficient, please upgrade to continue.</p>
<p style={descriptionStyle}>
{detail?.message || 'Your credits are insufficient, please upgrade to continue.'}
{detail?.current_balance !== undefined && detail?.required_tokens !== undefined && (
<>
<br />
<span style={{ color: 'rgba(255, 255, 255, 0.85)' }}>
Current balance: {detail.current_balance} / Required: {detail.required_tokens}
</span>
</>
)}
</p>
<button
onClick={() => window.location.href = '/pricing'}
style={btnStyle}