update 消息推送增加 消息类型 消息来源 前端跳转路径等扩展参数
This commit is contained in:
47
src/utils/push-message.ts
Normal file
47
src/utils/push-message.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
export const PUSH_MESSAGE_TYPE = {
|
||||
MESSAGE: 'message',
|
||||
NOTICE: 'notice',
|
||||
LLM: 'llm',
|
||||
CUSTOM: 'custom'
|
||||
} as const;
|
||||
|
||||
export interface PushMessagePayload {
|
||||
type?: string;
|
||||
source?: string;
|
||||
message?: string;
|
||||
data?: Record<string, any> | null;
|
||||
path?: string;
|
||||
query?: Record<string, any> | null;
|
||||
timestamp?: number;
|
||||
}
|
||||
|
||||
const MESSAGE_CENTER_TYPES = new Set<string>([PUSH_MESSAGE_TYPE.MESSAGE, PUSH_MESSAGE_TYPE.NOTICE]);
|
||||
|
||||
export const parsePushMessage = (raw: string): PushMessagePayload => {
|
||||
try {
|
||||
const payload = JSON.parse(raw) as PushMessagePayload;
|
||||
return {
|
||||
type: payload.type ?? PUSH_MESSAGE_TYPE.MESSAGE,
|
||||
source: payload.source ?? 'backend',
|
||||
message: payload.message ?? '',
|
||||
data: payload.data ?? null,
|
||||
path: payload.path,
|
||||
query: payload.query ?? null,
|
||||
timestamp: payload.timestamp ?? Date.now()
|
||||
};
|
||||
} catch {
|
||||
return {
|
||||
type: PUSH_MESSAGE_TYPE.MESSAGE,
|
||||
source: 'backend',
|
||||
message: raw,
|
||||
data: null,
|
||||
path: undefined,
|
||||
query: null,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const shouldAppendNotice = (payload: PushMessagePayload) => {
|
||||
return MESSAGE_CENTER_TYPES.has(payload.type ?? PUSH_MESSAGE_TYPE.MESSAGE);
|
||||
};
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getToken } from '@/utils/auth';
|
||||
import { ElNotification } from 'element-plus';
|
||||
import { useNoticeStore } from '@/store/modules/notice';
|
||||
import { parsePushMessage, PUSH_MESSAGE_TYPE, shouldAppendNotice } from '@/utils/push-message';
|
||||
|
||||
// 初始化
|
||||
export const initSSE = (url: any) => {
|
||||
@@ -26,17 +27,27 @@ export const initSSE = (url: any) => {
|
||||
|
||||
watch(data, () => {
|
||||
if (!data.value) return;
|
||||
useNoticeStore().addNotice({
|
||||
message: data.value,
|
||||
read: false,
|
||||
time: new Date().toLocaleString()
|
||||
});
|
||||
ElNotification({
|
||||
title: '消息',
|
||||
message: data.value,
|
||||
type: 'success',
|
||||
duration: 3000
|
||||
});
|
||||
const payload = parsePushMessage(data.value);
|
||||
if (shouldAppendNotice(payload)) {
|
||||
useNoticeStore().addNotice({
|
||||
title: payload.type === PUSH_MESSAGE_TYPE.NOTICE ? '通知公告' : '系统消息',
|
||||
type: payload.type,
|
||||
source: payload.source,
|
||||
message: payload.message ?? '',
|
||||
content: payload.data?.noticeContent,
|
||||
data: payload.data,
|
||||
path: payload.path,
|
||||
query: payload.query,
|
||||
read: false,
|
||||
time: new Date(payload.timestamp ?? Date.now()).toLocaleString()
|
||||
});
|
||||
ElNotification({
|
||||
title: payload.type === PUSH_MESSAGE_TYPE.NOTICE ? '通知公告' : '消息',
|
||||
message: payload.message ?? '',
|
||||
type: 'success',
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
data.value = null;
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { getToken } from '@/utils/auth';
|
||||
import { ElNotification } from 'element-plus';
|
||||
import { useNoticeStore } from '@/store/modules/notice';
|
||||
import { parsePushMessage, PUSH_MESSAGE_TYPE, shouldAppendNotice } from '@/utils/push-message';
|
||||
|
||||
// 初始化socket
|
||||
export const initWebSocket = (url: any) => {
|
||||
@@ -32,20 +33,30 @@ export const initWebSocket = (url: any) => {
|
||||
console.log('websocket已经断开');
|
||||
},
|
||||
onMessage: (_, e) => {
|
||||
if (e.data.indexOf('ping') > 0) {
|
||||
if (typeof e.data === 'string' && e.data.includes('ping')) {
|
||||
return;
|
||||
}
|
||||
useNoticeStore().addNotice({
|
||||
message: e.data,
|
||||
read: false,
|
||||
time: new Date().toLocaleString()
|
||||
});
|
||||
ElNotification({
|
||||
title: '消息',
|
||||
message: e.data,
|
||||
type: 'success',
|
||||
duration: 3000
|
||||
});
|
||||
const payload = parsePushMessage(String(e.data));
|
||||
if (shouldAppendNotice(payload)) {
|
||||
useNoticeStore().addNotice({
|
||||
title: payload.type === PUSH_MESSAGE_TYPE.NOTICE ? '通知公告' : '系统消息',
|
||||
type: payload.type,
|
||||
source: payload.source,
|
||||
message: payload.message ?? '',
|
||||
content: payload.data?.noticeContent,
|
||||
data: payload.data,
|
||||
path: payload.path,
|
||||
query: payload.query,
|
||||
read: false,
|
||||
time: new Date(payload.timestamp ?? Date.now()).toLocaleString()
|
||||
});
|
||||
ElNotification({
|
||||
title: payload.type === PUSH_MESSAGE_TYPE.NOTICE ? '通知公告' : '消息',
|
||||
message: payload.message ?? '',
|
||||
type: 'success',
|
||||
duration: 3000
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user