diff --git a/frontend/src/features/chats/composables/useChatUnreadCount.ts b/frontend/src/features/chats/composables/useChatUnreadCount.ts index fb5fa11..fa4c8d1 100644 --- a/frontend/src/features/chats/composables/useChatUnreadCount.ts +++ b/frontend/src/features/chats/composables/useChatUnreadCount.ts @@ -4,6 +4,9 @@ import type { RouteLocationNormalizedLoaded } from 'vue-router' import { fetchUnreadChatCount } from '@/features/chats/api/chats' import { useSessionStore } from '@/stores/session' +const pollingInterval = 300_000 +const unreadChangedEvent = 'chat-unread-changed' + export function useChatUnreadCount(route: RouteLocationNormalizedLoaded) { const session = useSessionStore() const unreadCount = ref(0) @@ -32,14 +35,19 @@ export function useChatUnreadCount(route: RouteLocationNormalizedLoaded) { unreadCount.value = count } } catch { - // 底部导航不弹错误提示,保持上一次未读数即可。 + // 不弹错误提示,保持上一次未读数即可。 } } + function onUnreadChanged() { + void loadUnreadCount() + } + function startPolling() { stopPolling() void loadUnreadCount() - timer = window.setInterval(loadUnreadCount, 30_000) + timer = window.setInterval(loadUnreadCount, pollingInterval) + window.addEventListener(unreadChangedEvent, onUnreadChanged) } onMounted(() => { @@ -48,7 +56,10 @@ export function useChatUnreadCount(route: RouteLocationNormalizedLoaded) { } }) - onBeforeUnmount(stopPolling) + onBeforeUnmount(() => { + stopPolling() + window.removeEventListener(unreadChangedEvent, onUnreadChanged) + }) watch( () => session.isLoggedIn, @@ -58,6 +69,7 @@ export function useChatUnreadCount(route: RouteLocationNormalizedLoaded) { return } stopPolling() + window.removeEventListener(unreadChangedEvent, onUnreadChanged) unreadCount.value = 0 } ) diff --git a/frontend/src/features/chats/views/ChatView.vue b/frontend/src/features/chats/views/ChatView.vue index 3f3e46a..f795876 100644 --- a/frontend/src/features/chats/views/ChatView.vue +++ b/frontend/src/features/chats/views/ChatView.vue @@ -48,6 +48,7 @@ const desktopNotification = useDesktopNotification('user') function handleSSEEvent(event: ChatEvent) { if (event.type === 'new_message') { + window.dispatchEvent(new Event('chat-unread-changed')) const msg = event.message if (msg) { const isSelf = msg.sender_type === 'user' && msg.sender_id === currentUserId diff --git a/frontend/src/features/chats/views/MessagesView.vue b/frontend/src/features/chats/views/MessagesView.vue index a7479a4..9f0aa6f 100644 --- a/frontend/src/features/chats/views/MessagesView.vue +++ b/frontend/src/features/chats/views/MessagesView.vue @@ -81,6 +81,7 @@ async function loadChats(isRefresh = false, showLoading = true) { function handleSSEEvent(event: ChatEvent) { if (event.type === 'new_message' || event.type === 'conversation_updated') { loadChats(true, false) + window.dispatchEvent(new Event('chat-unread-changed')) } if (event.type === 'new_message' && event.message) { const isSelf = event.message.sender_type === 'user' && event.message.sender_id === currentUserId diff --git a/frontend/src/features/chats/views/MobileChatView.vue b/frontend/src/features/chats/views/MobileChatView.vue index 8bb5854..038358c 100644 --- a/frontend/src/features/chats/views/MobileChatView.vue +++ b/frontend/src/features/chats/views/MobileChatView.vue @@ -44,6 +44,7 @@ const desktopNotification = useDesktopNotification('user') function handleSSEEvent(event: ChatEvent) { if (event.type === 'new_message') { + window.dispatchEvent(new Event('chat-unread-changed')) const msg = event.message if (msg) { const isSelf = msg.sender_type === 'user' && msg.sender_id === currentUserId