import { createPersistStore } from "@/store/createPersistStore"; import { CkChatState, CkUserInfo, CkTenant } from "./ckchat.data"; import { ContractData, weChatGroup, CkAccount, KfUserListData, ContactGroupByLabel, } from "@/pages/pc/ckbox/data"; import { kfUserService, weChatGroupService, contractService, messageListService, } from "@/utils/db"; export const useCkChatStore = createPersistStore( set => ({ userInfo: null, isLoggedIn: false, contractList: [], //联系人列表 chatSessions: [], //聊天会话 kfUserList: [], //客服列表 newContractList: [], //联系人分组 kfSelected: 0, //选中的客服 //客服列表 asyncKfUserList: async data => { set({ kfUserList: data }); // await kfUserService.createManyWithServerId(data); }, // 获取客服列表 getkfUserList: async () => { return await kfUserService.findAll(); }, asyncKfSelected: (data: number) => { set({ kfSelected: data }); // 清除getChatSessions缓存 const state = useCkChatStore.getState(); if ( state.getChatSessions && typeof state.getChatSessions === "function" ) { // 触发缓存重新计算 state.getChatSessions(); } }, // 异步设置会话列表 asyncNewContractList: (data: ContactGroupByLabel[]) => { set({ newContractList: data }); }, getNewContractList: () => { const state = useCkChatStore.getState(); return state.newContractList; }, // 异步设置会话列表 asyncChatSessions: data => { set({ chatSessions: data }); // 清除getChatSessions缓存 const state = useCkChatStore.getState(); if ( state.getChatSessions && typeof state.getChatSessions === "function" ) { // 触发缓存重新计算 state.getChatSessions(); } }, // 异步设置联系人列表 asyncContractList: async (data: ContractData[]) => { await contractService.createManyWithServerId(data); }, //异步设置联系人分组 asyncWeChatGroup: async (data: weChatGroup[]) => { await weChatGroupService.createManyWithServerId(data); }, //获取选中的客服信息 getKfSelectedUser: () => { const state = useCkChatStore.getState(); return state.kfUserList.find(item => item.id === state.kfSelected); }, getKfUserInfo: (wechatAccountId: number) => { const state = useCkChatStore.getState(); return state.kfUserList.find(item => item.id === wechatAccountId); }, // 删除控制终端用户 deleteCtrlUser: (userId: number) => { set(state => ({ kfUserList: state.kfUserList.filter(item => item.id !== userId), })); }, // 更新控制终端用户 updateCtrlUser: (user: KfUserListData) => { set(state => ({ kfUserList: state.kfUserList.map(item => item.id === user.id ? user : item, ), })); }, // 清空控制终端用户列表 clearkfUserList: () => { set({ kfUserList: [] }); }, // 获取聊天会话 - 使用缓存避免无限循环 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 => { // 检查是否已存在相同id的会话 const exists = state.chatSessions.some(item => item.id === session.id); // 如果已存在则不添加,否则添加到列表中 return { chatSessions: exists ? state.chatSessions : [...state.chatSessions, session as ContractData | weChatGroup], }; }); // 清除getChatSessions缓存 const state = useCkChatStore.getState(); if ( state.getChatSessions && typeof state.getChatSessions === "function" ) { // 触发缓存重新计算 state.getChatSessions(); } }, // 更新聊天会话 updateChatSession: (session: ContractData | weChatGroup) => { set(state => ({ chatSessions: state.chatSessions.map(item => 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) => { set({ userInfo, isLoggedIn: true }); }, // 清除用户信息 clearUserInfo: () => { set({ userInfo: null, isLoggedIn: false }); }, // 更新账户信息 updateAccount: (account: Partial) => { set(state => ({ userInfo: state.userInfo ? { ...state.userInfo, account: { ...state.userInfo.account, ...account }, } : null, })); }, // 更新租户信息 updateTenant: (tenant: Partial) => { set(state => ({ userInfo: state.userInfo ? { ...state.userInfo, tenant: { ...state.userInfo.tenant, ...tenant }, } : null, })); }, // 获取账户ID getAccountId: () => { const state = useCkChatStore.getState(); return Number(state.userInfo?.account?.id) || null; }, // 获取租户ID getTenantId: () => { const state = useCkChatStore.getState(); return state.userInfo?.tenant?.id || null; }, // 获取账户名称 getAccountName: () => { 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; }, }), { name: "ckchat-store", partialize: state => ({ userInfo: state.userInfo, isLoggedIn: state.isLoggedIn, }), onRehydrateStorage: () => state => { // console.log("CkChat store hydrated:", state); }, }, ); // 导出便捷的获取方法 export const getCkAccountId = () => useCkChatStore.getState().getAccountId(); export const getCkTenantId = () => useCkChatStore.getState().getTenantId(); export const getCkAccountName = () => useCkChatStore.getState().getAccountName(); export const getCkTenantName = () => useCkChatStore.getState().getTenantName(); export const getChatSessions = () => useCkChatStore.getState().getChatSessions(); export const addChatSession = (session: ContractData | weChatGroup) => useCkChatStore.getState().addChatSession(session); export const updateChatSession = (session: ContractData | weChatGroup) => useCkChatStore.getState().updateChatSession(session); export const deleteChatSession = (sessionId: string) => useCkChatStore.getState().deleteChatSession(sessionId); export const getkfUserList = () => useCkChatStore.getState().kfUserList; export const addCtrlUser = (user: KfUserListData) => useCkChatStore.getState().addCtrlUser(user); export const deleteCtrlUser = (userId: number) => useCkChatStore.getState().deleteCtrlUser(userId); export const updateCtrlUser = (user: KfUserListData) => useCkChatStore.getState().updateCtrlUser(user); export const asyncKfUserList = (data: KfUserListData[]) => useCkChatStore.getState().asyncKfUserList(data); 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[]) => useCkChatStore.getState().asyncWeChatGroup(data); export const getKfSelectedUser = () => useCkChatStore.getState().getKfSelectedUser(); export const getKfUserInfo = (wechatAccountId: number) => useCkChatStore.getState().getKfUserInfo(wechatAccountId);