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 6fc7d049..dfaab7e4 100644 --- a/Cunkebao/src/pages/pc/ckbox/components/SidebarMenu/MessageList/index.tsx +++ b/Cunkebao/src/pages/pc/ckbox/components/SidebarMenu/MessageList/index.tsx @@ -13,7 +13,7 @@ const MessageList: React.FC = () => { const { setCurrentContact, currentContract } = useWeChatStore(); const chatSessions = useCkChatStore(state => state.chatSessions); const onContactClick = (session: ContractData | weChatGroup) => { - setCurrentContact(session); + setCurrentContact(session, true); }; return (
diff --git a/Cunkebao/src/store/module/weChat/weChat.data.ts b/Cunkebao/src/store/module/weChat/weChat.data.ts index 78dc6508..a63ff292 100644 --- a/Cunkebao/src/store/module/weChat/weChat.data.ts +++ b/Cunkebao/src/store/module/weChat/weChat.data.ts @@ -11,7 +11,10 @@ export interface WeChatState { messagesLoading: boolean; // Actions - setCurrentContact: (contract: ContractData | weChatGroup) => void; + setCurrentContact: ( + contract: ContractData | weChatGroup, + isExist?: boolean, + ) => void; loadChatMessages: (contact: ContractData | weChatGroup) => Promise; // 视频消息处理方法 diff --git a/Cunkebao/src/store/module/weChat/weChat.ts b/Cunkebao/src/store/module/weChat/weChat.ts index 30d65ead..914f5e71 100644 --- a/Cunkebao/src/store/module/weChat/weChat.ts +++ b/Cunkebao/src/store/module/weChat/weChat.ts @@ -6,8 +6,8 @@ import { clearUnreadCount, updateConfig } from "@/pages/pc/ckbox/api"; import { ContractData, weChatGroup } from "@/pages/pc/ckbox/data"; import { addChatSession, - getChatSessions, updateChatSession, + useCkChatStore, } from "@/store/module/ckchat/ckchat"; export const useWeChatStore = create()( @@ -19,12 +19,19 @@ export const useWeChatStore = create()( messagesLoading: false, // Actions - setCurrentContact: (contract: ContractData | weChatGroup) => { + setCurrentContact: ( + contract: ContractData | weChatGroup, + isExist?: boolean, + ) => { const state = useWeChatStore.getState(); // 切换联系人时清空当前消息,等待重新加载 set({ currentMessages: [] }); clearUnreadCount([contract.id]).then(() => { - addChatSession(contract); + if (isExist) { + updateChatSession({ ...contract, unreadCount: 0 }); + } else { + addChatSession(contract); + } set({ currentContract: contract }); updateConfig({ id: contract.id, @@ -83,12 +90,14 @@ export const useWeChatStore = create()( })); } else { //更新消息列表unread数值,根据接收的++1 这样 - const chatSessions = getChatSessions(); + const chatSessions = useCkChatStore.getState().chatSessions; const session = chatSessions.find( item => item.id == message.wechatFriendId, ); - session.unreadCount = Number(session.unreadCount) + 1; - updateChatSession(session); + if (session) { + session.unreadCount = Number(session.unreadCount) + 1; + updateChatSession(session); + } } },