2025-08-30 11:52:52 +08:00
|
|
|
import { createPersistStore } from "@/store/createPersistStore";
|
|
|
|
|
import { CkChatState, CkUserInfo, CkTenant } from "./ckchat.data";
|
|
|
|
|
import {
|
|
|
|
|
ContractData,
|
2025-08-30 15:55:15 +08:00
|
|
|
weChatGroup,
|
2025-08-30 11:52:52 +08:00
|
|
|
CkAccount,
|
|
|
|
|
KfUserListData,
|
2025-08-30 15:55:15 +08:00
|
|
|
ContactGroupByLabel,
|
2025-08-30 11:52:52 +08:00
|
|
|
} from "@/pages/pc/ckbox/data";
|
2025-09-01 10:43:09 +08:00
|
|
|
import {
|
|
|
|
|
kfUserService,
|
|
|
|
|
weChatGroupService,
|
|
|
|
|
contractService,
|
|
|
|
|
messageListService,
|
|
|
|
|
} from "@/utils/db";
|
2025-08-30 11:52:52 +08:00
|
|
|
export const useCkChatStore = createPersistStore<CkChatState>(
|
|
|
|
|
set => ({
|
|
|
|
|
userInfo: null,
|
|
|
|
|
isLoggedIn: false,
|
|
|
|
|
contractList: [], //联系人列表
|
|
|
|
|
chatSessions: [], //聊天会话
|
|
|
|
|
kfUserList: [], //客服列表
|
|
|
|
|
newContractList: [], //联系人分组
|
2025-09-01 10:24:15 +08:00
|
|
|
kfSelected: 0, //选中的客服
|
2025-08-30 15:00:26 +08:00
|
|
|
//客服列表
|
|
|
|
|
asyncKfUserList: async data => {
|
2025-08-30 17:02:31 +08:00
|
|
|
set({ kfUserList: data });
|
|
|
|
|
// await kfUserService.createManyWithServerId(data);
|
2025-08-30 15:00:26 +08:00
|
|
|
},
|
|
|
|
|
// 获取客服列表
|
|
|
|
|
getkfUserList: async () => {
|
|
|
|
|
return await kfUserService.findAll();
|
2025-08-30 11:52:52 +08:00
|
|
|
},
|
|
|
|
|
asyncKfSelected: (data: number) => {
|
|
|
|
|
set({ kfSelected: data });
|
2025-09-01 10:43:09 +08:00
|
|
|
// 清除getChatSessions缓存
|
|
|
|
|
const state = useCkChatStore.getState();
|
|
|
|
|
if (
|
|
|
|
|
state.getChatSessions &&
|
|
|
|
|
typeof state.getChatSessions === "function"
|
|
|
|
|
) {
|
|
|
|
|
// 触发缓存重新计算
|
|
|
|
|
state.getChatSessions();
|
|
|
|
|
}
|
2025-08-30 11:52:52 +08:00
|
|
|
},
|
|
|
|
|
// 异步设置会话列表
|
2025-08-30 15:55:15 +08:00
|
|
|
asyncNewContractList: (data: ContactGroupByLabel[]) => {
|
2025-08-30 11:52:52 +08:00
|
|
|
set({ newContractList: data });
|
|
|
|
|
},
|
|
|
|
|
getNewContractList: () => {
|
|
|
|
|
const state = useCkChatStore.getState();
|
|
|
|
|
return state.newContractList;
|
|
|
|
|
},
|
|
|
|
|
// 异步设置会话列表
|
|
|
|
|
asyncChatSessions: data => {
|
|
|
|
|
set({ chatSessions: data });
|
2025-09-01 10:43:09 +08:00
|
|
|
// 清除getChatSessions缓存
|
|
|
|
|
const state = useCkChatStore.getState();
|
|
|
|
|
if (
|
|
|
|
|
state.getChatSessions &&
|
|
|
|
|
typeof state.getChatSessions === "function"
|
|
|
|
|
) {
|
|
|
|
|
// 触发缓存重新计算
|
|
|
|
|
state.getChatSessions();
|
|
|
|
|
}
|
2025-08-30 11:52:52 +08:00
|
|
|
},
|
|
|
|
|
// 异步设置联系人列表
|
2025-08-30 17:02:31 +08:00
|
|
|
asyncContractList: async (data: ContractData[]) => {
|
|
|
|
|
await contractService.createManyWithServerId(data);
|
2025-08-30 11:52:52 +08:00
|
|
|
},
|
2025-08-30 15:55:15 +08:00
|
|
|
//异步设置联系人分组
|
2025-08-30 17:02:31 +08:00
|
|
|
asyncWeChatGroup: async (data: weChatGroup[]) => {
|
|
|
|
|
await weChatGroupService.createManyWithServerId(data);
|
2025-08-30 15:55:15 +08:00
|
|
|
},
|
2025-08-30 15:00:26 +08:00
|
|
|
//获取选中的客服信息
|
2025-08-30 17:07:08 +08:00
|
|
|
getKfSelectedUser: () => {
|
2025-08-30 11:52:52 +08:00
|
|
|
const state = useCkChatStore.getState();
|
2025-08-30 15:00:26 +08:00
|
|
|
return state.kfUserList.find(item => item.id === state.kfSelected);
|
2025-08-30 11:52:52 +08:00
|
|
|
},
|
2025-08-30 17:15:22 +08:00
|
|
|
getKfUserInfo: (wechatAccountId: number) => {
|
|
|
|
|
const state = useCkChatStore.getState();
|
|
|
|
|
return state.kfUserList.find(item => item.id === wechatAccountId);
|
|
|
|
|
},
|
2025-08-30 15:00:26 +08:00
|
|
|
|
2025-08-30 11:52:52 +08:00
|
|
|
// 删除控制终端用户
|
|
|
|
|
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: [] });
|
|
|
|
|
},
|
2025-09-01 10:43:09 +08:00
|
|
|
// 获取聊天会话 - 使用缓存避免无限循环
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
})(),
|
2025-08-30 11:52:52 +08:00
|
|
|
// 添加聊天会话
|
2025-08-30 15:55:15 +08:00
|
|
|
addChatSession: (session: ContractData | weChatGroup) => {
|
2025-08-30 11:52:52 +08:00
|
|
|
set(state => {
|
|
|
|
|
// 检查是否已存在相同id的会话
|
|
|
|
|
const exists = state.chatSessions.some(item => item.id === session.id);
|
|
|
|
|
// 如果已存在则不添加,否则添加到列表中
|
|
|
|
|
return {
|
|
|
|
|
chatSessions: exists
|
|
|
|
|
? state.chatSessions
|
2025-08-30 15:55:15 +08:00
|
|
|
: [...state.chatSessions, session as ContractData | weChatGroup],
|
2025-08-30 11:52:52 +08:00
|
|
|
};
|
|
|
|
|
});
|
2025-09-01 10:43:09 +08:00
|
|
|
// 清除getChatSessions缓存
|
|
|
|
|
const state = useCkChatStore.getState();
|
|
|
|
|
if (
|
|
|
|
|
state.getChatSessions &&
|
|
|
|
|
typeof state.getChatSessions === "function"
|
|
|
|
|
) {
|
|
|
|
|
// 触发缓存重新计算
|
|
|
|
|
state.getChatSessions();
|
|
|
|
|
}
|
2025-08-30 11:52:52 +08:00
|
|
|
},
|
|
|
|
|
// 更新聊天会话
|
2025-08-30 15:55:15 +08:00
|
|
|
updateChatSession: (session: ContractData | weChatGroup) => {
|
2025-08-30 11:52:52 +08:00
|
|
|
set(state => ({
|
|
|
|
|
chatSessions: state.chatSessions.map(item =>
|
|
|
|
|
item.id === session.id ? session : item,
|
|
|
|
|
),
|
|
|
|
|
}));
|
2025-09-01 10:43:09 +08:00
|
|
|
// 清除getChatSessions缓存
|
|
|
|
|
const state = useCkChatStore.getState();
|
|
|
|
|
if (
|
|
|
|
|
state.getChatSessions &&
|
|
|
|
|
typeof state.getChatSessions === "function"
|
|
|
|
|
) {
|
|
|
|
|
// 触发缓存重新计算
|
|
|
|
|
state.getChatSessions();
|
|
|
|
|
}
|
2025-08-30 11:52:52 +08:00
|
|
|
},
|
|
|
|
|
// 删除聊天会话
|
|
|
|
|
deleteChatSession: (sessionId: string) => {
|
|
|
|
|
set(state => ({
|
|
|
|
|
chatSessions: state.chatSessions.filter(item => item.id !== sessionId),
|
|
|
|
|
}));
|
2025-09-01 10:43:09 +08:00
|
|
|
// 清除getChatSessions缓存
|
|
|
|
|
const state = useCkChatStore.getState();
|
|
|
|
|
if (
|
|
|
|
|
state.getChatSessions &&
|
|
|
|
|
typeof state.getChatSessions === "function"
|
|
|
|
|
) {
|
|
|
|
|
// 触发缓存重新计算
|
|
|
|
|
state.getChatSessions();
|
|
|
|
|
}
|
2025-08-30 11:52:52 +08:00
|
|
|
},
|
|
|
|
|
// 设置用户信息
|
|
|
|
|
setUserInfo: (userInfo: CkUserInfo) => {
|
|
|
|
|
set({ userInfo, isLoggedIn: true });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 清除用户信息
|
|
|
|
|
clearUserInfo: () => {
|
|
|
|
|
set({ userInfo: null, isLoggedIn: false });
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 更新账户信息
|
|
|
|
|
updateAccount: (account: Partial<CkAccount>) => {
|
|
|
|
|
set(state => ({
|
|
|
|
|
userInfo: state.userInfo
|
|
|
|
|
? {
|
|
|
|
|
...state.userInfo,
|
|
|
|
|
account: { ...state.userInfo.account, ...account },
|
|
|
|
|
}
|
|
|
|
|
: null,
|
|
|
|
|
}));
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 更新租户信息
|
|
|
|
|
updateTenant: (tenant: Partial<CkTenant>) => {
|
|
|
|
|
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();
|
2025-08-30 15:55:15 +08:00
|
|
|
export const addChatSession = (session: ContractData | weChatGroup) =>
|
2025-08-30 11:52:52 +08:00
|
|
|
useCkChatStore.getState().addChatSession(session);
|
2025-08-30 15:55:15 +08:00
|
|
|
export const updateChatSession = (session: ContractData | weChatGroup) =>
|
2025-08-30 11:52:52 +08:00
|
|
|
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);
|
2025-08-30 17:02:31 +08:00
|
|
|
export const asyncWeChatGroup = (data: weChatGroup[]) =>
|
|
|
|
|
useCkChatStore.getState().asyncWeChatGroup(data);
|
2025-08-30 17:15:22 +08:00
|
|
|
export const getKfSelectedUser = () =>
|
|
|
|
|
useCkChatStore.getState().getKfSelectedUser();
|
|
|
|
|
export const getKfUserInfo = (wechatAccountId: number) =>
|
|
|
|
|
useCkChatStore.getState().getKfUserInfo(wechatAccountId);
|