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); } },