From d1c7003d18c652eeab4eb33c7bd51dfe8f7469c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Wed, 17 Dec 2025 10:33:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E6=B8=85=E7=90=86=E9=A1=B9=E7=9B=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- how origin | 14 - page.html | 480 -------------- simple_chat_test.js | 138 ---- test-customer-service.js | 333 ---------- test_chat_connection.js | 96 --- test_chat_functionality.js | 1276 ------------------------------------ update_product_table.js | 75 --- 7 files changed, 2412 deletions(-) delete mode 100644 how origin delete mode 100644 page.html delete mode 100644 simple_chat_test.js delete mode 100644 test-customer-service.js delete mode 100644 test_chat_connection.js delete mode 100644 test_chat_functionality.js delete mode 100644 update_product_table.js diff --git a/how origin b/how origin deleted file mode 100644 index 1585038..0000000 --- a/how origin +++ /dev/null @@ -1,14 +0,0 @@ - - SSUUMMMMAARRYY OOFF LLEESSSS CCOOMMMMAANNDDSS - - Commands marked with * may be preceded by a number, _N. - Notes in parentheses indicate the behavior if _N is given. - A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K. - - h H Display this help. - q :q Q :Q ZZ Exit. - --------------------------------------------------------------------------- - - MMOOVVIINNGG - - e ^E j ^N CR * Forward one line (or _N lines). diff --git a/page.html b/page.html deleted file mode 100644 index db31592..0000000 --- a/page.html +++ /dev/null @@ -1,480 +0,0 @@ - - - - - - - 国内新闻_新闻中心_新浪网 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - - - - - - - - -
-
- - -
-
- - - - - -
- - - -
- -
-
- -
-
-
    -
    -
    - - -
    -
    -
    -
    - - - - -
    - - - -
    -
    - - - icon关闭 - - - - - - - - - - - - - - - - -
    - -
    -
    - - - - - - -
    -
    - -
    - - - - - - - 下载新浪新闻 - - - - - 新浪新闻App - - - - -
    -

    意见反馈

    -
    - - -
    -

    返回顶部

    -
    -
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/simple_chat_test.js b/simple_chat_test.js deleted file mode 100644 index b57e76f..0000000 --- a/simple_chat_test.js +++ /dev/null @@ -1,138 +0,0 @@ -// 简化版聊天功能测试 - -// 服务器配置 -const SERVER_URL = 'ws://localhost:3003'; - -// 测试数据 -const managerData = { - userId: 'manager_001', - type: 'manager', - name: '客服小刘' -}; - -const userData = { - userId: 'user_001', - type: 'user', - name: '测试用户' -}; - -// 测试结果跟踪 -const testResults = { - managerConnection: false, - managerAuth: false, - userConnection: false, - userAuth: false, - messageExchange: false, - onlineStatusDetection: false, - messageCenterFunctionality: false -}; - -function runSimpleChatTest() { - console.log('=== 开始简化版聊天功能测试 ==='); - - // 模拟客服连接 - try { - const WebSocket = require('ws'); - const managerSocket = new WebSocket(SERVER_URL); - - managerSocket.on('open', () => { - console.log('[✅] 客服连接已建立'); - testResults.managerConnection = true; - - // 发送客服认证 - const authMessage = { - type: 'auth', - data: { - userId: managerData.userId, - type: managerData.type, - name: managerData.name - } - }; - console.log('发送客服认证:', authMessage); - managerSocket.send(JSON.stringify(authMessage)); - }); - - managerSocket.on('message', (data) => { - console.log('[客服收到消息]:', data.toString()); - const message = JSON.parse(data); - - // 检查认证结果 - if (message.type === 'auth_success' || message.action === 'auth_response') { - console.log('[✅] 客服认证成功'); - testResults.managerAuth = true; - } - }); - - managerSocket.on('error', (error) => { - console.error('[❌] 客服连接错误:', error.message); - }); - - managerSocket.on('close', () => { - console.log('[🔌] 客服连接已关闭'); - }); - - // 延迟创建用户连接 - setTimeout(() => { - const userSocket = new WebSocket(SERVER_URL); - - userSocket.on('open', () => { - console.log('[✅] 用户连接已建立'); - testResults.userConnection = true; - - // 发送用户认证 - const userAuth = { - type: 'auth', - data: { - userId: userData.userId, - type: userData.type, - name: userData.name - } - }; - console.log('发送用户认证:', userAuth); - userSocket.send(JSON.stringify(userAuth)); - }); - - userSocket.on('message', (data) => { - console.log('[用户收到消息]:', data.toString()); - }); - - // 5秒后发送测试消息 - setTimeout(() => { - if (userSocket.readyState === WebSocket.OPEN) { - const testMessage = { - type: 'chat', - from: userData.userId, - to: managerData.userId, - content: '你好,这是一条测试消息', - timestamp: Date.now() - }; - console.log('用户发送测试消息:', testMessage); - userSocket.send(JSON.stringify(testMessage)); - } - }, 5000); - - }, 3000); - - // 15秒后显示测试结果 - setTimeout(() => { - console.log('\n=== 测试结果 ==='); - console.log('客服连接:', testResults.managerConnection ? '✅ 成功' : '❌ 失败'); - console.log('客服认证:', testResults.managerAuth ? '✅ 成功' : '❌ 失败'); - console.log('用户连接:', testResults.userConnection ? '✅ 成功' : '❌ 失败'); - console.log('\n测试完成!'); - - // 关闭连接 - managerSocket.close(); - process.exit(0); - - }, 15000); - - } catch (error) { - console.error('测试运行失败:', error.message); - } -} - -// 运行测试 -if (require.main === module) { - runSimpleChatTest(); -} diff --git a/test-customer-service.js b/test-customer-service.js deleted file mode 100644 index d2a47d1..0000000 --- a/test-customer-service.js +++ /dev/null @@ -1,333 +0,0 @@ -// 客服功能测试脚本 -// 用于验证客服认证、身份判断和双向沟通功能 - -console.log('===== 开始客服功能测试 ====='); - -// 模拟用户信息和环境 -const mockUserInfo = { - customerUser: { - id: 'test_customer_001', - userType: null, - type: null, - isService: false, - isManager: false - }, - serviceUser: { - id: 'test_service_001', - userType: 'customer_service', - type: 'service', - isService: true, - isManager: false - }, - managerUser: { - id: 'test_manager_001', - userType: 'customer_service', - type: 'manager', - isService: false, - isManager: true - } -}; - -// 测试1: 用户类型判断逻辑 -console.log('\n测试1: 用户类型判断逻辑'); -testUserTypeDetection(); - -// 测试2: WebSocket消息格式 -console.log('\n测试2: WebSocket消息格式'); -testWebSocketMessageFormat(); - -// 测试3: 消息处理逻辑 -console.log('\n测试3: 消息处理逻辑'); -testMessageProcessing(); - -// 测试4: 双向通信模式 -console.log('\n测试4: 双向通信模式'); -testBidirectionalCommunication(); - -console.log('\n===== 测试完成 ====='); - -// 测试用户类型判断逻辑 -function testUserTypeDetection() { - console.log('- 测试用户类型判断函数'); - - // 模拟用户类型判断函数 - function detectUserType(userInfo) { - if (!userInfo) return 'customer'; - - if (userInfo.userType === 'customer_service' || - userInfo.type === 'service' || - userInfo.type === 'manager' || - userInfo.isService || - userInfo.isManager) { - return 'customer_service'; - } - - return 'customer'; - } - - // 测试各种用户类型 - const testCases = [ - { input: mockUserInfo.customerUser, expected: 'customer', desc: '普通用户' }, - { input: mockUserInfo.serviceUser, expected: 'customer_service', desc: '客服用户' }, - { input: mockUserInfo.managerUser, expected: 'customer_service', desc: '管理员用户' }, - { input: null, expected: 'customer', desc: '空用户信息' }, - { input: {}, expected: 'customer', desc: '空对象' } - ]; - - let passed = 0; - let failed = 0; - - testCases.forEach((testCase, index) => { - const result = detectUserType(testCase.input); - const isPass = result === testCase.expected; - - if (isPass) { - passed++; - console.log(` ✓ 测试${index + 1} (${testCase.desc}): 期望 ${testCase.expected}, 结果 ${result}`); - } else { - failed++; - console.log(` ✗ 测试${index + 1} (${testCase.desc}): 期望 ${testCase.expected}, 结果 ${result}`); - } - }); - - console.log(` 结果: 通过 ${passed}, 失败 ${failed}`); -} - -// 测试WebSocket消息格式 -function testWebSocketMessageFormat() { - console.log('- 测试WebSocket消息格式'); - - // 模拟创建消息函数 - function createWebSocketMessage(senderId, receiverId, content, senderType) { - return { - type: 'chat_message', - direction: senderType === 'customer_service' ? 'service_to_customer' : 'customer_to_service', - data: { - receiverId: receiverId, - senderId: senderId, - senderType: senderType, - content: content, - contentType: 1, - timestamp: Date.now() - } - }; - } - - // 测试客服发送消息 - const serviceMsg = createWebSocketMessage( - mockUserInfo.serviceUser.id, - mockUserInfo.customerUser.id, - '您好,有什么可以帮助您的吗?', - 'customer_service' - ); - - // 测试客户发送消息 - const customerMsg = createWebSocketMessage( - mockUserInfo.customerUser.id, - mockUserInfo.serviceUser.id, - '我想咨询一下产品信息', - 'customer' - ); - - console.log(' 客服消息格式:'); - console.log(` - type: ${serviceMsg.type}`); - console.log(` - direction: ${serviceMsg.direction}`); - console.log(` - senderId: ${serviceMsg.data.senderId}`); - console.log(` - receiverId: ${serviceMsg.data.receiverId}`); - console.log(` - senderType: ${serviceMsg.data.senderType}`); - - console.log(' 客户消息格式:'); - console.log(` - type: ${customerMsg.type}`); - console.log(` - direction: ${customerMsg.direction}`); - console.log(` - senderId: ${customerMsg.data.senderId}`); - console.log(` - receiverId: ${customerMsg.data.receiverId}`); - console.log(` - senderType: ${customerMsg.data.senderType}`); - - // 验证必要字段 - const requiredFields = ['type', 'direction', 'data']; - const requiredDataFields = ['receiverId', 'senderId', 'senderType', 'content', 'contentType', 'timestamp']; - - let hasAllRequiredFields = true; - - requiredFields.forEach(field => { - if (!(field in serviceMsg)) { - console.log(` ✗ 消息缺少必要字段: ${field}`); - hasAllRequiredFields = false; - } - }); - - requiredDataFields.forEach(field => { - if (!(field in serviceMsg.data)) { - console.log(` ✗ 消息data缺少必要字段: ${field}`); - hasAllRequiredFields = false; - } - }); - - if (hasAllRequiredFields) { - console.log(' ✓ 消息格式验证通过'); - } else { - console.log(' ✗ 消息格式验证失败'); - } -} - -// 测试消息处理逻辑 -function testMessageProcessing() { - console.log('- 测试消息处理逻辑'); - - // 模拟处理接收到的消息 - function processChatMessage(message, currentUserId, currentUserType) { - if (!message || !message.data) { - return null; - } - - // 判断消息方向 - const isFromMe = message.data.senderId === currentUserId; - const isFromService = message.data.senderType === 'customer_service'; - const isFromCustomer = message.data.senderType === 'customer'; - - // 构建本地消息对象 - const localMessage = { - id: message.id || Date.now().toString(), - content: message.data.content || '', - contentType: message.data.contentType || 1, - timestamp: message.data.timestamp || Date.now(), - isFromMe: isFromMe, - senderType: message.data.senderType || 'unknown', - serverData: message, - status: 'received' - }; - - return localMessage; - } - - // 测试消息 - const testMessage = { - id: 'msg_001', - type: 'chat_message', - direction: 'customer_to_service', - data: { - receiverId: mockUserInfo.serviceUser.id, - senderId: mockUserInfo.customerUser.id, - senderType: 'customer', - content: '测试消息', - contentType: 1, - timestamp: Date.now() - } - }; - - // 从客服视角处理 - const serviceProcessed = processChatMessage( - testMessage, - mockUserInfo.serviceUser.id, - 'customer_service' - ); - - // 从客户视角处理 - const customerProcessed = processChatMessage( - testMessage, - mockUserInfo.customerUser.id, - 'customer' - ); - - console.log(' 客服视角处理结果:'); - console.log(` - 是否来自自己: ${serviceProcessed.isFromMe}`); - console.log(` - 发送方类型: ${serviceProcessed.senderType}`); - console.log(` - 内容: ${serviceProcessed.content}`); - - console.log(' 客户视角处理结果:'); - console.log(` - 是否来自自己: ${customerProcessed.isFromMe}`); - console.log(` - 发送方类型: ${customerProcessed.senderType}`); - console.log(` - 内容: ${customerProcessed.content}`); - - // 验证处理逻辑 - const isServiceLogicCorrect = !serviceProcessed.isFromMe && serviceProcessed.senderType === 'customer'; - const isCustomerLogicCorrect = customerProcessed.isFromMe && customerProcessed.senderType === 'customer'; - - if (isServiceLogicCorrect && isCustomerLogicCorrect) { - console.log(' ✓ 消息处理逻辑验证通过'); - } else { - console.log(' ✗ 消息处理逻辑验证失败'); - if (!isServiceLogicCorrect) console.log(' - 客服视角处理错误'); - if (!isCustomerLogicCorrect) console.log(' - 客户视角处理错误'); - } -} - -// 测试双向通信模式 -function testBidirectionalCommunication() { - console.log('- 测试双向通信模式'); - - // 模拟对话流程 - const conversation = [ - { - sender: 'customer', - content: '您好,我想咨询一下产品价格', - expectedDirection: 'customer_to_service' - }, - { - sender: 'service', - content: '您好,请问您想了解哪种产品的价格呢?', - expectedDirection: 'service_to_customer' - }, - { - sender: 'customer', - content: '就是你们的主打产品', - expectedDirection: 'customer_to_service' - }, - { - sender: 'service', - content: '我们的主打产品价格是¥199,现在有优惠活动', - expectedDirection: 'service_to_customer' - } - ]; - - let conversationLog = []; - - conversation.forEach((msg, index) => { - const isFromService = msg.sender === 'service'; - const senderId = isFromService ? mockUserInfo.serviceUser.id : mockUserInfo.customerUser.id; - const receiverId = isFromService ? mockUserInfo.customerUser.id : mockUserInfo.serviceUser.id; - const senderType = isFromService ? 'customer_service' : 'customer'; - - const message = { - id: `msg_${index + 1}`, - type: 'chat_message', - direction: msg.expectedDirection, - data: { - receiverId: receiverId, - senderId: senderId, - senderType: senderType, - content: msg.content, - contentType: 1, - timestamp: Date.now() + index - } - }; - - conversationLog.push({ - role: isFromService ? '客服' : '客户', - content: msg.content, - direction: msg.expectedDirection - }); - - // 验证消息方向 - if (message.direction !== msg.expectedDirection) { - console.log(` ✗ 消息${index + 1}方向错误: 期望${msg.expectedDirection}, 实际${message.direction}`); - } - }); - - // 打印对话流程 - console.log(' 双向对话流程:'); - conversationLog.forEach((msg, index) => { - console.log(` ${index + 1}. [${msg.role}] ${msg.content} (${msg.direction})`); - }); - - console.log(' ✓ 双向通信模式验证完成'); -} - -// 导出测试结果 -module.exports = { - mockUserInfo, - testUserTypeDetection, - testWebSocketMessageFormat, - testMessageProcessing, - testBidirectionalCommunication -}; diff --git a/test_chat_connection.js b/test_chat_connection.js deleted file mode 100644 index 1cd74aa..0000000 --- a/test_chat_connection.js +++ /dev/null @@ -1,96 +0,0 @@ -// 测试聊天功能连接的脚本 -const WebSocket = require('ws'); - -// 假设服务器WebSocket地址 -const SERVER_URL = 'ws://localhost:3000'; // 根据实际服务器地址调整 - -// 模拟用户和客服的连接 -function testUserToManagerCommunication() { - console.log('开始测试用户和客服之间的消息传递...'); - - // 模拟客服连接 - const managerSocket = new WebSocket(SERVER_URL); - - managerSocket.on('open', () => { - console.log('客服连接已建立'); - - // 客服认证 - managerSocket.send(JSON.stringify({ - type: 'auth', - data: { - userId: 'manager_1', - type: 'manager', - name: '测试客服' - } - })); - }); - - managerSocket.on('message', (data) => { - try { - const message = JSON.parse(data.toString()); - console.log('客服收到消息:', message); - } catch (e) { - console.error('客服解析消息失败:', e); - } - }); - - managerSocket.on('error', (error) => { - console.error('客服连接错误:', error); - }); - - // 延迟2秒后创建用户连接 - setTimeout(() => { - const userSocket = new WebSocket(SERVER_URL); - - userSocket.on('open', () => { - console.log('用户连接已建立'); - - // 用户认证 - userSocket.send(JSON.stringify({ - type: 'auth', - data: { - userId: 'user_1', - type: 'user', - name: '测试用户' - } - })); - - // 再延迟1秒后发送消息 - setTimeout(() => { - console.log('用户发送测试消息...'); - userSocket.send(JSON.stringify({ - type: 'chat_message', - data: { - managerId: 'manager_1', - content: '这是一条测试消息', - contentType: 1, // 文本消息 - timestamp: Date.now() - } - })); - }, 1000); - }); - - userSocket.on('message', (data) => { - try { - const message = JSON.parse(data.toString()); - console.log('用户收到消息:', message); - } catch (e) { - console.error('用户解析消息失败:', e); - } - }); - - userSocket.on('error', (error) => { - console.error('用户连接错误:', error); - }); - - // 清理连接 - setTimeout(() => { - console.log('测试完成,关闭连接'); - userSocket.close(); - managerSocket.close(); - }, 10000); - }, 2000); -} - -// 运行测试 -testUserToManagerCommunication(); \ No newline at end of file diff --git a/test_chat_functionality.js b/test_chat_functionality.js deleted file mode 100644 index 8340175..0000000 --- a/test_chat_functionality.js +++ /dev/null @@ -1,1276 +0,0 @@ -// 完整的聊天功能测试脚本 - 根据WebSocket管理器实现调整 -const WebSocket = require('ws'); - -// 服务器WebSocket地址 - 使用3003端口 -const SERVER_URL = 'ws://localhost:3003'; - -console.log('===================================='); -console.log('开始全面测试聊天功能'); -console.log(`连接到服务器: ${SERVER_URL}`); -console.log('====================================\n'); - -// 调试开关 -const DEBUG = true; - -// 消息发送配置 -const MESSAGE_CONFIG = { - // 基础发送间隔 (ms) - baseInterval: 2000, - // 最大重试次数 - maxRetries: 3, - // 初始超时时间 (ms) - initialTimeout: 5000, - // 动态间隔调整因子 - dynamicAdjustmentFactor: 0.2, - // 最大消息队列长度 - maxQueueSize: 100, - // 心跳间隔 (ms) - heartbeatInterval: 10000, - // 消息中心查询间隔 (ms) - messageCenterInterval: 5000 -}; - -// 测试结果跟踪 -const testResults = { - managerConnection: false, - userConnection: false, - managerAuth: false, - userAuth: false, - onlineStatusDetection: false, - identityRecognition: false, - messageFromUserToManager: false, - messageFromManagerToUser: false, - messageCenterFunctionality: false -}; - -// 消息队列管理 -const messageQueue = { - queue: [], - isProcessing: false, - - // 添加消息到队列 - enqueue: function(message, priority = 'normal', retryCount = 0) { - if (this.queue.length >= MESSAGE_CONFIG.maxQueueSize) { - console.warn('[警告] 消息队列已满,丢弃新消息'); - return false; - } - - // 优先级映射 - const priorityMap = { high: 0, normal: 1, low: 2 }; - - const queueItem = { - message: message, - priority: priorityMap[priority] || 1, - retryCount: retryCount, - timestamp: new Date().getTime(), - messageId: generateMessageId() - }; - - this.queue.push(queueItem); - - // 根据优先级排序 - this.queue.sort((a, b) => a.priority - b.priority); - - console.log(`[队列] 添加消息到队列 (优先级: ${priority}, 队列大小: ${this.queue.length})`); - - // 如果队列未在处理中,开始处理 - if (!this.isProcessing) { - this.processNext(); - } - - return queueItem.messageId; - }, - - // 处理队列中的下一条消息 - processNext: function() { - if (this.queue.length === 0) { - this.isProcessing = false; - return; - } - - this.isProcessing = true; - const item = this.queue.shift(); - - console.log(`[队列] 处理消息 (重试: ${item.retryCount}/${MESSAGE_CONFIG.maxRetries})`); - - // 设置消息发送超时 - const timeoutId = setTimeout(() => { - console.error(`[队列] 消息发送超时: ${item.messageId}`); - - // 如果未达到最大重试次数,则重新入队 - if (item.retryCount < MESSAGE_CONFIG.maxRetries) { - console.log(`[队列] 消息重新入队进行重试: ${item.messageId}`); - this.enqueue(item.message, item.priority === 0 ? 'high' : 'normal', item.retryCount + 1); - } else { - console.error(`[队列] 消息达到最大重试次数,发送失败: ${item.messageId}`); - // 记录失败的消息 - messageTracker.updateMessageStatus(item.messageId, 'failed'); - } - - // 处理下一条消息 - this.processNext(); - }, MESSAGE_CONFIG.initialTimeout + (item.retryCount * 2000)); - - // 发送消息 - try { - if (managerSocket && managerSocket.readyState === WebSocket.OPEN) { - managerSocket.send(JSON.stringify(item.message)); - console.log(`[队列] 消息已发送: ${item.messageId}`); - - // 记录发送状态 - messageTracker.updateMessageStatus(item.messageId, 'sent'); - - // 清除超时 - clearTimeout(timeoutId); - - // 动态调整下一条消息的间隔 - const nextInterval = this.calculateDynamicInterval(); - setTimeout(() => { - this.processNext(); - }, nextInterval); - } else { - console.error('[队列] WebSocket连接未打开,推迟消息发送'); - clearTimeout(timeoutId); - - // 重新入队 - this.enqueue(item.message, 'high', item.retryCount); - - // 稍后重试 - setTimeout(() => { - this.processNext(); - }, 1000); - } - } catch (error) { - console.error('[队列] 消息发送错误:', error); - clearTimeout(timeoutId); - - // 重新入队 - if (item.retryCount < MESSAGE_CONFIG.maxRetries) { - this.enqueue(item.message, item.priority === 0 ? 'high' : 'normal', item.retryCount + 1); - } - - this.processNext(); - } - }, - - // 动态计算下一次发送间隔 - calculateDynamicInterval: function() { - // 获取最近的响应时间历史 - const recentResponses = messageTracker.messagesSent.filter(m => - m.status === 'delivered' || m.status === 'sent' - ).slice(-5); - - if (recentResponses.length === 0) { - return MESSAGE_CONFIG.baseInterval; - } - - // 计算平均响应时间 - const avgResponseTime = recentResponses.reduce((sum, msg) => { - const responseTime = (msg.updatedAt || new Date().getTime()) - msg.timestamp; - return sum + responseTime; - }, 0) / recentResponses.length; - - // 基于响应时间动态调整间隔 - const dynamicInterval = MESSAGE_CONFIG.baseInterval + - (avgResponseTime * MESSAGE_CONFIG.dynamicAdjustmentFactor); - - // 限制最小和最大间隔 - const minInterval = MESSAGE_CONFIG.baseInterval * 0.5; - const maxInterval = MESSAGE_CONFIG.baseInterval * 3; - - return Math.max(minInterval, Math.min(maxInterval, dynamicInterval)); - }, - - // 清空队列 - clear: function() { - this.queue = []; - console.log('[队列] 消息队列已清空'); - }, - - // 获取队列状态 - getStatus: function() { - return { - size: this.queue.length, - isProcessing: this.isProcessing, - highPriorityCount: this.queue.filter(item => item.priority === 0).length, - normalPriorityCount: this.queue.filter(item => item.priority === 1).length, - lowPriorityCount: this.queue.filter(item => item.priority === 2).length - }; - } -}; - -// 消息发送跟踪对象 -const messageTracker = { - messagesSent: [], - messagesReceived: [], - messageAttempts: 0, - successfulReplies: 0, - lastMessageTime: null, - addSentMessage: function(message, formatIndex) { - const msgId = generateMessageId(); - this.messagesSent.push({ - id: msgId, - content: message.content || (message.data && message.data.content) || (message.message && message.message.content) || (message.payload && message.payload.content), - timestamp: new Date().getTime(), - format: formatIndex, - status: 'sending', - fullMessage: message - }); - console.log(`[跟踪] 消息已加入发送队列 (ID: ${msgId}, 格式: ${formatIndex})`); - this.messageAttempts++; - this.lastMessageTime = new Date().getTime(); - return msgId; - }, - updateMessageStatus: function(msgId, status) { - const msg = this.messagesSent.find(m => m.id === msgId); - if (msg) { - msg.status = status; - msg.updatedAt = new Date().getTime(); - console.log(`[跟踪] 消息状态更新 (ID: ${msgId}, 状态: ${status})`); - if (status === 'sent' || status === 'delivered') { - this.successfulReplies++; - } - } - }, - addReceivedMessage: function(message) { - this.messagesReceived.push({ - id: generateMessageId(), - content: message.content || (message.data && message.data.content) || (message.message && message.message.content) || (message.payload && message.payload.content), - timestamp: new Date().getTime(), - sender: message.from || message.sender || message.managerId, - receiver: message.to || message.recipient || message.userId, - fullMessage: message - }); - console.log(`[跟踪] 收到新消息 (发送者: ${message.from || message.sender || message.managerId})`); - }, - logStats: function() { - console.log('===================================='); - console.log('📊 消息发送统计:'); - console.log(`- 总发送尝试: ${this.messageAttempts}`); - console.log(`- 成功回复: ${this.successfulReplies}`); - console.log(`- 发送消息数: ${this.messagesSent.length}`); - console.log(`- 接收消息数: ${this.messagesReceived.length}`); - console.log(`- 最后消息时间: ${this.lastMessageTime ? new Date(this.lastMessageTime).toLocaleTimeString() : '无'}`); - console.log('===================================='); - } -}; - -// 连接状态跟踪器 -const connectionTracker = { - managerState: 'disconnected', - userState: 'disconnected', - managerStateChanges: [], - userStateChanges: [], - updateManagerState: function(state) { - this.managerState = state; - const timestamp = new Date().getTime(); - this.managerStateChanges.push({ state, timestamp }); - console.log(`[连接] 客服连接状态变更: ${state} (${new Date(timestamp).toLocaleTimeString()})`); - }, - updateUserState: function(state) { - this.userState = state; - const timestamp = new Date().getTime(); - this.userStateChanges.push({ state, timestamp }); - console.log(`[连接] 用户连接状态变更: ${state} (${new Date(timestamp).toLocaleTimeString()})`); - }, - logConnectionHistory: function() { - console.log('===================================='); - console.log('📱 连接历史:'); - console.log('客服连接:'); - this.managerStateChanges.forEach(change => { - console.log(`- ${new Date(change.timestamp).toLocaleTimeString()}: ${change.state}`); - }); - console.log('用户连接:'); - this.userStateChanges.forEach(change => { - console.log(`- ${new Date(change.timestamp).toLocaleTimeString()}: ${change.state}`); - }); - console.log('===================================='); - } -}; - -// 模拟数据 -const managerData = { - userId: 'manager_1001', - type: 'manager', // 使用type字段 - name: '刘海' -}; - -const userData = { - userId: 'user_001', - type: 'user', // 使用type字段而不是customer - name: '测试用户' -}; - -// 测试函数:显示测试结果 -function displayTestResults() { - console.log('\n===================================='); - console.log('测试结果汇总:'); - console.log('------------------------------------'); - - Object.entries(testResults).forEach(([key, value]) => { - const status = value ? '✅ 通过' : '❌ 失败'; - console.log(`${key}: ${status}`); - }); - - console.log('------------------------------------'); - - const allPassed = Object.values(testResults).every(result => result); - if (allPassed) { - console.log('🎉 所有测试通过!聊天功能正常工作。'); - } else { - console.log('🔴 部分测试失败,请检查相关功能。'); - } - - console.log('===================================='); -} - -// 使用正确的认证格式 - 基于test_chat_connection.js的参考实现 -function createAuthMessage(userId, type, name) { - return { - type: 'auth', - data: { - userId: userId, - type: type, // 使用type字段而不是userType - name: name // 添加name字段以符合认证要求 - } - }; -} - -// 测试主函数 -function runChatFunctionalityTests() { - // 模拟客服连接 - const managerSocket = new WebSocket(SERVER_URL); - let userSocket = null; - let managerAuthSent = false; - let userAuthSent = false; - let heartbeatInterval = null; - let messageCenterCheckInterval = null; - - // 客服连接处理 - managerSocket.on('open', () => { - connectionTracker.updateManagerState('connected'); - console.log('[1/6] 客服WebSocket连接已建立'); - testResults.managerConnection = true; - - if (DEBUG) { - console.log(`[调试] 客服连接详情: 地址: ${SERVER_URL}`); - } - - // 重新启动队列处理 - console.log('[队列] 连接恢复,重新启动队列处理'); - messageQueue.processNext(); - - // 发送客服认证消息 - 尝试多种格式 - console.log('[2/6] 客服开始认证...'); - - // 格式1: 简化的login格式 - const authFormat1 = { - action: 'login', - managerId: managerData.userId, - name: managerData.name - }; - console.log('尝试格式1: 简化login格式'); - managerSocket.send(JSON.stringify(authFormat1)); - managerAuthSent = true; - - // 延迟后尝试格式2 - setTimeout(() => { - if (!testResults.managerAuth) { - const authFormat2 = { - type: 'manager_login', - userId: managerData.userId, - name: managerData.name - }; - console.log('尝试格式2: manager_login类型'); - managerSocket.send(JSON.stringify(authFormat2)); - } - }, 2000); - - // 延迟后尝试格式3 - setTimeout(() => { - if (!testResults.managerAuth) { - const authFormat3 = { - cmd: 'auth', - userId: managerData.userId, - role: 'manager', - name: managerData.name - }; - console.log('尝试格式3: cmd:auth'); - managerSocket.send(JSON.stringify(authFormat3)); - } - }, 4000); - - // 延迟后尝试格式4 - setTimeout(() => { - if (!testResults.managerAuth) { - const authFormat4 = { - event: 'manager_auth', - data: { - id: managerData.userId, - name: managerData.name - } - }; - console.log('尝试格式4: event:manager_auth'); - managerSocket.send(JSON.stringify(authFormat4)); - } - }, 6000); - - // 3秒后如果没有认证成功,尝试备用格式 - setTimeout(() => { - if (!testResults.managerAuth) { - console.log('[2/6] 尝试备用认证格式...'); - managerSocket.send(JSON.stringify({ - type: 'auth', - data: { - userId: managerData.userId, - type: managerData.type, - name: managerData.name - } - })); - } - }, 3000); - - // 直接尝试监听消息中心,即使未完全认证 - setTimeout(() => { - console.log('🎯 客服尝试直接监听用户消息...'); - testResults.managerAuth = true; // 为了测试流程继续,暂时标记为通过 - testResults.onlineStatusDetection = true; - console.log('[2/6] ✅ 客服认证流程跳过'); - console.log('[3/6] ✅ 在线状态检测通过'); - }, 8000); - - // 智能心跳管理 - let heartbeatInterval; - function setupSmartHeartbeat() { - // 清除已存在的定时器 - if (heartbeatInterval) { - clearInterval(heartbeatInterval); - } - - // 使用配置的间隔时间 - heartbeatInterval = setInterval(() => { - if (managerSocket.readyState === WebSocket.OPEN) { - const heartbeat = { - type: 'heartbeat', - timestamp: new Date().getTime(), - status: { - queueSize: messageQueue.getStatus().size, - activeConnections: connectionTracker.managerState === 'connected' ? 1 : 0 - } - }; - - // 心跳消息使用正常优先级 - const queueId = messageQueue.enqueue(heartbeat, 'normal'); - if (DEBUG) { - console.log(`[调试] 心跳包已加入队列 (队列ID: ${queueId})`); - } - } - }, MESSAGE_CONFIG.heartbeatInterval); - } - - // 初始化智能心跳 - setupSmartHeartbeat(); - - // 定期检查队列状态 - const queueStatusInterval = setInterval(() => { - const status = messageQueue.getStatus(); - if (status.size > 10) { - console.warn(`[警告] 消息队列积压: ${status.size}条消息`); - } - }, 30000); - - // 设置消息中心定期查询 - 使用动态间隔 - let messageCenterCheckInterval; - function setupMessageCenterQuery() { - // 清除已存在的定时器 - if (messageCenterCheckInterval) { - clearInterval(messageCenterCheckInterval); - } - - // 使用配置的间隔时间 - messageCenterCheckInterval = setInterval(() => { - if (managerSocket.readyState === WebSocket.OPEN) { - console.log('🔄 定期查询消息中心...'); - // 尝试多种消息中心查询格式 - const queryFormats = [ - { - type: 'get_messages', - managerId: managerData.userId - }, - { - action: 'fetch_messages', - userId: managerData.userId, - role: 'manager' - }, - { - cmd: 'get_chat_list', - managerId: managerData.userId - }, - { - type: 'query_message_center', - userId: managerData.userId - } - ]; - - // 随机选择一个格式查询,增加成功几率 - const randomFormat = queryFormats[Math.floor(Math.random() * queryFormats.length)]; - console.log('使用随机消息中心查询格式:', randomFormat); - - // 通过队列发送查询(低优先级) - const queueId = messageQueue.enqueue(randomFormat, 'low'); - console.log(`[队列] 消息中心查询已加入队列 (队列ID: ${queueId})`); - } - }, MESSAGE_CONFIG.messageCenterInterval); - } - - // 初始化消息中心查询 - setupMessageCenterQuery(); - }); - - managerSocket.on('message', (data) => { - try { - const message = JSON.parse(data.toString()); - - // 记录接收到的消息 - messageTracker.addReceivedMessage(message); - - // 消息类型分析 - const messageType = message.type || message.action || message.command || 'unknown_type'; - console.log('📨 客服收到消息:', messageType); - - if (DEBUG) { - // 检查是否为消息中心查询响应 - if (messageType.includes('message') && (messageType.includes('response') || messageType.includes('list') || messageType.includes('result'))) { - console.log(`[调试] 消息中心响应: 消息数量 ${message.messages ? message.messages.length : 0}`); - } - - // 显示认证相关消息的详情 - if (messageType.includes('auth')) { - console.log(`[调试] 认证消息详情: ${JSON.stringify(message)}`); - } - } - - console.log('📨 客服收到消息:', message); - - // 处理认证成功响应 - auth_success类型 - if (message.type === 'auth_success') { - console.log('[2/6] ✅ 客服认证成功'); - testResults.managerAuth = true; - - // 检查在线状态 - testResults.onlineStatusDetection = true; - console.log('[3/6] ✅ 在线状态检测通过'); - - // 检查身份识别 - 从payload中获取用户信息 - if (message.payload && message.payload.type === managerData.type) { - testResults.identityRecognition = true; - console.log('[4/6] ✅ 身份识别通过'); - } - return; - } - - // 处理认证响应 - auth_response类型 - if (message.type === 'auth_response') { - if (message.success) { - console.log('[2/6] ✅ 客服认证成功'); - testResults.managerAuth = true; - - // 检查在线状态 - testResults.onlineStatusDetection = true; - console.log('[3/6] ✅ 在线状态检测通过'); - - // 检查身份识别 - if (message.data && message.data.type === managerData.type) { - testResults.identityRecognition = true; - console.log('[4/6] ✅ 身份识别通过'); - } - } else { - console.log(`[2/6] ❌ 客服认证失败: ${message.message || '未知错误'}`); - } - return; - } - - // 处理login_response类型 - if (message.type === 'login_response') { - if (message.success) { - console.log('[2/6] ✅ 客服认证成功 (login_response)'); - testResults.managerAuth = true; - - // 检查在线状态 - testResults.onlineStatusDetection = true; - console.log('[3/6] ✅ 在线状态检测通过'); - - // 检查身份识别 - if (message.payload && message.payload.type === managerData.type) { - testResults.identityRecognition = true; - console.log('[4/6] ✅ 身份识别通过'); - } - } else { - console.log(`[2/6] ❌ 客服认证失败: ${message.message || '未知错误'}`); - } - return; - } - - // 处理心跳消息 - if (message.type === 'ping' || message.type === 'heartbeat') { - console.log('💓 收到心跳请求,发送pong响应'); - managerSocket.send(JSON.stringify({ type: 'pong' })); - - // 心跳间隙立即查询消息中心 - setTimeout(() => { - console.log('💓 心跳间隙查询消息中心'); - managerSocket.send(JSON.stringify({ - type: 'get_messages', - managerId: managerData.userId, - timestamp: Date.now() - })); - }, 100); - return; - } - - // 处理用户发送的消息 - 增强的识别逻辑 - const isFromUser = - message.from === userData.userId || - message.sender === userData.userId || - message.data?.from === userData.userId || - message.data?.sender === userData.userId; - - if ((message.type === 'chat_message' || message.type === 'message' || - message.cmd === 'chat_message' || message.action === 'chat_message') && - isFromUser) { - const content = message.data?.content || message.content || message.msg || message.message; - console.log(`[4/6] ✅ 客服成功接收到用户消息: "${content}"`); - testResults.messageFromUserToManager = true; - - // 立即回复用户,不管认证状态如何 - console.log('[5/6] 客服尝试回复用户...'); - - // 准备增强版多种回复格式 - 增加更多格式支持和错误处理 - const replyFormats = [ - { - type: 'chat_message', - from: managerData.userId, - userId: userData.userId, - content: '您好,感谢您的咨询!这是客服回复。', - timestamp: Date.now(), - sessionId: 'session_' + Date.now(), - messageId: generateMessageId() - }, - { - action: 'reply', - data: { - from: managerData.userId, - to: userData.userId, - content: '您好,感谢您的咨询!这是备用格式回复。', - timestamp: Date.now(), - messageType: 'text', - status: 'sending' - } - }, - { - cmd: 'send_message', - from: managerData.userId, - to: userData.userId, - content: '您好,我是刘海客服,很高兴为您服务!', - timestamp: Date.now(), - priority: 'high' - }, - { - type: 'reply', - sender: managerData.userId, - receiver: userData.userId, - content: '您好,有什么可以帮助您的吗?', - timestamp: Date.now(), - direction: 'manager_to_user' - }, - { - event: 'message_sent', - payload: { - content: '您好,这里是客服中心!', - managerId: managerData.userId, - userId: userData.userId, - messageId: generateMessageId(), - channel: 'chat' - } - }, - { - cmd: 'response', - params: { - content: '感谢您的咨询,我会尽快为您解答!', - from: managerData.userId, - target: userData.userId, - messageType: 'reply', - timestamp: Date.now() - } - } - ]; - - // 发送消息并添加确认处理 - function sendReplyWithConfirmation(format, formatIndex, priority = 'high') { - if (managerSocket.readyState === WebSocket.OPEN) { - console.log(`客服回复消息格式${formatIndex + 1}:`, format); - - // 添加队列特定字段 - format._queueMetadata = { - formatIndex: formatIndex, - originalPriority: priority, - sendTime: new Date().getTime() - }; - - // 记录消息跟踪 - const trackingId = messageTracker.addSentMessage(format, formatIndex); - - // 使用消息队列发送消息 - const queueId = messageQueue.enqueue(format, priority); - console.log(`[队列] 消息已加入发送队列 (队列ID: ${queueId})`); - - // 添加发送确认检测 - setTimeout(() => { - if (!testResults.messageFromManagerToUser) { - console.log(`⏳ 等待格式${formatIndex + 1}消息发送确认...`); - } - }, 200); - } else { - console.error('❌ 客服连接已关闭,无法发送回复'); - // 尝试重新连接并发送 - setTimeout(() => { - if (managerSocket.readyState === WebSocket.CLOSED) { - console.log('🔄 尝试重新连接客服WebSocket...'); - // 这里可以添加重连逻辑 - } - }, 1000); - } - } - - // 立即发送第一种格式 - sendReplyWithConfirmation(replyFormats[0], 0); - - // 依次发送其他格式,确保至少有一种能被接收 - replyFormats.slice(1).forEach((format, index) => { - setTimeout(() => { - sendReplyWithConfirmation(format, index + 1); - }, (index + 1) * 1000); - }); - - // 备用方案:使用直接消息方式 - setTimeout(() => { - if (!testResults.messageFromManagerToUser && managerSocket.readyState === WebSocket.OPEN) { - console.log('🔄 使用备用方案:直接发送消息'); - const directMessage = { - type: 'direct_message', - from: managerData.userId, - to: userData.userId, - content: '您好,这是一条直接发送的消息。', - bypass_normal: true, - timestamp: Date.now() - }; - managerSocket.send(JSON.stringify(directMessage)); - } - }, 5000); - } - - // 生成唯一消息ID函数 - function generateMessageId() { - return 'msg_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9); - } - - // 处理系统消息或广播 - if (message.type === 'system' || message.type === 'broadcast') { - console.log('📢 收到系统消息:', message); - } - - // 处理消息中心通知 - 增强格式支持 - const isMessageCenterUpdate = - message.type === 'message_center_update' || - message.type === 'new_message' || - message.type === 'notification' || - message.type === 'chat_list' || - message.type === 'unread_count' || - message.type === 'messages' || - message.type === 'message_list' || - message.action === 'message_update' || - message.cmd === 'message_list'; - - if (isMessageCenterUpdate) { - console.log('📬 消息中心收到更新通知:', message); - testResults.messageCenterFunctionality = true; - console.log('[6/6] ✅ 消息中心功能检测通过'); - - // 智能提取消息列表 - 支持多种数据结构 - let messageList = []; - if (Array.isArray(message.data)) { - messageList = message.data; - } else if (Array.isArray(message.messages)) { - messageList = message.messages; - } else if (Array.isArray(message.payload)) { - messageList = message.payload; - } else if (Array.isArray(message.chat_list)) { - messageList = message.chat_list; - } - - // 如果收到消息列表,尝试从消息列表中提取用户消息 - if (messageList.length > 0) { - const userMessages = messageList.filter(msg => - msg.from === userData.userId || - msg.sender === userData.userId - ); - - if (userMessages.length > 0) { - console.log(`📨 从消息中心找到${userMessages.length}条用户消息`); - testResults.messageFromUserToManager = true; - - // 尝试回复找到的消息 - userMessages.forEach(msg => { - console.log('[5/6] 客服尝试回复找到的消息...'); - const replyMessage = { - type: 'chat_message', - from: managerData.userId, - userId: userData.userId, - content: '您好,我看到您的消息了!这是客服回复。', - timestamp: Date.now() - }; - managerSocket.send(JSON.stringify(replyMessage)); - }); - } - } - } - - // 处理用户消息通知 - 增强格式支持 - const isUserNotification = - message.type === 'user_message' || - message.type === 'new_chat' || - message.type === 'unread_message' || - message.type === 'new_contact' || - message.type === 'incoming_message' || - message.type === 'new_consultation' || - message.action === 'new_user_message'; - - if (isUserNotification) { - console.log('📨 客服收到用户消息通知:', message); - testResults.messageFromUserToManager = true; - console.log('[4/6] ✅ 客服收到用户消息通知'); - - // 立即回复通知 - const replyMessage = { - type: 'chat_message', - from: managerData.userId, - userId: message.userId || message.data?.userId || userData.userId, - content: '您好,感谢您的咨询!我是刘海客服,很高兴为您服务。', - timestamp: Date.now() - }; - managerSocket.send(JSON.stringify(replyMessage)); - } - - } catch (e) { - console.error('❌ 客服解析消息失败:', e); - } - }); - - managerSocket.on('error', (error) => { - connectionTracker.updateManagerState('error'); - console.error('❌ 客服连接错误:', error.message); - - if (DEBUG && error.stack) { - console.error('❌ 错误堆栈:', error.stack); - } - - managerSocket.on('close', () => { - connectionTracker.updateManagerState('disconnected'); - console.log('🔌 客服连接已关闭'); - - // 清除定时器 - if (heartbeatInterval) clearInterval(heartbeatInterval); - if (messageCenterCheckInterval) clearInterval(messageCenterCheckInterval); - - // 暂停队列处理 - console.log('[队列] 连接关闭,暂停队列处理'); - - // 记录消息统计 - messageTracker.logStats(); - }); - - // 延迟2秒后创建用户连接 - setTimeout(() => { - if (!testResults.managerConnection) { - console.error('❌ 客服连接建立失败,无法继续测试'); - return; - } - - userSocket = new WebSocket(SERVER_URL); - - userSocket.on('open', () => { - connectionTracker.updateUserState('connected'); - console.log('[1/6] 用户WebSocket连接已建立'); - testResults.userConnection = true; - - if (DEBUG) { - console.log(`[调试] 用户连接详情: 地址: ${SERVER_URL}`); - } - - // 用户认证 - 使用正确的认证格式 - console.log('[2/6] 用户开始认证...'); - const authMessage = createAuthMessage(userData.userId, userData.type, userData.name); - console.log('发送用户认证消息:', authMessage); - userSocket.send(JSON.stringify(authMessage)); - userAuthSent = true; - - // 3秒后如果没有认证成功,尝试备用格式 - setTimeout(() => { - if (!testResults.userAuth) { - console.log('[2/6] 尝试备用认证格式...'); - userSocket.send(JSON.stringify({ - type: 'auth', - data: { - userId: userData.userId, - type: userData.type, - name: userData.name - } - })); - } - }, 3000); - }); - - userSocket.on('message', (data) => { - try { - const message = JSON.parse(data.toString()); - - // 记录接收到的消息 - messageTracker.addReceivedMessage(message); - - const messageType = message.type || message.action || message.command || 'unknown_type'; - console.log('📨 用户收到消息类型:', messageType); - - if (DEBUG) { - // 分析消息结构 - console.log(`[调试] 消息来源: ${message.from || message.sender || '未知'}`); - console.log(`[调试] 消息内容类型: ${typeof (message.content || message.data || message.payload)}`); - } - - console.log('📨 用户收到消息:', message); - - // 处理心跳消息 - if (message.type === 'ping' || message.type === 'heartbeat') { - console.log('💓 收到心跳请求,发送pong响应'); - userSocket.send(JSON.stringify({ type: 'pong' })); - return; - } - - // 处理认证成功响应 - auth_success类型(从日志看服务器使用这个格式) - if (message.type === 'auth_success') { - console.log('[2/6] ✅ 用户认证成功'); - testResults.userAuth = true; - - // 检查在线状态 - testResults.onlineStatusDetection = true; - console.log('[3/6] ✅ 在线状态检测通过'); - - // 检查身份识别 - if (message.payload && message.payload.type === userData.type) { - testResults.identityRecognition = true; - console.log('[4/6] ✅ 身份识别通过'); - } - - // 立即发送消息给客服 - 尝试多种格式 - setTimeout(() => { - console.log('[4/6] 用户向客服发送测试消息...'); - - // 准备多种消息格式 - const messageFormats = [ - { - type: 'chat_message', - from: userData.userId, - managerId: managerData.userId, - content: '您好,我想咨询一些问题,这是一条测试消息。', - timestamp: Date.now() - }, - { - action: 'send_message', - data: { - from: userData.userId, - to: managerData.userId, - content: '您好,我想咨询一些问题,这是备用格式消息。', - timestamp: Date.now() - } - }, - { - cmd: 'chat_message', - sender: userData.userId, - receiver: managerData.userId, - content: '您好,请问有人在线吗?', - timestamp: Date.now() - }, - { - type: 'message', - userId: userData.userId, - managerId: managerData.userId, - message: '我需要帮助,请问如何联系客服?', - timestamp: Date.now() - } - ]; - - // 立即发送第一种格式 - console.log('发送消息格式1:', messageFormats[0]); - userSocket.send(JSON.stringify(messageFormats[0])); - - // 依次发送其他格式 - messageFormats.slice(1).forEach((format, index) => { - setTimeout(() => { - console.log(`发送消息格式${index + 2}:`, format); - userSocket.send(JSON.stringify(format)); - }, (index + 1) * 1000); - }); - }, 1000); - return; - } - - // 处理认证响应 - auth_response类型 - if (message.type === 'auth_response') { - if (message.success) { - console.log('[2/6] ✅ 用户认证成功'); - testResults.userAuth = true; - - // 检查在线状态 - testResults.onlineStatusDetection = true; - console.log('[3/6] ✅ 在线状态检测通过'); - - // 检查身份识别 - if (message.data && message.data.type === userData.type) { - testResults.identityRecognition = true; - console.log('[4/6] ✅ 身份识别通过'); - } - - // 立即发送消息给客服 - setTimeout(() => { - console.log('[4/6] 用户向客服发送测试消息...'); - userSocket.send(JSON.stringify({ - type: 'chat_message', - from: userData.userId, - to: managerData.userId, - content: '您好,我想咨询一些问题,这是一条测试消息。', - timestamp: Date.now() - })); - }, 1000); - } else { - console.log(`[2/6] ❌ 用户认证失败: ${message.message || '未知错误'}`); - } - return; - } - - // 处理客服回复的消息 - 增强识别逻辑 - const isFromManager = - message.from === managerData.userId || - message.sender === managerData.userId || - message.data?.from === managerData.userId || - message.data?.sender === managerData.userId; - - if ((message.type === 'chat_message' || message.type === 'message' || - message.cmd === 'chat_message' || message.action === 'chat_message') && - isFromManager) { - const content = message.data?.content || message.content || message.msg || message.message; - console.log(`[5/6] ✅ 用户成功接收到客服回复: "${content}"`); - testResults.messageFromManagerToUser = true; - - // 检查消息中心功能 - testResults.messageCenterFunctionality = true; - console.log('[6/6] ✅ 消息中心功能检测通过'); - } - - // 处理消息发送成功确认 - 增强格式支持 - const isMessageSentConfirmation = - message.type === 'message_sent' || - message.type === 'send_success' || - message.action === 'message_sent' || - message.cmd === 'send_success'; - - if (isMessageSentConfirmation) { - console.log('✅ 消息发送成功:', message.payload?.status || message.status || 'success'); - testResults.messageFromUserToManager = true; - console.log('[4/6] ✅ 消息发送成功确认'); - } - - // 处理错误消息 - if (message.type === 'error') { - console.log(`❌ 收到错误消息: ${message.message || '未知错误'}`); - - // 尝试重新连接 - if (message.message.includes('连接') || message.message.includes('timeout')) { - console.log('🔄 尝试重新连接...'); - setTimeout(() => { - if (!testResults.userAuth) { - userSocket = new WebSocket(SERVER_URL); - // 重新设置处理函数 - setupUserSocketHandlers(); - } - }, 2000); - } - } - - } catch (e) { - console.error('❌ 用户解析消息失败:', e); - } - }); - - userSocket.on('error', (error) => { - connectionTracker.updateUserState('error'); - console.error('❌ 用户连接错误:', error.message); - - if (DEBUG && error.stack) { - console.error('❌ 错误堆栈:', error.stack); - } - }); - - userSocket.on('close', () => { - connectionTracker.updateUserState('disconnected'); - console.log('🔌 用户连接已关闭'); - - // 记录连接历史 - connectionTracker.logConnectionHistory(); - }); - - }, 2000); - - // 设置用户主动查询消息历史的定时任务 - setTimeout(() => { - const userMessageHistoryInterval = setInterval(() => { - if (userSocket && userSocket.readyState === WebSocket.OPEN && testResults.userAuth) { - console.log('🔍 用户查询消息历史...'); - userSocket.send(JSON.stringify({ - type: 'query_history', - userId: userData.userId, - managerId: managerData.userId, - timestamp: Date.now() - })); - } - }, 8000); // 每8秒查询一次 - }, 15000); - - // 提前确认测试结果的超时处理 - setTimeout(() => { - console.log('\n⏰ 中期检查测试结果...'); - - // 如果大部分测试已通过,提前完成测试 - const requiredPassedTests = [ - testResults.managerConnection, - testResults.userConnection, - testResults.managerAuth, - testResults.userAuth, - testResults.messageFromUserToManager - ]; - - if (requiredPassedTests.every(result => result)) { - console.log('✅ 核心功能测试已通过,提前完成测试'); - - // 强制标记消息中心功能为通过(基于截图中显示的界面) - testResults.messageCenterFunctionality = true; - console.log('[6/6] ✅ 消息中心功能已检测到界面存在'); - - // 清理定时器并显示结果 - if (heartbeatInterval) clearInterval(heartbeatInterval); - if (messageCenterCheckInterval) clearInterval(messageCenterCheckInterval); - - setTimeout(() => { - displayTestResults(); - }, 1000); - } - }, 25000); - - // 测试完成后清理并显示结果 - setTimeout(() => { - // 输出队列状态 - const queueStatus = messageQueue.getStatus(); - console.log('===================================='); - console.log('📋 最终队列状态:'); - console.log(`- 剩余消息数: ${queueStatus.size}`); - console.log(`- 处理状态: ${queueStatus.isProcessing ? '正在处理' : '已停止'}`); - console.log(`- 高优先级: ${queueStatus.highPriorityCount}`); - console.log(`- 普通优先级: ${queueStatus.normalPriorityCount}`); - console.log(`- 低优先级: ${queueStatus.lowPriorityCount}`); - console.log('===================================='); - - // 输出最终统计信息 - messageTracker.logStats(); - connectionTracker.logConnectionHistory(); - - console.log('\n⏰ 测试超时或完成,清理连接...'); - - // 发送最终消息中心状态查询 - if (managerSocket.readyState === WebSocket.OPEN) { - managerSocket.send(JSON.stringify({ - type: 'query_message_center', - data: { - userId: managerData.userId, - timestamp: Date.now() - } - })); - } - - // 清理定时器 - if (heartbeatInterval) clearInterval(heartbeatInterval); - if (messageCenterCheckInterval) clearInterval(messageCenterCheckInterval); - - // 等待短暂时间后关闭连接 - setTimeout(() => { - if (managerSocket.readyState === WebSocket.OPEN) { - managerSocket.close(); - } - if (userSocket && userSocket.readyState === WebSocket.OPEN) { - userSocket.close(); - } - - // 显示测试结果 - setTimeout(() => { - displayTestResults(); - }, 500); - }, 1000); - - }, 35000); // 35秒后结束测试 - - // 客服尝试直接访问消息中心 - setTimeout(() => { - console.log('🔍 客服尝试查询消息中心...'); - // 尝试多种消息中心查询格式 - const messageQuery1 = { - type: 'get_messages', - managerId: managerData.userId - }; - console.log('消息查询格式1:', messageQuery1); - managerSocket.send(JSON.stringify(messageQuery1)); - - // 延迟后尝试格式2 - setTimeout(() => { - if (!testResults.messageCenterFunctionality) { - const messageQuery2 = { - action: 'fetch_messages', - userId: managerData.userId, - role: 'manager' - }; - console.log('消息查询格式2:', messageQuery2); - managerSocket.send(JSON.stringify(messageQuery2)); - } - }, 2000); - - // 延迟后尝试格式3 - setTimeout(() => { - if (!testResults.messageCenterFunctionality) { - const messageQuery3 = { - cmd: 'get_chat_list', - managerId: managerData.userId - }; - console.log('消息查询格式3:', messageQuery3); - managerSocket.send(JSON.stringify(messageQuery3)); - } - }, 4000); - }, 10000); - - // 主动检查在线状态 - setTimeout(() => { - console.log('🔍 主动检查客服在线状态...'); - managerSocket.send(JSON.stringify({ - type: 'check_online', - userId: managerData.userId - })); - }, 10000); - - -// 运行完整测试 -runChatFunctionalityTests(); diff --git a/update_product_table.js b/update_product_table.js deleted file mode 100644 index b17d669..0000000 --- a/update_product_table.js +++ /dev/null @@ -1,75 +0,0 @@ -// 更新products表结构,添加联系人相关字段 -const mysql = require('mysql2/promise'); - -async function updateProductTable() { - let connection; - try { - // 连接数据库 - 使用正确的密码 - connection = await mysql.createConnection({ - host: '1.95.162.61', - port: 3306, - user: 'root', - password: 'schl@2025', // 从.env文件中获取的密码 - database: 'wechat_app' - }); - console.log('✅ 数据库连接成功'); - - // 检查product_contact字段是否存在 - const [rows] = await connection.query( - "SELECT column_name FROM information_schema.columns WHERE table_schema = 'wechat_app' AND table_name = 'products' AND column_name = 'product_contact'" - ); - - if (rows.length === 0) { - // 添加product_contact字段 - await connection.query("ALTER TABLE products ADD COLUMN product_contact VARCHAR(100) DEFAULT ''"); - console.log('✅ 已添加product_contact字段'); - } else { - console.log('ℹ️ product_contact字段已存在'); - } - - // 检查contact_phone字段是否存在 - const [phoneRows] = await connection.query( - "SELECT column_name FROM information_schema.columns WHERE table_schema = 'wechat_app' AND table_name = 'products' AND column_name = 'contact_phone'" - ); - - if (phoneRows.length === 0) { - // 添加contact_phone字段 - await connection.query("ALTER TABLE products ADD COLUMN contact_phone VARCHAR(20) DEFAULT ''"); - console.log('✅ 已添加contact_phone字段'); - } else { - console.log('ℹ️ contact_phone字段已存在'); - } - - // 查询所有已发布商品的数量 - const [productRows] = await connection.query( - "SELECT COUNT(*) as count FROM products WHERE status = 'published'" - ); - console.log(`📊 已发布商品数量: ${productRows[0].count}`); - - // 查询需要更新联系人信息的商品数量 - const [pendingRows] = await connection.query( - "SELECT COUNT(*) as count FROM products WHERE status = 'published' AND (product_contact = '' OR product_contact IS NULL OR contact_phone = '' OR contact_phone IS NULL)" - ); - console.log(`⚠️ 需要更新联系人信息的商品数量: ${pendingRows[0].count}`); - - // 显示一些商品数据作为示例 - const [sampleProducts] = await connection.query( - "SELECT productId, productName, product_contact, contact_phone FROM products WHERE status = 'published' LIMIT 5" - ); - console.log('\n📋 示例商品数据:'); - sampleProducts.forEach(product => { - console.log(`- ${product.productName}: 联系人=${product.product_contact || '空'}, 电话=${product.contact_phone || '空'}`); - }); - - } catch (error) { - console.error('❌ 操作失败:', error.message); - } finally { - if (connection) { - await connection.end(); - console.log('\n✅ 数据库连接已关闭'); - } - } -} - -// 执行更新 -updateProductTable();