diff --git a/Cunkebao/src/pages/pc/ckbox/components/SidebarMenu/WechatFriends/index.tsx b/Cunkebao/src/pages/pc/ckbox/components/SidebarMenu/WechatFriends/index.tsx index 1e652ce6..47acabf8 100644 --- a/Cunkebao/src/pages/pc/ckbox/components/SidebarMenu/WechatFriends/index.tsx +++ b/Cunkebao/src/pages/pc/ckbox/components/SidebarMenu/WechatFriends/index.tsx @@ -16,7 +16,27 @@ const ContactListSimple: React.FC = ({ onContactClick, selectedContactId, }) => { - const newContractList = useCkChatStore(state => state.getNewContractList()); + const [newContractList, setNewContractList] = useState([]); + const getNewContractListFn = useCkChatStore( + state => state.getNewContractList, + ); + const kfSelected = useCkChatStore(state => state.kfSelected); + const countLables = useCkChatStore(state => state.countLables); + + // 使用useEffect来处理异步的getNewContractList调用 + useEffect(() => { + const fetchNewContractList = async () => { + try { + const result = await getNewContractListFn(); + setNewContractList(result || []); + } catch (error) { + console.error("获取联系人分组列表失败:", error); + setNewContractList([]); + } + }; + + fetchNewContractList(); + }, [getNewContractListFn, kfSelected, countLables]); const [activeKey, setActiveKey] = useState([]); // 默认展开第一个分组 diff --git a/Cunkebao/src/store/module/ckchat/ckchat.ts b/Cunkebao/src/store/module/ckchat/ckchat.ts index b639367f..c2257798 100644 --- a/Cunkebao/src/store/module/ckchat/ckchat.ts +++ b/Cunkebao/src/store/module/ckchat/ckchat.ts @@ -30,12 +30,21 @@ export const useCkChatStore = createPersistStore( return await kfUserService.findAll(); }, // 异步设置标签列表 - asyncCountLables: (data: ContactGroupByLabel[]) => { + asyncCountLables: async (data: ContactGroupByLabel[]) => { set({ countLables: data }); + // 清除getNewContractList缓存 + const state = useCkChatStore.getState(); + if ( + state.getNewContractList && + typeof state.getNewContractList === "function" + ) { + // 触发缓存重新计算 + await state.getNewContractList(); + } }, - asyncKfSelected: (data: number) => { + asyncKfSelected: async (data: number) => { set({ kfSelected: data }); - // 清除getChatSessions缓存 + // 清除getChatSessions、getContractList和getNewContractList缓存 const state = useCkChatStore.getState(); if ( state.getChatSessions && @@ -56,14 +65,50 @@ export const useCkChatStore = createPersistStore( typeof state.getNewContractList === "function" ) { // 触发缓存重新计算 - state.getNewContractList(); + await state.getNewContractList(); } }, // 获取联系人分组列表 - 使用缓存避免无限循环 - getNewContractList: async () => { + getNewContractList: (() => { + let cachedResult: any = null; + let lastKfSelected: number | null = null; + let lastCountLablesLength: number = 0; + + return async () => { + const state = useCkChatStore.getState(); + + // 检查是否需要重新计算缓存 + const shouldRecalculate = + cachedResult === null || + lastKfSelected !== state.kfSelected || + lastCountLablesLength !== (state.countLables?.length || 0); + + if (shouldRecalculate) { + // 使用createContractList构建联系人分组数据 + cachedResult = await createContractList( + state.kfSelected, + state.countLables, + ); + lastKfSelected = state.kfSelected; + lastCountLablesLength = state.countLables?.length || 0; + } + + return cachedResult; + }; + })(), + // 异步设置联系人分组列表 + asyncNewContractList: async (data: any[]) => { + set({ newContractList: data }); + // 清除getNewContractList缓存 const state = useCkChatStore.getState(); - return await createContractList(state.kfSelected, state.countLables); + if ( + state.getNewContractList && + typeof state.getNewContractList === "function" + ) { + // 触发缓存重新计算 + await state.getNewContractList(); + } }, // 异步设置会话列表 asyncChatSessions: data => { @@ -351,5 +396,7 @@ export const getNewContractList = () => useCkChatStore.getState().getNewContractList(); export const asyncCountLables = (data: ContactGroupByLabel[]) => useCkChatStore.getState().asyncCountLables(data); +export const asyncNewContractList = (data: any[]) => + useCkChatStore.getState().asyncNewContractList(data); export const getCountLables = () => useCkChatStore.getState().countLables; useCkChatStore.getState().getKfSelectedUser();