From f05efe464afd5c4773013fbf54bf4f30427874e2 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 11:40:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(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=E9=80=BB=E8=BE=91=E5=B9=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=93=E5=AD=98=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将联系人列表构建逻辑提取到独立api文件 - 添加getContractList缓存机制避免重复计算 - 优化kfSelected变更时的缓存清除逻辑 - 移除main.ts中冗余的联系人分组逻辑 --- Cunkebao/src/pages/pc/ckbox/main.ts | 132 +-------------------- Cunkebao/src/store/module/ckchat/api.ts | 65 ++++++++++ Cunkebao/src/store/module/ckchat/ckchat.ts | 59 ++++++++- 3 files changed, 122 insertions(+), 134 deletions(-) create mode 100644 Cunkebao/src/store/module/ckchat/api.ts diff --git a/Cunkebao/src/pages/pc/ckbox/main.ts b/Cunkebao/src/pages/pc/ckbox/main.ts index 66faf674..480cc674 100644 --- a/Cunkebao/src/pages/pc/ckbox/main.ts +++ b/Cunkebao/src/pages/pc/ckbox/main.ts @@ -12,16 +12,11 @@ import { getControlTerminalList, getContactList, getGroupList, - WechatGroup, } from "./api"; const { sendCommand } = useWebSocketStore.getState(); import { useUserStore } from "@/store/module/user"; -import { kfUserService, weChatGroupService, contractService } from "@/utils/db"; -import { - ContractData, - weChatGroup, - KfUserListData, -} from "@/pages/pc/ckbox/data"; + +import { KfUserListData } from "@/pages/pc/ckbox/data"; const { login2 } = useUserStore.getState(); //获取触客宝基础信息 export const chatInitAPIdata = async () => { @@ -48,10 +43,8 @@ export const chatInitAPIdata = async () => { await asyncWeChatGroup(groupList); - //构建联系人列表标签 - const newContractList = await createContractList(); // 会话列表分组 - await asyncNewContractList(newContractList); + await asyncNewContractList(0); //获取消息会话列表并按lastUpdateTime排序 const filterUserSessions = contractList?.filter( v => v?.config && v.config?.chat, @@ -89,7 +82,6 @@ export const chatInitAPIdata = async () => { contractList, groupList, kfUserList, - newContractList, }; } catch (error) { console.error("获取联系人列表失败:", error); @@ -97,130 +89,12 @@ export const chatInitAPIdata = async () => { } }; -//构建联系人列表标签 -export const createContractList = async () => { - const LablesRes = await Promise.all( - [1, 2].map(item => - WechatGroup({ - groupType: item, - }), - ), - ); - const [friend, group] = LablesRes; - const countLables = [ - ...[ - { - id: 0, - groupName: "默认群分组", - groupType: 2, - }, - ], - ...group, - ...friend, - ...[ - { - id: 0, - groupName: "未分组", - groupType: 1, - }, - ], - ]; - - // 根据 groupType 决定查询不同的服务 - const dataByLabels = []; - for (const label of countLables) { - let data; - if (label.groupType === 1) { - // groupType: 1, 查询 contractService - data = await contractService.findWhere("groupId", label.id); - // console.log(`标签 ${label.groupName} 对应的联系人数据:`, data); - } else if (label.groupType === 2) { - // groupType: 2, 查询 weChatGroupService - data = await weChatGroupService.findWhere("groupId", label.id); - } else { - console.warn(`未知的 groupType: ${label.groupType}`); - data = []; - } - dataByLabels.push({ - ...label, - contacts: data, - }); - } - - return dataByLabels; -}; - /** * 根据标签组织联系人 * @param contractList 联系人列表 * @param countLables 标签列表 * @returns 按标签分组的联系人 */ -export const organizeContactsByLabels = ( - countLables: any[], - contractList: ContractData[], - groupList: weChatGroup[], -) => { - // 创建结果对象,用于存储按标签分组的联系人 - const result: { [key: string]: any[] } = {}; - - // 初始化结果对象,为每个标签创建一个空数组 - countLables.forEach(label => { - if (label && label.groupName) { - result[label.groupName] = []; - } - }); - - // 创建未分组标签,用于存放没有匹配到任何标签的联系人 - const ungroupedLabel = "未分组"; - result[ungroupedLabel] = []; - - // 遍历联系人列表 - contractList.forEach(contact => { - // 确保联系人有labels字段且是数组 - if (contact && Array.isArray(contact.labels)) { - // 标记联系人是否已被分配到某个组 - let isAssigned = false; - - // 遍历标签列表 - countLables.forEach(label => { - if (label && label.groupName) { - // 检查联系人的labels是否包含当前标签的groupName - if (contact.labels.includes(label.groupName)) { - // 将联系人添加到对应标签的数组中 - result[label.groupName].push(contact); - isAssigned = true; - } - } - }); - - // 如果联系人没有被分配到任何组,则添加到未分组 - if (!isAssigned) { - result[ungroupedLabel].push(contact); - } - } else { - // 如果联系人没有labels字段或不是数组,也添加到未分组 - result[ungroupedLabel].push(contact); - } - }); - - // 将结果转换为数组格式,确保未分组在最后 - const resultArray = Object.entries(result).map(([groupName, contacts]) => ({ - groupName, - contacts, - })); - - // 将未分组移到数组末尾 - const ungroupedIndex = resultArray.findIndex( - item => item.groupName === ungroupedLabel, - ); - if (ungroupedIndex !== -1) { - const ungrouped = resultArray.splice(ungroupedIndex, 1)[0]; - resultArray.push(ungrouped); - } - - return resultArray; -}; //获取控制终端列表 export const getControlTerminalListByWechatAccountIds = ( diff --git a/Cunkebao/src/store/module/ckchat/api.ts b/Cunkebao/src/store/module/ckchat/api.ts new file mode 100644 index 00000000..c14a7918 --- /dev/null +++ b/Cunkebao/src/store/module/ckchat/api.ts @@ -0,0 +1,65 @@ +//构建联系人列表标签 +import { weChatGroupService, contractService } from "@/utils/db"; +import { request } from "@/api/request2"; +export function WechatGroup(params) { + return request("/api/WechatGroup/list", params, "GET"); +} + +export const createContractList = async (kfSelected: number) => { + const LablesRes = await Promise.all( + [1, 2].map(item => + WechatGroup({ + groupType: item, + }), + ), + ); + const [friend, group] = LablesRes; + const countLables = [ + ...[ + { + id: 0, + groupName: "默认群分组", + groupType: 2, + }, + ], + ...group, + ...friend, + ...[ + { + id: 0, + groupName: "未分组", + groupType: 1, + }, + ], + ]; + + // 根据 groupType 决定查询不同的服务 + const dataByLabels = []; + for (const label of countLables) { + let data; + if (label.groupType === 1) { + // groupType: 1, 查询 contractService + data = await contractService.findWhere("groupId", label.id); + // 过滤出 kfSelected 对应的联系人 + if (kfSelected && kfSelected != 0) { + data = data.filter(contact => contact.wechatAccountId === kfSelected); + } + // console.log(`标签 ${label.groupName} 对应的联系人数据:`, data); + } else if (label.groupType === 2) { + // groupType: 2, 查询 weChatGroupService + data = await weChatGroupService.findWhere("groupId", label.id); + if (kfSelected && kfSelected != 0) { + data = data.filter(contact => contact.wechatAccountId === kfSelected); + } + } else { + console.warn(`未知的 groupType: ${label.groupType}`); + data = []; + } + dataByLabels.push({ + ...label, + contacts: data, + }); + } + + return dataByLabels; +}; diff --git a/Cunkebao/src/store/module/ckchat/ckchat.ts b/Cunkebao/src/store/module/ckchat/ckchat.ts index bc816756..feb648c1 100644 --- a/Cunkebao/src/store/module/ckchat/ckchat.ts +++ b/Cunkebao/src/store/module/ckchat/ckchat.ts @@ -13,6 +13,7 @@ import { contractService, messageListService, } from "@/utils/db"; +import { createContractList } from "@/store/module/ckchat/api"; export const useCkChatStore = createPersistStore( set => ({ userInfo: null, @@ -33,7 +34,7 @@ export const useCkChatStore = createPersistStore( }, asyncKfSelected: (data: number) => { set({ kfSelected: data }); - // 清除getChatSessions缓存 + // 清除getChatSessions和getContractList缓存 const state = useCkChatStore.getState(); if ( state.getChatSessions && @@ -42,9 +43,17 @@ export const useCkChatStore = createPersistStore( // 触发缓存重新计算 state.getChatSessions(); } + if ( + state.getContractList && + typeof state.getContractList === "function" + ) { + // 触发缓存重新计算 + state.getContractList(); + } }, // 异步设置会话列表 - asyncNewContractList: (data: ContactGroupByLabel[]) => { + asyncNewContractList: async (kfSelected: number) => { + const data: ContactGroupByLabel[] = await createContractList(kfSelected); set({ newContractList: data }); }, getNewContractList: () => { @@ -66,8 +75,47 @@ export const useCkChatStore = createPersistStore( }, // 异步设置联系人列表 asyncContractList: async (data: ContractData[]) => { + set({ contractList: data }); await contractService.createManyWithServerId(data); + // 清除getContractList缓存 + const state = useCkChatStore.getState(); + if ( + state.getContractList && + typeof state.getContractList === "function" + ) { + // 触发缓存重新计算 + state.getContractList(); + } }, + // 获取联系人列表 - 使用缓存避免无限循环 + getContractList: (() => { + let cachedResult: any = null; + let lastKfSelected: number | null = null; + let lastContractListLength: number = 0; + + return () => { + const state = useCkChatStore.getState(); + + // 检查是否需要重新计算缓存 + const shouldRecalculate = + cachedResult === null || + lastKfSelected !== state.kfSelected || + lastContractListLength !== state.contractList.length; + + if (shouldRecalculate) { + const filteredContracts = state.contractList.filter( + item => item.wechatAccountId === state.kfSelected, + ); + + cachedResult = + state.kfSelected !== 0 ? filteredContracts : state.contractList; + lastKfSelected = state.kfSelected; + lastContractListLength = state.contractList.length; + } + + return cachedResult; + }; + })(), //异步设置联系人分组 asyncWeChatGroup: async (data: weChatGroup[]) => { await weChatGroupService.createManyWithServerId(data); @@ -284,9 +332,8 @@ export const asyncContractList = (data: ContractData[]) => useCkChatStore.getState().asyncContractList(data); export const asyncChatSessions = (data: ContractData[]) => useCkChatStore.getState().asyncChatSessions(data); -export const asyncNewContractList = ( - data: { groupName: string; contacts: any[] }[], -) => useCkChatStore.getState().asyncNewContractList(data); +export const asyncNewContractList = (kfSelected: number) => + useCkChatStore.getState().asyncNewContractList(kfSelected); export const asyncKfSelected = (data: number) => useCkChatStore.getState().asyncKfSelected(data); export const asyncWeChatGroup = (data: weChatGroup[]) => @@ -295,3 +342,5 @@ export const getKfSelectedUser = () => useCkChatStore.getState().getKfSelectedUser(); export const getKfUserInfo = (wechatAccountId: number) => useCkChatStore.getState().getKfUserInfo(wechatAccountId); +export const getContractList = () => + useCkChatStore.getState().getContractList();