|
|
@ -8,13 +8,15 @@ Page({ |
|
|
inputValue: '', |
|
|
inputValue: '', |
|
|
loading: false, |
|
|
loading: false, |
|
|
chatTitle: '聊天对象', |
|
|
chatTitle: '聊天对象', |
|
|
managerPhone: null |
|
|
managerPhone: null, |
|
|
|
|
|
timer: null |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
onLoad: function (options) { |
|
|
onLoad: function (options) { |
|
|
if (options.id) { |
|
|
if (options.id) { |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
chatId: options.id |
|
|
chatId: options.id, |
|
|
|
|
|
managerPhone: options.id // 直接将chatId作为managerPhone使用,因为聊天列表页传递的是manager_phone
|
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// 如果有传递name参数,直接使用该名称作为聊天标题
|
|
|
// 如果有传递name参数,直接使用该名称作为聊天标题
|
|
|
@ -23,60 +25,30 @@ Page({ |
|
|
chatTitle: decodeURIComponent(options.name) |
|
|
chatTitle: decodeURIComponent(options.name) |
|
|
}); |
|
|
}); |
|
|
} else { |
|
|
} else { |
|
|
// 否则从API获取聊天标题
|
|
|
// 如果没有传递name参数,加载聊天标题
|
|
|
this.loadChatTitle(); |
|
|
this.loadChatTitle(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
this.loadMessages(); |
|
|
this.loadMessages(); |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 加载聊天标题
|
|
|
|
|
|
loadChatTitle: function () { |
|
|
|
|
|
// 从本地存储获取聊天列表
|
|
|
|
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
|
|
let userPhone = null; |
|
|
|
|
|
|
|
|
|
|
|
// 尝试从users中获取手机号
|
|
|
|
|
|
if (userId && users[userId]) { |
|
|
|
|
|
if (users[userId].phoneNumber) { |
|
|
|
|
|
userPhone = users[userId].phoneNumber; |
|
|
|
|
|
} else if (users[userId].phone) { |
|
|
|
|
|
userPhone = users[userId].phone; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果还没有获取到,尝试从全局用户信息获取
|
|
|
// 设置定时器,每5秒查询一次数据库同步内容
|
|
|
if (!userPhone) { |
|
|
this.startTimer(); |
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
}, |
|
|
if (userInfo) { |
|
|
|
|
|
if (userInfo.phoneNumber) { |
|
|
|
|
|
userPhone = userInfo.phoneNumber; |
|
|
|
|
|
} else if (userInfo.phone) { |
|
|
|
|
|
userPhone = userInfo.phone; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果还没有获取到,尝试从直接存储获取
|
|
|
// 页面显示时重新启动定时器
|
|
|
if (!userPhone) { |
|
|
onShow: function () { |
|
|
if (wx.getStorageSync('phoneNumber')) { |
|
|
if (!this.data.timer) { |
|
|
userPhone = wx.getStorageSync('phoneNumber'); |
|
|
this.startTimer(); |
|
|
} else if (wx.getStorageSync('phone')) { |
|
|
|
|
|
userPhone = wx.getStorageSync('phone'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
if (userPhone) { |
|
|
// 加载聊天标题
|
|
|
// 获取聊天列表
|
|
|
loadChatTitle: function () { |
|
|
API.getChatList(userPhone).then(chatList => { |
|
|
// 直接使用已有的managerPhone获取业务员信息
|
|
|
if (Array.isArray(chatList)) { |
|
|
const managerPhone = this.data.managerPhone; |
|
|
// 找到当前聊天项
|
|
|
if (managerPhone) { |
|
|
const currentChat = chatList.find(item => item.id === this.data.chatId); |
|
|
|
|
|
if (currentChat && currentChat.manager_phone) { |
|
|
|
|
|
this.setData({ managerPhone: currentChat.manager_phone }); |
|
|
|
|
|
// 获取业务员信息
|
|
|
// 获取业务员信息
|
|
|
API.getSalesPersonnelInfo(currentChat.manager_phone).then(personnelInfo => { |
|
|
API.getSalesPersonnelInfo(managerPhone).then(personnelInfo => { |
|
|
if (personnelInfo && personnelInfo.alias) { |
|
|
if (personnelInfo && personnelInfo.alias) { |
|
|
this.setData({ chatTitle: personnelInfo.alias }); |
|
|
this.setData({ chatTitle: personnelInfo.alias }); |
|
|
} |
|
|
} |
|
|
@ -84,11 +56,6 @@ Page({ |
|
|
console.error('获取业务员信息失败:', error); |
|
|
console.error('获取业务员信息失败:', error); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
}).catch(error => { |
|
|
|
|
|
console.error('获取聊天列表失败:', error); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
onBack: function () { |
|
|
onBack: function () { |
|
|
@ -126,6 +93,7 @@ Page({ |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
loadMessages: function () { |
|
|
loadMessages: function () { |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
this.setData({ loading: true }); |
|
|
this.setData({ loading: true }); |
|
|
|
|
|
|
|
|
// 获取当前用户的手机号
|
|
|
// 获取当前用户的手机号
|
|
|
@ -179,7 +147,9 @@ Page({ |
|
|
content: message.content, |
|
|
content: message.content, |
|
|
sender: sender, |
|
|
sender: sender, |
|
|
time: time, |
|
|
time: time, |
|
|
originalTime: message.created_at |
|
|
originalTime: message.created_at, |
|
|
|
|
|
sender_phone: message.sender_phone, |
|
|
|
|
|
receiver_phone: message.receiver_phone |
|
|
}; |
|
|
}; |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
@ -190,14 +160,23 @@ Page({ |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
messages: processedMessages, |
|
|
messages: processedMessages, |
|
|
loading: false |
|
|
loading: false, |
|
|
|
|
|
scrollToView: processedMessages.length > 0 ? 'latest-message' : '' |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 如果managerPhone尚未设置,尝试从聊天记录中获取
|
|
|
|
|
|
if (!this.data.managerPhone && processedMessages.length > 0) { |
|
|
|
|
|
const firstMessage = processedMessages[0]; |
|
|
|
|
|
const managerPhone = (firstMessage.sender_phone === userPhone) ? firstMessage.receiver_phone : firstMessage.sender_phone; |
|
|
|
|
|
this.setData({ managerPhone: managerPhone }); |
|
|
|
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
messages: [], |
|
|
messages: [], |
|
|
loading: false |
|
|
loading: false |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
resolve(); |
|
|
}).catch(error => { |
|
|
}).catch(error => { |
|
|
console.error('加载聊天记录失败:', error); |
|
|
console.error('加载聊天记录失败:', error); |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
@ -207,6 +186,8 @@ Page({ |
|
|
title: '加载聊天记录失败', |
|
|
title: '加载聊天记录失败', |
|
|
icon: 'none' |
|
|
icon: 'none' |
|
|
}); |
|
|
}); |
|
|
|
|
|
reject(error); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
@ -314,30 +295,208 @@ Page({ |
|
|
const content = this.data.inputValue.trim(); |
|
|
const content = this.data.inputValue.trim(); |
|
|
if (!content) return; |
|
|
if (!content) return; |
|
|
|
|
|
|
|
|
|
|
|
// 获取当前用户的手机号
|
|
|
|
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
|
|
let userPhone = null; |
|
|
|
|
|
|
|
|
|
|
|
// 尝试从users中获取手机号
|
|
|
|
|
|
if (userId && users[userId]) { |
|
|
|
|
|
if (users[userId].phoneNumber) { |
|
|
|
|
|
userPhone = users[userId].phoneNumber; |
|
|
|
|
|
} else if (users[userId].phone) { |
|
|
|
|
|
userPhone = users[userId].phone; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果还没有获取到,尝试从全局用户信息获取
|
|
|
|
|
|
if (!userPhone) { |
|
|
|
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
|
|
|
if (userInfo) { |
|
|
|
|
|
if (userInfo.phoneNumber) { |
|
|
|
|
|
userPhone = userInfo.phoneNumber; |
|
|
|
|
|
} else if (userInfo.phone) { |
|
|
|
|
|
userPhone = userInfo.phone; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果还没有获取到,尝试从直接存储获取
|
|
|
|
|
|
if (!userPhone) { |
|
|
|
|
|
if (wx.getStorageSync('phoneNumber')) { |
|
|
|
|
|
userPhone = wx.getStorageSync('phoneNumber'); |
|
|
|
|
|
} else if (wx.getStorageSync('phone')) { |
|
|
|
|
|
userPhone = wx.getStorageSync('phone'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取客服手机号
|
|
|
|
|
|
let managerPhone = this.data.managerPhone; |
|
|
|
|
|
|
|
|
|
|
|
if (!userPhone) { |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '获取用户手机号失败,请重新登录', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果managerPhone尚未设置,尝试从聊天记录中获取
|
|
|
|
|
|
if (!managerPhone && this.data.messages && this.data.messages.length > 0) { |
|
|
|
|
|
// 从第一条消息中提取与当前用户不同的手机号作为managerPhone
|
|
|
|
|
|
const firstMessage = this.data.messages[0]; |
|
|
|
|
|
if (firstMessage && firstMessage.sender_phone) { |
|
|
|
|
|
managerPhone = (firstMessage.sender_phone === userPhone) ? firstMessage.receiver_phone : firstMessage.sender_phone; |
|
|
|
|
|
this.setData({ managerPhone: managerPhone }); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 如果managerPhone仍然不存在,尝试直接从API获取
|
|
|
|
|
|
if (!managerPhone) { |
|
|
|
|
|
// 显示加载提示
|
|
|
|
|
|
wx.showLoading({ |
|
|
|
|
|
title: '获取客服信息中...', |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 先尝试从聊天列表获取
|
|
|
|
|
|
API.getChatList(userPhone).then(chatList => { |
|
|
|
|
|
if (Array.isArray(chatList)) { |
|
|
|
|
|
// 找到当前聊天项
|
|
|
|
|
|
const currentChat = chatList.find(item => item.id === this.data.chatId); |
|
|
|
|
|
if (currentChat && currentChat.manager_phone) { |
|
|
|
|
|
managerPhone = currentChat.manager_phone; |
|
|
|
|
|
this.setData({ managerPhone: managerPhone }); |
|
|
|
|
|
|
|
|
|
|
|
// 获取到managerPhone后立即发送消息
|
|
|
|
|
|
this.sendMessageAfterGetManagerPhone(userPhone, managerPhone, content); |
|
|
|
|
|
} else { |
|
|
|
|
|
// 如果聊天列表中没有找到,尝试直接从聊天记录获取
|
|
|
|
|
|
this.loadMessages().then(() => { |
|
|
|
|
|
if (this.data.messages.length > 0) { |
|
|
|
|
|
const firstMessage = this.data.messages[0]; |
|
|
|
|
|
managerPhone = (firstMessage.sender_phone === userPhone) ? firstMessage.receiver_phone : firstMessage.sender_phone; |
|
|
|
|
|
this.setData({ managerPhone: managerPhone }); |
|
|
|
|
|
this.sendMessageAfterGetManagerPhone(userPhone, managerPhone, content); |
|
|
|
|
|
} else { |
|
|
|
|
|
wx.hideLoading(); |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '获取客服信息失败,请稍候重试', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}).catch(() => { |
|
|
|
|
|
wx.hideLoading(); |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '获取客服信息失败,请稍候重试', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
wx.hideLoading(); |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '获取客服信息失败,请稍候重试', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}).catch(() => { |
|
|
|
|
|
wx.hideLoading(); |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '获取客服信息失败,请稍候重试', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 正常发送消息
|
|
|
|
|
|
this.sendMessageAfterGetManagerPhone(userPhone, managerPhone, content); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 当获取到managerPhone后发送消息
|
|
|
|
|
|
sendMessageAfterGetManagerPhone: function (userPhone, managerPhone, content) { |
|
|
|
|
|
// 隐藏加载提示
|
|
|
|
|
|
wx.hideLoading(); |
|
|
|
|
|
|
|
|
|
|
|
// 创建临时消息
|
|
|
|
|
|
const tempId = Date.now(); |
|
|
const newMessage = { |
|
|
const newMessage = { |
|
|
id: Date.now(), |
|
|
id: tempId, |
|
|
content: content, |
|
|
content: content, |
|
|
sender: 'me', |
|
|
sender: 'me', |
|
|
time: '刚刚' |
|
|
time: '刚刚' |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 先在本地显示消息
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
messages: [...this.data.messages, newMessage], |
|
|
messages: [...this.data.messages, newMessage], |
|
|
inputValue: '' |
|
|
inputValue: '', |
|
|
|
|
|
scrollToView: 'latest-message' |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// 模拟对方回复
|
|
|
// 调用API发送消息到数据库
|
|
|
setTimeout(() => { |
|
|
API.sendMessage(userPhone, managerPhone, content).then(res => { |
|
|
const reply = { |
|
|
console.log('消息发送成功:', res); |
|
|
id: Date.now() + 1, |
|
|
// 如果需要,可以更新消息的id为服务器返回的id
|
|
|
content: '这是一条自动回复', |
|
|
if (res && res.data && res.data.id) { |
|
|
sender: 'other', |
|
|
const updatedMessages = this.data.messages.map(msg => { |
|
|
time: '刚刚' |
|
|
if (msg.id === tempId) { |
|
|
|
|
|
return { |
|
|
|
|
|
...msg, |
|
|
|
|
|
id: res.data.id |
|
|
}; |
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
return msg; |
|
|
|
|
|
}); |
|
|
|
|
|
this.setData({ |
|
|
|
|
|
messages: updatedMessages |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}).catch(error => { |
|
|
|
|
|
console.error('消息发送失败:', error); |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '消息发送失败,请稍后重试', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 移除模拟对方回复的代码,改为定时器同步数据库内容
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 启动定时器
|
|
|
|
|
|
startTimer: function () { |
|
|
|
|
|
// 清除之前的定时器(如果有)
|
|
|
|
|
|
if (this.data.timer) { |
|
|
|
|
|
clearInterval(this.data.timer); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 设置新的定时器,每5秒调用一次loadMessages方法
|
|
|
|
|
|
const timer = setInterval(() => { |
|
|
|
|
|
this.loadMessages(); |
|
|
|
|
|
}, 5000); |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
messages: [...this.data.messages, reply] |
|
|
timer: timer |
|
|
|
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 页面隐藏时清除定时器
|
|
|
|
|
|
onHide: function () { |
|
|
|
|
|
if (this.data.timer) { |
|
|
|
|
|
clearInterval(this.data.timer); |
|
|
|
|
|
this.setData({ |
|
|
|
|
|
timer: null |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 页面卸载时清除定时器
|
|
|
|
|
|
onUnload: function () { |
|
|
|
|
|
if (this.data.timer) { |
|
|
|
|
|
clearInterval(this.data.timer); |
|
|
|
|
|
this.setData({ |
|
|
|
|
|
timer: null |
|
|
}); |
|
|
}); |
|
|
}, 1000); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
}, |
|
|
}); |
|
|
}); |