From 533c16be3f3314d1aa99f604df1294625a055483 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: Wed, 3 Sep 2025 17:47:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(weChat):=20=E6=B7=BB=E5=8A=A0receivedMsg?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=A4=84=E7=90=86=E6=8E=A5=E6=94=B6=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增receivedMsg方法用于处理微信消息接收逻辑,区分当前会话消息和未读消息更新 优化updateChatSession实现,使用深拷贝避免直接修改状态 调整ckchat.data.ts中方法顺序,保持代码整洁 --- .../src/store/module/ckchat/ckchat.data.ts | 4 +-- Cunkebao/src/store/module/ckchat/ckchat.ts | 20 ++++--------- .../src/store/module/weChat/weChat.data.ts | 1 + Cunkebao/src/store/module/weChat/weChat.ts | 29 ++++++++++++++++++- .../src/store/module/websocket/msgManage.ts | 5 ++-- 5 files changed, 40 insertions(+), 19 deletions(-) diff --git a/Cunkebao/src/store/module/ckchat/ckchat.data.ts b/Cunkebao/src/store/module/ckchat/ckchat.data.ts index f9780ab6..af7f8743 100644 --- a/Cunkebao/src/store/module/ckchat/ckchat.data.ts +++ b/Cunkebao/src/store/module/ckchat/ckchat.data.ts @@ -52,13 +52,13 @@ export interface CkChatState { asyncKfUserList: (data: KfUserListData[]) => void; getKfUserInfo: (wechatAccountId: number) => KfUserListData | undefined; asyncContractList: (data: ContractData[]) => void; + getChatSessions: () => any[]; asyncChatSessions: (data: any[]) => void; + updateChatSession: (session: ContractData | weChatGroup) => void; deleteCtrlUser: (userId: number) => void; updateCtrlUser: (user: KfUserListData) => void; clearkfUserList: () => void; - getChatSessions: () => any[]; addChatSession: (session: any) => void; - updateChatSession: (session: any) => void; deleteChatSession: (sessionId: string) => void; setUserInfo: (userInfo: CkUserInfo) => void; clearUserInfo: () => void; diff --git a/Cunkebao/src/store/module/ckchat/ckchat.ts b/Cunkebao/src/store/module/ckchat/ckchat.ts index 054c4bd5..3bdf173a 100644 --- a/Cunkebao/src/store/module/ckchat/ckchat.ts +++ b/Cunkebao/src/store/module/ckchat/ckchat.ts @@ -9,6 +9,7 @@ import { } from "@/pages/pc/ckbox/data"; import { kfUserService, weChatGroupService, contractService } from "@/utils/db"; import { createContractList } from "@/store/module/ckchat/api"; +import { deepCopy } from "../../../utils/common"; export const useCkChatStore = createPersistStore( set => ({ @@ -384,20 +385,11 @@ export const useCkChatStore = createPersistStore( }, // 更新聊天会话 updateChatSession: (session: ContractData | weChatGroup) => { - set(state => ({ - chatSessions: state.chatSessions.map(item => - item.id === session.id ? session : item, - ), - })); - // 清除getChatSessions缓存 - const state = useCkChatStore.getState(); - if ( - state.getChatSessions && - typeof state.getChatSessions === "function" - ) { - // 触发缓存重新计算 - state.getChatSessions(); - } + const state = deepCopy(useCkChatStore.getState()); + const newSession = state.chatSessions.map(item => + item.id === session.id ? session : item, + ); + set({ chatSessions: newSession }); }, // 删除聊天会话 deleteChatSession: (sessionId: string) => { diff --git a/Cunkebao/src/store/module/weChat/weChat.data.ts b/Cunkebao/src/store/module/weChat/weChat.data.ts index dfd2e103..78dc6508 100644 --- a/Cunkebao/src/store/module/weChat/weChat.data.ts +++ b/Cunkebao/src/store/module/weChat/weChat.data.ts @@ -18,4 +18,5 @@ export interface WeChatState { setVideoLoading: (messageId: number, isLoading: boolean) => void; setVideoUrl: (messageId: number, videoUrl: string) => void; addMessage: (message: ChatRecord) => void; + receivedMsg: (message: ChatRecord) => void; } diff --git a/Cunkebao/src/store/module/weChat/weChat.ts b/Cunkebao/src/store/module/weChat/weChat.ts index fd547121..6280d68c 100644 --- a/Cunkebao/src/store/module/weChat/weChat.ts +++ b/Cunkebao/src/store/module/weChat/weChat.ts @@ -4,7 +4,11 @@ import { getChatMessages } from "@/pages/pc/ckbox/api"; import { WeChatState } from "./weChat.data"; import { clearUnreadCount, updateConfig } from "@/pages/pc/ckbox/api"; import { ContractData, weChatGroup } from "@/pages/pc/ckbox/data"; -import { addChatSession } from "@/store/module/ckchat/ckchat"; +import { + addChatSession, + getChatSessions, + updateChatSession, +} from "@/store/module/ckchat/ckchat"; export const useWeChatStore = create()( persist( @@ -68,6 +72,29 @@ export const useWeChatStore = create()( })); }, + receivedMsg: message => { + const currentContract = useWeChatStore.getState().currentContract; + if ( + currentContract && + currentContract.wechatAccountId == message.wechatAccountId && + currentContract.id == message.wechatFriendId + ) { + set(state => ({ + currentMessages: [...state.currentMessages, message], + })); + } else { + //更新消息列表unread数值,根据接收的++1 这样 + const chatSessions = getChatSessions(); + const session = chatSessions.find( + item => item.id == message.wechatFriendId, + ); + session.unreadCount++; + console.log("新消息", session); + + updateChatSession(session); + } + }, + updateMessage: (messageId, updates) => { set(state => ({ currentMessages: state.currentMessages.map(msg => diff --git a/Cunkebao/src/store/module/websocket/msgManage.ts b/Cunkebao/src/store/module/websocket/msgManage.ts index 3aca7d7a..423fab57 100644 --- a/Cunkebao/src/store/module/websocket/msgManage.ts +++ b/Cunkebao/src/store/module/websocket/msgManage.ts @@ -9,6 +9,8 @@ import { useWeChatStore } from "@/store/module/weChat/weChat"; type MessageHandler = (message: WebSocketMessage) => void; const setVideoUrl = useWeChatStore.getState().setVideoUrl; const addMessage = useWeChatStore.getState().addMessage; +const receivedMsg = useWeChatStore.getState().receivedMsg; + // 消息处理器映射 const messageHandlers: Record = { // 微信账号存活状态响应 @@ -41,9 +43,8 @@ const messageHandlers: Record = { }, //收到消息 CmdNewMessage: (message: Messages) => { - console.log("收到消息", message.friendMessage); // 在这里添加具体的处理逻辑 - addMessage(message.friendMessage); + receivedMsg(message.friendMessage); }, // 登录响应