From 1889b3b71baef8d22e2b0788ec0e5c37881100f1 Mon Sep 17 00:00:00 2001 From: kicer Date: Wed, 22 Jul 2026 14:59:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E8=81=94=E9=99=84=E4=BB=B6=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/server.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/server/server.js b/server/server.js index e5db249..7b165ed 100644 --- a/server/server.js +++ b/server/server.js @@ -408,16 +408,19 @@ function handleAIResult(aiResult, roomId, username, originalText, attachments) { // 引用最近图片:用户要求把之前的图片关联到某采购 let usedRecentImage = false; if (aiResult.use_recent_image) { - const recentMsg = db.prepare("SELECT attachments FROM messages WHERE room_id = ? AND attachments IS NOT NULL AND attachments != '' ORDER BY timestamp DESC LIMIT 1").get(roomId); + console.log('🔍 use_recent_image: 查找最近图片消息, roomId=', roomId); + const recentMsg = db.prepare("SELECT attachments FROM messages WHERE room_id = ? AND attachments IS NOT NULL AND attachments != '' AND attachments != '[]' ORDER BY timestamp DESC LIMIT 1").get(roomId); + console.log('🔍 最近消息:', recentMsg ? recentMsg.attachments : '(无)'); if (recentMsg) { try { const files = JSON.parse(recentMsg.attachments); - if (files.length) { attachments = files; usedRecentImage = true; } - } catch(e) {} + if (files.length) { attachments = files; usedRecentImage = true; console.log('✅ 引用图片:', files.length, '个'); } + } catch(e) { console.error('❌ 解析附件失败:', e); } } } const createdTime = aiResult.created_time || now; let purchase = db.prepare('SELECT * FROM purchases WHERE room_id = ? AND item LIKE ? AND status != ?').get(roomId, `%${aiResult.purchase_item}%`, '已完成'); + console.log('📦 purchase_item:', aiResult.purchase_item, purchase ? '(已有)' : '(新建)'); let replyText = ''; const quantity = aiResult.quantity || 1; const unitPrice = aiResult.unit_price || 0; @@ -477,14 +480,17 @@ function handleAIResult(aiResult, roomId, username, originalText, attachments) { } } if (attachments?.length) { + console.log('📎 插入附件:', attachments.length, '个, purchase_id=', purchase.id); const insertAttach = db.prepare('INSERT INTO purchase_attachments (purchase_id, file_path, uploaded_by, timestamp) VALUES (?,?,?,?)'); - attachments.forEach(fp => insertAttach.run(purchase.id, fp, username, now)); + attachments.forEach(fp => { console.log(' 📎', fp); insertAttach.run(purchase.id, fp, username, now); }); if (usedRecentImage) { replyText = `📎 已为「${aiResult.purchase_item}」关联 ${attachments.length} 个附件`; db.prepare('INSERT INTO purchase_history (purchase_id, action, user, timestamp) VALUES (?,?,?,?)').run(purchase.id, `添加附件:${attachments.length} 个`, username, now); + console.log('✅ usedRecentImage 完成, replyText:', replyText); } } else if (usedRecentImage) { replyText = `❌ 未找到最近的图片消息,请先发送图片再试。`; + console.log('⚠️ usedRecentImage 但附件为空'); } storeAndBroadcastText(roomId, '小财', replyText); addToHistory(roomId, 'assistant', replyText);