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/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();