From 832be374b550bcf1eb51980c54b487c67f32beb3 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: Mon, 1 Sep 2025 14:58:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ckchat):=20=E9=87=8D=E6=9E=84=E8=81=94?= =?UTF-8?q?=E7=B3=BB=E4=BA=BA=E5=88=97=E8=A1=A8=E8=8E=B7=E5=8F=96=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=B9=B6=E6=B7=BB=E5=8A=A0=E7=BC=93=E5=AD=98=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将联系人列表获取逻辑从直接调用改为使用useEffect异步获取 - 为getNewContractList添加缓存机制避免重复计算 - 在相关状态变更时自动清除缓存 - 新增asyncNewContractList方法用于设置联系人列表 --- .../SidebarMenu/WechatFriends/index.tsx | 22 ++++++- Cunkebao/src/store/module/ckchat/ckchat.ts | 59 +++++++++++++++++-- 2 files changed, 74 insertions(+), 7 deletions(-) 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();