You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
5.7 KiB
168 lines
5.7 KiB
|
3 months ago
|
// 客服在线状态验证工具
|
||
|
|
const WebSocket = require('ws');
|
||
|
|
const http = require('http');
|
||
|
|
|
||
|
|
// 服务器地址
|
||
|
|
const SERVER_URL = 'ws://localhost:3003';
|
||
|
|
const API_URL = 'http://localhost:3003/api/managers';
|
||
|
|
|
||
|
|
// 用户提供的客服信息
|
||
|
|
const customerServicePhone = '17780155537'; // 从用户截图获取的手机号
|
||
|
|
const customerServiceId = '22'; // 刘杨的ID
|
||
|
|
|
||
|
|
console.log('====================================');
|
||
|
|
console.log(' 客服在线状态验证工具');
|
||
|
|
console.log('====================================');
|
||
|
|
console.log(`测试客服: 手机号 ${customerServicePhone}, ID ${customerServiceId}`);
|
||
|
|
console.log('这将模拟客服登录后的WebSocket连接和认证过程');
|
||
|
|
|
||
|
|
// 检查API中的客服状态
|
||
|
|
function checkManagerOnlineStatus() {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
http.get(API_URL, (res) => {
|
||
|
|
let data = '';
|
||
|
|
|
||
|
|
res.on('data', (chunk) => {
|
||
|
|
data += chunk;
|
||
|
|
});
|
||
|
|
|
||
|
|
res.on('end', () => {
|
||
|
|
try {
|
||
|
|
const result = JSON.parse(data);
|
||
|
|
const targetManager = result.data.find(m => m.managerId === customerServiceId || m.phoneNumber === customerServicePhone);
|
||
|
|
|
||
|
|
if (targetManager) {
|
||
|
|
console.log(`\n📊 客服信息:`);
|
||
|
|
console.log(` 姓名: ${targetManager.name}`);
|
||
|
|
console.log(` 在线状态: ${targetManager.online ? '✅ 在线' : '❌ 离线'}`);
|
||
|
|
console.log(` 部门: ${targetManager.organization}`);
|
||
|
|
console.log(` 职位: ${targetManager.projectName}`);
|
||
|
|
} else {
|
||
|
|
console.log('❌ 未找到目标客服信息');
|
||
|
|
}
|
||
|
|
|
||
|
|
resolve(targetManager ? targetManager.online : false);
|
||
|
|
} catch (e) {
|
||
|
|
console.error('❌ 解析API响应失败:', e);
|
||
|
|
reject(e);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}).on('error', (error) => {
|
||
|
|
console.error('❌ API请求失败:', error);
|
||
|
|
reject(error);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 模拟客服WebSocket连接和认证
|
||
|
|
function simulateCustomerServiceLogin() {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
console.log('\n🔄 模拟客服登录后WebSocket连接...');
|
||
|
|
|
||
|
|
const ws = new WebSocket(SERVER_URL);
|
||
|
|
|
||
|
|
ws.on('open', () => {
|
||
|
|
console.log('✅ WebSocket连接已建立');
|
||
|
|
|
||
|
|
// 使用与前端一致的认证消息格式
|
||
|
|
// 注意:JavaScript对象不能有重复键名,需要使用不同的字段来表示认证类型和用户类型
|
||
|
|
const authMessage = {
|
||
|
|
type: 'auth', // 认证类型必须是'auth'
|
||
|
|
managerId: customerServiceId,
|
||
|
|
userType: 'manager' // 用户类型使用不同的字段名
|
||
|
|
};
|
||
|
|
|
||
|
|
console.log('📱 发送客服认证消息:', JSON.stringify(authMessage));
|
||
|
|
ws.send(JSON.stringify(authMessage));
|
||
|
|
});
|
||
|
|
|
||
|
|
ws.on('message', (data) => {
|
||
|
|
try {
|
||
|
|
const message = JSON.parse(data.toString());
|
||
|
|
console.log('📨 收到服务器响应:', message);
|
||
|
|
|
||
|
|
if (message.type === 'auth_success' && message.payload && message.payload.type === 'manager') {
|
||
|
|
console.log('✅ 客服认证成功!');
|
||
|
|
resolve(ws);
|
||
|
|
} else {
|
||
|
|
console.error('❌ 认证失败或不是客服类型');
|
||
|
|
ws.close();
|
||
|
|
reject(new Error('认证失败'));
|
||
|
|
}
|
||
|
|
} catch (e) {
|
||
|
|
console.error('❌ 解析消息失败:', e);
|
||
|
|
ws.close();
|
||
|
|
reject(e);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
ws.on('error', (error) => {
|
||
|
|
console.error('❌ WebSocket错误:', error);
|
||
|
|
reject(error);
|
||
|
|
});
|
||
|
|
|
||
|
|
// 超时处理
|
||
|
|
setTimeout(() => {
|
||
|
|
console.error('❌ 认证超时');
|
||
|
|
ws.close();
|
||
|
|
reject(new Error('认证超时'));
|
||
|
|
}, 5000);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 主验证函数
|
||
|
|
async function runVerification() {
|
||
|
|
try {
|
||
|
|
// 1. 检查初始状态
|
||
|
|
console.log('\n=== 步骤1: 检查初始在线状态 ===');
|
||
|
|
const initialStatus = await checkManagerOnlineStatus();
|
||
|
|
|
||
|
|
// 2. 模拟客服登录和认证
|
||
|
|
console.log('\n=== 步骤2: 模拟客服登录和WebSocket认证 ===');
|
||
|
|
const ws = await simulateCustomerServiceLogin();
|
||
|
|
|
||
|
|
// 3. 等待3秒后再次检查在线状态
|
||
|
|
console.log('\n=== 步骤3: 等待并检查连接后的在线状态 ===');
|
||
|
|
console.log('等待3秒后检查状态...');
|
||
|
|
|
||
|
|
setTimeout(async () => {
|
||
|
|
try {
|
||
|
|
const updatedStatus = await checkManagerOnlineStatus();
|
||
|
|
|
||
|
|
console.log('\n====================================');
|
||
|
|
console.log('验证结果总结:');
|
||
|
|
console.log('====================================');
|
||
|
|
|
||
|
|
if (updatedStatus) {
|
||
|
|
console.log('🎉 成功!客服在线状态现在显示为在线');
|
||
|
|
console.log('✅ 这意味着WebSocket认证和状态同步正常工作');
|
||
|
|
console.log('✅ 前端修改后的认证消息格式与后端兼容');
|
||
|
|
} else {
|
||
|
|
console.log('❌ 失败!客服仍然显示离线');
|
||
|
|
console.log('请检查以下可能的问题:');
|
||
|
|
console.log('1. 确认managerId是否正确');
|
||
|
|
console.log('2. 检查认证消息格式是否正确');
|
||
|
|
console.log('3. 查看服务器日志是否有错误信息');
|
||
|
|
}
|
||
|
|
|
||
|
|
// 保持连接30秒,让用户有时间在前端查看状态
|
||
|
|
console.log('\n🔄 保持WebSocket连接30秒,请在前端查看客服在线状态...');
|
||
|
|
|
||
|
|
setTimeout(() => {
|
||
|
|
console.log('\n✅ 验证完成!');
|
||
|
|
ws.close();
|
||
|
|
}, 30000);
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('\n❌ 检查在线状态失败:', error);
|
||
|
|
ws.close();
|
||
|
|
}
|
||
|
|
}, 3000);
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('\n❌ 验证过程中出现错误:', error);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 运行验证
|
||
|
|
runVerification();
|