From 750b91db6649a6aaf56fffa03d3012ebced34403 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 12 Jun 2025 22:58:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=9C=AA=E8=AF=BB=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E8=AE=A1=E6=95=B0=E5=92=8C=E6=A1=8C=E9=9D=A2=E5=9B=BE?= =?UTF-8?q?=E6=A0=87=E5=BE=BD=E7=AB=A0=E6=9B=B4=E6=96=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/types.ts | 2 + src/layouts/components/UserNotification.vue | 41 ++++++++++--- src/service-worker.ts | 60 +++++++++++++++++- src/types/global.d.ts | 17 ++++++ src/utils/badge.ts | 68 +++++++++++++++++++++ 5 files changed, 178 insertions(+), 10 deletions(-) create mode 100644 src/types/global.d.ts create mode 100644 src/utils/badge.ts diff --git a/src/api/types.ts b/src/api/types.ts index c25359f8..1eaa6d4f 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -1008,6 +1008,8 @@ export interface SystemNotification { text: string // 通知时间 date: string + // 是否已读 + read?: boolean } // 下载器配置 diff --git a/src/layouts/components/UserNotification.vue b/src/layouts/components/UserNotification.vue index 78a040ab..02894a3f 100644 --- a/src/layouts/components/UserNotification.vue +++ b/src/layouts/components/UserNotification.vue @@ -2,6 +2,7 @@ import { formatDateDifference } from '@core/utils/formatters' import { SystemNotification } from '@/api/types' import { useI18n } from 'vue-i18n' +import { updateAppBadge, clearAppBadge } from '@/utils/badge' const { t } = useI18n() @@ -17,6 +18,34 @@ let eventSource: EventSource | null = null // 弹窗 const appsMenu = ref(false) +// 未读消息数量 +const unreadCount = computed(() => { + return notificationList.value.filter(item => !item.read).length +}) + +// 更新桌面图标徽章 +async function updateBadge() { + const count = unreadCount.value + await updateAppBadge(count) +} + +// 清除桌面图标徽章 +async function clearBadge() { + await clearAppBadge() +} + +// 标记所有消息为已读 +function markAllAsRead() { + hasNewMessage.value = false + // 标记所有消息为已读 + notificationList.value.forEach(item => { + item.read = true + }) + // 清除桌面图标徽章 + clearBadge() + appsMenu.value = false +} + // SSE持续接收消息 function startSSEMessager() { // 延迟 3 秒启动 SSE,避免相关认证信息尚未写入 Cookie 导致 403 @@ -27,6 +56,8 @@ function startSSEMessager() { const noti: SystemNotification = JSON.parse(event.data) notificationList.value.unshift(noti) hasNewMessage.value = true + // 更新桌面图标徽章 + updateBadge() } }) }, 3000) @@ -70,15 +101,7 @@ onBeforeUnmount(() => {