桌面通知(系统级消息提醒

This commit is contained in:
yml
2026-06-05 20:06:50 +08:00
parent e32362f425
commit bdd1ba6052
8 changed files with 383 additions and 17 deletions
@@ -17,6 +17,7 @@ import {
import { uploadAdminFile } from '@/shared/api/files'
import ChatAttachmentImage from '@/components/ChatAttachmentImage.vue'
import { useChatSSE, type ChatEvent } from '@/features/chats/composables/useChatSSE'
import { useDesktopNotification } from '@/features/chats/composables/useDesktopNotification'
import { formatDateMinute } from '@/utils/time'
import TransferDialog from '../components/TransferDialog.vue'
import QuickReplyDialog from '../components/QuickReplyDialog.vue'
@@ -51,6 +52,9 @@ const activeMembers = computed(() => {
})
const canSend = computed(() => content.value.trim() !== '' || attachments.value.length > 0)
// 桌面通知
const desktopNotification = useDesktopNotification('admin')
function getParticipantRemark(participant: any) {
if (!active.value) return ''
const myParticipant = active.value.participants?.find(
@@ -66,6 +70,8 @@ function handleSSEEvent(event: ChatEvent) {
if (event.type === 'new_message' && active.value && event.conversation_id === active.value.id) {
const msg = event.message
if (msg && !messages.value.some(m => m.id === msg.id)) {
const isSelf = msg.sender_type === 'admin' && msg.sender_id === currentAdminId
messages.value = [...messages.value, {
id: msg.id,
conversation_id: msg.conversation_id,
@@ -74,13 +80,18 @@ function handleSSEEvent(event: ChatEvent) {
sender_role: msg.sender_role as ChatMessage['sender_role'],
sender_name: msg.sender_name,
sender_avatar: '',
is_self: msg.sender_type === 'admin' && msg.sender_id === currentAdminId,
is_self: isSelf,
content_type: msg.content_type as ChatMessage['content_type'],
content: msg.content,
attachment_urls: msg.attachment_urls || [],
created_at: msg.created_at,
}]
nextTick(() => scrollBottom())
// 不是自己发送的消息才发送通知
if (!isSelf) {
desktopNotification.notify(event, active.value.id)
}
}
}
}
@@ -89,6 +100,8 @@ const { onEvent } = useChatSSE('admin', '/api/admin/chats/events')
onEvent(handleSSEEvent)
onMounted(async () => {
// 静默请求通知权限(用户交互后才会弹窗)
desktopNotification.requestPermissionSilently()
await Promise.all([loadConversations(), loadQuickReplies()])
})