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.
38 lines
1.5 KiB
38 lines
1.5 KiB
/**
|
|
* 用户关联记录创建工具函数
|
|
* 用于在用户授权成功后自动创建contacts和usermanagements表关联记录
|
|
*/
|
|
async function createUserAssociations(user) {
|
|
try {
|
|
// 确保用户对象有效
|
|
if (!user || !user.userId) {
|
|
console.error('创建用户关联记录失败: 用户对象或userId无效');
|
|
return false;
|
|
}
|
|
|
|
console.log('为用户创建关联记录:', user.userId);
|
|
|
|
// 1. 创建或更新联系人记录
|
|
const [contactResult] = await sequelize.query(
|
|
'INSERT INTO contacts (userId, nickName, phoneNumber) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE nickName = ?, phoneNumber = ?',
|
|
{ replacements: [user.userId, user.nickName || '默认联系人', user.phoneNumber || '', user.nickName || '默认联系人', user.phoneNumber || ''] }
|
|
);
|
|
|
|
// 2. 创建或更新用户管理记录
|
|
const [managementResult] = await sequelize.query(
|
|
'INSERT INTO usermanagements (userId) VALUES (?) ON DUPLICATE KEY UPDATE userId = ?',
|
|
{ replacements: [user.userId, user.userId] }
|
|
);
|
|
|
|
console.log('用户关联记录创建成功:', user.userId);
|
|
return true;
|
|
} catch (error) {
|
|
console.error('创建用户关联记录失败:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 在server-mysql.js文件中的用户授权相关代码后添加:
|
|
// 示例:在用户创建或更新成功后调用
|
|
// const user = { userId: '...', nickName: '...', phoneNumber: '...' };
|
|
// await createUserAssociations(user);
|