From 30cafc5619d537dde29bb264a6c79c7d53866089 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 18:04:39 +0800 Subject: [PATCH] =?UTF-8?q?refactor(weChat/ckchat):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BC=9A=E8=AF=9D=E6=9B=B4=E6=96=B0=E9=80=BB=E8=BE=91=E5=B9=B6?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=86=97=E4=BD=99=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复未读消息计数时可能出现的类型问题 - 直接使用chatSessions状态而非getChatSessions方法 - 简化会话更新逻辑,移除不必要的缓存处理 - 使用更简洁的路径引用deepCopy工具 --- .../SidebarMenu/MessageList/index.tsx | 2 +- Cunkebao/src/store/module/ckchat/ckchat.ts | 31 ++++--------------- Cunkebao/src/store/module/weChat/weChat.ts | 4 +-- 3 files changed, 8 insertions(+), 29 deletions(-) diff --git a/Cunkebao/src/pages/pc/ckbox/components/SidebarMenu/MessageList/index.tsx b/Cunkebao/src/pages/pc/ckbox/components/SidebarMenu/MessageList/index.tsx index dc4901e1..6fc7d049 100644 --- a/Cunkebao/src/pages/pc/ckbox/components/SidebarMenu/MessageList/index.tsx +++ b/Cunkebao/src/pages/pc/ckbox/components/SidebarMenu/MessageList/index.tsx @@ -11,7 +11,7 @@ interface MessageListProps {} const MessageList: React.FC = () => { const { setCurrentContact, currentContract } = useWeChatStore(); - const chatSessions = useCkChatStore(state => state.getChatSessions()); + const chatSessions = useCkChatStore(state => state.chatSessions); const onContactClick = (session: ContractData | weChatGroup) => { setCurrentContact(session); }; diff --git a/Cunkebao/src/store/module/ckchat/ckchat.ts b/Cunkebao/src/store/module/ckchat/ckchat.ts index b83cd436..686ee2f9 100644 --- a/Cunkebao/src/store/module/ckchat/ckchat.ts +++ b/Cunkebao/src/store/module/ckchat/ckchat.ts @@ -9,7 +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"; +import { deepCopy } from "@/utils/common"; export const useCkChatStore = createPersistStore( set => ({ @@ -373,39 +373,20 @@ export const useCkChatStore = createPersistStore( : [...state.chatSessions, session as ContractData | weChatGroup], }; }); - // 清除getChatSessions缓存 - const state = useCkChatStore.getState(); - if ( - state.getChatSessions && - typeof state.getChatSessions === "function" - ) { - // 触发缓存重新计算 - state.getChatSessions(); - } }, // 更新聊天会话 updateChatSession: (session: ContractData | weChatGroup) => { - const state = deepCopy(useCkChatStore.getState()); - const newSession = state.chatSessions.map(item => - item.id === session.id ? session : item, - ); - console.log("新数组", newSession); - set({ chatSessions: newSession }); + set(state => ({ + chatSessions: state.chatSessions.map(item => + item.id === session.id ? { ...item, ...session } : item, + ), + })); }, // 删除聊天会话 deleteChatSession: (sessionId: string) => { set(state => ({ chatSessions: state.chatSessions.filter(item => item.id !== sessionId), })); - // 清除getChatSessions缓存 - const state = useCkChatStore.getState(); - if ( - state.getChatSessions && - typeof state.getChatSessions === "function" - ) { - // 触发缓存重新计算 - state.getChatSessions(); - } }, // 设置用户信息 setUserInfo: (userInfo: CkUserInfo) => { diff --git a/Cunkebao/src/store/module/weChat/weChat.ts b/Cunkebao/src/store/module/weChat/weChat.ts index 6280d68c..182fa937 100644 --- a/Cunkebao/src/store/module/weChat/weChat.ts +++ b/Cunkebao/src/store/module/weChat/weChat.ts @@ -88,9 +88,7 @@ export const useWeChatStore = create()( const session = chatSessions.find( item => item.id == message.wechatFriendId, ); - session.unreadCount++; - console.log("新消息", session); - + session.unreadCount = Number(session.unreadCount) + 1; updateChatSession(session); } },