From 48ce12532cf7495bfa03d22c6386a77b64e4075b Mon Sep 17 00:00:00 2001 From: admin <1297598740@qq.com> Date: Fri, 30 Jan 2026 13:09:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=EF=BC=8C=E6=94=AF=E6=8C=81=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E5=92=8C=E8=AF=AD=E9=9F=B3=E8=BE=93=E5=85=A5=E3=80=81AI?= =?UTF-8?q?=E5=9B=9E=E5=A4=8D=E6=B5=81=E5=BC=8F=E5=B1=95=E7=A4=BA=E5=8F=8A?= =?UTF-8?q?=E8=AE=B0=E8=B4=A6=E7=A1=AE=E8=AE=A4=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Chat/Chat.tsx | 48 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/pages/Chat/Chat.tsx b/src/pages/Chat/Chat.tsx index b74ef59..094f0d1 100644 --- a/src/pages/Chat/Chat.tsx +++ b/src/pages/Chat/Chat.tsx @@ -87,8 +87,52 @@ export default function Chat() { )); } - } catch (error) { + } catch (error: any) { console.error('Failed to send message:', error); + // Default error message + const errorMsg = '连接中断,请重试'; + + setMessages(prev => prev.map(msg => + msg.id === assistantMsgId + ? { ...msg, content: msg.content + `\n\n❌ [${errorMsg}]` } + : msg + )); + } finally { + setIsLoading(false); + } + }; + + // 确认记账 + const handleConfirm = async (card: ConfirmationCard) => { + setIsLoading(true); + try { + await aiService.confirmTransaction(card); + + const confirmMessage: Message = { + id: Date.now().toString(), + role: 'assistant', + content: `✅ 已记录!${card.type === 'income' ? '收入' : '支出'} ${card.amount.toFixed(2)} 元\n分类:${card.category}\n账户:${card.account}`, + timestamp: new Date(), + }; + + setMessages(prev => { + // 移除最后一条消息的确认卡片 + const updated = prev.map((msg, idx) => { + if (idx === prev.length - 1 && msg.confirmationCard) { + return { ...msg, confirmationCard: undefined }; + } + return msg; + }); + return [...updated, confirmMessage]; + }); + + // 清除会话以开始新对话 + aiService.clearSession(); + setSessionId(null); + } catch (error: any) { + console.error('Failed to confirm transaction:', error); + const errorMsg = error.response?.data?.error || '抱歉,记账失败,请稍后再试。'; + const errorMessage: Message = { id: Date.now().toString(), role: 'assistant', @@ -246,7 +290,7 @@ export default function Chat() { ) : ( <> - {messages.map((msg, index) => ( + {messages.map((msg) => (