7 changed files with 0 additions and 2412 deletions
@ -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). |
|
||||
File diff suppressed because one or more lines are too long
@ -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(); |
|
||||
} |
|
||||
@ -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 |
|
||||
}; |
|
||||
@ -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(); |
|
||||
File diff suppressed because it is too large
@ -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(); |
|
||||
Loading…
Reference in new issue