up. 优化代码内容

This commit is contained in:
扶桑花间 2025-08-29 22:13:22 +08:00
parent f5d587a276
commit b1455cf70f
3 changed files with 30 additions and 16 deletions

View File

@ -121,6 +121,16 @@ export function createWsInstance(config: WsConfig): WsInstance {
socket.value.send(payload); socket.value.send(payload);
addMessage(payload, WsMessageType.OUTGOING); addMessage(payload, WsMessageType.OUTGOING);
}; };
/**
*
* @param event
* @param data
*/
const sendSystemMessage = (event: WsSendEventType, data: any) => {
if (!socket.value || !isConnected.value || !data) return;
const payload = JSON.stringify({ type: 'system', event, data });
socket.value.send(payload);
};
const clearMessages = () => (messages.value = []); const clearMessages = () => (messages.value = []);
@ -139,6 +149,7 @@ export function createWsInstance(config: WsConfig): WsInstance {
connectSocket, connectSocket,
disconnectSocket, disconnectSocket,
sendMessage, sendMessage,
sendSystemMessage,
clearMessages clearMessages
}; };
} }

View File

@ -5,6 +5,10 @@ export enum WsEvent {
CONNECTED = 'CONNECTED', CONNECTED = 'CONNECTED',
LOCK_CLIENT_SCREEN = 'LOCK_CLIENT_SCREEN' LOCK_CLIENT_SCREEN = 'LOCK_CLIENT_SCREEN'
} }
export enum WsSystemEvent {
LOGIN = 'login'
}
export enum WsSendEventType { export enum WsSendEventType {
BIND_CONNECT_ID = 'bind_connect_id', BIND_CONNECT_ID = 'bind_connect_id',
@ -61,6 +65,9 @@ export interface WsInstance {
connectSocket: () => void; connectSocket: () => void;
disconnectSocket: () => void; disconnectSocket: () => void;
/** 发送系统消息(跟踪系统事件) **/
sendSystemMessage: (event: WsSystemEvent, data: any) => void;
sendMessage: (event: WsSendEventType, data: any) => void; sendMessage: (event: WsSendEventType, data: any) => void;
clearMessages: () => void; clearMessages: () => void;
events: WsEventManager; // 重命名为更清晰的events events: WsEventManager; // 重命名为更清晰的events

View File

@ -9,15 +9,14 @@ import type { User } from '@/api/system/user/model';
import type { Menu } from '@/api/system/menu/model'; import type { Menu } from '@/api/system/menu/model';
import type { DictionaryData } from '@/api/system/dictionary-data/model'; import type { DictionaryData } from '@/api/system/dictionary-data/model';
import { getUserInfo } from '@/api/layout'; import { getUserInfo } from '@/api/layout';
import {getClientInfo, getToken} from '@/utils/token-util'; import { getToken } from '@/utils/token-util';
import { import {
WsEventContext,
wsEventManager, wsEventManager,
wsInstance,
WsEvent, WsEvent,
WsInstance, WsSystemEvent
WsSendEventType,
wsInstance
} from '@/plugins/websocket'; } from '@/plugins/websocket';
import { useClientStore } from '@/store/modules/client';
/** 直接指定菜单数据 */ /** 直接指定菜单数据 */
const USER_MENUS: Menu[] | null = null; const USER_MENUS: Menu[] | null = null;
@ -74,18 +73,15 @@ export const useUserStore = defineStore('user', {
}) })
); );
this.setMenus(menus); this.setMenus(menus);
// 订阅客户端连接事件
console.log('管理器塞事件进去'); wsEventManager.subscribe(WsEvent.CONNECTED, () => {
wsEventManager.subscribe(WsEvent.CONNECTED, (sub: WsEventContext) => { // Ws登录授权
console.log('事件执行, 绑定连接和用户关系'); const clientStore = useClientStore();
// 绑定用户到websocket的connect_id中 wsInstance?.sendSystemMessage(WsSystemEvent.LOGIN, {
const clientInfo = getClientInfo();
wsInstance.sendMessage(WsSendEventType.BIND_CONNECT_ID, {
token: getToken(), token: getToken(),
userId: result.userId, clientId: clientStore.clientId,
clientId: clientInfo.id, clientName: clientStore.clientName,
clientName: clientInfo.name, clientVersion: clientStore.clientVersion
clientVersion: clientInfo.version
}); });
}); });
return { menus, homePath }; return { menus, homePath };