重构数据库管理逻辑,新增旧数据库清理功能,优化数据库初始化流程,更新联系人管理和消息处理逻辑,提升代码可读性和用户体验。

This commit is contained in:
2025-10-24 16:14:13 +08:00
parent e1d4f678ab
commit 28cbcea4f9
13 changed files with 278 additions and 561 deletions

View File

@@ -1,5 +1,5 @@
//构建联系人列表标签
import { weChatGroupService, contractService } from "@/utils/db";
import { contactUnifiedService } from "@/utils/db";
import { request } from "@/api/request2";
import { ContactGroupByLabel } from "@/pages/pc/ckbox/data";
@@ -11,7 +11,6 @@ export const createContractList = async (
kfSelected: number,
countLables: ContactGroupByLabel[],
) => {
// 根据 groupType 决定查询不同的服务
const realGroup = countLables
.filter(item => item.id !== 0)
.map(item => item.id);
@@ -19,44 +18,27 @@ export const createContractList = async (
const dataByLabels = [];
for (const label of countLables) {
let data;
if (Number(label.groupType) === 1) {
if (label.id == 0) {
data = await contractService.findWhereMultiple([
{
field: "groupId",
operator: "notIn",
value: realGroup,
},
]);
} else {
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 (Number(label.groupType) === 2) {
// groupType: 2, 查询 weChatGroupService
if (label.id == 0) {
data = await weChatGroupService.findWhereMultiple([
{
field: "groupId",
operator: "notIn",
value: realGroup,
},
]);
} else {
data = await weChatGroupService.findWhere("groupId", label.id);
// 过滤出 kfSelected 对应的联系人
}
if (kfSelected && kfSelected != 0) {
data = data.filter(contact => contact.wechatAccountId === kfSelected);
}
// 使用统一的联系人服务查询
if (label.id == 0) {
// 未分组:排除所有已分组的联系人
data = await contactUnifiedService.findWhereMultiple([
{
field: "groupId",
operator: "notIn",
value: realGroup,
},
]);
} else {
console.warn(`未知的 groupType: ${label.groupType}`);
data = [];
// 指定分组:查询该分组的联系人
data = await contactUnifiedService.findWhere("groupId", label.id);
}
// 过滤出 kfSelected 对应的联系人
if (kfSelected && kfSelected != 0) {
data = data.filter(contact => contact.wechatAccountId === kfSelected);
}
dataByLabels.push({
...label,
contacts: data,

View File

@@ -7,9 +7,10 @@ import {
KfUserListData,
ContactGroupByLabel,
} from "@/pages/pc/ckbox/data";
import { weChatGroupService, contractService } from "@/utils/db";
import { ContactManager } from "@/utils/dbAction";
import { createContractList } from "@/store/module/ckchat/api";
import { useWeChatStore } from "@/store/module/weChat/weChat";
import { useUserStore } from "@/store/module/user";
// 从weChat store获取clearCurrentContact方法
const getClearCurrentContact = () =>
useWeChatStore.getState().clearCurrentContact;
@@ -69,7 +70,27 @@ export const useCkChatStore = createPersistStore<CkChatState>(
},
//异步设置联系人分组
asyncWeChatGroup: async (data: weChatGroup[]) => {
await weChatGroupService.createManyWithServerId(data);
const currentUserId = useUserStore.getState().user?.id || 0;
// 转换为统一的 Contact 格式并保存
await ContactManager.addContacts(
data.map(g => ({
serverId: `group_${g.id}`,
userId: currentUserId,
id: g.id,
type: "group" as const,
wechatAccountId: g.wechatAccountId,
nickname: g.nickname,
conRemark: g.conRemark,
avatar: g.chatroomAvatar || "",
lastUpdateTime: new Date().toISOString(),
sortKey: "",
searchKey: (g.conRemark || g.nickname || "").toLowerCase(),
chatroomId: g.chatroomId,
chatroomOwner: g.chatroomOwner,
selfDisplayName: g.selfDisplyName,
notice: g.notice,
})),
);
},
//获取选中的客服信息
getKfSelectedUser: () => {

View File

@@ -18,7 +18,8 @@ import {
getFriendInjectConfig,
} from "@/pages/pc/ckbox/api";
import { ChatRecord, ContractData, weChatGroup } from "@/pages/pc/ckbox/data";
import { weChatGroupService, contractService } from "@/utils/db";
import { ContactManager } from "@/utils/dbAction";
import { useUserStore } from "@/store/module/user";
import {
addChatSession,
updateChatSession,
@@ -323,21 +324,21 @@ export const useWeChatStore = create<WeChatState>()(
pinChatSessionToTop(getMessageId);
} else {
// 如果会话不存在,创建新会话
if (isWechatGroup) {
const [group] = await weChatGroupService.findByIds(getMessageId);
if (group) {
addChatSession({
...group,
config: { unreadCount: 1 },
});
// 新创建的会话会自动添加到列表顶部,无需额外置顶
}
} else {
const [user] = await contractService.findByIds(getMessageId);
const currentUserId = useUserStore.getState().user?.id || 0;
const contactType = isWechatGroup ? "group" : "friend";
// 从统一联系人表查询
const contacts =
await ContactManager.getUserContacts(currentUserId);
const contact = contacts.find(
c => c.id === getMessageId[0] && c.type === contactType,
);
if (contact) {
addChatSession({
...user,
...contact,
config: { unreadCount: 1 },
});
} as any);
// 新创建的会话会自动添加到列表顶部,无需额外置顶
}
}

View File

@@ -18,7 +18,8 @@ import {
getFriendInjectConfig,
} from "@/pages/pc/ckbox/api";
import { ChatRecord, ContractData, weChatGroup } from "@/pages/pc/ckbox/data";
import { weChatGroupService, contractService } from "@/utils/db";
import { ContactManager } from "@/utils/dbAction";
import { useUserStore } from "@/store/module/user";
import {
addChatSession,
updateChatSession,
@@ -342,22 +343,21 @@ export const useWeChatStore = create<WeChatState>()(
pinChatSessionToTop(getMessageId);
} else {
// 如果会话不存在,创建新会话
if (isWechatGroup) {
const [group] =
await weChatGroupService.findByIds(getMessageId);
if (group) {
addChatSession({
...group,
config: { unreadCount: 1 },
});
// 新创建的会话会自动添加到列表顶部,无需额外置顶
}
} else {
const [user] = await contractService.findByIds(getMessageId);
const currentUserId = useUserStore.getState().user?.id || 0;
const contactType = isWechatGroup ? "group" : "friend";
// 从统一联系人表查询
const contacts =
await ContactManager.getUserContacts(currentUserId);
const contact = contacts.find(
c => c.id === getMessageId[0] && c.type === contactType,
);
if (contact) {
addChatSession({
...user,
...contact,
config: { unreadCount: 1 },
});
} as any);
// 新创建的会话会自动添加到列表顶部,无需额外置顶
}
}