diff --git a/src/composables/sse/useOnlineCount.ts b/src/composables/sse/useOnlineCount.ts deleted file mode 100644 index edcd1e48..00000000 --- a/src/composables/sse/useOnlineCount.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { ref, onMounted, getCurrentInstance } from "vue"; -import { useSse } from "./useSse"; - -let globalInstance: ReturnType | null = null; - -function createOnlineCountComposable() { - const onlineUserCount = ref(0); - const lastUpdateTime = ref(0); - - const sse = useSse(); - - let unsubscribe: (() => void) | null = null; - - // 处理在线人数变更消息 - const handleOnlineCountMessage = (count: number) => { - if (!Number.isFinite(count) || count < 0) return; - onlineUserCount.value = count; - lastUpdateTime.value = Date.now(); - }; - - const initialize = () => { - sse.connect(); - unsubscribe = sse.on("online-count", handleOnlineCountMessage); - }; - - const cleanup = () => { - if (unsubscribe) { - unsubscribe(); - unsubscribe = null; - } - onlineUserCount.value = 0; - lastUpdateTime.value = 0; - }; - - return { - onlineUserCount: readonly(onlineUserCount), - lastUpdateTime: readonly(lastUpdateTime), - isConnected: sse.isConnected, - connectionState: sse.connectionState, - initialize, - cleanup, - }; -} - -/** - * 在线用户计数组合式函数(单例模式) - */ -export function useOnlineCount(options: { autoInit?: boolean } = {}) { - const { autoInit = true } = options; - - if (!globalInstance) { - globalInstance = createOnlineCountComposable(); - } - - const instance = getCurrentInstance(); - if (autoInit && instance) { - onMounted(() => { - if (!globalInstance!.isConnected.value) { - globalInstance!.initialize(); - } - }); - } - - return globalInstance; -} diff --git a/src/views/dashboard/index.vue b/src/views/dashboard/index.vue index d5abb43c..cef08438 100644 --- a/src/views/dashboard/index.vue +++ b/src/views/dashboard/index.vue @@ -261,11 +261,11 @@ import { Document, VideoPlay, } from "@element-plus/icons-vue"; -import { useOnlineCount } from "@/composables"; +import { useOnlineUsers } from "@/composables"; const userStore = useUserStore(); const settingsStore = useSettingsStore(); -const { onlineUserCount, isConnected } = useOnlineCount(); +const { onlineUserCount, isConnected } = useOnlineUsers(); const hours = new Date().getHours(); const greetings = computed(() => { diff --git a/src/views/demo/dict-sync.vue b/src/views/demo/dict-sync.vue index 52b8e838..8da486ad 100644 --- a/src/views/demo/dict-sync.vue +++ b/src/views/demo/dict-sync.vue @@ -144,7 +144,8 @@ import { useDictStoreHook } from "@/stores/dict"; import { useDateFormat } from "@vueuse/core"; import DictAPI from "@/api/system/dict"; import type { DictItemForm } from "@/api/system/dict"; -import { useDictSync, DictMessage } from "@/composables"; +import { useDictSync } from "@/composables"; +import type { DictChangeMessage } from "@/composables"; // 性别字典编码 const DICT_CODE = "gender"; @@ -186,7 +187,7 @@ const setupSse = () => { dictSse.initialize(); // 注册字典消息回调 - unregisterCallback = dictSse.onDictChange((message: DictMessage) => { + unregisterCallback = dictSse.onDictChange((message: DictChangeMessage) => { // 只有当消息是关于性别字典的更新时才处理 if (message.dictCode === DICT_CODE) { // 更新最后更新时间