diff --git a/Cunkebao/src/pages/mobile/test/db.tsx b/Cunkebao/src/pages/mobile/test/db.tsx index 04330b40..9843dbc7 100644 --- a/Cunkebao/src/pages/mobile/test/db.tsx +++ b/Cunkebao/src/pages/mobile/test/db.tsx @@ -331,10 +331,10 @@ const DatabaseTestPage: React.FC = () => { try { await db.transaction( "rw", - [db.kfUsers, db.groups, db.contracts, db.newContractList], + [db.kfUsers, db.weChatGroups, db.contracts, db.newContractList], async () => { await db.kfUsers.clear(); - await db.groups.clear(); + await db.weChatGroups.clear(); await db.contracts.clear(); await db.newContractList.clear(); }, 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 812636f5..82b227bc 100644 --- a/Cunkebao/src/pages/pc/ckbox/components/SidebarMenu/WechatFriends/index.tsx +++ b/Cunkebao/src/pages/pc/ckbox/components/SidebarMenu/WechatFriends/index.tsx @@ -16,8 +16,8 @@ const ContactListSimple: React.FC = ({ onContactClick, selectedContactId, }) => { - // const newContractList = useCkChatStore(state => state.newContractList); const newContractList = useCkChatStore(state => state.newContractList); + const [activeKey, setActiveKey] = useState([]); // 默认展开第一个分组 // 分页加载相关状态 diff --git a/Cunkebao/src/pages/pc/ckbox/components/VerticalUserList/index.tsx b/Cunkebao/src/pages/pc/ckbox/components/VerticalUserList/index.tsx index ff4af178..b0c28db0 100644 --- a/Cunkebao/src/pages/pc/ckbox/components/VerticalUserList/index.tsx +++ b/Cunkebao/src/pages/pc/ckbox/components/VerticalUserList/index.tsx @@ -14,24 +14,24 @@ const VerticalUserList: React.FC = () => { const handleUserSelect = (userId: number) => { asyncKfSelected(userId); }; - const getkfUserList = useCkChatStore(state => state.getkfUserList); + const kfUserList = useCkChatStore(state => state.kfUserList); const kfSelected = useCkChatStore(state => state.kfSelected); const [kefuList, setKefuList] = useState([]); - // 获取客服列表数据 - useEffect(() => { - const fetchKfUserList = async () => { - try { - const data = await getkfUserList(); - setKefuList(data || []); - } catch (error) { - console.error("获取客服列表失败:", error); - setKefuList([]); - } - }; + // // 获取客服列表数据 + // useEffect(() => { + // const fetchKfUserList = async () => { + // try { + // const data = await getkfUserList(); + // setKefuList(data || []); + // } catch (error) { + // console.error("获取客服列表失败:", error); + // setKefuList([]); + // } + // }; - fetchKfUserList(); - }, [getkfUserList]); + // fetchKfUserList(); + // }, [getkfUserList]); return (
{
全部好友
- {kefuList.map(user => ( + {kfUserList.map(user => (
{ await getControlTerminalListByWechatAccountIds(uniqueWechatAccountIds); //获取用户列表 - asyncKfUserList(kfUserList); + await asyncKfUserList(kfUserList); //获取群列表 const groupList = await getAllGroupList(); + await asyncWeChatGroup(groupList); + //构建联系人列表标签 const newContractList = await createContractList(); // 会话列表分组 - asyncNewContractList(newContractList); + await asyncNewContractList(newContractList); //获取消息会话列表并按lastUpdateTime排序 const filterUserSessions = contractList?.filter( v => v?.config && v.config?.chat, @@ -103,29 +107,47 @@ export const createContractList = async () => { ), ); const [friend, group] = LablesRes; - const lastIndex = friend.length + group.length + 1; const countLables = [ ...[ { id: 0, groupName: "默认群分组", + groupType: 2, }, ], - ...friend, ...group, + ...friend, ...[ { - id: lastIndex, + id: 0, groupName: "未分组", + groupType: 1, }, ], ]; - return countLables; + // 根据 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, + }); + } - // 根据countLables中的groupName整理contractList数据 - // 返回按标签分组的联系人数组,包括未分组标签(在数组最后) - // return organizeContactsByLabels(countLables, contractList, groupList); + return dataByLabels; }; /** diff --git a/Cunkebao/src/store/module/ckchat/ckchat.ts b/Cunkebao/src/store/module/ckchat/ckchat.ts index 5bbd149b..73749218 100644 --- a/Cunkebao/src/store/module/ckchat/ckchat.ts +++ b/Cunkebao/src/store/module/ckchat/ckchat.ts @@ -7,7 +7,7 @@ import { KfUserListData, ContactGroupByLabel, } from "@/pages/pc/ckbox/data"; -import { kfUserService } from "@/utils/db"; +import { kfUserService, weChatGroupService, contractService } from "@/utils/db"; export const useCkChatStore = createPersistStore( set => ({ userInfo: null, @@ -19,7 +19,8 @@ export const useCkChatStore = createPersistStore( kfSelected: 0, //客服列表 asyncKfUserList: async data => { - await kfUserService.createManyWithServerId(data); + set({ kfUserList: data }); + // await kfUserService.createManyWithServerId(data); }, // 获取客服列表 getkfUserList: async () => { @@ -41,12 +42,12 @@ export const useCkChatStore = createPersistStore( set({ chatSessions: data }); }, // 异步设置联系人列表 - asyncContractList: data => { - set({ contractList: data }); + asyncContractList: async (data: ContractData[]) => { + await contractService.createManyWithServerId(data); }, //异步设置联系人分组 - asyncWeChatGroup: (data: ContactGroupByLabel[]) => { - set({ weChatGroup: data }); + asyncWeChatGroup: async (data: weChatGroup[]) => { + await weChatGroupService.createManyWithServerId(data); }, //获取选中的客服信息 getgetKfSelectedUser: () => { @@ -210,3 +211,5 @@ export const asyncNewContractList = ( ) => useCkChatStore.getState().asyncNewContractList(data); export const asyncKfSelected = (data: number) => useCkChatStore.getState().asyncKfSelected(data); +export const asyncWeChatGroup = (data: weChatGroup[]) => + useCkChatStore.getState().asyncWeChatGroup(data); diff --git a/Cunkebao/src/utils/db.ts b/Cunkebao/src/utils/db.ts index 33ccc16b..3257fae1 100644 --- a/Cunkebao/src/utils/db.ts +++ b/Cunkebao/src/utils/db.ts @@ -38,7 +38,7 @@ export interface KfUserWithServerId extends Omit { id?: number; // 接口数据的原始ID字段 } -export interface GroupWithServerId extends Omit { +export interface weChatGroupServerId extends Omit { serverId: number | string; // 服务器ID作为主键 id?: number; // 接口数据的原始ID字段 } @@ -60,7 +60,7 @@ export interface NewContactListData { // 数据库类 class CunkebaoDatabase extends Dexie { kfUsers!: Table; - weChatGroup!: Table; + weChatGroup!: Table; contracts!: Table; newContractList!: Table;