Browse Source

修复客服聊天功能:改进聊天会话ID传递逻辑,优先使用客服手机号作为聊天ID

pull/1/head
徐飞洋 3 months ago
parent
commit
7d0c834ba5
  1. 24
      pages/chat-detail/index.js
  2. 19
      pages/customer-service/index.js

24
pages/chat-detail/index.js

@ -19,19 +19,29 @@ Page({
}, },
onLoad: function (options) { onLoad: function (options) {
if (options.id) { console.log('聊天详情页面加载 - 传递的参数:', JSON.stringify(options, null, 2));
// 同时支持id和userId参数,优先使用userId
const chatId = options.userId || options.id;
if (chatId) {
console.log('聊天详情页面 - 设置聊天ID:', chatId);
this.setData({ this.setData({
chatId: options.id, chatId: chatId,
managerPhone: options.id managerPhone: options.phone || chatId // 使用传递的phone作为managerPhone,否则使用chatId
}); });
console.log('聊天详情页面 - 设置客服手机号:', this.data.managerPhone);
if (options.name) { if (options.name || options.userName) {
// 同时支持name和userName参数
const chatTitle = decodeURIComponent(options.name || options.userName);
console.log('聊天详情页面 - 设置聊天标题:', chatTitle);
this.setData({ this.setData({
chatTitle: decodeURIComponent(options.name) chatTitle: chatTitle
}); });
} else { } else {
this.loadChatTitle(); this.loadChatTitle();
} }
} else {
console.error('聊天详情页面 - 未传递有效的聊天ID');
} }
this.loadMessages(); // 使用默认值true,首次进入页面时自动滚动到底部 this.loadMessages(); // 使用默认值true,首次进入页面时自动滚动到底部
this.startTimer(); this.startTimer();
@ -119,9 +129,12 @@ Page({
return; return;
} }
console.log('加载聊天消息 - 聊天ID:', this.data.chatId, '用户手机号:', userPhone);
// 获取聊天消息 // 获取聊天消息
API.getChatMessages(this.data.chatId, userPhone).then(res => { API.getChatMessages(this.data.chatId, userPhone).then(res => {
console.log('加载聊天消息 - API返回:', JSON.stringify(res, null, 2));
if (Array.isArray(res)) { if (Array.isArray(res)) {
console.log('加载聊天消息 - API返回消息数量:', res.length);
// 处理消息并排序 // 处理消息并排序
const processedMessages = res.map(message => { const processedMessages = res.map(message => {
const sender = (message.sender_phone === userPhone) ? 'me' : 'other'; const sender = (message.sender_phone === userPhone) ? 'me' : 'other';
@ -185,6 +198,7 @@ Page({
} }
}); });
} else { } else {
console.log('加载聊天消息 - API返回非数组数据:', res);
this.setData({ this.setData({
messages: [], messages: [],
loading: false loading: false

19
pages/customer-service/index.js

@ -335,17 +335,26 @@ Page({
// 调用API添加聊天记录 // 调用API添加聊天记录
api.addChatRecord(userPhone, service.phoneNumber).then(res => { api.addChatRecord(userPhone, service.phoneNumber).then(res => {
console.log('添加聊天记录成功:', res); console.log('添加聊天记录成功:', JSON.stringify(res, null, 2));
// 隐藏加载提示 // 隐藏加载提示
wx.hideLoading(); wx.hideLoading();
// 确保使用managerId作为聊天对象的唯一标识符 // 打印所有可能的chatSessionId来源
const chatUserId = service?.managerId || id; console.log('聊天会话ID候选值:', {
res_data_chatSessionId: res?.data?.chatSessionId,
res_chatSessionId: res?.chatSessionId,
service_managerId: service?.managerId,
service_id: id,
service_phoneNumber: service?.phoneNumber
});
// 尝试从服务器响应中获取聊天会话ID,如果没有则使用客服手机号,再没有则使用managerId或id
const chatSessionId = res?.data?.chatSessionId || res?.chatSessionId || service?.phoneNumber || service?.managerId || id;
// 跳转到聊天页面 // 跳转到聊天页面
wx.navigateTo({ wx.navigateTo({
url: `/pages/chat-detail/index?userId=${chatUserId}&userName=${encodeURIComponent(service?.alias || '')}&phone=${service?.phoneNumber || ''}&isManager=true` url: `/pages/chat-detail/index?userId=${chatSessionId}&userName=${encodeURIComponent(service?.alias || '')}&phone=${service?.phoneNumber || ''}&isManager=true`
}); });
console.log('跳转到聊天页面:', { chatUserId, userName: service?.alias }); console.log('跳转到聊天页面:', { chatUserId: chatSessionId, userName: service?.alias, serverResponse: JSON.stringify(res, null, 2) });
}).catch(err => { }).catch(err => {
console.error('添加聊天记录失败:', err); console.error('添加聊天记录失败:', err);
// 隐藏加载提示 // 隐藏加载提示

Loading…
Cancel
Save