|
|
|
@ -9,7 +9,8 @@ Page({ |
|
|
|
timer: null, |
|
|
|
debugCount: 0, // 调试信息输出计数器
|
|
|
|
loginToastShown: false, // 登录提示是否已显示
|
|
|
|
lastLoadTime: 0 // 用于节流的时间戳
|
|
|
|
lastLoadTime: 0, // 用于节流的时间戳
|
|
|
|
chatContentCache: {} // 缓存聊天项的内容,避免重复获取
|
|
|
|
}, |
|
|
|
|
|
|
|
onLoad: function (options) { |
|
|
|
@ -216,11 +217,18 @@ Page({ |
|
|
|
// 调用API获取聊天列表
|
|
|
|
API.getChatList(userPhone).then(res => { |
|
|
|
if (res && Array.isArray(res)) { |
|
|
|
console.log('===== 获取聊天列表原始数据 ====='); |
|
|
|
console.log(JSON.stringify(res, null, 2)); |
|
|
|
|
|
|
|
// 创建业务员信息缓存,避免重复调用API
|
|
|
|
const personnelCache = {}; |
|
|
|
|
|
|
|
// 处理每个聊天项,获取业务员信息
|
|
|
|
const chatListPromises = res.map(async (chatItem) => { |
|
|
|
const chatListPromises = res.map(async (chatItem, index) => { |
|
|
|
console.log(`\n===== 处理聊天项 ${index} =====`); |
|
|
|
console.log('原始聊天项数据:', JSON.stringify(chatItem, null, 2)); |
|
|
|
console.log('原始unread值:', chatItem.unread, '类型:', typeof chatItem.unread); |
|
|
|
|
|
|
|
if (chatItem.manager_phone) { |
|
|
|
try { |
|
|
|
// 先检查缓存中是否已有该业务员信息
|
|
|
|
@ -253,35 +261,92 @@ Page({ |
|
|
|
chatItem.avatar = chatItem.avatar || 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'; |
|
|
|
} |
|
|
|
|
|
|
|
// 修复:对所有没有消息内容的聊天项获取最新消息内容
|
|
|
|
if (!chatItem.content) { |
|
|
|
// 获取用户已查看过的聊天列表
|
|
|
|
const viewedChats = wx.getStorageSync('viewedChats') || {}; |
|
|
|
const chatId = chatItem.manager_phone; |
|
|
|
|
|
|
|
// 获取最新消息内容和时间
|
|
|
|
let lastMessage = null; |
|
|
|
try { |
|
|
|
const messages = await API.getChatMessages(chatItem.manager_phone, userPhone, { limit: 1 }); |
|
|
|
const cacheKey = chatId; |
|
|
|
const cache = this.data.chatContentCache[cacheKey]; |
|
|
|
const now = Date.now(); |
|
|
|
|
|
|
|
// 每次刷新列表时都尝试获取最新消息,确保消息实时性
|
|
|
|
const messages = await API.getChatMessages(chatId, userPhone, { limit: 1 }); |
|
|
|
if (messages.length > 0) { |
|
|
|
chatItem.content = messages[0].content || '暂无消息内容'; |
|
|
|
lastMessage = messages[0]; |
|
|
|
chatItem.content = lastMessage.content || '暂无消息内容'; |
|
|
|
// 同时更新时间为最新消息的时间
|
|
|
|
chatItem.time = messages[0].created_at || null; |
|
|
|
} else { |
|
|
|
chatItem.time = lastMessage.created_at || null; |
|
|
|
|
|
|
|
// 更新缓存
|
|
|
|
const newCache = { |
|
|
|
...this.data.chatContentCache, |
|
|
|
[cacheKey]: { |
|
|
|
content: chatItem.content, |
|
|
|
time: chatItem.time, |
|
|
|
timestamp: now, |
|
|
|
messageId: lastMessage.id || lastMessage.message_id || null |
|
|
|
} |
|
|
|
}; |
|
|
|
this.setData({ chatContentCache: newCache }); |
|
|
|
console.log('更新聊天内容缓存:', cacheKey); |
|
|
|
} else if (!chatItem.content) { |
|
|
|
chatItem.content = '暂无消息内容'; |
|
|
|
} |
|
|
|
} catch (error) { |
|
|
|
console.error('获取聊天消息失败:', error); |
|
|
|
// 如果获取失败,尝试使用缓存内容
|
|
|
|
const cacheKey = chatId; |
|
|
|
const cache = this.data.chatContentCache[cacheKey]; |
|
|
|
if (cache && !chatItem.content) { |
|
|
|
chatItem.content = cache.content; |
|
|
|
chatItem.time = cache.time; |
|
|
|
console.log('获取最新消息失败,使用缓存内容:', cacheKey); |
|
|
|
} else if (!chatItem.content) { |
|
|
|
chatItem.content = '暂无消息内容'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 格式化时间
|
|
|
|
chatItem.time = this.formatDateTime(chatItem.time || null); |
|
|
|
// 确保unread字段存在
|
|
|
|
chatItem.unread = chatItem.unread || false; |
|
|
|
|
|
|
|
// 未读状态判断:基于用户是否查看过该聊天
|
|
|
|
// 只在用户未查看过时显示未读红点
|
|
|
|
console.log('当前已查看的聊天:', viewedChats); |
|
|
|
console.log('当前聊天ID:', chatId); |
|
|
|
|
|
|
|
// 检查该聊天是否已被查看过
|
|
|
|
const isViewed = viewedChats[chatId] === true; |
|
|
|
console.log('是否已查看:', isViewed); |
|
|
|
|
|
|
|
// 设置未读状态:未查看过则显示未读
|
|
|
|
chatItem.unread = !isViewed; |
|
|
|
console.log('设置unread的值:', chatItem.unread); |
|
|
|
|
|
|
|
// 保存最后一条消息的ID或内容作为标识(可选)
|
|
|
|
if (lastMessage) { |
|
|
|
chatItem.lastMessageId = lastMessage.id || lastMessage.message_id; |
|
|
|
chatItem.lastMessageContent = lastMessage.content; |
|
|
|
} |
|
|
|
|
|
|
|
console.log('处理完成的聊天项:', JSON.stringify(chatItem, null, 2)); |
|
|
|
return chatItem; |
|
|
|
}); |
|
|
|
|
|
|
|
// 等待所有聊天项处理完成
|
|
|
|
Promise.all(chatListPromises).then(processedChatList => { |
|
|
|
console.log('\n===== 所有聊天项处理完成 ====='); |
|
|
|
console.log('处理后的列表:', JSON.stringify(processedChatList, null, 2)); |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
chatList: processedChatList, |
|
|
|
filteredChatList: processedChatList |
|
|
|
}, () => { |
|
|
|
console.log('===== 页面数据已更新 ====='); |
|
|
|
console.log('chatList:', JSON.stringify(this.data.chatList, null, 2)); |
|
|
|
console.log('filteredChatList:', JSON.stringify(this.data.filteredChatList, null, 2)); |
|
|
|
}); |
|
|
|
wx.hideLoading(); |
|
|
|
}).catch(error => { |
|
|
|
@ -344,6 +409,73 @@ Page({ |
|
|
|
// 使用对方的电话号码作为聊天ID,而不是会话ID
|
|
|
|
const chatId = chatItem.manager_phone; |
|
|
|
|
|
|
|
// 获取用户手机号
|
|
|
|
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'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 1. 更新localStorage中的viewedChats,标记为已查看
|
|
|
|
const viewedChats = wx.getStorageSync('viewedChats') || {}; |
|
|
|
viewedChats[chatId] = true; |
|
|
|
wx.setStorageSync('viewedChats', viewedChats); |
|
|
|
console.log('更新localStorage中的viewedChats:', chatId, viewedChats[chatId]); |
|
|
|
|
|
|
|
// 2. 更新filteredChatList中的unread状态
|
|
|
|
const updatedFilteredChatList = [...this.data.filteredChatList]; |
|
|
|
updatedFilteredChatList[index].unread = false; |
|
|
|
|
|
|
|
// 3. 更新chatList中的unread状态
|
|
|
|
const updatedChatList = [...this.data.chatList]; |
|
|
|
const originalIndex = updatedChatList.findIndex(item => item.manager_phone === chatItem.manager_phone); |
|
|
|
if (originalIndex !== -1) { |
|
|
|
updatedChatList[originalIndex].unread = false; |
|
|
|
} |
|
|
|
|
|
|
|
// 4. 更新页面数据
|
|
|
|
this.setData({ |
|
|
|
filteredChatList: updatedFilteredChatList, |
|
|
|
chatList: updatedChatList |
|
|
|
}); |
|
|
|
|
|
|
|
console.log('标记聊天为已读:', chatId); |
|
|
|
|
|
|
|
// 5. 调用API将服务器端的消息标记为已读
|
|
|
|
API.markMessagesAsRead(chatId, userPhone).then(() => { |
|
|
|
console.log('服务器端消息标记为已读成功'); |
|
|
|
}).catch(error => { |
|
|
|
console.error('服务器端消息标记为已读失败:', error); |
|
|
|
}); |
|
|
|
|
|
|
|
// 跳转到聊天详情页,传递chatId和name参数
|
|
|
|
wx.navigateTo({ |
|
|
|
url: '/pages/chat-detail/index?id=' + chatId + '&name=' + encodeURIComponent(chatItem.name), |
|
|
|
|