no message

This commit is contained in:
wangdongbo 2026-02-10 16:47:19 +08:00
parent 4e6cdf4bdd
commit 17aefb966e
2 changed files with 14 additions and 41 deletions

View File

@ -330,16 +330,14 @@ const setupConversationListener = () => {
existing.lastMessageTime !== conversationData.lastMessageTime ||
existing.unreadCount !== conversationData.unreadCount
) {
// 只更新变化的字段,保持头像和未读数稳定
// 只更新变化的字段,保持头像稳定
conversationList.value[existingIndex] = {
...conversationData,
// 保持原有头像,避免闪动
avatar: existing.avatar || conversationData.avatar,
// 保留较大的未读数(避免被后端数据覆盖)
unreadCount: Math.max(
existing.unreadCount || 0,
conversationData.unreadCount || 0
),
// 【修复】直接使用 TIM SDK 返回的未读数,不使用 Math.max
// 这样才能正确处理标记已读的情况(unreadCount 从 N 变为 0)
unreadCount: conversationData.unreadCount || 0
};
needSort = true;
console.log(
@ -367,40 +365,12 @@ const setupConversationListener = () => {
globalTimChatManager.setCallback("onMessageReceived", (message) => {
console.log("消息列表页面收到新消息:", message);
// 找到对应的会话
const conversationID = message.conversationID;
const conversationIndex = conversationList.value.findIndex(
(conv) => conv.conversationID === conversationID
);
if (conversationIndex !== -1) {
const conversation = conversationList.value[conversationIndex];
// 检查当前页面栈,判断用户是否正在查看该会话的聊天详情页
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
// 获取当前页面的 groupID 参数(如果在聊天详情页)
const currentGroupID = currentPage?.options?.groupID;
const isViewingThisConversation =
currentPage?.route === "pages/message/index" &&
currentGroupID === conversation.groupID;
// 如果用户正在查看这个具体的会话,不增加未读数
if (isViewingThisConversation) {
console.log("用户正在查看该会话,不增加未读数");
return;
}
// 只在用户不在该会话的聊天页面时才增加未读数
conversation.unreadCount = (conversation.unreadCount || 0) + 1;
console.log(
"已更新会话未读数:",
conversation.name,
"unreadCount:",
conversation.unreadCount
);
}
// 【修复】不再手动更新未读数
// TIM SDK 会在收到新消息时自动更新会话的未读数,并触发 onConversationListUpdated 事件
// 手动 +1 可能导致未读数不准确(重复计数)
//
// 注意:onConversationListUpdated 事件会在消息接收后自动触发,
// 其中包含了正确的未读数,因此这里不需要手动处理
});
};

View File

@ -144,7 +144,10 @@ function mergeConversationData(conversation, groupDetailsMap) {
name: formatConversationName(groupDetail),
// 更新头像(优先使用已有头像,避免闪动)
avatar: conversation.avatar || groupDetail.patient?.avatar || '/static/default-avatar.png'
avatar: conversation.avatar || groupDetail.patient?.avatar || '/static/default-avatar.png',
// 【修复】保留未读消息数(确保不被覆盖)
unreadCount: conversation.unreadCount || 0
}
}