refactor(websocket): 优化消息处理器并清理认证失败时的缓存数据

- 移除未使用的WebSocket状态检查代码
- 重新组织导入语句顺序
- 改进CmdNotify处理器,在认证失败时清除所有本地存储和数据库
- 将消息处理器调整为异步函数以支持数据库操作
This commit is contained in:
超级老白兔
2025-09-18 15:25:33 +08:00
parent 85c73a5069
commit ac53861a85
2 changed files with 20 additions and 15 deletions

View File

@@ -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)) {

View File

@@ -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<string, MessageHandler> = {
// 在这里添加具体的处理逻辑
},
// 通知消息
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<string, MessageHandler> = {
},
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";
}
},
// 可以继续添加更多处理器...
};