fix: 🐛 解决WebSocket模块导入冲突问题
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import { useDictSync } from "@/composables/useDictSync";
|
import { useDictSync } from "@/composables/useDictSync";
|
||||||
import { Auth } from "@/utils/auth";
|
import { Auth } from "@/utils/auth";
|
||||||
import { useUserStore } from "@/store";
|
// 不直接导入 store 或 userStore
|
||||||
import { watch } from "vue";
|
|
||||||
|
|
||||||
// 全局 WebSocket 实例管理
|
// 全局 WebSocket 实例管理
|
||||||
const websocketInstances = new Map<string, any>();
|
const websocketInstances = new Map<string, any>();
|
||||||
@@ -73,9 +72,6 @@ export function setupWebSocket() {
|
|||||||
// 在窗口关闭前断开WebSocket连接
|
// 在窗口关闭前断开WebSocket连接
|
||||||
window.addEventListener("beforeunload", handleWindowClose);
|
window.addEventListener("beforeunload", handleWindowClose);
|
||||||
|
|
||||||
// 监听用户注销事件
|
|
||||||
watchUserLogout();
|
|
||||||
|
|
||||||
console.log("[WebSocketPlugin] WebSocket服务初始化完成");
|
console.log("[WebSocketPlugin] WebSocket服务初始化完成");
|
||||||
isInitialized = true;
|
isInitialized = true;
|
||||||
}, 1000); // 延迟1秒初始化
|
}, 1000); // 延迟1秒初始化
|
||||||
@@ -92,26 +88,6 @@ function handleWindowClose() {
|
|||||||
cleanupWebSocket();
|
cleanupWebSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 监听用户注销
|
|
||||||
*/
|
|
||||||
function watchUserLogout() {
|
|
||||||
const userStore = useUserStore();
|
|
||||||
|
|
||||||
// 监听用户信息变化,当用户信息被清空时断开连接
|
|
||||||
watch(
|
|
||||||
() => userStore.userInfo,
|
|
||||||
(newUserInfo, oldUserInfo) => {
|
|
||||||
// 从有用户信息变为无用户信息,说明用户注销了
|
|
||||||
if (oldUserInfo?.username && !newUserInfo?.username) {
|
|
||||||
console.log("[WebSocketPlugin] 检测到用户注销,断开WebSocket连接");
|
|
||||||
cleanupWebSocket();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{ deep: true }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清理WebSocket连接
|
* 清理WebSocket连接
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { Auth } from "@/utils/auth";
|
|||||||
import { usePermissionStoreHook } from "@/store/modules/permission.store";
|
import { usePermissionStoreHook } from "@/store/modules/permission.store";
|
||||||
import { useDictStoreHook } from "@/store/modules/dict.store";
|
import { useDictStoreHook } from "@/store/modules/dict.store";
|
||||||
import { useTagsViewStore } from "@/store";
|
import { useTagsViewStore } from "@/store";
|
||||||
|
import { cleanupWebSocket } from "@/plugins/websocket";
|
||||||
|
|
||||||
export const useUserStore = defineStore("user", () => {
|
export const useUserStore = defineStore("user", () => {
|
||||||
const userInfo = useStorage<UserInfo>("userInfo", {} as UserInfo);
|
const userInfo = useStorage<UserInfo>("userInfo", {} as UserInfo);
|
||||||
@@ -91,15 +92,8 @@ export const useUserStore = defineStore("user", () => {
|
|||||||
useTagsViewStore().delAllViews();
|
useTagsViewStore().delAllViews();
|
||||||
|
|
||||||
// 3. 清理 WebSocket 连接
|
// 3. 清理 WebSocket 连接
|
||||||
// 动态导入避免循环依赖
|
cleanupWebSocket();
|
||||||
import("@/plugins/websocket")
|
console.log("[UserStore] WebSocket connections cleaned up");
|
||||||
.then(({ cleanupWebSocket }) => {
|
|
||||||
cleanupWebSocket();
|
|
||||||
console.log("[UserStore] WebSocket connections cleaned up");
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error("[UserStore] Failed to cleanup WebSocket:", error);
|
|
||||||
});
|
|
||||||
|
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user