mirror of
https://gitee.com/JavaLionLi/plus-ui.git
synced 2026-09-16 00:38:19 +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';
|
import { getSseEventBus } from '@/utils/sseEventBus';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Strongly-typed payload of the unified "notice" SSE event.
|
* Strongly-typed SSE envelope shared by every event:
|
||||||
* Backend sends one of: LOGIN_WELCOME, WORKFLOW_TASK, SYSTEM_NOTICE
|
* { msgType: "...", data: { ...business fields... } }
|
||||||
* (all share the same shape — type field for dispatching, title + message for display).
|
* Frontend dispatches by msgType, then casts data to the corresponding shape.
|
||||||
*/
|
*/
|
||||||
interface NoticePayload {
|
interface SseEnvelope<T = unknown> {
|
||||||
type: string;
|
msgType: string;
|
||||||
|
data: T;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** data shape under msgType in {LOGIN_WELCOME, WORKFLOW_TASK, SYSTEM_NOTICE} */
|
||||||
|
interface NoticeData {
|
||||||
title: string;
|
title: string;
|
||||||
message: string;
|
message: string;
|
||||||
}
|
}
|
||||||
@ -31,29 +36,40 @@ export const initSSE = (url: any) => {
|
|||||||
connect(url);
|
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) {
|
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);
|
||||||
|
|
||||||
// "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) => {
|
eventSource.addEventListener('notice', (e: MessageEvent) => {
|
||||||
if (!e.data) return;
|
if (!e.data) return;
|
||||||
let payload: NoticePayload;
|
const env = parseEnvelope<NoticeData>(e.data);
|
||||||
try {
|
if (!env) return;
|
||||||
payload = JSON.parse(e.data);
|
const { title, message } = env.data;
|
||||||
} catch (err) {
|
|
||||||
console.warn('SSE notice: invalid JSON payload', e.data, err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
useNoticeStore().addNotice({
|
useNoticeStore().addNotice({
|
||||||
title: payload.title,
|
title,
|
||||||
message: payload.message,
|
message,
|
||||||
read: false,
|
read: false,
|
||||||
time: new Date().toLocaleString()
|
time: new Date().toLocaleString()
|
||||||
});
|
});
|
||||||
ElNotification({
|
ElNotification({
|
||||||
title: payload.title,
|
title,
|
||||||
message: payload.message,
|
message,
|
||||||
type: 'success',
|
type: 'success',
|
||||||
duration: 3000
|
duration: 3000
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user