From b1455cf70f25be8e123ecd61c04a6d2b6bf9ce01 Mon Sep 17 00:00:00 2001 From: v Date: Fri, 29 Aug 2025 22:13:22 +0800 Subject: [PATCH] =?UTF-8?q?up.=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- z_ele/src/plugins/websocket/core/instance.ts | 11 ++++++++ z_ele/src/plugins/websocket/types.ts | 7 +++++ z_ele/src/store/modules/user.ts | 28 +++++++++----------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/z_ele/src/plugins/websocket/core/instance.ts b/z_ele/src/plugins/websocket/core/instance.ts index 42e7a88..8384021 100644 --- a/z_ele/src/plugins/websocket/core/instance.ts +++ b/z_ele/src/plugins/websocket/core/instance.ts @@ -121,6 +121,16 @@ export function createWsInstance(config: WsConfig): WsInstance { socket.value.send(payload); 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 = []); @@ -139,6 +149,7 @@ export function createWsInstance(config: WsConfig): WsInstance { connectSocket, disconnectSocket, sendMessage, + sendSystemMessage, clearMessages }; } diff --git a/z_ele/src/plugins/websocket/types.ts b/z_ele/src/plugins/websocket/types.ts index 291b084..3e32750 100644 --- a/z_ele/src/plugins/websocket/types.ts +++ b/z_ele/src/plugins/websocket/types.ts @@ -5,6 +5,10 @@ export enum WsEvent { CONNECTED = 'CONNECTED', LOCK_CLIENT_SCREEN = 'LOCK_CLIENT_SCREEN' } +export enum WsSystemEvent { + LOGIN = 'login' +} + export enum WsSendEventType { BIND_CONNECT_ID = 'bind_connect_id', @@ -61,6 +65,9 @@ export interface WsInstance { connectSocket: () => void; disconnectSocket: () => void; + /** 发送系统消息(跟踪系统事件) **/ + sendSystemMessage: (event: WsSystemEvent, data: any) => void; + sendMessage: (event: WsSendEventType, data: any) => void; clearMessages: () => void; events: WsEventManager; // 重命名为更清晰的events diff --git a/z_ele/src/store/modules/user.ts b/z_ele/src/store/modules/user.ts index b618686..5aa73f3 100644 --- a/z_ele/src/store/modules/user.ts +++ b/z_ele/src/store/modules/user.ts @@ -9,15 +9,14 @@ import type { User } from '@/api/system/user/model'; import type { Menu } from '@/api/system/menu/model'; import type { DictionaryData } from '@/api/system/dictionary-data/model'; import { getUserInfo } from '@/api/layout'; -import {getClientInfo, getToken} from '@/utils/token-util'; +import { getToken } from '@/utils/token-util'; import { - WsEventContext, wsEventManager, + wsInstance, WsEvent, - WsInstance, - WsSendEventType, - wsInstance + WsSystemEvent } from '@/plugins/websocket'; +import { useClientStore } from '@/store/modules/client'; /** 直接指定菜单数据 */ const USER_MENUS: Menu[] | null = null; @@ -74,18 +73,15 @@ export const useUserStore = defineStore('user', { }) ); this.setMenus(menus); - - console.log('管理器塞事件进去'); - wsEventManager.subscribe(WsEvent.CONNECTED, (sub: WsEventContext) => { - console.log('事件执行, 绑定连接和用户关系'); - // 绑定用户到websocket的connect_id中 - const clientInfo = getClientInfo(); - wsInstance.sendMessage(WsSendEventType.BIND_CONNECT_ID, { + // 订阅客户端连接事件 + wsEventManager.subscribe(WsEvent.CONNECTED, () => { + // Ws登录授权 + const clientStore = useClientStore(); + wsInstance?.sendSystemMessage(WsSystemEvent.LOGIN, { token: getToken(), - userId: result.userId, - clientId: clientInfo.id, - clientName: clientInfo.name, - clientVersion: clientInfo.version + clientId: clientStore.clientId, + clientName: clientStore.clientName, + clientVersion: clientStore.clientVersion }); }); return { menus, homePath };