|
|
@ -16,8 +16,16 @@ Page({ |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
chatId: options.id |
|
|
chatId: options.id |
|
|
}); |
|
|
}); |
|
|
// 获取聊天标题
|
|
|
|
|
|
this.loadChatTitle(); |
|
|
// 如果有传递name参数,直接使用该名称作为聊天标题
|
|
|
|
|
|
if (options.name) { |
|
|
|
|
|
this.setData({ |
|
|
|
|
|
chatTitle: decodeURIComponent(options.name) |
|
|
|
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
// 否则从API获取聊天标题
|
|
|
|
|
|
this.loadChatTitle(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
this.loadMessages(); |
|
|
this.loadMessages(); |
|
|
}, |
|
|
}, |
|
|
@ -87,37 +95,119 @@ Page({ |
|
|
wx.navigateBack(); |
|
|
wx.navigateBack(); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 格式化时间显示
|
|
|
|
|
|
formatDateTime: function (dateString) { |
|
|
|
|
|
if (!dateString) return '刚刚'; |
|
|
|
|
|
|
|
|
|
|
|
const now = new Date(); |
|
|
|
|
|
const msgDate = new Date(dateString); |
|
|
|
|
|
const diffMs = now - msgDate; |
|
|
|
|
|
const diffMins = Math.floor(diffMs / 60000); |
|
|
|
|
|
const diffHours = Math.floor(diffMs / 3600000); |
|
|
|
|
|
const diffDays = Math.floor(diffMs / 86400000); |
|
|
|
|
|
|
|
|
|
|
|
if (diffMins < 1) { |
|
|
|
|
|
return '刚刚'; |
|
|
|
|
|
} else if (diffMins < 60) { |
|
|
|
|
|
return `${diffMins}分钟前`; |
|
|
|
|
|
} else if (diffHours < 24) { |
|
|
|
|
|
return `${diffHours}小时前`; |
|
|
|
|
|
} else if (diffDays < 7) { |
|
|
|
|
|
return `${diffDays}天前`; |
|
|
|
|
|
} else { |
|
|
|
|
|
// 超过一周显示具体日期
|
|
|
|
|
|
const year = msgDate.getFullYear(); |
|
|
|
|
|
const month = (msgDate.getMonth() + 1).toString().padStart(2, '0'); |
|
|
|
|
|
const day = msgDate.getDate().toString().padStart(2, '0'); |
|
|
|
|
|
const hours = msgDate.getHours().toString().padStart(2, '0'); |
|
|
|
|
|
const minutes = msgDate.getMinutes().toString().padStart(2, '0'); |
|
|
|
|
|
return `${year}-${month}-${day} ${hours}:${minutes}`; |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
loadMessages: function () { |
|
|
loadMessages: function () { |
|
|
this.setData({ loading: true }); |
|
|
this.setData({ loading: true }); |
|
|
|
|
|
|
|
|
// 模拟加载聊天记录
|
|
|
// 获取当前用户的手机号
|
|
|
setTimeout(() => { |
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
const messages = [ |
|
|
const userId = wx.getStorageSync('userId'); |
|
|
{ |
|
|
let userPhone = null; |
|
|
id: 1, |
|
|
|
|
|
content: '您好,有什么可以帮助您的吗?', |
|
|
// 尝试从users中获取手机号
|
|
|
sender: 'other', |
|
|
if (userId && users[userId]) { |
|
|
time: '刚刚' |
|
|
if (users[userId].phoneNumber) { |
|
|
}, |
|
|
userPhone = users[userId].phoneNumber; |
|
|
{ |
|
|
} else if (users[userId].phone) { |
|
|
id: 2, |
|
|
userPhone = users[userId].phone; |
|
|
content: '你好,我想咨询一下产品信息', |
|
|
} |
|
|
sender: 'me', |
|
|
} |
|
|
time: '刚刚' |
|
|
|
|
|
}, |
|
|
// 如果还没有获取到,尝试从全局用户信息获取
|
|
|
{ |
|
|
if (!userPhone) { |
|
|
id: 3, |
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
content: '当然可以,请问您想了解哪种产品?', |
|
|
if (userInfo) { |
|
|
sender: 'other', |
|
|
if (userInfo.phoneNumber) { |
|
|
time: '5分钟前' |
|
|
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'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 使用新添加的API获取聊天记录
|
|
|
|
|
|
API.getChatMessages(this.data.chatId, userPhone).then(res => { |
|
|
|
|
|
if (Array.isArray(res)) { |
|
|
|
|
|
// 处理每条消息,确定发送者和格式化时间
|
|
|
|
|
|
const processedMessages = res.map(message => { |
|
|
|
|
|
// 判断消息是发送还是接收
|
|
|
|
|
|
const sender = (message.sender_phone === userPhone) ? 'me' : 'other'; |
|
|
|
|
|
|
|
|
|
|
|
// 格式化时间
|
|
|
|
|
|
const time = this.formatDateTime(message.created_at); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
id: message.id, |
|
|
|
|
|
content: message.content, |
|
|
|
|
|
sender: sender, |
|
|
|
|
|
time: time, |
|
|
|
|
|
originalTime: message.created_at |
|
|
|
|
|
}; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 按时间顺序排序(升序)
|
|
|
|
|
|
processedMessages.sort((a, b) => { |
|
|
|
|
|
return new Date(a.originalTime) - new Date(b.originalTime); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
messages: processedMessages, |
|
|
|
|
|
loading: false |
|
|
|
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
this.setData({ |
|
|
|
|
|
messages: [], |
|
|
|
|
|
loading: false |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}).catch(error => { |
|
|
|
|
|
console.error('加载聊天记录失败:', error); |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
messages: messages, |
|
|
|
|
|
loading: false |
|
|
loading: false |
|
|
}); |
|
|
}); |
|
|
}, 1000); |
|
|
wx.showToast({ |
|
|
|
|
|
title: '加载聊天记录失败', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
loadMoreMessages: function () { |
|
|
loadMoreMessages: function () { |
|
|
@ -126,28 +216,92 @@ Page({ |
|
|
|
|
|
|
|
|
this.setData({ loading: true }); |
|
|
this.setData({ loading: true }); |
|
|
|
|
|
|
|
|
// 模拟加载更多历史消息
|
|
|
// 获取当前用户的手机号
|
|
|
setTimeout(() => { |
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
const moreMessages = [ |
|
|
const userId = wx.getStorageSync('userId'); |
|
|
{ |
|
|
let userPhone = null; |
|
|
id: 4, |
|
|
|
|
|
content: '你好,我想了解一下你们的鸡蛋产品', |
|
|
// 尝试从users中获取手机号
|
|
|
sender: 'me', |
|
|
if (userId && users[userId]) { |
|
|
time: '1小时前' |
|
|
if (users[userId].phoneNumber) { |
|
|
}, |
|
|
userPhone = users[userId].phoneNumber; |
|
|
{ |
|
|
} else if (users[userId].phone) { |
|
|
id: 5, |
|
|
userPhone = users[userId].phone; |
|
|
content: '您好,欢迎咨询我们的鸡蛋产品', |
|
|
} |
|
|
sender: 'other', |
|
|
} |
|
|
time: '1小时前' |
|
|
|
|
|
|
|
|
// 如果还没有获取到,尝试从全局用户信息获取
|
|
|
|
|
|
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'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取最早的消息时间,用于分页加载
|
|
|
|
|
|
const earliestTime = this.data.messages.length > 0 ? this.data.messages[0].originalTime : null; |
|
|
|
|
|
|
|
|
|
|
|
// 使用API获取更多聊天记录(带分页参数)
|
|
|
|
|
|
API.getChatMessages(this.data.chatId, userPhone, { before: earliestTime }).then(res => { |
|
|
|
|
|
if (Array.isArray(res) && res.length > 0) { |
|
|
|
|
|
// 处理每条消息,确定发送者和格式化时间
|
|
|
|
|
|
const processedMessages = res.map(message => { |
|
|
|
|
|
// 判断消息是发送还是接收
|
|
|
|
|
|
const sender = (message.sender_phone === userPhone) ? 'me' : 'other'; |
|
|
|
|
|
|
|
|
|
|
|
// 格式化时间
|
|
|
|
|
|
const time = this.formatDateTime(message.created_at); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
id: message.id, |
|
|
|
|
|
content: message.content, |
|
|
|
|
|
sender: sender, |
|
|
|
|
|
time: time, |
|
|
|
|
|
originalTime: message.created_at |
|
|
|
|
|
}; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 按时间顺序排序(升序)
|
|
|
|
|
|
processedMessages.sort((a, b) => { |
|
|
|
|
|
return new Date(a.originalTime) - new Date(b.originalTime); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
messages: [...processedMessages, ...this.data.messages], |
|
|
|
|
|
loading: false |
|
|
|
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
this.setData({ |
|
|
|
|
|
loading: false |
|
|
|
|
|
}); |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '没有更多消息了', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}).catch(error => { |
|
|
|
|
|
console.error('加载更多聊天记录失败:', error); |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
messages: [...moreMessages, ...this.data.messages], |
|
|
|
|
|
loading: false |
|
|
loading: false |
|
|
}); |
|
|
}); |
|
|
}, 1000); |
|
|
wx.showToast({ |
|
|
|
|
|
title: '加载更多消息失败', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
onInputChange: function (e) { |
|
|
onInputChange: function (e) { |
|
|
|