const mysql = require('mysql2/promise'); // 数据库配置 const dbConfig = { host: '1.95.162.61', port: 3306, user: 'root', password: 'schl@2025', database: 'wechat_app' }; async function verifyMessageStorage() { let connection; try { console.log('开始验证消息存储修复...'); // 连接数据库 connection = await mysql.createConnection(dbConfig); console.log('✅ 数据库连接成功'); // 查询会话表中的最新会话 console.log('\n📊 查询最新会话信息:'); const [conversations] = await connection.execute( 'SELECT conversation_id, userId, managerId, last_message, last_message_time, updated_at FROM chat_conversations ORDER BY updated_at DESC LIMIT 5' ); console.log(`找到 ${conversations.length} 个会话记录`); conversations.forEach((conv, index) => { console.log(`\n会话 ${index + 1}:`); console.log(` 会话ID: ${conv.conversation_id}`); console.log(` 用户ID: ${conv.userId}`); console.log(` 客服ID: ${conv.managerId}`); console.log(` 最后消息: ${conv.last_message ? conv.last_message.substring(0, 30) + '...' : '无'}`); console.log(` 最后消息时间: ${new Date(conv.last_message_time).toLocaleString('zh-CN')}`); }); // 查询消息表中的最新消息 console.log('\n📨 查询最新消息记录:'); const [messages] = await connection.execute( 'SELECT message_id, conversation_id, sender_type, sender_id, receiver_id, content, created_at FROM chat_messages ORDER BY created_at DESC LIMIT 10' ); console.log(`找到 ${messages.length} 条消息记录`); messages.forEach((msg, index) => { const senderTypeText = msg.sender_type === 1 ? '用户' : '客服'; console.log(`\n消息 ${index + 1}:`); console.log(` 消息ID: ${msg.message_id}`); console.log(` 会话ID: ${msg.conversation_id}`); console.log(` 发送类型: ${senderTypeText}`); console.log(` 发送者ID: ${msg.sender_id}`); console.log(` 接收者ID: ${msg.receiver_id}`); console.log(` 内容: ${msg.content ? msg.content.substring(0, 30) + '...' : '无'}`); console.log(` 创建时间: ${new Date(msg.created_at).toLocaleString('zh-CN')}`); }); // 检查特定会话的消息 if (conversations.length > 0) { const targetConversationId = conversations[0].conversation_id; console.log(`\n🔍 检查特定会话 ${targetConversationId} 的消息:`); const [specificMessages] = await connection.execute( 'SELECT message_id, sender_type, sender_id, content, created_at FROM chat_messages WHERE conversation_id = ? ORDER BY created_at DESC', [targetConversationId] ); console.log(` 该会话有 ${specificMessages.length} 条消息`); } // 总结 console.log('\n✅ 验证完成!'); if (messages.length > 0) { console.log('🎉 消息存储功能正常工作,已成功存储消息到chat_messages表!'); } else { console.log('⚠️ 未找到新的消息记录,请确认前端是否发送了消息进行测试'); } } catch (error) { console.error('❌ 验证过程中出错:', error.message); } finally { if (connection) { await connection.end(); console.log('\n📤 数据库连接已关闭'); } } } // 执行验证 verifyMessageStorage();