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.
167 lines
5.2 KiB
167 lines
5.2 KiB
|
3 months ago
|
// 测试用户类型同步修复
|
||
|
|
// 此脚本用于验证updateManagerOnlineStatus函数中的用户类型更新逻辑
|
||
|
|
|
||
|
|
const { Sequelize } = require('sequelize');
|
||
|
|
const fs = require('fs');
|
||
|
|
const path = require('path');
|
||
|
|
|
||
|
|
// 读取环境变量
|
||
|
|
const envFile = path.join(__dirname, '.env');
|
||
|
|
if (fs.existsSync(envFile)) {
|
||
|
|
const envContent = fs.readFileSync(envFile, 'utf8');
|
||
|
|
envContent.split('\n').forEach(line => {
|
||
|
|
const match = line.match(/^(\w+)=(.*)$/);
|
||
|
|
if (match) {
|
||
|
|
process.env[match[1]] = match[2];
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// 数据库配置 - 使用独立的数据源连接,与server-mysql.js保持一致
|
||
|
|
const dbConfig = {
|
||
|
|
host: process.env.DB_HOST || '1.95.162.61',
|
||
|
|
port: process.env.DB_PORT || 3306,
|
||
|
|
user: process.env.DB_USER || 'root',
|
||
|
|
password: process.env.DB_PASSWORD || ''
|
||
|
|
};
|
||
|
|
|
||
|
|
console.log('数据库连接配置:');
|
||
|
|
console.log(JSON.stringify(dbConfig, null, 2));
|
||
|
|
|
||
|
|
// 创建独立的数据源连接
|
||
|
|
const wechatAppSequelize = new Sequelize(
|
||
|
|
'wechat_app',
|
||
|
|
dbConfig.user,
|
||
|
|
dbConfig.password,
|
||
|
|
{
|
||
|
|
host: dbConfig.host,
|
||
|
|
port: dbConfig.port,
|
||
|
|
dialect: 'mysql',
|
||
|
|
pool: {
|
||
|
|
max: 10,
|
||
|
|
min: 0,
|
||
|
|
acquire: 30000,
|
||
|
|
idle: 10000
|
||
|
|
},
|
||
|
|
logging: true,
|
||
|
|
define: {
|
||
|
|
timestamps: false
|
||
|
|
},
|
||
|
|
timezone: '+00:00'
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
const userLoginSequelize = new Sequelize(
|
||
|
|
'userlogin',
|
||
|
|
dbConfig.user,
|
||
|
|
dbConfig.password,
|
||
|
|
{
|
||
|
|
host: dbConfig.host,
|
||
|
|
port: dbConfig.port,
|
||
|
|
dialect: 'mysql',
|
||
|
|
pool: {
|
||
|
|
max: 10,
|
||
|
|
min: 0,
|
||
|
|
acquire: 30000,
|
||
|
|
idle: 10000
|
||
|
|
},
|
||
|
|
logging: true,
|
||
|
|
define: {
|
||
|
|
timestamps: false
|
||
|
|
},
|
||
|
|
timezone: '+00:00'
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
// 测试电话号码
|
||
|
|
const testPhoneNumber = '17780155537';
|
||
|
|
|
||
|
|
async function testTypeSyncFix() {
|
||
|
|
console.log('\n=== 开始用户类型同步修复测试 ===\n');
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 1. 首先检查users表中当前用户类型 (使用wechatAppSequelize)
|
||
|
|
console.log('1. 检查users表中的当前用户类型...');
|
||
|
|
const userResult = await wechatAppSequelize.query(
|
||
|
|
'SELECT * FROM users WHERE phoneNumber = ?',
|
||
|
|
{ replacements: [testPhoneNumber], type: wechatAppSequelize.QueryTypes.SELECT }
|
||
|
|
);
|
||
|
|
|
||
|
|
if (userResult && userResult.length > 0) {
|
||
|
|
console.log('用户信息:', userResult[0]);
|
||
|
|
console.log(`当前用户类型: ${userResult[0].type}`);
|
||
|
|
} else {
|
||
|
|
console.log('用户不存在:', testPhoneNumber);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. 检查personnel表中的客服信息 (使用userLoginSequelize)
|
||
|
|
console.log('\n2. 检查personnel表中的客服信息...');
|
||
|
|
const personnelResult = await userLoginSequelize.query(
|
||
|
|
'SELECT * FROM personnel WHERE phoneNumber = ?',
|
||
|
|
{ replacements: [testPhoneNumber], type: userLoginSequelize.QueryTypes.SELECT }
|
||
|
|
);
|
||
|
|
|
||
|
|
if (personnelResult && personnelResult.length > 0) {
|
||
|
|
console.log('客服信息:', personnelResult[0]);
|
||
|
|
const managerId = personnelResult[0].id || personnelResult[0].userId;
|
||
|
|
console.log(`客服managerId: ${managerId}`);
|
||
|
|
|
||
|
|
// 3. 测试用户类型更新逻辑 (使用wechatAppSequelize)
|
||
|
|
console.log('\n3. 测试用户类型更新逻辑...');
|
||
|
|
const updateResult = await wechatAppSequelize.query(
|
||
|
|
'UPDATE users SET type = ? WHERE phoneNumber = ? AND type = ?',
|
||
|
|
{ replacements: ['manager', testPhoneNumber, 'customer'] }
|
||
|
|
);
|
||
|
|
|
||
|
|
const affectedRows = updateResult[1].affectedRows;
|
||
|
|
if (affectedRows > 0) {
|
||
|
|
console.log(`✓ 成功更新用户类型: 手机号=${testPhoneNumber}, 用户类型从customer更新为manager`);
|
||
|
|
} else {
|
||
|
|
console.log(`✓ 用户类型无需更新: 手机号=${testPhoneNumber}, 可能已经是manager类型`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 4. 验证更新结果 (使用wechatAppSequelize)
|
||
|
|
console.log('\n4. 验证更新结果...');
|
||
|
|
const updatedUserResult = await wechatAppSequelize.query(
|
||
|
|
'SELECT * FROM users WHERE phoneNumber = ?',
|
||
|
|
{ replacements: [testPhoneNumber], type: wechatAppSequelize.QueryTypes.SELECT }
|
||
|
|
);
|
||
|
|
|
||
|
|
if (updatedUserResult && updatedUserResult.length > 0) {
|
||
|
|
console.log('更新后的用户信息:', updatedUserResult[0]);
|
||
|
|
console.log(`更新后的用户类型: ${updatedUserResult[0].type}`);
|
||
|
|
|
||
|
|
if (updatedUserResult[0].type === 'manager') {
|
||
|
|
console.log('✓ 验证成功: 用户类型已更新为manager');
|
||
|
|
} else {
|
||
|
|
console.log('⚠ 验证警告: 用户类型仍为:', updatedUserResult[0].type);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} else {
|
||
|
|
console.log('该手机号不是客服');
|
||
|
|
}
|
||
|
|
|
||
|
|
// 总结
|
||
|
|
console.log('\n=== 测试总结 ===');
|
||
|
|
console.log('1. 数据库连接: 成功');
|
||
|
|
console.log('2. users表查询: 成功');
|
||
|
|
console.log('3. personnel表查询: 成功');
|
||
|
|
console.log('4. 用户类型更新逻辑: 已测试');
|
||
|
|
console.log('5. 跨数据源操作: 已验证');
|
||
|
|
console.log('\n提示: 服务器已重启,客服登录后将会自动更新用户类型');
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('测试过程中出现错误:', error);
|
||
|
|
console.error('错误详情:', error.stack);
|
||
|
|
} finally {
|
||
|
|
// 关闭数据库连接
|
||
|
|
await wechatAppSequelize.close();
|
||
|
|
await userLoginSequelize.close();
|
||
|
|
console.log('\n数据库连接已关闭');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 运行测试
|
||
|
|
testTypeSyncFix();
|