Compare commits

..

No commits in common. "0f26ac4c9bdfc786ff8b091745ace4c8430336b5" and "01b703e689990e4c5b61a29efe17394330fb5228" have entirely different histories.

7 changed files with 161 additions and 378 deletions

View File

@ -1,7 +1,6 @@
<script> <script>
import dbStore from "@/store/db"; import dbStore from "@/store/db";
import accountStore from "@/store/account"; import accountStore from "@/store/account";
import { globalUnreadListenerManager } from "@/utils/global-unread-listener.js";
export default { export default {
async onLaunch() { async onLaunch() {
@ -39,8 +38,6 @@ export default {
const success = await account.initIMAfterLogin(); const success = await account.initIMAfterLogin();
if (success) { if (success) {
console.log("IM 初始化成功"); console.log("IM 初始化成功");
// IM
globalUnreadListenerManager.setup();
} else { } else {
console.warn("IM 初始化失败"); console.warn("IM 初始化失败");
} }

View File

@ -6,24 +6,17 @@ $text-color-sub: #999;
$primary-color: #0877F1; $primary-color: #0877F1;
.chat-page { .chat-page {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
height: 100vh;
background-color: #f5f5f5; background-color: #f5f5f5;
overflow: hidden; overflow: hidden;
transition: padding-bottom 0.25s cubic-bezier(0.25, 0.46, 0.45, 0.94);
} }
/* 患者信息栏样式 */ /* 患者信息栏样式 */
.patient-info-bar { .patient-info-bar {
position: fixed; position: sticky;
top: 0; top: 0;
left: 0;
right: 0;
background: #fff; background: #fff;
border-bottom: 1rpx solid #f0f0f0; border-bottom: 1rpx solid #f0f0f0;
padding: 20rpx 32rpx; padding: 20rpx 32rpx;
@ -117,15 +110,11 @@ $primary-color: #0877F1;
} }
.chat-content { .chat-content {
position: fixed; flex: 1;
top: 100rpx; /* 患者信息栏高度,根据实际调整 */
left: 0;
right: 0;
bottom: 120rpx; /* 输入框高度,根据实际调整 */
box-sizing: border-box; box-sizing: border-box;
overflow-x: hidden; overflow-x: hidden;
overflow-y: auto; overflow-y: auto;
transition: bottom 0.25s cubic-bezier(0.25, 0.46, 0.45, 0.94); min-height: 0;
} }
.chat-content-compressed { .chat-content-compressed {
@ -390,24 +379,18 @@ $primary-color: #0877F1;
} }
.input-section { .input-section {
position: fixed;
bottom: 0;
left: 0;
right: 0;
background: #fff; background: #fff;
border-top: 1rpx solid #e0e0e0; border-top: 1rpx solid #e0e0e0;
position: relative;
z-index: 200; z-index: 200;
padding-bottom: env(safe-area-inset-bottom);
flex-shrink: 0; flex-shrink: 0;
transform: translateZ(0); /* 开启硬件加速,提升性能 */
transition: bottom 0.25s cubic-bezier(0.25, 0.46, 0.45, 0.94);
will-change: bottom;
} }
.input-toolbar { .input-toolbar {
display: flex; display: flex;
align-items: center; align-items: center;
padding: 12rpx 20rpx; padding: 16rpx 20rpx;
padding-bottom: env(safe-area-inset-bottom);
gap: 12rpx; gap: 12rpx;
} }
@ -513,8 +496,7 @@ $primary-color: #0877F1;
justify-content: flex-start; justify-content: flex-start;
background: #fff; background: #fff;
border-top: 1rpx solid #eee; border-top: 1rpx solid #eee;
padding: 20rpx 0 20rpx 60rpx; padding: 20rpx 0 40rpx 60rpx;
padding-bottom: env(safe-area-inset-bottom);
gap: 40rpx 50rpx; gap: 40rpx 50rpx;
flex-wrap: wrap; flex-wrap: wrap;
background-color: #f5f5f5; background-color: #f5f5f5;

View File

@ -1,5 +1,5 @@
<template> <template>
<view class="input-section" :style="{ bottom: props.keyboardHeight + 'px' }"> <view class="input-section">
<view class="input-toolbar"> <view class="input-toolbar">
<view @click="toggleVoiceInput" class="voice-toggle-btn"> <view @click="toggleVoiceInput" class="voice-toggle-btn">
<image v-if="showVoiceInput" src="/static/jianpan.png" class="voice-toggle-icon" mode="aspectFit"></image> <image v-if="showVoiceInput" src="/static/jianpan.png" class="voice-toggle-icon" mode="aspectFit"></image>
@ -8,7 +8,7 @@
<view class="input-area"> <view class="input-area">
<textarea v-if="!showVoiceInput" class="text-input" v-model="inputText" placeholder="我来说两句..." <textarea v-if="!showVoiceInput" class="text-input" v-model="inputText" placeholder="我来说两句..."
@confirm="sendTextMessage" @focus="handleInputFocus" @input="handleInput" @confirm="sendTextMessage" @focus="handleInputFocus" @input="handleInput"
:auto-height="true" :show-confirm-bar="false" :adjust-position="false" :hold-keyboard="true" /> :auto-height="true" :show-confirm-bar="false" :adjust-position="true" :cursor-spacing="40" />
<input v-else class="voice-input-btn" :class="{ recording: isRecording }" @touchstart="startRecord" <input v-else class="voice-input-btn" :class="{ recording: isRecording }" @touchstart="startRecord"
@touchmove="onRecordTouchMove" @touchend="stopRecord" @touchcancel="cancelRecord" :placeholder="isRecording ? '松开发送' : '按住说话'" disabled> @touchmove="onRecordTouchMove" @touchend="stopRecord" @touchcancel="cancelRecord" :placeholder="isRecording ? '松开发送' : '按住说话'" disabled>
</input> </input>
@ -77,7 +77,6 @@ const props = defineProps({
groupId: { type: String, default: '' }, groupId: { type: String, default: '' },
userId: { type: String, default: '' }, userId: { type: String, default: '' },
corpId: { type: String, default: '' }, corpId: { type: String, default: '' },
keyboardHeight: { type: Number, default: 0 },
}); });
// Emits // Emits
@ -151,15 +150,11 @@ const initRecorderManager = () => {
const sendTextMessage = async () => { const sendTextMessage = async () => {
if (!inputText.value.trim()) return; if (!inputText.value.trim()) return;
const textToSend = inputText.value; await sendMessage("text", inputText.value);
inputText.value = ""; inputText.value = "";
await sendMessage("text", textToSend); //
uni.hideKeyboard();
//
nextTick(() => {
// hold-keyboard
});
}; };

View File

@ -1,6 +1,5 @@
<template> <template>
<page-meta :page-style="'overflow:' + (keyboardHeight > 0 ? 'hidden' : 'visible')"></page-meta> <view class="chat-page">
<view class="chat-page" :style="{ paddingBottom: keyboardHeight + 'px' }">
<!-- 患者信息栏 --> <!-- 患者信息栏 -->
<view class="patient-info-bar" v-if="patientInfo.name"> <view class="patient-info-bar" v-if="patientInfo.name">
<view class="patient-info-content"> <view class="patient-info-content">
@ -23,7 +22,6 @@
<!-- 聊天消息区域 --> <!-- 聊天消息区域 -->
<scroll-view <scroll-view
class="chat-content" class="chat-content"
:style="{ bottom: (keyboardHeight > 0 ? keyboardHeight + 60 : 60) + 'px' }"
scroll-y="true" scroll-y="true"
enhanced="true" enhanced="true"
bounces="false" bounces="false"
@ -148,7 +146,6 @@
" "
:userId="openid" :userId="openid"
:corpId="corpId.value" :corpId="corpId.value"
:keyboardHeight="keyboardHeight"
@scrollToBottom="() => scrollToBottom(true)" @scrollToBottom="() => scrollToBottom(true)"
@messageSent="() => scrollToBottom(true)" @messageSent="() => scrollToBottom(true)"
/> />
@ -161,7 +158,6 @@ import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
import { storeToRefs } from "pinia"; import { storeToRefs } from "pinia";
import useAccountStore from "@/store/account.js"; import useAccountStore from "@/store/account.js";
import { globalTimChatManager } from "@/utils/tim-chat.js"; import { globalTimChatManager } from "@/utils/tim-chat.js";
import { globalUnreadListenerManager } from "@/utils/global-unread-listener.js";
import { import {
startIMMonitoring, startIMMonitoring,
stopIMMonitoring, stopIMMonitoring,
@ -216,9 +212,6 @@ const chatInfo = ref({
avatar: "/static/home/avatar.svg", avatar: "/static/home/avatar.svg",
}); });
//
const keyboardHeight = ref(0);
// //
const isEvaluationPopupOpen = ref(false); const isEvaluationPopupOpen = ref(false);
@ -449,22 +442,6 @@ onLoad((options) => {
chatInfo.value.userID = options.userID; chatInfo.value.userID = options.userID;
} }
//
uni.onKeyboardHeightChange((res) => {
console.log("键盘高度变化:", res.height);
const oldHeight = keyboardHeight.value;
keyboardHeight.value = res.height;
// 00
if (oldHeight === 0 && res.height > 0) {
nextTick(() => {
setTimeout(() => {
scrollToBottom(true);
}, 100);
});
}
});
checkLoginAndInitTIM(); checkLoginAndInitTIM();
updateNavigationTitle(); updateNavigationTitle();
}); });
@ -550,10 +527,9 @@ const initTIMCallbacks = async () => {
.setMessageRead({ .setMessageRead({
conversationID: chatInfo.value.conversationID, conversationID: chatInfo.value.conversationID,
}) })
.then(async () => { .then(() => {
console.log("✓ 收到新消息后已标记为已读"); console.log("✓ 收到新消息后已标记为已读");
// tabBar // tabBar
await globalUnreadListenerManager.refreshBadge();
}) })
.catch((error) => { .catch((error) => {
console.error("✗ 标记已读失败:", error); console.error("✗ 标记已读失败:", error);
@ -704,10 +680,9 @@ const loadMessageList = async () => {
.setMessageRead({ .setMessageRead({
conversationID: chatInfo.value.conversationID, conversationID: chatInfo.value.conversationID,
}) })
.then(async () => { .then(() => {
console.log("✓ 会话已标记为已读:", chatInfo.value.conversationID); console.log("✓ 会话已标记为已读:", chatInfo.value.conversationID);
// tabBar // tabBar
await globalUnreadListenerManager.refreshBadge();
}) })
.catch((error) => { .catch((error) => {
console.error("✗ 标记会话已读失败:", error); console.error("✗ 标记会话已读失败:", error);
@ -870,11 +845,6 @@ onHide(() => {
// ID // ID
timChatManager.currentConversationID = null; timChatManager.currentConversationID = null;
console.log("✓ 页面隐藏已清空当前会话ID"); console.log("✓ 页面隐藏已清空当前会话ID");
// tabBar
if (globalUnreadListenerManager.isInitialized) {
globalUnreadListenerManager.refreshBadge();
}
}); });
// //
@ -1020,9 +990,6 @@ const handleApplyConsult = async () => {
onUnmounted(() => { onUnmounted(() => {
clearMessageCache(); clearMessageCache();
//
uni.offKeyboardHeightChange();
timChatManager.setCallback("onSDKReady", null); timChatManager.setCallback("onSDKReady", null);
timChatManager.setCallback("onSDKNotReady", null); timChatManager.setCallback("onSDKNotReady", null);
timChatManager.setCallback("onMessageReceived", null); timChatManager.setCallback("onMessageReceived", null);

View File

@ -76,13 +76,12 @@
</template> </template>
<script setup> <script setup>
import { ref, computed, onUnmounted } from "vue"; import { ref, computed } from "vue";
import { onLoad, onShow, onHide } from "@dcloudio/uni-app"; import { onLoad, onShow, onHide } from "@dcloudio/uni-app";
import { storeToRefs } from "pinia"; import { storeToRefs } from "pinia";
import useAccountStore from "@/store/account.js"; import useAccountStore from "@/store/account.js";
import { globalTimChatManager } from "@/utils/tim-chat.js"; import { globalTimChatManager } from "@/utils/tim-chat.js";
import { mergeConversationWithGroupDetails } from "@/utils/conversation-merger.js"; import { mergeConversationWithGroupDetails } from "@/utils/conversation-merger.js";
import { globalUnreadListenerManager } from "@/utils/global-unread-listener.js";
import useGroupAvatars from "./hooks/use-group-avatars.js"; import useGroupAvatars from "./hooks/use-group-avatars.js";
import GroupAvatar from "@/components/group-avatar.vue"; import GroupAvatar from "@/components/group-avatar.vue";
@ -252,143 +251,125 @@ const loadConversationList = async () => {
// //
let updateTimer = null; let updateTimer = null;
// //
const handleConversationListUpdate = async (eventData) => { const setupConversationListener = () => {
console.log("【消息列表页】会话列表更新事件:", eventData); if (!globalTimChatManager) return;
//
// globalTimChatManager.setCallback("onConversationListUpdated", (eventData) => {
if (eventData && !Array.isArray(eventData) && eventData.conversationID) { console.log("会话列表更新事件:", eventData);
const conversationID = eventData.conversationID; //
const existingIndex = conversationList.value.findIndex( if (eventData && !Array.isArray(eventData) && eventData.conversationID) {
(conv) => conv.conversationID === conversationID const conversationID = eventData.conversationID;
);
if (existingIndex !== -1) {
//
if (eventData.unreadCount !== undefined) {
conversationList.value[existingIndex].unreadCount =
eventData.unreadCount;
console.log(
`【消息列表页】已更新会话未读数: ${conversationList.value[existingIndex].name}, unreadCount: ${eventData.unreadCount}`
);
}
//
if (eventData.lastMessage) {
conversationList.value[existingIndex].lastMessage = eventData.lastMessage.messageForShow || '';
conversationList.value[existingIndex].lastMessageTime = eventData.lastMessage.lastTime || Date.now();
//
conversationList.value.sort((a, b) => b.lastMessageTime - a.lastMessageTime);
}
}
return;
}
// eventData
if (!eventData || !Array.isArray(eventData)) {
console.warn("【消息列表页】会话列表更新事件数据格式错误");
return;
}
//
if (updateTimer) {
clearTimeout(updateTimer);
}
updateTimer = setTimeout(async () => {
//
const groupConversations = eventData.filter(
(conv) => conv.conversationID && conv.conversationID.startsWith("GROUP")
);
console.log(`【消息列表页】收到 ${groupConversations.length} 个群聊会话更新`);
// 使 TimChatManager
const formattedConversations = groupConversations.map((conv) =>
globalTimChatManager.formatConversationData(conv)
);
//
const mergedConversations = await mergeConversationWithGroupDetails(
formattedConversations
);
if (!mergedConversations || mergedConversations.length === 0) {
console.log("【消息列表页】合并后的会话数据为空,跳过更新");
return;
}
let needSort = false;
//
mergedConversations.forEach((conversationData) => {
const conversationID = conversationData.conversationID;
const existingIndex = conversationList.value.findIndex( const existingIndex = conversationList.value.findIndex(
(conv) => conv.conversationID === conversationID (conv) => conv.conversationID === conversationID
); );
if (existingIndex !== -1) { if (existingIndex !== -1) {
const existing = conversationList.value[existingIndex]; //
// if (eventData.unreadCount !== undefined) {
if ( conversationList.value[existingIndex].unreadCount =
existing.lastMessage !== conversationData.lastMessage || eventData.unreadCount;
existing.lastMessageTime !== conversationData.lastMessageTime ||
existing.unreadCount !== conversationData.unreadCount
) {
//
conversationList.value[existingIndex] = {
...conversationData,
//
avatar: existing.avatar || conversationData.avatar,
// 使 TIM SDK
unreadCount: conversationData.unreadCount || 0
};
needSort = true;
console.log( console.log(
`【消息列表页】已更新会话: ${conversationData.name}, unreadCount: ${conversationList.value[existingIndex].unreadCount}` `已清空会话未读数: ${conversationList.value[existingIndex].name}, unreadCount: ${eventData.unreadCount}`
); );
} }
} else {
//
conversationList.value.push(conversationData);
needSort = true;
console.log(`【消息列表页】已添加新会话: ${conversationData.name}`);
} }
}); return;
//
if (needSort) {
conversationList.value.sort(
(a, b) => b.lastMessageTime - a.lastMessageTime
);
} }
}, 100); // 100ms
};
// // eventData
const handleMessageReceived = (message) => { if (!eventData || !Array.isArray(eventData)) {
console.log("【消息列表页】收到新消息:", message); console.warn("会话列表更新事件数据格式错误");
// TIM SDK onConversationListUpdated return;
// }
};
// //
const setupConversationListener = () => { if (updateTimer) {
if (!globalTimChatManager) return; clearTimeout(updateTimer);
}
console.log("【消息列表页】设置会话监听器");
updateTimer = setTimeout(async () => {
// //
// const groupConversations = eventData.filter(
if (globalUnreadListenerManager.isInitialized) { (conv) => conv.conversationID && conv.conversationID.startsWith("GROUP")
globalUnreadListenerManager.addCallback("onConversationListUpdated", handleConversationListUpdate); );
globalUnreadListenerManager.addCallback("onMessageReceived", handleMessageReceived);
console.log("【消息列表页】已添加回调到全局监听器回调链"); console.log(`收到 ${groupConversations.length} 个群聊会话更新`);
} else {
console.warn("【消息列表页】全局未读监听器未初始化,使用直接回调方式"); // 使 TimChatManager
// const formattedConversations = groupConversations.map((conv) =>
globalTimChatManager.setCallback("onConversationListUpdated", handleConversationListUpdate); globalTimChatManager.formatConversationData(conv)
globalTimChatManager.setCallback("onMessageReceived", handleMessageReceived); );
}
//
const mergedConversations = await mergeConversationWithGroupDetails(
formattedConversations
);
if (!mergedConversations || mergedConversations.length === 0) {
console.log("合并后的会话数据为空,跳过更新");
return;
}
let needSort = false;
//
mergedConversations.forEach((conversationData) => {
const conversationID = conversationData.conversationID;
const existingIndex = conversationList.value.findIndex(
(conv) => conv.conversationID === conversationID
);
if (existingIndex !== -1) {
const existing = conversationList.value[existingIndex];
//
if (
existing.lastMessage !== conversationData.lastMessage ||
existing.lastMessageTime !== conversationData.lastMessageTime ||
existing.unreadCount !== conversationData.unreadCount
) {
//
conversationList.value[existingIndex] = {
...conversationData,
//
avatar: existing.avatar || conversationData.avatar,
// 使 TIM SDK 使 Math.max
// unreadCount N 0
unreadCount: conversationData.unreadCount || 0
};
needSort = true;
console.log(
`已更新会话: ${conversationData.name}, unreadCount: ${conversationList.value[existingIndex].unreadCount}`
);
}
} else {
//
conversationList.value.push(conversationData);
needSort = true;
console.log(`已添加新会话: ${conversationData.name}`);
}
});
//
if (needSort) {
conversationList.value.sort(
(a, b) => b.lastMessageTime - a.lastMessageTime
);
}
}, 100); // 100ms
});
//
globalTimChatManager.setCallback("onMessageReceived", (message) => {
console.log("消息列表页面收到新消息:", message);
//
// TIM SDK onConversationListUpdated
// +1
//
// onConversationListUpdated
//
});
}; };
// //
@ -436,8 +417,6 @@ const formatMessageTime = (timestamp) => {
// //
const handleClickConversation = async (conversation) => { const handleClickConversation = async (conversation) => {
console.log("点击会话:", conversation); console.log("点击会话:", conversation);
//
const conversationIndex = conversationList.value.findIndex( const conversationIndex = conversationList.value.findIndex(
(conv) => conv.conversationID === conversation.conversationID (conv) => conv.conversationID === conversation.conversationID
); );
@ -445,21 +424,6 @@ const handleClickConversation = async (conversation) => {
conversationList.value[conversationIndex].unreadCount = 0; conversationList.value[conversationIndex].unreadCount = 0;
} }
//
if (globalTimChatManager && globalTimChatManager.tim) {
try {
await globalTimChatManager.tim.setMessageRead({
conversationID: conversation.conversationID,
});
console.log("✓ 已标记会话为已读:", conversation.conversationID);
// tabBar
await globalUnreadListenerManager.refreshBadge();
} catch (error) {
console.error("✗ 标记会话已读失败:", error);
}
}
// //
uni.navigateTo({ uni.navigateTo({
url: `/pages/message/index?conversationID=${conversation.conversationID}&groupID=${conversation.groupID}`, url: `/pages/message/index?conversationID=${conversation.conversationID}&groupID=${conversation.groupID}`,
@ -513,42 +477,19 @@ const cleanMessageText = (text) => {
// //
onShow(async () => { onShow(async () => {
// tabBar
if (globalUnreadListenerManager.isInitialized) {
await globalUnreadListenerManager.refreshBadge();
}
}); });
// //
onHide(() => { onHide(() => {
console.log("【消息列表页】页面隐藏");
// //
if (updateTimer) { if (updateTimer) {
clearTimeout(updateTimer); clearTimeout(updateTimer);
updateTimer = null; updateTimer = null;
} }
if (globalTimChatManager) {
// globalTimChatManager globalTimChatManager.setCallback("onConversationListUpdated", null);
// globalTimChatManager.setCallback("onMessageReceived", null);
//
});
//
onUnmounted(() => {
console.log("【消息列表页】页面卸载,清理回调");
//
if (updateTimer) {
clearTimeout(updateTimer);
updateTimer = null;
}
//
if (globalUnreadListenerManager.isInitialized) {
globalUnreadListenerManager.removeCallback("onConversationListUpdated", handleConversationListUpdate);
globalUnreadListenerManager.removeCallback("onMessageReceived", handleMessageReceived);
console.log("【消息列表页】已从回调链移除回调");
} }
}); });
</script> </script>

View File

@ -3,7 +3,6 @@ import { defineStore } from "pinia";
import api from '@/utils/api'; import api from '@/utils/api';
import { toast } from '@/utils/widget'; import { toast } from '@/utils/widget';
import { initGlobalTIM, globalTimChatManager } from "@/utils/tim-chat.js"; import { initGlobalTIM, globalTimChatManager } from "@/utils/tim-chat.js";
import { globalUnreadListenerManager } from "@/utils/global-unread-listener.js";
const env = __VITE_ENV__; const env = __VITE_ENV__;
export default defineStore("accountStore", () => { export default defineStore("accountStore", () => {
@ -77,10 +76,6 @@ export default defineStore("accountStore", () => {
isIMInitialized.value = true; isIMInitialized.value = true;
console.log('IM 初始化成功'); console.log('IM 初始化成功');
// IM 初始化成功后,设置全局未读消息监听
globalUnreadListenerManager.setup();
return true; return true;
} catch (error) { } catch (error) {
console.log('IM初始化异常跳过 IM 初始化:', error.message); console.log('IM初始化异常跳过 IM 初始化:', error.message);
@ -96,11 +91,6 @@ export default defineStore("accountStore", () => {
await globalTimChatManager.destroy(); await globalTimChatManager.destroy();
console.log('腾讯IM退出成功'); console.log('腾讯IM退出成功');
} }
// 清除全局未读监听
if (globalUnreadListenerManager.isInitialized) {
globalUnreadListenerManager.destroy();
}
} catch (error) { } catch (error) {
console.error('退出腾讯IM失败:', error); console.error('退出腾讯IM失败:', error);
} }
@ -113,11 +103,6 @@ export default defineStore("accountStore", () => {
// 清除本地存储 // 清除本地存储
uni.removeStorageSync('account'); uni.removeStorageSync('account');
uni.removeStorageSync('openid'); uni.removeStorageSync('openid');
// 清除 tabBar 徽章
uni.removeTabBarBadge({
index: 1
});
} }
async function getExternalUserId(corpId) { async function getExternalUserId(corpId) {

View File

@ -7,11 +7,8 @@ import { globalTimChatManager } from './tim-chat.js';
class GlobalUnreadListenerManager { class GlobalUnreadListenerManager {
constructor() { constructor() {
this.isInitialized = false; this.isInitialized = false;
this.callbackChain = { this.originalConversationListCallback = null;
onConversationListUpdated: [], this.originalMessageReceivedCallback = null;
onMessageReceived: []
};
this.updateTimer = null; // 防抖定时器
} }
/** /**
@ -28,76 +25,39 @@ class GlobalUnreadListenerManager {
return; return;
} }
// 保存原始回调到回调链 // 保存原始回调(在覆盖前保存)
const originalConversationListCallback = globalTimChatManager.callbacks.onConversationListUpdated; this.originalConversationListCallback = globalTimChatManager.callbacks.onConversationListUpdated;
const originalMessageReceivedCallback = globalTimChatManager.callbacks.onMessageReceived; this.originalMessageReceivedCallback = globalTimChatManager.callbacks.onMessageReceived;
if (originalConversationListCallback && typeof originalConversationListCallback === 'function') { console.log('保存原始回调:', {
this.callbackChain.onConversationListUpdated.push(originalConversationListCallback); hasConversationListCallback: !!this.originalConversationListCallback,
} hasMessageReceivedCallback: !!this.originalMessageReceivedCallback
if (originalMessageReceivedCallback && typeof originalMessageReceivedCallback === 'function') {
this.callbackChain.onMessageReceived.push(originalMessageReceivedCallback);
}
console.log('全局未读监听初始化,回调链长度:', {
onConversationListUpdated: this.callbackChain.onConversationListUpdated.length,
onMessageReceived: this.callbackChain.onMessageReceived.length
}); });
// 监听会话列表更新事件(包括消息接收和已读状态变化) // 监听会话列表更新事件(包括消息接收和已读状态变化)
globalTimChatManager.setCallback('onConversationListUpdated', (eventData) => { globalTimChatManager.setCallback('onConversationListUpdated', (eventData) => {
console.log('【全局未读监听】onConversationListUpdated 触发,回调链长度:', this.callbackChain.onConversationListUpdated.length); console.log('onConversationListUpdated 触发,调用原始回调');
// 调用原始回调(如果存在)
// 执行回调链中的所有回调 if (this.originalConversationListCallback && typeof this.originalConversationListCallback === 'function') {
this.callbackChain.onConversationListUpdated.forEach((callback, index) => { this.originalConversationListCallback(eventData);
try { }
console.log(`【全局未读监听】执行回调链 #${index + 1}`); // 更新 tabBar 徽章
callback(eventData); this.updateTabBarBadge();
} catch (error) {
console.error(`【全局未读监听】执行 onConversationListUpdated 回调 #${index + 1} 失败:`, error);
}
});
// 防抖更新 tabBar 徽章
this.debouncedUpdateTabBarBadge();
}); });
// 监听消息接收事件 // 监听消息接收事件
globalTimChatManager.setCallback('onMessageReceived', (message) => { globalTimChatManager.setCallback('onMessageReceived', (message) => {
console.log('【全局未读监听】onMessageReceived 触发,回调链长度:', this.callbackChain.onMessageReceived.length); console.log('onMessageReceived 触发,调用原始回调');
// 调用原始回调(如果存在)
// 执行回调链中的所有回调 if (this.originalMessageReceivedCallback && typeof this.originalMessageReceivedCallback === 'function') {
this.callbackChain.onMessageReceived.forEach((callback, index) => { this.originalMessageReceivedCallback(message);
try { }
console.log(`【全局未读监听】执行回调链 #${index + 1}`); // 更新 tabBar 徽章
callback(message); this.updateTabBarBadge();
} catch (error) {
console.error(`【全局未读监听】执行 onMessageReceived 回调 #${index + 1} 失败:`, error);
}
});
// 防抖更新 tabBar 徽章
this.debouncedUpdateTabBarBadge();
}); });
this.isInitialized = true; this.isInitialized = true;
console.log('全局未读消息监听已设置'); console.log('全局未读消息监听已设置');
// 初始化时立即更新一次徽章
this.updateTabBarBadge();
}
/**
* 防抖更新 tabBar 徽章
* 避免频繁更新导致性能问题
*/
debouncedUpdateTabBarBadge() {
if (this.updateTimer) {
clearTimeout(this.updateTimer);
}
this.updateTimer = setTimeout(() => {
this.updateTabBarBadge();
}, 200); // 减少到 200ms 防抖延迟,提高响应速度
} }
/** /**
@ -107,22 +67,21 @@ class GlobalUnreadListenerManager {
async updateTabBarBadge() { async updateTabBarBadge() {
try { try {
if (!globalTimChatManager || !globalTimChatManager.tim) { if (!globalTimChatManager || !globalTimChatManager.tim) {
console.warn('【全局未读监听】globalTimChatManager 或 tim 未初始化'); console.warn('globalTimChatManager 或 tim 未初始化');
return; return;
} }
const response = await globalTimChatManager.tim.getConversationList(); const response = await globalTimChatManager.tim.getConversationList();
if (!response || !response.data || !response.data.conversationList) { if (!response || !response.data || !response.data.conversationList) {
console.warn('【全局未读监听】获取会话列表返回数据异常'); console.warn('获取会话列表返回数据异常');
return; return;
} }
const totalUnreadCount = this.calculateGroupUnreadCount(response.data.conversationList); const totalUnreadCount = this.calculateGroupUnreadCount(response.data.conversationList);
console.log('【全局未读监听】计算总未读数:', totalUnreadCount);
this.setTabBarBadge(totalUnreadCount); this.setTabBarBadge(totalUnreadCount);
} catch (error) { } catch (error) {
console.error('【全局未读监听】更新 tabBar 徽章失败:', error); console.error('更新 tabBar 徽章失败:', error);
} }
} }
@ -148,68 +107,25 @@ class GlobalUnreadListenerManager {
index: tabIndex, index: tabIndex,
text: count > 99 ? '99+' : String(count) text: count > 99 ? '99+' : String(count)
}); });
console.log(`已更新 tabBar 徽章(索引 ${tabIndex}:`, count); console.log(`已更新 tabBar 徽章(索引 ${tabIndex}:`, count);
} else { } else {
uni.removeTabBarBadge({ // uni.removeTabBarBadge({
index: tabIndex // index: tabIndex
}); // });
console.log(`已移除 tabBar 徽章(索引 ${tabIndex}`); console.log(`已移除 tabBar 徽章(索引 ${tabIndex}`);
} }
} }
/**
* 手动刷新徽章
* 用于页面显示时强制刷新
*/
async refreshBadge() { async refreshBadge() {
console.log('手动刷新 tabBar 徽章'); console.log('手动刷新 tabBar 徽章');
await this.updateTabBarBadge(); await this.updateTabBarBadge();
} }
/**
* 添加回调到回调链
* @param {string} eventName - 事件名称
* @param {Function} callback - 回调函数
*/
addCallback(eventName, callback) {
if (this.callbackChain[eventName] && typeof callback === 'function') {
// 避免重复添加同一个回调
if (!this.callbackChain[eventName].includes(callback)) {
this.callbackChain[eventName].push(callback);
console.log(`✓ 已添加回调到 ${eventName} 回调链,当前链长度:`, this.callbackChain[eventName].length);
} else {
console.log(`⚠️ 回调已存在于 ${eventName} 回调链中,跳过添加`);
}
}
}
/**
* 从回调链中移除回调
* @param {string} eventName - 事件名称
* @param {Function} callback - 回调函数
*/
removeCallback(eventName, callback) {
if (this.callbackChain[eventName]) {
const index = this.callbackChain[eventName].indexOf(callback);
if (index > -1) {
this.callbackChain[eventName].splice(index, 1);
console.log(`✓ 已从 ${eventName} 回调链移除回调,当前链长度:`, this.callbackChain[eventName].length);
}
}
}
/** /**
* 清除监听可选 * 清除监听可选
*/ */
destroy() { destroy() {
if (this.updateTimer) {
clearTimeout(this.updateTimer);
this.updateTimer = null;
}
this.callbackChain = {
onConversationListUpdated: [],
onMessageReceived: []
};
this.isInitialized = false; this.isInitialized = false;
console.log('全局未读消息监听已清除'); console.log('全局未读消息监听已清除');
} }