mirror of
https://gitee.com/JavaLionLi/plus-ui.git
synced 2026-09-13 07:43:43 +08:00
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:
parent
0d03699fcf
commit
6ac60c7201
@ -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
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user