diff --git a/src/utils/sse.ts b/src/utils/sse.ts index 5669c477..d1463cc2 100644 --- a/src/utils/sse.ts +++ b/src/utils/sse.ts @@ -3,6 +3,17 @@ import { ElNotification } from 'element-plus'; import { useNoticeStore } from '@/store/modules/notice'; import { getSseEventBus } from '@/utils/sseEventBus'; +/** + * Strongly-typed payload of the unified "notice" SSE event. + * Backend sends one of: LOGIN_WELCOME, WORKFLOW_TASK, SYSTEM_NOTICE + * (all share the same shape — type field for dispatching, title + message for display). + */ +interface NoticePayload { + type: string; + title: string; + message: string; +} + // Business-level SSE event names forwarded to the global event bus. // Feature modules subscribe via getSseEventBus().on('xxx', handler). const FORWARD_EVENTS = ['customerservice']; @@ -24,17 +35,25 @@ function connect(url: string) { const fullUrl = url + '?Authorization=Bearer ' + getToken() + '&clientid=' + import.meta.env.VITE_APP_CLIENT_ID; eventSource = new EventSource(fullUrl); - // Legacy "message" event: keep existing notification behavior - eventSource.addEventListener('message', (e: MessageEvent) => { + // "notice" event: login welcome / workflow / system announcement — strongly-typed JSON payload + eventSource.addEventListener('notice', (e: MessageEvent) => { if (!e.data) return; + let payload: NoticePayload; + try { + payload = JSON.parse(e.data); + } catch (err) { + console.warn('SSE notice: invalid JSON payload', e.data, err); + return; + } useNoticeStore().addNotice({ - message: e.data, + title: payload.title, + message: payload.message, read: false, time: new Date().toLocaleString() }); ElNotification({ - title: '消息', - message: e.data, + title: payload.title, + message: payload.message, type: 'success', duration: 3000 });