|
|
@ -1,6 +1,4 @@ |
|
|
// pages/consult/consult.js
|
|
|
|
|
|
// import http from '../../../utils/api' // 您的注释代码保留
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// import http from '../../../utils/api'
|
|
|
Page({ |
|
|
Page({ |
|
|
data: { |
|
|
data: { |
|
|
// 专家信息
|
|
|
// 专家信息
|
|
|
@ -48,6 +46,7 @@ Page({ |
|
|
showDateDivider: true, |
|
|
showDateDivider: true, |
|
|
todayDate: '', |
|
|
todayDate: '', |
|
|
loading: false, |
|
|
loading: false, |
|
|
|
|
|
loadingMore: false, |
|
|
|
|
|
|
|
|
// 滚动相关
|
|
|
// 滚动相关
|
|
|
isScrolling: false, |
|
|
isScrolling: false, |
|
|
@ -64,8 +63,11 @@ Page({ |
|
|
pageSize: 20, |
|
|
pageSize: 20, |
|
|
hasMore: true, |
|
|
hasMore: true, |
|
|
|
|
|
|
|
|
// 时间显示间隔(分钟)
|
|
|
|
|
|
timeInterval: 5 |
|
|
|
|
|
|
|
|
// 时间显示间隔(分钟) - 微信默认为5分钟
|
|
|
|
|
|
timeInterval: 5, |
|
|
|
|
|
|
|
|
|
|
|
// 用于存储最后一条显示时间的消息的时间戳
|
|
|
|
|
|
lastShowTimeStamp: 0 |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
onLoad: function(options) { |
|
|
onLoad: function(options) { |
|
|
@ -258,6 +260,51 @@ Page({ |
|
|
// this.loadDefaultExpertInfo(expertId);
|
|
|
// this.loadDefaultExpertInfo(expertId);
|
|
|
// }
|
|
|
// }
|
|
|
// });
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟数据
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
|
const experts = [ |
|
|
|
|
|
{ |
|
|
|
|
|
id: 1, |
|
|
|
|
|
name: '张明专家', |
|
|
|
|
|
title: '资深畜牧兽医', |
|
|
|
|
|
expertise: '牛羊疾病防治', |
|
|
|
|
|
avatar: '/images/avatars/expert1.png', |
|
|
|
|
|
online: true, |
|
|
|
|
|
phone: '13800138000' |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
id: 2, |
|
|
|
|
|
name: '李华专家', |
|
|
|
|
|
title: '高级畜牧师', |
|
|
|
|
|
expertise: '饲料营养', |
|
|
|
|
|
avatar: '/images/avatars/expert2.png', |
|
|
|
|
|
online: false, |
|
|
|
|
|
phone: '13800138001' |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
id: 3, |
|
|
|
|
|
name: '王强专家', |
|
|
|
|
|
title: '兽医专家', |
|
|
|
|
|
expertise: '疾病防治', |
|
|
|
|
|
avatar: '/images/avatars/expert3.png', |
|
|
|
|
|
online: true, |
|
|
|
|
|
phone: '13800138002' |
|
|
|
|
|
} |
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
const expertInfo = experts.find(e => e.id == expertId) || experts[0]; |
|
|
|
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
expertInfo, |
|
|
|
|
|
loading: false |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
wx.hideLoading(); |
|
|
|
|
|
|
|
|
|
|
|
// 加载聊天记录
|
|
|
|
|
|
this.loadChatHistory(); |
|
|
|
|
|
}, 500); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 加载默认专家信息(当接口失败时使用)
|
|
|
// 加载默认专家信息(当接口失败时使用)
|
|
|
@ -331,9 +378,17 @@ Page({ |
|
|
// const messages = res.data.list || [];
|
|
|
// const messages = res.data.list || [];
|
|
|
|
|
|
|
|
|
// if (messages.length > 0) {
|
|
|
// if (messages.length > 0) {
|
|
|
// // 处理消息时间显示
|
|
|
|
|
|
|
|
|
// // 处理消息时间显示 - 使用完全修复的时间处理逻辑
|
|
|
// const processedMessages = this.processMessageTimes(messages);
|
|
|
// const processedMessages = this.processMessageTimes(messages);
|
|
|
|
|
|
|
|
|
|
|
|
// // 调试:查看处理后的消息
|
|
|
|
|
|
// console.log('处理后的消息数据:', processedMessages.map(msg => ({
|
|
|
|
|
|
// id: msg.id,
|
|
|
|
|
|
// showTime: msg.showTime,
|
|
|
|
|
|
// time: this.formatTime(msg.timestamp),
|
|
|
|
|
|
// timestamp: msg.timestamp
|
|
|
|
|
|
// })));
|
|
|
|
|
|
|
|
|
// this.setData({
|
|
|
// this.setData({
|
|
|
// messageList: processedMessages,
|
|
|
// messageList: processedMessages,
|
|
|
// loading: false,
|
|
|
// loading: false,
|
|
|
@ -359,6 +414,114 @@ Page({ |
|
|
// this.loadMockChatHistory();
|
|
|
// this.loadMockChatHistory();
|
|
|
// }
|
|
|
// }
|
|
|
// });
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟数据 - 修复时间戳问题
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
|
const now = Date.now(); |
|
|
|
|
|
let mockMessages = []; |
|
|
|
|
|
|
|
|
|
|
|
if (page === 1) { |
|
|
|
|
|
// 第一页数据 - 测试不同时间间隔的消息
|
|
|
|
|
|
mockMessages = [ |
|
|
|
|
|
{ |
|
|
|
|
|
id: 'msg-1', |
|
|
|
|
|
sender: 'expert', |
|
|
|
|
|
type: 'text', |
|
|
|
|
|
content: '您好,我是张明专家,有什么可以帮您?', |
|
|
|
|
|
timestamp: now - 10 * 60 * 1000, // 10分钟前 - 应该显示时间
|
|
|
|
|
|
status: 'success' |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
id: 'msg-2', |
|
|
|
|
|
sender: 'user', |
|
|
|
|
|
type: 'text', |
|
|
|
|
|
content: '您好,我养的牛最近食欲不振,请问是什么原因?', |
|
|
|
|
|
timestamp: now - 9 * 60 * 1000, // 9分钟前 - 不显示时间(与上条间隔1分钟)
|
|
|
|
|
|
status: 'success' |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
id: 'msg-3', |
|
|
|
|
|
sender: 'expert', |
|
|
|
|
|
type: 'text', |
|
|
|
|
|
content: '可能是饲料问题或环境变化引起的,请描述一下具体情况。', |
|
|
|
|
|
timestamp: now - 7 * 60 * 1000, // 7分钟前 - 显示时间(与上条间隔2分钟,但与第一条间隔3分钟)
|
|
|
|
|
|
status: 'success' |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
id: 'msg-4', |
|
|
|
|
|
sender: 'user', |
|
|
|
|
|
type: 'text', |
|
|
|
|
|
content: '具体症状是拉稀,体温偏高,精神状态不好。', |
|
|
|
|
|
timestamp: now - 2 * 60 * 1000, // 2分钟前 - 显示时间(与上条间隔5分钟)
|
|
|
|
|
|
status: 'success' |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
id: 'msg-5', |
|
|
|
|
|
sender: 'expert', |
|
|
|
|
|
type: 'text', |
|
|
|
|
|
content: '明白了,建议您调整饲料配方,添加一些益生菌。', |
|
|
|
|
|
timestamp: now - 1 * 60 * 1000, // 1分钟前 - 不显示时间(与上条间隔1分钟)
|
|
|
|
|
|
status: 'success' |
|
|
|
|
|
} |
|
|
|
|
|
]; |
|
|
|
|
|
} else { |
|
|
|
|
|
// 更多数据
|
|
|
|
|
|
mockMessages = [ |
|
|
|
|
|
{ |
|
|
|
|
|
id: 'msg-6', |
|
|
|
|
|
sender: 'user', |
|
|
|
|
|
type: 'text', |
|
|
|
|
|
content: '之前喂的是玉米秸秆,需要换饲料吗?', |
|
|
|
|
|
timestamp: now - 30 * 60 * 1000, // 30分钟前
|
|
|
|
|
|
status: 'success' |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
id: 'msg-7', |
|
|
|
|
|
sender: 'expert', |
|
|
|
|
|
type: 'text', |
|
|
|
|
|
content: '可以尝试添加一些豆粕和麦麸,改善营养结构。', |
|
|
|
|
|
timestamp: now - 25 * 60 * 1000, // 25分钟前
|
|
|
|
|
|
status: 'success' |
|
|
|
|
|
} |
|
|
|
|
|
]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (mockMessages.length > 0) { |
|
|
|
|
|
// 处理消息时间显示 - 使用完全修复的时间处理逻辑
|
|
|
|
|
|
const processedMessages = this.processMessageTimes(mockMessages); |
|
|
|
|
|
|
|
|
|
|
|
// 调试:查看处理后的消息
|
|
|
|
|
|
console.log('处理后的消息数据:', processedMessages.map(msg => ({ |
|
|
|
|
|
id: msg.id, |
|
|
|
|
|
showTime: msg.showTime, |
|
|
|
|
|
time: this.formatTime(msg.timestamp), |
|
|
|
|
|
timestamp: msg.timestamp, |
|
|
|
|
|
sender: msg.sender |
|
|
|
|
|
}))); |
|
|
|
|
|
|
|
|
|
|
|
let newMessageList = []; |
|
|
|
|
|
if (page === 1) { |
|
|
|
|
|
newMessageList = processedMessages; |
|
|
|
|
|
} else { |
|
|
|
|
|
newMessageList = [...processedMessages, ...this.data.messageList]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
messageList: newMessageList, |
|
|
|
|
|
loading: false, |
|
|
|
|
|
loadingMore: false, |
|
|
|
|
|
hasMore: mockMessages.length >= pageSize |
|
|
|
|
|
}, () => { |
|
|
|
|
|
if (page === 1) { |
|
|
|
|
|
// 滚动到底部
|
|
|
|
|
|
this.scrollToBottom(true); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} else { |
|
|
|
|
|
// 如果没有历史记录,添加一条欢迎消息
|
|
|
|
|
|
this.addWelcomeMessage(); |
|
|
|
|
|
} |
|
|
|
|
|
}, 800); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 加载模拟聊天记录(当接口失败时使用)
|
|
|
// 加载模拟聊天记录(当接口失败时使用)
|
|
|
@ -371,42 +534,40 @@ Page({ |
|
|
sender: 'expert', |
|
|
sender: 'expert', |
|
|
type: 'text', |
|
|
type: 'text', |
|
|
content: '您好,我是张明专家,有什么可以帮您?', |
|
|
content: '您好,我是张明专家,有什么可以帮您?', |
|
|
timestamp: now - 3600000, // 1小时前
|
|
|
|
|
|
showTime: true |
|
|
|
|
|
|
|
|
timestamp: now - 10 * 60 * 1000, // 10分钟前
|
|
|
|
|
|
status: 'success' |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
id: 'msg-2', |
|
|
id: 'msg-2', |
|
|
sender: 'user', |
|
|
sender: 'user', |
|
|
type: 'text', |
|
|
type: 'text', |
|
|
content: '您好,我养的牛最近食欲不振,请问是什么原因?', |
|
|
content: '您好,我养的牛最近食欲不振,请问是什么原因?', |
|
|
timestamp: now - 1800000, // 30分钟前
|
|
|
|
|
|
status: 'success', |
|
|
|
|
|
showTime: false |
|
|
|
|
|
|
|
|
timestamp: now - 8 * 60 * 1000, // 8分钟前
|
|
|
|
|
|
status: 'success' |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
id: 'msg-3', |
|
|
id: 'msg-3', |
|
|
sender: 'expert', |
|
|
sender: 'expert', |
|
|
type: 'text', |
|
|
type: 'text', |
|
|
content: '可能是饲料问题或环境变化引起的,请描述一下具体情况。', |
|
|
content: '可能是饲料问题或环境变化引起的,请描述一下具体情况。', |
|
|
timestamp: now - 1200000, // 20分钟前
|
|
|
|
|
|
showTime: false |
|
|
|
|
|
|
|
|
timestamp: now - 6 * 60 * 1000, // 6分钟前
|
|
|
|
|
|
status: 'success' |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
id: 'msg-4', |
|
|
id: 'msg-4', |
|
|
sender: 'user', |
|
|
sender: 'user', |
|
|
type: 'text', |
|
|
type: 'text', |
|
|
content: '具体症状是...', |
|
|
content: '具体症状是...', |
|
|
timestamp: now - 600000, // 10分钟前
|
|
|
|
|
|
status: 'success', |
|
|
|
|
|
showTime: true |
|
|
|
|
|
|
|
|
timestamp: now - 4 * 60 * 1000, // 4分钟前
|
|
|
|
|
|
status: 'success' |
|
|
}, |
|
|
}, |
|
|
{ |
|
|
{ |
|
|
id: 'msg-5', |
|
|
id: 'msg-5', |
|
|
sender: 'expert', |
|
|
sender: 'expert', |
|
|
type: 'text', |
|
|
type: 'text', |
|
|
content: '明白了,建议您调整饲料配方。', |
|
|
content: '明白了,建议您调整饲料配方。', |
|
|
timestamp: now - 300000, // 5分钟前
|
|
|
|
|
|
showTime: false |
|
|
|
|
|
|
|
|
timestamp: now - 2 * 60 * 1000, // 2分钟前
|
|
|
|
|
|
status: 'success' |
|
|
} |
|
|
} |
|
|
]; |
|
|
]; |
|
|
|
|
|
|
|
|
@ -430,11 +591,13 @@ Page({ |
|
|
type: 'text', |
|
|
type: 'text', |
|
|
content: `您好,我是${this.data.expertInfo.name},有什么可以帮您?`, |
|
|
content: `您好,我是${this.data.expertInfo.name},有什么可以帮您?`, |
|
|
timestamp: Date.now(), |
|
|
timestamp: Date.now(), |
|
|
showTime: true |
|
|
|
|
|
|
|
|
status: 'success' |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const processedMessage = this.processSingleMessageTime(welcomeMessage, []); |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
messageList: [welcomeMessage], |
|
|
|
|
|
|
|
|
messageList: [processedMessage], |
|
|
loading: false |
|
|
loading: false |
|
|
}, () => { |
|
|
}, () => { |
|
|
// 滚动到底部
|
|
|
// 滚动到底部
|
|
|
@ -442,15 +605,15 @@ Page({ |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 处理消息时间显示 - 优化微信样式
|
|
|
|
|
|
|
|
|
// 完全修复:处理消息时间显示逻辑(类似微信)
|
|
|
processMessageTimes: function(messages) { |
|
|
processMessageTimes: function(messages) { |
|
|
if (!messages || messages.length === 0) return []; |
|
|
if (!messages || messages.length === 0) return []; |
|
|
|
|
|
|
|
|
// 按时间排序(从早到晚)
|
|
|
// 按时间排序(从早到晚)
|
|
|
const sortedMessages = [...messages].sort((a, b) => a.timestamp - b.timestamp); |
|
|
const sortedMessages = [...messages].sort((a, b) => a.timestamp - b.timestamp); |
|
|
|
|
|
|
|
|
let lastShowTime = 0; |
|
|
|
|
|
const processedMessages = []; |
|
|
const processedMessages = []; |
|
|
|
|
|
let lastShowTime = null; // 最后一条显示时间消息的时间戳
|
|
|
|
|
|
|
|
|
for (let i = 0; i < sortedMessages.length; i++) { |
|
|
for (let i = 0; i < sortedMessages.length; i++) { |
|
|
const msg = { ...sortedMessages[i] }; |
|
|
const msg = { ...sortedMessages[i] }; |
|
|
@ -465,23 +628,62 @@ Page({ |
|
|
msg.showTime = true; |
|
|
msg.showTime = true; |
|
|
lastShowTime = msg.timestamp; |
|
|
lastShowTime = msg.timestamp; |
|
|
} else { |
|
|
} else { |
|
|
// 计算与上一条显示时间的时间差(分钟)
|
|
|
|
|
|
|
|
|
// 计算与最后一条显示时间的消息的时间差(分钟)
|
|
|
const timeDiffMinutes = (msg.timestamp - lastShowTime) / (1000 * 60); |
|
|
const timeDiffMinutes = (msg.timestamp - lastShowTime) / (1000 * 60); |
|
|
|
|
|
|
|
|
// 超过5分钟显示时间(微信默认5分钟)
|
|
|
|
|
|
msg.showTime = timeDiffMinutes >= this.data.timeInterval; |
|
|
|
|
|
|
|
|
|
|
|
if (msg.showTime) { |
|
|
|
|
|
|
|
|
// 超过5分钟显示时间
|
|
|
|
|
|
if (timeDiffMinutes >= this.data.timeInterval) { |
|
|
|
|
|
msg.showTime = true; |
|
|
lastShowTime = msg.timestamp; |
|
|
lastShowTime = msg.timestamp; |
|
|
|
|
|
} else { |
|
|
|
|
|
msg.showTime = false; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
processedMessages.push(msg); |
|
|
processedMessages.push(msg); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 存储最后一条显示时间的消息的时间戳
|
|
|
|
|
|
if (lastShowTime) { |
|
|
|
|
|
this.setData({ lastShowTimeStamp: lastShowTime }); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return processedMessages; |
|
|
return processedMessages; |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 处理单条消息的时间显示(添加新消息时调用)
|
|
|
|
|
|
processSingleMessageTime: function(message, messageList) { |
|
|
|
|
|
const msg = { ...message }; |
|
|
|
|
|
|
|
|
|
|
|
// 确保时间戳是有效数字
|
|
|
|
|
|
if (!msg.timestamp || isNaN(msg.timestamp) || msg.timestamp <= 0) { |
|
|
|
|
|
msg.timestamp = Date.now(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (messageList.length === 0) { |
|
|
|
|
|
// 第一条消息
|
|
|
|
|
|
msg.showTime = true; |
|
|
|
|
|
this.setData({ lastShowTimeStamp: msg.timestamp }); |
|
|
|
|
|
return msg; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取最后一条显示时间的消息
|
|
|
|
|
|
const lastShowTime = this.data.lastShowTimeStamp; |
|
|
|
|
|
|
|
|
|
|
|
// 计算时间差(分钟)
|
|
|
|
|
|
const timeDiffMinutes = (msg.timestamp - lastShowTime) / (1000 * 60); |
|
|
|
|
|
|
|
|
|
|
|
// 超过5分钟显示时间
|
|
|
|
|
|
if (timeDiffMinutes >= this.data.timeInterval) { |
|
|
|
|
|
msg.showTime = true; |
|
|
|
|
|
this.setData({ lastShowTimeStamp: msg.timestamp }); |
|
|
|
|
|
} else { |
|
|
|
|
|
msg.showTime = false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return msg; |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
// 返回上一页
|
|
|
// 返回上一页
|
|
|
goBack: function() { |
|
|
goBack: function() { |
|
|
wx.navigateBack(); |
|
|
wx.navigateBack(); |
|
|
@ -555,11 +757,10 @@ Page({ |
|
|
type: 'text', |
|
|
type: 'text', |
|
|
content: content, |
|
|
content: content, |
|
|
timestamp: Date.now(), |
|
|
timestamp: Date.now(), |
|
|
status: 'sending', |
|
|
|
|
|
showTime: this.shouldShowTime() |
|
|
|
|
|
|
|
|
status: 'sending' |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 添加到消息列表
|
|
|
|
|
|
|
|
|
// 处理时间显示并添加到消息列表
|
|
|
this.addMessageToList(newMessage); |
|
|
this.addMessageToList(newMessage); |
|
|
|
|
|
|
|
|
// 清空输入框
|
|
|
// 清空输入框
|
|
|
@ -605,46 +806,27 @@ Page({ |
|
|
// });
|
|
|
// });
|
|
|
// }
|
|
|
// }
|
|
|
// });
|
|
|
// });
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 判断是否显示时间(基于时间间隔)
|
|
|
|
|
|
shouldShowTime: function() { |
|
|
|
|
|
const { messageList, timeInterval } = this.data; |
|
|
|
|
|
if (messageList.length === 0) return true; |
|
|
|
|
|
|
|
|
|
|
|
const lastMessage = messageList[messageList.length - 1]; |
|
|
|
|
|
|
|
|
|
|
|
// 如果上一条消息显示了时间,检查时间间隔
|
|
|
|
|
|
if (lastMessage.showTime) { |
|
|
|
|
|
const timeDiff = Date.now() - lastMessage.timestamp; |
|
|
|
|
|
const timeDiffMinutes = timeDiff / (1000 * 60); |
|
|
|
|
|
return timeDiffMinutes >= timeInterval; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 模拟发送成功
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
|
this.updateMessageStatus(newMessage.id, 'success'); |
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
// 模拟专家回复
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
|
this.receiveExpertReply(); |
|
|
|
|
|
}, 1000 + Math.random() * 1000); |
|
|
|
|
|
}, 500); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 添加消息到列表
|
|
|
// 添加消息到列表
|
|
|
addMessageToList: function(message) { |
|
|
addMessageToList: function(message) { |
|
|
const { messageList, timeInterval } = this.data; |
|
|
|
|
|
|
|
|
|
|
|
// 确保时间戳有效
|
|
|
|
|
|
if (!message.timestamp || isNaN(message.timestamp) || message.timestamp <= 0) { |
|
|
|
|
|
message.timestamp = Date.now(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (messageList.length > 0) { |
|
|
|
|
|
const lastMessage = messageList[messageList.length - 1]; |
|
|
|
|
|
const timeDiff = message.timestamp - lastMessage.timestamp; |
|
|
|
|
|
const timeDiffMinutes = timeDiff / (1000 * 60); |
|
|
|
|
|
|
|
|
const { messageList } = this.data; |
|
|
|
|
|
|
|
|
// 根据时间间隔判断是否显示时间
|
|
|
|
|
|
message.showTime = timeDiffMinutes >= timeInterval; |
|
|
|
|
|
} else { |
|
|
|
|
|
message.showTime = true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 处理消息时间显示
|
|
|
|
|
|
const processedMessage = this.processSingleMessageTime(message, messageList); |
|
|
|
|
|
|
|
|
messageList.push(message); |
|
|
|
|
|
|
|
|
// 添加到列表
|
|
|
|
|
|
messageList.push(processedMessage); |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
messageList |
|
|
messageList |
|
|
@ -685,7 +867,7 @@ Page({ |
|
|
type: 'text', |
|
|
type: 'text', |
|
|
content: randomReply, |
|
|
content: randomReply, |
|
|
timestamp: Date.now(), |
|
|
timestamp: Date.now(), |
|
|
showTime: this.shouldShowTime() |
|
|
|
|
|
|
|
|
status: 'success' |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
this.addMessageToList(newMessage); |
|
|
this.addMessageToList(newMessage); |
|
|
@ -735,22 +917,27 @@ Page({ |
|
|
}, 200); |
|
|
}, 200); |
|
|
|
|
|
|
|
|
// 检查是否需要加载更多
|
|
|
// 检查是否需要加载更多
|
|
|
if (scrollTop <= 100 && !this.data.loading && this.data.hasMore) { |
|
|
|
|
|
|
|
|
if (scrollTop <= 100 && !this.data.loadingMore && this.data.hasMore && this.data.page > 1) { |
|
|
this.loadMoreMessages(); |
|
|
this.loadMoreMessages(); |
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 加载更多消息
|
|
|
// 加载更多消息
|
|
|
loadMoreMessages: function() { |
|
|
loadMoreMessages: function() { |
|
|
if (this.data.loading || !this.data.hasMore) return; |
|
|
|
|
|
|
|
|
if (this.data.loadingMore || !this.data.hasMore) return; |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
page: this.data.page + 1, |
|
|
|
|
|
loading: true |
|
|
|
|
|
|
|
|
loadingMore: true |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
// 加载更多聊天记录
|
|
|
// 加载更多聊天记录
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
|
this.setData({ |
|
|
|
|
|
page: this.data.page + 1 |
|
|
|
|
|
}, () => { |
|
|
this.loadChatHistory(); |
|
|
this.loadChatHistory(); |
|
|
|
|
|
}); |
|
|
|
|
|
}, 500); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 滚动到底部
|
|
|
// 滚动到底部
|
|
|
@ -761,9 +948,11 @@ Page({ |
|
|
scrollAnimate: animate |
|
|
scrollAnimate: animate |
|
|
}, () => { |
|
|
}, () => { |
|
|
// 设置一个足够大的值确保滚动到底部
|
|
|
// 设置一个足够大的值确保滚动到底部
|
|
|
|
|
|
setTimeout(() => { |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
scrollTop: 999999 |
|
|
scrollTop: 999999 |
|
|
}); |
|
|
}); |
|
|
|
|
|
}, 100); |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
@ -818,32 +1007,11 @@ Page({ |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 拍照
|
|
|
|
|
|
takePhoto: function() { |
|
|
|
|
|
this.hideMediaActionSheet(); |
|
|
|
|
|
|
|
|
|
|
|
wx.chooseImage({ |
|
|
|
|
|
count: 1, |
|
|
|
|
|
sizeType: ['compressed'], |
|
|
|
|
|
sourceType: ['camera'], |
|
|
|
|
|
success: (res) => { |
|
|
|
|
|
console.log('拍照成功:', res.tempFilePaths); |
|
|
|
|
|
this.uploadImages(res.tempFilePaths); |
|
|
|
|
|
}, |
|
|
|
|
|
fail: (err) => { |
|
|
|
|
|
console.error('拍照失败:', err); |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '拍照失败', |
|
|
|
|
|
icon: 'none' |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 选择视频
|
|
|
// 选择视频
|
|
|
chooseVideo: function() { |
|
|
chooseVideo: function() { |
|
|
this.hideMediaActionSheet(); |
|
|
this.hideMediaActionSheet(); |
|
|
|
|
|
|
|
|
wx.chooseVideo({ |
|
|
wx.chooseVideo({ |
|
|
sourceType: ['album'], |
|
|
sourceType: ['album'], |
|
|
compressed: true, |
|
|
compressed: true, |
|
|
@ -862,11 +1030,6 @@ Page({ |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 录制语音
|
|
|
|
|
|
recordAudio: function() { |
|
|
|
|
|
this.hideMediaActionSheet(); |
|
|
|
|
|
this.startVoiceRecord(); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 选择文件
|
|
|
// 选择文件
|
|
|
chooseFile: function() { |
|
|
chooseFile: function() { |
|
|
@ -924,8 +1087,7 @@ Page({ |
|
|
extension: extension, |
|
|
extension: extension, |
|
|
timestamp: Date.now(), |
|
|
timestamp: Date.now(), |
|
|
status: 'uploading', |
|
|
status: 'uploading', |
|
|
progress: 0, |
|
|
|
|
|
showTime: this.shouldShowTime() |
|
|
|
|
|
|
|
|
progress: 0 |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
this.addMessageToList(message); |
|
|
this.addMessageToList(message); |
|
|
@ -997,7 +1159,7 @@ Page({ |
|
|
type: 'text', |
|
|
type: 'text', |
|
|
content: randomReply, |
|
|
content: randomReply, |
|
|
timestamp: Date.now(), |
|
|
timestamp: Date.now(), |
|
|
showTime: this.shouldShowTime() |
|
|
|
|
|
|
|
|
status: 'success' |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
this.addMessageToList(newMessage); |
|
|
this.addMessageToList(newMessage); |
|
|
@ -1069,8 +1231,7 @@ Page({ |
|
|
content: tempFilePath, |
|
|
content: tempFilePath, |
|
|
duration: duration, |
|
|
duration: duration, |
|
|
timestamp: Date.now(), |
|
|
timestamp: Date.now(), |
|
|
status: 'sending', |
|
|
|
|
|
showTime: this.shouldShowTime() |
|
|
|
|
|
|
|
|
status: 'sending' |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
this.addMessageToList(message); |
|
|
this.addMessageToList(message); |
|
|
@ -1087,7 +1248,7 @@ Page({ |
|
|
type: 'text', |
|
|
type: 'text', |
|
|
content: '语音收到了,我会仔细听取分析。', |
|
|
content: '语音收到了,我会仔细听取分析。', |
|
|
timestamp: Date.now(), |
|
|
timestamp: Date.now(), |
|
|
showTime: this.shouldShowTime() |
|
|
|
|
|
|
|
|
status: 'success' |
|
|
}; |
|
|
}; |
|
|
this.addMessageToList(reply); |
|
|
this.addMessageToList(reply); |
|
|
}, 1500); |
|
|
}, 1500); |
|
|
@ -1151,71 +1312,35 @@ Page({ |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 优化:微信样式时间格式化
|
|
|
|
|
|
|
|
|
// 修复:时间格式化函数 - 确保正确显示
|
|
|
formatTime: function(timestamp) { |
|
|
formatTime: function(timestamp) { |
|
|
|
|
|
// 调试日志
|
|
|
|
|
|
console.log('formatTime 接收到的timestamp:', timestamp, '类型:', typeof timestamp); |
|
|
|
|
|
|
|
|
if (!timestamp || timestamp <= 0) { |
|
|
if (!timestamp || timestamp <= 0) { |
|
|
console.warn('无效的时间戳:', timestamp); |
|
|
console.warn('无效的时间戳:', timestamp); |
|
|
return ''; |
|
|
|
|
|
|
|
|
return '未知时间'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const timeNum = Number(timestamp); |
|
|
const timeNum = Number(timestamp); |
|
|
if (isNaN(timeNum)) { |
|
|
if (isNaN(timeNum)) { |
|
|
return ''; |
|
|
|
|
|
|
|
|
console.warn('时间戳不是有效数字:', timestamp); |
|
|
|
|
|
return '未知时间'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const date = new Date(timeNum); |
|
|
const date = new Date(timeNum); |
|
|
if (isNaN(date.getTime())) { |
|
|
if (isNaN(date.getTime())) { |
|
|
return ''; |
|
|
|
|
|
|
|
|
console.warn('无法创建有效日期对象:', timeNum); |
|
|
|
|
|
return '未知时间'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const now = new Date(); |
|
|
|
|
|
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()); |
|
|
|
|
|
const yesterday = new Date(today.getTime() - 86400000); |
|
|
|
|
|
|
|
|
|
|
|
const msgDate = new Date(date.getFullYear(), date.getMonth(), date.getDate()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 只显示小时和分钟
|
|
|
const hours = date.getHours().toString().padStart(2, '0'); |
|
|
const hours = date.getHours().toString().padStart(2, '0'); |
|
|
const minutes = date.getMinutes().toString().padStart(2, '0'); |
|
|
const minutes = date.getMinutes().toString().padStart(2, '0'); |
|
|
const timeStr = `${hours}:${minutes}`; |
|
|
|
|
|
|
|
|
|
|
|
// 今天
|
|
|
|
|
|
if (msgDate.getTime() === today.getTime()) { |
|
|
|
|
|
// 小于1分钟显示"刚刚"
|
|
|
|
|
|
const diffMinutes = (now.getTime() - date.getTime()) / (1000 * 60); |
|
|
|
|
|
if (diffMinutes < 1) { |
|
|
|
|
|
return '刚刚'; |
|
|
|
|
|
} |
|
|
|
|
|
// 小于1小时显示"X分钟前"
|
|
|
|
|
|
else if (diffMinutes < 60) { |
|
|
|
|
|
return `${Math.floor(diffMinutes)}分钟前`; |
|
|
|
|
|
} |
|
|
|
|
|
// 今天超过1小时显示时间
|
|
|
|
|
|
else { |
|
|
|
|
|
return timeStr; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
// 昨天
|
|
|
|
|
|
else if (msgDate.getTime() === yesterday.getTime()) { |
|
|
|
|
|
return `昨天 ${timeStr}`; |
|
|
|
|
|
} |
|
|
|
|
|
// 本周内(7天内)
|
|
|
|
|
|
else { |
|
|
|
|
|
const diffDays = (today.getTime() - msgDate.getTime()) / (1000 * 60 * 60 * 24); |
|
|
|
|
|
if (diffDays < 7) { |
|
|
|
|
|
const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']; |
|
|
|
|
|
return `${weekDays[date.getDay()]} ${timeStr}`; |
|
|
|
|
|
} |
|
|
|
|
|
// 今年内
|
|
|
|
|
|
else if (date.getFullYear() === now.getFullYear()) { |
|
|
|
|
|
const month = date.getMonth() + 1; |
|
|
|
|
|
const day = date.getDate(); |
|
|
|
|
|
return `${month}月${day}日 ${timeStr}`; |
|
|
|
|
|
} |
|
|
|
|
|
// 更早
|
|
|
|
|
|
else { |
|
|
|
|
|
return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${timeStr}`; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const result = `${hours}:${minutes}`; |
|
|
|
|
|
console.log('formatTime 结果:', result); |
|
|
|
|
|
return result; |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 格式化文件大小
|
|
|
// 格式化文件大小
|
|
|
|