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

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

@@ -16,12 +16,6 @@
*/
import Dexie, { Table } from "dexie";
import {
KfUserListData,
weChatGroup,
ContractData,
MessageListData,
} from "@/pages/pc/ckbox/data";
// ==================== 用户登录记录 ====================
export interface UserLoginRecord {
@@ -119,32 +113,9 @@ export interface ContactLabelMap {
lastUpdateTime: string;
}
// ==================== 保留原有数据类型(向后兼容) ====================
export interface KfUserWithServerId extends Omit<KfUserListData, "id"> {
serverId: number | string; // 服务器ID作为主键
userId: number; // 用户ID数据隔离
id?: number; // 接口数据的原始ID字段
}
export interface NewContactListData {
serverId: number | string; // 服务器ID作为主键
userId: number; // 用户ID数据隔离
id?: number; // 接口数据的原始ID字段
groupName: string;
contacts: Contact[];
weChatGroup: Contact[];
}
// 数据库类
class CunkebaoDatabase extends Dexie {
// ==================== 保留原有表(向后兼容) ====================
kfUsers!: Table<KfUserWithServerId>;
weChatGroup!: Table<weChatGroup>;
contracts!: Table<ContractData>;
newContactList!: Table<NewContactListData>;
messageList!: Table<MessageListData>;
// ==================== 新增统一表 ====================
// ==================== 统一表结构 ====================
chatSessions!: Table<ChatSession>; // 统一会话表
contactsUnified!: Table<Contact>; // 统一联系人表
contactLabelMap!: Table<ContactLabelMap>; // 联系人标签映射表
@@ -153,63 +124,15 @@ class CunkebaoDatabase extends Dexie {
constructor() {
super("CunkebaoDatabase");
// 版本1使用serverId作为主键的架构保留原有表
// 版本1统一表结构
this.version(1).stores({
kfUsers:
"serverId, id, tenantId, wechatId, nickname, alias, avatar, gender, region, signature, bindQQ, bindEmail, bindMobile, createTime, currentDeviceId, isDeleted, deleteTime, groupId, memo, wechatVersion, lastUpdateTime, isOnline",
weChatGroup:
"serverId, id, wechatAccountId, tenantId, accountId, chatroomId, chatroomOwner, conRemark, nickname, chatroomAvatar,wechatChatroomId, groupId, config, notice, selfDisplyName",
contracts:
"serverId, id, wechatAccountId, wechatId, alias, conRemark, nickname, quanPin, avatar, gender, region, addFrom, phone, signature, accountId, extendFields, city, lastUpdateTime, isPassed, tenantId, groupId, thirdParty, additionalPicture, desc, config, lastMessageTime, duplicate",
newContactList: "serverId, id, groupName, contacts",
messageList:
"serverId, id, dataType, wechatAccountId, tenantId, accountId, nickname, avatar, groupId, config, labels, wechatId, alias, conRemark, quanPin, gender, region, addFrom, phone, signature, extendFields, city, lastUpdateTime, isPassed, thirdParty, additionalPicture, desc, lastMessageTime, duplicate, chatroomId, chatroomOwner, chatroomAvatar, notice, selfDisplyName",
});
// 版本2添加用户隔离字段到原有表
this.version(2)
.stores({
kfUsers:
"serverId, userId, id, tenantId, wechatId, nickname, alias, avatar, gender, region, signature, bindQQ, bindEmail, bindMobile, createTime, currentDeviceId, isDeleted, deleteTime, groupId, memo, wechatVersion, lastUpdateTime, isOnline",
weChatGroup:
"serverId, userId, id, wechatAccountId, tenantId, accountId, chatroomId, chatroomOwner, conRemark, nickname, chatroomAvatar,wechatChatroomId, groupId, config, notice, selfDisplyName",
contracts:
"serverId, userId, id, wechatAccountId, wechatId, alias, conRemark, nickname, quanPin, avatar, gender, region, addFrom, phone, signature, accountId, extendFields, city, lastUpdateTime, isPassed, tenantId, groupId, thirdParty, additionalPicture, desc, config, lastMessageTime, duplicate",
newContactList: "serverId, userId, id, groupName, contacts",
messageList:
"serverId, userId, id, dataType, wechatAccountId, tenantId, accountId, nickname, avatar, groupId, config, labels, wechatId, alias, conRemark, quanPin, gender, region, addFrom, phone, signature, extendFields, city, lastUpdateTime, isPassed, thirdParty, additionalPicture, desc, lastMessageTime, duplicate, chatroomId, chatroomOwner, chatroomAvatar, notice, selfDisplyName",
})
.upgrade(trans => {
// 为现有数据添加默认userId=0需要手动清理
return trans
.table("kfUsers")
.toCollection()
.modify(item => {
item.userId = 0;
});
});
// 版本3添加新的统一表和用户登录记录表
this.version(3).stores({
// 保留原有表
kfUsers:
"serverId, userId, id, tenantId, wechatId, nickname, alias, avatar, gender, region, signature, bindQQ, bindEmail, bindMobile, createTime, currentDeviceId, isDeleted, deleteTime, groupId, memo, wechatVersion, lastUpdateTime, isOnline",
weChatGroup:
"serverId, userId, id, wechatAccountId, tenantId, accountId, chatroomId, chatroomOwner, conRemark, nickname, chatroomAvatar,wechatChatroomId, groupId, config, notice, selfDisplyName",
contracts:
"serverId, userId, id, wechatAccountId, wechatId, alias, conRemark, nickname, quanPin, avatar, gender, region, addFrom, phone, signature, accountId, extendFields, city, lastUpdateTime, isPassed, tenantId, groupId, thirdParty, additionalPicture, desc, config, lastMessageTime, duplicate",
newContactList: "serverId, userId, id, groupName, contacts",
messageList:
"serverId, userId, id, dataType, wechatAccountId, tenantId, accountId, nickname, avatar, groupId, config, labels, wechatId, alias, conRemark, quanPin, gender, region, addFrom, phone, signature, extendFields, city, lastUpdateTime, isPassed, thirdParty, additionalPicture, desc, lastMessageTime, duplicate, chatroomId, chatroomOwner, chatroomAvatar, notice, selfDisplyName",
// 新增统一表
// 会话表索引:支持按用户、类型、时间、置顶等查询
chatSessions:
"serverId, userId, id, type, wechatAccountId, [userId+type], [userId+wechatAccountId], [userId+lastUpdateTime], sortKey, nickname, conRemark, avatar, content, lastUpdateTime",
// 联系人表索引:支持按用户、类型、标签、搜索等查询
contactsUnified:
"serverId, userId, id, type, wechatAccountId, [userId+type], [userId+wechatAccountId], sortKey, searchKey, nickname, conRemark, avatar, lastUpdateTime",
"serverId, userId, id, type, wechatAccountId, [userId+type], [userId+wechatAccountId], sortKey, searchKey, nickname, conRemark, avatar, lastUpdateTime, groupId",
// 联系人标签映射表索引:支持按用户、标签、联系人、类型查询
contactLabelMap:
@@ -479,14 +402,7 @@ export class DatabaseService<T> {
}
}
// 创建表的服务实例
export const kfUserService = new DatabaseService(db.kfUsers);
export const weChatGroupService = new DatabaseService(db.weChatGroup);
export const contractService = new DatabaseService(db.contracts);
export const newContactListService = new DatabaseService(db.newContactList);
export const messageListService = new DatabaseService(db.messageList);
// 新增统一表服务
// 创建统一表的服务实例
export const chatSessionService = new DatabaseService(db.chatSessions);
export const contactUnifiedService = new DatabaseService(db.contactsUnified);
export const contactLabelMapService = new DatabaseService(db.contactLabelMap);