refactor(sse)!: 'message' event → 'notice' event with strongly-typed JSON payload

- 'notice' event handler parses JSON {type, title, message} → noticeStore + ElNotification
- Backend 老 3 处调用全部改为发 NoticePayload 强类型(LOGIN_WELCOME/WORKFLOW_TASK/SYSTEM_NOTICE),前端按 type 字段二次分发预留
This commit is contained in:
i548450 2026-06-09 12:21:21 +08:00
parent 0d03699fcf
commit 6ac60c7201

View File

@ -3,6 +3,17 @@ import { ElNotification } from 'element-plus';
import { useNoticeStore } from '@/store/modules/notice'; import { useNoticeStore } from '@/store/modules/notice';
import { getSseEventBus } from '@/utils/sseEventBus'; 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. // Business-level SSE event names forwarded to the global event bus.
// Feature modules subscribe via getSseEventBus().on('xxx', handler). // Feature modules subscribe via getSseEventBus().on('xxx', handler).
const FORWARD_EVENTS = ['customerservice']; 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; const fullUrl = url + '?Authorization=Bearer ' + getToken() + '&clientid=' + import.meta.env.VITE_APP_CLIENT_ID;
eventSource = new EventSource(fullUrl); eventSource = new EventSource(fullUrl);
// Legacy "message" event: keep existing notification behavior // "notice" event: login welcome / workflow / system announcement — strongly-typed JSON payload
eventSource.addEventListener('message', (e: MessageEvent) => { eventSource.addEventListener('notice', (e: MessageEvent) => {
if (!e.data) return; 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({ useNoticeStore().addNotice({
message: e.data, title: payload.title,
message: payload.message,
read: false, read: false,
time: new Date().toLocaleString() time: new Date().toLocaleString()
}); });
ElNotification({ ElNotification({
title: '消息', title: payload.title,
message: e.data, message: payload.message,
type: 'success', type: 'success',
duration: 3000 duration: 3000
}); });