mirror of
https://gitee.com/JavaLionLi/plus-ui.git
synced 2026-09-13 07:43:43 +08:00
refactor(sse): wire 形态改为 SseEnvelope<T>{msgType, data}
- 'notice' event handler 解析 envelope,从 data 取 title/message - 抽 parseEnvelope 通用函数,校验 msgType 必填
This commit is contained in:
parent
6ac60c7201
commit
5d2d9a6f61
@ -4,12 +4,17 @@ 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).
|
||||
* Strongly-typed SSE envelope shared by every event:
|
||||
* { msgType: "...", data: { ...business fields... } }
|
||||
* Frontend dispatches by msgType, then casts data to the corresponding shape.
|
||||
*/
|
||||
interface NoticePayload {
|
||||
type: string;
|
||||
interface SseEnvelope<T = unknown> {
|
||||
msgType: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
/** data shape under msgType in {LOGIN_WELCOME, WORKFLOW_TASK, SYSTEM_NOTICE} */
|
||||
interface NoticeData {
|
||||
title: string;
|
||||
message: string;
|
||||
}
|
||||
@ -31,29 +36,40 @@ export const initSSE = (url: any) => {
|
||||
connect(url);
|
||||
};
|
||||
|
||||
function parseEnvelope<T = unknown>(raw: string): SseEnvelope<T> | null {
|
||||
try {
|
||||
const env = JSON.parse(raw);
|
||||
if (env && typeof env === 'object' && typeof env.msgType === 'string') {
|
||||
return env as SseEnvelope<T>;
|
||||
}
|
||||
console.warn('SSE: payload missing msgType', raw);
|
||||
return null;
|
||||
} catch (err) {
|
||||
console.warn('SSE: invalid JSON envelope', raw, err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function connect(url: string) {
|
||||
const fullUrl = url + '?Authorization=Bearer ' + getToken() + '&clientid=' + import.meta.env.VITE_APP_CLIENT_ID;
|
||||
eventSource = new EventSource(fullUrl);
|
||||
|
||||
// "notice" event: login welcome / workflow / system announcement — strongly-typed JSON payload
|
||||
// "notice" event: login welcome / workflow task / system announcement.
|
||||
// All three share NoticeData shape; msgType differentiates the source.
|
||||
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;
|
||||
}
|
||||
const env = parseEnvelope<NoticeData>(e.data);
|
||||
if (!env) return;
|
||||
const { title, message } = env.data;
|
||||
useNoticeStore().addNotice({
|
||||
title: payload.title,
|
||||
message: payload.message,
|
||||
title,
|
||||
message,
|
||||
read: false,
|
||||
time: new Date().toLocaleString()
|
||||
});
|
||||
ElNotification({
|
||||
title: payload.title,
|
||||
message: payload.message,
|
||||
title,
|
||||
message,
|
||||
type: 'success',
|
||||
duration: 3000
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user