Browse Source

修复聊天功能:1. 统一聊天入口跳转逻辑;2. 防止重复添加聊天记录

pull/1/head
徐飞洋 3 months ago
parent
commit
64b83399fc
  1. 4
      pages/customer-service/detail/index.js
  2. 52
      server-example/server-mysql.js

4
pages/customer-service/detail/index.js

@ -177,9 +177,9 @@ Page({
console.log('添加聊天记录成功:', res);
// 隐藏加载提示
wx.hideLoading();
// 导航到聊天页面
// 导航到聊天详情页面
wx.navigateTo({
url: `/pages/chat/index?id=${customerData.id}&name=${customerData.alias}&phone=${customerData.phoneNumber}`
url: `/pages/chat-detail/index?id=${customerData.phoneNumber}&name=${customerData.alias}`
});
}).catch(err => {
console.error('添加聊天记录失败:', err);

52
server-example/server-mysql.js

@ -8082,7 +8082,8 @@ app.post('/api/chat/add', async (req, res) => {
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_user_phone (user_phone),
INDEX idx_manager_phone (manager_phone)
INDEX idx_manager_phone (manager_phone),
UNIQUE KEY idx_user_manager (user_phone, manager_phone)
) ENGINE=InnoDB;`
);
console.log('✅ chat_list表检查/创建成功');
@ -8090,20 +8091,59 @@ app.post('/api/chat/add', async (req, res) => {
console.warn('创建chat_list表时出错(可能已存在):', createError.message);
}
// 如果表已存在但没有唯一索引,尝试添加
try {
await sequelize.query(
'ALTER TABLE chat_list ADD CONSTRAINT IF NOT EXISTS idx_user_manager UNIQUE (user_phone, manager_phone)'
);
console.log('✅ 尝试添加唯一索引成功');
} catch (indexError) {
console.warn('添加唯一索引失败(可能已存在):', indexError.message);
}
// 检查是否已经存在相同的聊天记录
const existingRecords = await sequelize.query(
'SELECT COUNT(*) as count FROM chat_list WHERE (user_phone = ? AND manager_phone = ?) OR (user_phone = ? AND manager_phone = ?)',
{ replacements: [user_phone, manager_phone, manager_phone, user_phone], type: sequelize.QueryTypes.SELECT }
);
const recordCount = existingRecords[0].count;
if (recordCount > 0) {
console.log('✅ 聊天记录已存在,无需重复添加');
return res.status(200).json({
success: true,
message: '聊天记录已存在'
});
}
// 插入两条记录:
// 1. 用户手机号 -> 业务员手机号
await sequelize.query(
'INSERT INTO chat_list (user_phone, manager_phone) VALUES (?, ?)',
'INSERT IGNORE INTO chat_list (user_phone, manager_phone) VALUES (?, ?)',
{ replacements: [user_phone, manager_phone] }
);
res.status(200).json({
success: true,
// 2. 业务员手机号 -> 用户手机号
await sequelize.query(
'INSERT INTO chat_list (user_phone, manager_phone) VALUES (?, ?)',
'INSERT IGNORE INTO chat_list (user_phone, manager_phone) VALUES (?, ?)',
{ replacements: [manager_phone, user_phone] }
);
});
console.log('✅ 成功插入两条聊天记录');
res.status(200).json({
success: true,
message: '聊天记录添加成功'
});
} catch (error) {
console.error('添加聊天记录失败:', error);
res.status(500).json({
success: false,
code: 500,
message: '添加聊天记录失败: ' + error.message
});
}
});
// 新增:获取业务员信息接口 - 根据手机号查询

Loading…
Cancel
Save