Эх сурвалжийг харах

feat(components): 增加消息组件事件监听和通知功能

1. 添加了对 'remind' 事件的监听,用于处理接收到的提醒消息。
2. 增加了对事件数据的解析和通知弹窗的展示。
3. 优化了日志输出,方便调试。
4. 移除了 `onBeforeUnmount` 生命周期钩子中的事件源关闭逻辑,避免不必要的资源释放。
luoyali 9 сар өмнө
parent
commit
c20d8770fb

+ 20 - 5
src/layout/components/Header/components/Message.vue

@@ -96,6 +96,9 @@ onMounted(() => {
 	})
 
 	eventSource.onmessage = (event: MessageEvent) => {
+		// const data = JSON.parse()
+		console.log('Received message-----:', event)
+
 		const eventType = event.type
 		const notificationTitles: { [key: string]: string } = {
 			message: '消息',
@@ -113,16 +116,28 @@ onMounted(() => {
 		})
 	}
 
+	eventSource.addEventListener('remind', (event: MessageEvent) => {
+		const data = JSON.parse(event.data)
+		console.log('Received remind event:', event)
+		// 处理接收到的提醒消息
+		ElNotification({
+			title: data.title,
+			message: data.content,
+			type: 'success',
+			duration: 10000
+		})
+	})
+
 	eventSource.onerror = (error: any) => {
 		console.error('EventSource failed:', error)
 	}
 })
 
-onBeforeUnmount(() => {
-	if (eventSource) {
-		eventSource.close()
-	}
-})
+// onBeforeUnmount(() => {
+// 	if (eventSource) {
+// 		eventSource.close()
+// 	}
+// })
 </script>
 
 <style scoped lang="scss">