diff --git a/Cunkebao/src/store/module/ckchat/ckchat.ts b/Cunkebao/src/store/module/ckchat/ckchat.ts index a4810c99..bc816756 100644 --- a/Cunkebao/src/store/module/ckchat/ckchat.ts +++ b/Cunkebao/src/store/module/ckchat/ckchat.ts @@ -7,7 +7,12 @@ import { KfUserListData, ContactGroupByLabel, } from "@/pages/pc/ckbox/data"; -import { kfUserService, weChatGroupService, contractService } from "@/utils/db"; +import { + kfUserService, + weChatGroupService, + contractService, + messageListService, +} from "@/utils/db"; export const useCkChatStore = createPersistStore( set => ({ userInfo: null, @@ -28,6 +33,15 @@ export const useCkChatStore = createPersistStore( }, asyncKfSelected: (data: number) => { set({ kfSelected: data }); + // 清除getChatSessions缓存 + const state = useCkChatStore.getState(); + if ( + state.getChatSessions && + typeof state.getChatSessions === "function" + ) { + // 触发缓存重新计算 + state.getChatSessions(); + } }, // 异步设置会话列表 asyncNewContractList: (data: ContactGroupByLabel[]) => { @@ -40,6 +54,15 @@ export const useCkChatStore = createPersistStore( // 异步设置会话列表 asyncChatSessions: data => { set({ chatSessions: data }); + // 清除getChatSessions缓存 + const state = useCkChatStore.getState(); + if ( + state.getChatSessions && + typeof state.getChatSessions === "function" + ) { + // 触发缓存重新计算 + state.getChatSessions(); + } }, // 异步设置联系人列表 asyncContractList: async (data: ContractData[]) => { @@ -77,17 +100,35 @@ export const useCkChatStore = createPersistStore( clearkfUserList: () => { set({ kfUserList: [] }); }, - // 获取聊天会话 - getChatSessions: () => { - const state = useCkChatStore.getState(); - if (state.kfSelected != 0) { - return state.chatSessions.filter( - item => item.wechatAccountId === state.kfSelected, - ); - } else { - return state.chatSessions; - } - }, + // 获取聊天会话 - 使用缓存避免无限循环 + getChatSessions: (() => { + let cachedResult: any = null; + let lastKfSelected: number | null = null; + let lastChatSessionsLength: number = 0; + + return () => { + const state = useCkChatStore.getState(); + + // 检查是否需要重新计算缓存 + const shouldRecalculate = + cachedResult === null || + lastKfSelected !== state.kfSelected || + lastChatSessionsLength !== state.chatSessions.length; + + if (shouldRecalculate) { + const filteredSessions = state.chatSessions.filter( + item => item.wechatAccountId === state.kfSelected, + ); + + cachedResult = + state.kfSelected !== 0 ? filteredSessions : state.chatSessions; + lastKfSelected = state.kfSelected; + lastChatSessionsLength = state.chatSessions.length; + } + + return cachedResult; + }; + })(), // 添加聊天会话 addChatSession: (session: ContractData | weChatGroup) => { set(state => { @@ -100,6 +141,15 @@ 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) => { @@ -108,12 +158,30 @@ export const useCkChatStore = createPersistStore( item.id === session.id ? session : item, ), })); + // 清除getChatSessions缓存 + const state = useCkChatStore.getState(); + if ( + state.getChatSessions && + typeof state.getChatSessions === "function" + ) { + // 触发缓存重新计算 + state.getChatSessions(); + } }, // 删除聊天会话 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) => {