diff --git a/Cunkebao/src/store/module/ckchat/ckchat.data.ts b/Cunkebao/src/store/module/ckchat/ckchat.data.ts index 8c7d320f..c7d28f66 100644 --- a/Cunkebao/src/store/module/ckchat/ckchat.data.ts +++ b/Cunkebao/src/store/module/ckchat/ckchat.data.ts @@ -40,6 +40,7 @@ export interface CkChatState { getKfSelectedUser: () => KfUserListData | undefined; countLables: ContactGroupByLabel[]; newContractList: ContactGroupByLabel[]; + getContractList: () => ContractData[]; getNewContractList: () => ContactGroupByLabel[]; asyncKfSelected: (data: number) => void; asyncWeChatGroup: (data: weChatGroup[]) => void; diff --git a/Cunkebao/src/store/module/ckchat/ckchat.ts b/Cunkebao/src/store/module/ckchat/ckchat.ts index edeb1478..b639367f 100644 --- a/Cunkebao/src/store/module/ckchat/ckchat.ts +++ b/Cunkebao/src/store/module/ckchat/ckchat.ts @@ -7,12 +7,9 @@ import { KfUserListData, ContactGroupByLabel, } from "@/pages/pc/ckbox/data"; -import { - kfUserService, - weChatGroupService, - contractService, - messageListService, -} from "@/utils/db"; +import { kfUserService, weChatGroupService, contractService } from "@/utils/db"; +import { createContractList } from "@/store/module/ckchat/api"; + export const useCkChatStore = createPersistStore( set => ({ userInfo: null, @@ -20,6 +17,7 @@ export const useCkChatStore = createPersistStore( contractList: [], //联系人列表 chatSessions: [], //聊天会话 kfUserList: [], //客服列表 + countLables: [], //标签列表 newContractList: [], //联系人分组 kfSelected: 0, //选中的客服 //客服列表 @@ -31,48 +29,103 @@ export const useCkChatStore = createPersistStore( getkfUserList: async () => { return await kfUserService.findAll(); }, + // 异步设置标签列表 + asyncCountLables: (data: ContactGroupByLabel[]) => { + set({ countLables: data }); + }, asyncKfSelected: (data: number) => { set({ kfSelected: data }); // 清除getChatSessions缓存 const state = useCkChatStore.getState(); - if (ChatSessions && typeof sahatSessions === 'function') { - ' // 触' state.getChatSessions(); + if ( + state.getChatSessions && + typeof state.getChatSessions === "function" + ) { + // 触发缓存重新计算 + state.getChatSessions(); + } + if ( + state.getContractList && + typeof state.getContractList === "function" + ) { + // 触发缓存重新计算 + state.getContractList(); + } + if ( + state.getNewContractList && + typeof state.getNewContractList === "function" + ) { + // 触发缓存重新计算 + state.getNewContractList(); } }, - // 异步设置会话列表 - asyncNewContractList: (data: ContactGroupByLabel[]) => { - set({ newContractList: data }); - }, - getNewContractList: () => { + + // 获取联系人分组列表 - 使用缓存避免无限循环 + getNewContractList: async () => { const state = useCkChatStore.getState(); - return state.newContractList; + return await createContractList(state.kfSelected, state.countLables); }, // 异步设置会话列表 asyncChatSessions: data => { set({ chatSessions: data }); // 清除getChatSessions缓存 const state = useCkChatStore.getState(); - if (state.getChatSessions &&tate.getChatSessions ==='n') { + if ( + state.getChatSessions && + typeof state.getChatSessions === "function" + ) { // 触发缓存重新计算 - 'state.ge'sions(); + state.getChatSessions(); } }, // 异步设置联系人列表 asyncContractList: async (data: ContractData[]) => { - aServerId(data); - }, - //获取选中的客服信息 - getKfSelecteuhedResult === null || - lastKfSelected !== state.kfSelected || - lastChatSessionsLength !== state.chatSessions.length; - + 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); - const filteredSessions = state.chatSessions.filter( - item => item.wechatAccountId === state.kfSelected, + }, + //获取选中的客服信息 getKfSelectedUser: () => { const state = useCkChatStore.getState(); return state.kfUserList.find(item => item.id === state.kfSelected); @@ -97,29 +150,36 @@ export const useCkChatStore = createPersistStore( })); }, // 清空控制终端用户列表 - clearkfUserList: () => { + clearkfUserList: () => { set({ kfUserList: [] }); }, - / / 获取聊天会话 - 使用缓存避免无限循环 + // 获取聊天会话 - 使用缓存避免无限循环 getChatSessions: (() => { - let cachedResult: any = null; + let cachedResult: any = null; let lastKfSelected: number | null = null; let lastChatSessionsLength: number = 0; return () => { - const state = useCkChatStore.getState(); + 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; + state.kfSelected !== 0 ? filteredSessions : state.chatSessions; lastKfSelected = state.kfSelected; lastChatSessionsLength = state.chatSessions.length; } - return cach edResult; + return cachedResult; }; })(), // 添加聊天会话 @@ -138,32 +198,33 @@ export const useCkChatStore = createPersistStore( const state = useCkChatStore.getState(); if ( state.getChatSessions && - typeof state.getChatSessions === "function" + typeof state.getChatSessions === "function" ) { // 触发缓存重新计算 - state.getChatSessi ons(); + state.getChatSessions(); } }, // 更新聊天会话 - updateChatSession: (session: ContractData | weChatGroup) => { + updateChatSession: (session: ContractData | weChatGroup) => { set(state => ({ chatSessions: state.chatSessions.map(item => - item.id === session. id ? session : item, + item.id === session.id ? session : item, ), })); // 清除getChatSessions缓存 const state = useCkChatStore.getState(); if ( state.getChatSessions && - t ypeof state.getChatSessiosion" - ) { 发缓存重新计算 + typeof state.getChatSessions === "function" + ) { + // 触发缓存重新计算 state.getChatSessions(); } }, // 删除聊天会话 deleteChatSession: (sessionId: string) => { set(state => ({ - chatSe ssions: state.chatSessions.filter(item => item.id !== sessionId), + chatSessions: state.chatSessions.filter(item => item.id !== sessionId), })); // 清除getChatSessions缓存 const state = useCkChatStore.getState(); @@ -185,10 +246,12 @@ export const useCkChatStore = createPersistStore( set({ userInfo: null, isLoggedIn: false }); }, - // 更新账户信息teAccount: (accountnt>) => { - set(stat 'o: state.userInfo - ' ? { - ' ...state.userInfo, + // 更新账户信息 + updateAccount: (account: Partial) => { + set(state => ({ + userInfo: state.userInfo + ? { + ...state.userInfo, account: { ...state.userInfo.account, ...account }, } : null, @@ -201,11 +264,15 @@ export const useCkChatStore = createPersistStore( userInfo: state.userInfo ? { ...state.userInfo, - tenant: { ...state.usernt, ...tenant }, - : null, - },' - // ' getAc () => { - const state = useCk'hatStore'e(); + tenant: { ...state.userInfo.tenant, ...tenant }, + } + : null, + })); + }, + + // 获取账户ID + getAccountId: () => { + const state = useCkChatStore.getState(); return Number(state.userInfo?.account?.id) || null; }, @@ -217,13 +284,15 @@ export const useCkChatStore = createPersistStore( // 获取账户名称 getAccountName: () => { - cons useCkChatStore.getState return ( - state.userInfo?'account? state.userInfo?.accerName || + const state = useCkChatStore.getState(); + return ( + state.userInfo?.account?.realName || + state.userInfo?.account?.userName || null ); - ' }, + }, - '名称 + // 获取租户名称 getTenantName: () => { const state = useCkChatStore.getState(); return state.userInfo?.tenant?.name || null; @@ -268,9 +337,6 @@ 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 asyncKfSelected = (data: number) => useCkChatStore.getState().asyncKfSelected(data); export const asyncWeChatGroup = (data: weChatGroup[]) => @@ -278,4 +344,12 @@ export const asyncWeChatGroup = (data: weChatGroup[]) => export const getKfSelectedUser = () => useCkChatStore.getState().getKfSelectedUser(); export const getKfUserInfo = (wechatAccountId: number) => - useCkChatStore.getState().getKfUserInfo(wechatAcctList(); + useCkChatStore.getState().getKfUserInfo(wechatAccountId); +export const getContractList = () => + useCkChatStore.getState().getContractList(); +export const getNewContractList = () => + useCkChatStore.getState().getNewContractList(); +export const asyncCountLables = (data: ContactGroupByLabel[]) => + useCkChatStore.getState().asyncCountLables(data); +export const getCountLables = () => useCkChatStore.getState().countLables; +useCkChatStore.getState().getKfSelectedUser();