From ac53861a858cb9a882761e1840b31b8e55f81256 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B6=85=E7=BA=A7=E8=80=81=E7=99=BD=E5=85=94?= Date: Thu, 18 Sep 2025 15:25:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor(websocket):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=E5=99=A8=E5=B9=B6=E6=B8=85?= =?UTF-8?q?=E7=90=86=E8=AE=A4=E8=AF=81=E5=A4=B1=E8=B4=A5=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除未使用的WebSocket状态检查代码 - 重新组织导入语句顺序 - 改进CmdNotify处理器,在认证失败时清除所有本地存储和数据库 - 将消息处理器调整为异步函数以支持数据库操作 --- Touchkebao/src/pages/pc/ckbox/weChat/main.ts | 2 +- .../src/store/module/websocket/msgManage.ts | 33 +++++++++++-------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/Touchkebao/src/pages/pc/ckbox/weChat/main.ts b/Touchkebao/src/pages/pc/ckbox/weChat/main.ts index 61b2bfaa..3a017b35 100644 --- a/Touchkebao/src/pages/pc/ckbox/weChat/main.ts +++ b/Touchkebao/src/pages/pc/ckbox/weChat/main.ts @@ -103,7 +103,7 @@ export const chatInitAPIdata = async () => { //发起soket连接 export const initSocket = () => { // 检查WebSocket是否已经连接 - const { status } = useWebSocketStore.getState(); + // const { status } = useWebSocketStore.getState(); // 如果已经连接或正在连接,则不重复连接 // if (["connected", "connecting"].includes(status)) { diff --git a/Touchkebao/src/store/module/websocket/msgManage.ts b/Touchkebao/src/store/module/websocket/msgManage.ts index 054f7f7d..49f466c7 100644 --- a/Touchkebao/src/store/module/websocket/msgManage.ts +++ b/Touchkebao/src/store/module/websocket/msgManage.ts @@ -1,9 +1,10 @@ //消息管理器 -import { deepCopy } from "@/utils/common"; -import { WebSocketMessage } from "./websocket"; import { getkfUserList, asyncKfUserList } from "@/store/module/ckchat/ckchat"; -import { Messages } from "./msg.data"; import { useWeChatStore } from "../weChat/weChat"; +import { WebSocketMessage } from "./websocket"; +import { deepCopy } from "@/utils/common"; +import { Messages } from "./msg.data"; +import { db } from "@/utils/db"; // 消息处理器类型定义 type MessageHandler = (message: WebSocketMessage) => void; const addMessage = useWeChatStore.getState().addMessage; @@ -65,16 +66,6 @@ const messageHandlers: Record = { // 在这里添加具体的处理逻辑 }, - // 通知消息 - CmdNotify: message => { - console.log("通知消息", message); - // 在这里添加具体的处理逻辑 - if (message.notify == "Kicked out") { - // 被踢出时直接跳转到登录页面 - window.location.href = "/login"; - } - }, - CmdDownloadVideoResult: message => { // 在这里添加具体的处理逻辑 console.log("视频下载结果:", message); @@ -82,11 +73,25 @@ const messageHandlers: Record = { }, CmdFetchMomentResult: message => { - console.log("朋友圈数据结果", message); addMomentCommon(message.result); updateMomentCommonLoading(false); }, + CmdNotify: async (message: WebSocketMessage) => { + console.log("通知消息", message); + // 在这里添加具体的处理逻辑 + if (message.notify == "Auth failed") { + // 被踢出时删除所有缓存数据 + localStorage.clear(); + // 删除 + await db.kfUsers.clear(); + await db.weChatGroup.clear(); + await db.contracts.clear(); + await db.newContractList.clear(); + window.location.href = "/login"; + } + }, + // 可以继续添加更多处理器... };