桌面通知(系统级消息提醒
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
||||
import { uploadFile } 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'
|
||||
|
||||
const currentUserId = Number(localStorage.getItem('user_id') || 0)
|
||||
@@ -37,10 +38,15 @@ const memberText = computed(() => {
|
||||
return participants.map(item => `${roleLabel(item.role)}:${item.display_name}`).join(' / ')
|
||||
})
|
||||
|
||||
// 桌面通知
|
||||
const desktopNotification = useDesktopNotification('user')
|
||||
|
||||
function handleSSEEvent(event: ChatEvent) {
|
||||
if (event.type === 'new_message' && event.conversation_id === conversationID.value) {
|
||||
const msg = event.message
|
||||
if (msg && !messages.value.some(m => m.id === msg.id)) {
|
||||
const isSelf = msg.sender_type === 'user' && msg.sender_id === currentUserId
|
||||
|
||||
messages.value = [...messages.value, {
|
||||
id: msg.id,
|
||||
conversation_id: msg.conversation_id,
|
||||
@@ -49,13 +55,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 === 'user' && msg.sender_id === currentUserId,
|
||||
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, conversationID.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (event.type === 'conversation_updated' && event.conversation_id === conversationID.value) {
|
||||
@@ -67,6 +78,8 @@ const { onEvent } = useChatSSE('user', '/api/chats/events')
|
||||
onEvent(handleSSEEvent)
|
||||
|
||||
onMounted(async () => {
|
||||
// 静默请求通知权限(用户交互后才会弹窗)
|
||||
desktopNotification.requestPermissionSilently()
|
||||
await loadAll()
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user