|
|
@ -290,6 +290,96 @@ Page({ |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 封装测试聊天列表功能的函数
|
|
|
|
|
|
async testChatListFunctionality(userPhone, managerPhone) { |
|
|
|
|
|
try { |
|
|
|
|
|
console.log('=== 开始测试聊天列表功能 ==='); |
|
|
|
|
|
console.log('测试用户手机号:', userPhone); |
|
|
|
|
|
console.log('测试客服手机号:', managerPhone); |
|
|
|
|
|
|
|
|
|
|
|
// 1. 测试添加聊天记录(双向)
|
|
|
|
|
|
console.log('\n1. 测试添加聊天记录(双向)...'); |
|
|
|
|
|
const addChatResponse = await api.addChatRecord(userPhone, managerPhone); |
|
|
|
|
|
console.log('添加聊天记录响应:', addChatResponse); |
|
|
|
|
|
|
|
|
|
|
|
// 2. 测试用户获取聊天列表
|
|
|
|
|
|
console.log('\n2. 测试用户获取聊天列表...'); |
|
|
|
|
|
const userChatListResponse = await api.getChatList(userPhone); |
|
|
|
|
|
console.log('用户聊天列表响应:', userChatListResponse); |
|
|
|
|
|
console.log('用户聊天列表数量:', Array.isArray(userChatListResponse) ? userChatListResponse.length : 0); |
|
|
|
|
|
|
|
|
|
|
|
// 3. 测试客服获取聊天列表
|
|
|
|
|
|
console.log('\n3. 测试客服获取聊天列表...'); |
|
|
|
|
|
const managerChatListResponse = await api.getChatList(managerPhone); |
|
|
|
|
|
console.log('客服聊天列表响应:', managerChatListResponse); |
|
|
|
|
|
console.log('客服聊天列表数量:', Array.isArray(managerChatListResponse) ? managerChatListResponse.length : 0); |
|
|
|
|
|
|
|
|
|
|
|
// 4. 验证双向聊天记录是否都能正确获取
|
|
|
|
|
|
console.log('\n4. 验证双向聊天记录...'); |
|
|
|
|
|
|
|
|
|
|
|
// 检查用户是否能看到与客服的聊天记录
|
|
|
|
|
|
const userHasManagerChat = Array.isArray(userChatListResponse) && userChatListResponse.some(chat => |
|
|
|
|
|
(chat.user_phone === userPhone && chat.manager_phone === managerPhone) || |
|
|
|
|
|
(chat.user_phone === managerPhone && chat.manager_phone === userPhone) |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
// 检查客服是否能看到与用户的聊天记录
|
|
|
|
|
|
const managerHasUserChat = Array.isArray(managerChatListResponse) && managerChatListResponse.some(chat => |
|
|
|
|
|
(chat.user_phone === userPhone && chat.manager_phone === managerPhone) || |
|
|
|
|
|
(chat.user_phone === managerPhone && chat.manager_phone === userPhone) |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (userHasManagerChat) { |
|
|
|
|
|
console.log('✓ 用户可以看到与客服的聊天记录'); |
|
|
|
|
|
} else { |
|
|
|
|
|
console.log('❌ 用户无法看到与客服的聊天记录'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (managerHasUserChat) { |
|
|
|
|
|
console.log('✓ 客服可以看到与用户的聊天记录'); |
|
|
|
|
|
} else { |
|
|
|
|
|
console.log('❌ 客服无法看到与用户的聊天记录'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 5. 测试重复添加聊天记录(应该不会重复创建)
|
|
|
|
|
|
console.log('\n5. 测试重复添加聊天记录...'); |
|
|
|
|
|
const duplicateAddResponse = await api.addChatRecord(userPhone, managerPhone); |
|
|
|
|
|
console.log('重复添加聊天记录响应:', duplicateAddResponse); |
|
|
|
|
|
|
|
|
|
|
|
// 6. 再次测试获取聊天列表,确保数量没有变化
|
|
|
|
|
|
console.log('\n6. 再次测试获取聊天列表,确保数量没有变化...'); |
|
|
|
|
|
const finalUserChatListResponse = await api.getChatList(userPhone); |
|
|
|
|
|
console.log('最终用户聊天列表数量:', Array.isArray(finalUserChatListResponse) ? finalUserChatListResponse.length : 0); |
|
|
|
|
|
|
|
|
|
|
|
const initialLength = Array.isArray(userChatListResponse) ? userChatListResponse.length : 0; |
|
|
|
|
|
const finalLength = Array.isArray(finalUserChatListResponse) ? finalUserChatListResponse.length : 0; |
|
|
|
|
|
|
|
|
|
|
|
if (finalLength === initialLength) { |
|
|
|
|
|
console.log('✓ 重复添加聊天记录没有导致重复数据'); |
|
|
|
|
|
} else { |
|
|
|
|
|
console.log('❌ 重复添加聊天记录导致了重复数据'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('\n=== 聊天列表功能测试完成 ==='); |
|
|
|
|
|
|
|
|
|
|
|
// 总结测试结果
|
|
|
|
|
|
if (userHasManagerChat && managerHasUserChat) { |
|
|
|
|
|
console.log('\n🎉 所有测试通过!聊天列表功能正常工作。'); |
|
|
|
|
|
return true; |
|
|
|
|
|
} else { |
|
|
|
|
|
console.log('\n❌ 测试失败!聊天列表功能存在问题。'); |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error('测试过程中出现错误:', error.message); |
|
|
|
|
|
if (error.response) { |
|
|
|
|
|
console.error('错误响应数据:', error.response.data); |
|
|
|
|
|
} |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
onChat: function (e) { |
|
|
onChat: function (e) { |
|
|
const id = e.currentTarget.dataset.id; |
|
|
const id = e.currentTarget.dataset.id; |
|
|
const service = this.data.customerServices.find(item => item.id === id); |
|
|
const service = this.data.customerServices.find(item => item.id === id); |
|
|
@ -312,12 +402,18 @@ Page({ |
|
|
userPhone = wx.getStorageSync('phoneNumber'); |
|
|
userPhone = wx.getStorageSync('phoneNumber'); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
// 如果都获取不到,使用用户提供的默认登录手机号
|
|
|
|
|
|
if (!userPhone) { |
|
|
|
|
|
userPhone = '18482694520'; |
|
|
|
|
|
} |
|
|
} catch (e) { |
|
|
} catch (e) { |
|
|
console.error('获取用户手机号失败:', e); |
|
|
console.error('获取用户手机号失败:', e); |
|
|
|
|
|
// 如果获取失败,使用用户提供的默认登录手机号
|
|
|
|
|
|
userPhone = '18482694520'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
console.log('当前用户手机号:', userPhone); |
|
|
console.log('当前用户手机号:', userPhone); |
|
|
console.log('客服手机号:', service.phoneNumber); |
|
|
console.log('客服信息:', service); |
|
|
|
|
|
|
|
|
// 验证手机号
|
|
|
// 验证手机号
|
|
|
if (!userPhone) { |
|
|
if (!userPhone) { |
|
|
@ -328,35 +424,44 @@ Page({ |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 验证客服手机号
|
|
|
|
|
|
if (!service.phoneNumber) { |
|
|
|
|
|
console.error('客服手机号不存在:', service.name); |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '客服信息不完整,请稍后重试', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('客服手机号:', service.phoneNumber); |
|
|
|
|
|
|
|
|
// 显示加载提示
|
|
|
// 显示加载提示
|
|
|
wx.showLoading({ |
|
|
wx.showLoading({ |
|
|
title: '正在建立聊天...', |
|
|
title: '正在建立聊天...', |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// 调用API添加聊天记录
|
|
|
// 调用修复函数,确保用户和客服之间的聊天记录是双向的(成双成对)
|
|
|
api.addChatRecord(userPhone, service.phoneNumber).then(res => { |
|
|
api.fixChatRecordsPair(userPhone, service.phoneNumber).then(res => { |
|
|
console.log('添加聊天记录成功:', JSON.stringify(res, null, 2)); |
|
|
console.log('聊天建立成功:', JSON.stringify(res, null, 2)); |
|
|
// 隐藏加载提示
|
|
|
// 隐藏加载提示
|
|
|
wx.hideLoading(); |
|
|
wx.hideLoading(); |
|
|
|
|
|
|
|
|
// 打印所有可能的chatSessionId来源
|
|
|
// 使用客服手机号作为聊天会话ID
|
|
|
console.log('聊天会话ID候选值:', { |
|
|
const chatSessionId = service.phoneNumber; |
|
|
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=${chatSessionId}&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: chatSessionId, userName: service?.alias, serverResponse: JSON.stringify(res, null, 2) }); |
|
|
console.log('跳转到聊天页面:', { |
|
|
|
|
|
chatUserId: chatSessionId, |
|
|
|
|
|
userName: service?.alias, |
|
|
|
|
|
customerServicePhone: service?.phoneNumber, |
|
|
|
|
|
userPhone: userPhone |
|
|
|
|
|
}); |
|
|
}).catch(err => { |
|
|
}).catch(err => { |
|
|
console.error('添加聊天记录失败:', err); |
|
|
console.error('建立聊天失败:', err); |
|
|
// 隐藏加载提示
|
|
|
// 隐藏加载提示
|
|
|
wx.hideLoading(); |
|
|
wx.hideLoading(); |
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
|