新增IM聊天

This commit is contained in:
geht
2026-05-28 14:37:05 +08:00
parent 99e574f600
commit 3539eab924
35 changed files with 2864 additions and 36 deletions

View File

@@ -2,16 +2,55 @@
import { unref } from 'vue';
import { useWebSocket, WebSocketResult } from '@vueuse/core';
import md5 from 'crypto-js/md5';
import { getToken } from '/@/utils/auth';
import { useGlobSetting } from '/@/hooks/setting';
import { useUserStore } from '/@/store/modules/user';
let result: WebSocketResult<any>;
const listeners = new Map();
let connectedUrl = '';
/**
* 构建系统 WebSocket 地址(含 context-path如 /jeecg-boot
*/
export function buildSystemWebSocketUrl(): string {
const glob = useGlobSetting();
const userStore = useUserStore();
const userInfo = unref(userStore.getUserInfo);
if (!userInfo?.id) {
return '';
}
const token = getToken() || '';
const wsClientId = md5(token).toString();
const wsUserId = `${userInfo.id}_${wsClientId}`;
let base = (glob.domainUrl || '').replace('https://', 'wss://').replace('http://', 'ws://');
base = base.replace(/\/$/, '');
const apiPath = (glob.apiUrl || '/jeecg-boot').replace(/\/$/, '');
const prefix = apiPath.startsWith('/') ? apiPath : `/${apiPath}`;
return `${base}${prefix}/websocket/${wsUserId}`;
}
/**
* 确保 WebSocket 已连接(聊天页等场景可主动调用)
*/
export function ensureWebSocketConnected(): void {
const url = buildSystemWebSocketUrl();
if (!url) {
return;
}
if (result?.status?.value === 'OPEN' && connectedUrl === url) {
return;
}
connectWebSocket(url);
}
/**
* 开启 WebSocket 链接,全局只需执行一次
* @param url
*/
export function connectWebSocket(url: string) {
connectedUrl = url;
// 代码逻辑说明: v2.4.6 的 websocket 服务端,存在性能和安全问题。 #3278
const token = (getToken() || '') as string;
result = useWebSocket(url, {