更新ContractData接口以使extendFields可选并修改相关序列化逻辑。增强数据库模式以支持extendFields作为JSON字符串,并确保在数据操作期间进行正确处理。改进ContactManager中的比较逻辑以包含extendFields。

This commit is contained in:
2025-11-13 17:51:22 +08:00
parent a6ee45f3e3
commit c41d8125da
8 changed files with 136 additions and 19 deletions

View File

@@ -62,6 +62,7 @@ export interface ChatSession {
notice?: string; // 群公告
phone?: string; // 联系人电话
region?: string; // 联系人地区
extendFields?: string; // 扩展字段JSON 字符串)
}
// ==================== 统一联系人表(兼容好友和群聊) ====================
@@ -92,6 +93,7 @@ export interface Contact {
signature?: string; // 个性签名
phone?: string; // 手机号
quanPin?: string; // 全拼
extendFields?: string; // 扩展字段JSON 字符串)
// 群聊特有字段type='group'时有效)
chatroomId?: string; // 群聊ID
@@ -147,6 +149,41 @@ class CunkebaoDatabase extends Dexie {
userLoginRecords:
"serverId, userId, lastLoginTime, loginCount, createTime, lastActiveTime",
});
this.version(2)
.stores({
chatSessions:
"serverId, userId, id, type, wechatAccountId, [userId+type], [userId+wechatAccountId], [userId+lastUpdateTime], [userId+aiType], sortKey, nickname, conRemark, avatar, content, lastUpdateTime, aiType, phone, region, extendFields",
contactsUnified:
"serverId, userId, id, type, wechatAccountId, [userId+type], [userId+wechatAccountId], [userId+aiType], sortKey, searchKey, nickname, conRemark, avatar, lastUpdateTime, groupId, aiType, phone, region, extendFields",
contactLabelMap:
"serverId, userId, labelId, contactId, contactType, [userId+labelId], [userId+contactId], [userId+labelId+sortKey], sortKey, searchKey, avatar, nickname, conRemark, unreadCount, lastUpdateTime",
userLoginRecords:
"serverId, userId, lastLoginTime, loginCount, createTime, lastActiveTime",
})
.upgrade(async tx => {
await tx
.table("chatSessions")
.toCollection()
.modify(session => {
if (!("extendFields" in session) || session.extendFields == null) {
session.extendFields = "{}";
} else if (typeof session.extendFields !== "string") {
session.extendFields = JSON.stringify(session.extendFields);
}
});
await tx
.table("contactsUnified")
.toCollection()
.modify(contact => {
if (!("extendFields" in contact) || contact.extendFields == null) {
contact.extendFields = "{}";
} else if (typeof contact.extendFields !== "string") {
contact.extendFields = JSON.stringify(contact.extendFields);
}
});
});
}
}
@@ -295,18 +332,18 @@ export class DatabaseService<T> {
// 基础 CRUD 操作 - 使用serverId作为主键
async create(data: Omit<T, "serverId">): Promise<string | number> {
return await this.table.add(data as T);
return await this.table.add(this.prepareDataForWrite(data) as T);
}
// 创建数据(直接使用接口数据)
// 接口数据的id字段直接作为serverId主键原id字段保留
async createWithServerId(data: any): Promise<string | number> {
const dataToInsert = {
const dataToInsert = this.prepareDataForWrite({
...data,
serverId: data.id, // 使用接口的id作为serverId主键
phone: data.phone ?? "",
region: data.region ?? "",
};
});
return await this.table.add(dataToInsert as T);
}
@@ -325,7 +362,10 @@ export class DatabaseService<T> {
}
async update(serverId: string | number, data: Partial<T>): Promise<number> {
return await this.table.update(serverId, data as any);
return await this.table.update(
serverId,
this.prepareDataForWrite(data) as any,
);
}
async updateMany(
@@ -334,7 +374,7 @@ export class DatabaseService<T> {
return await this.table.bulkUpdate(
dataList.map(item => ({
key: item.serverId,
changes: item.data as any,
changes: this.prepareDataForWrite(item.data) as any,
})),
);
}
@@ -342,7 +382,8 @@ export class DatabaseService<T> {
async createMany(
dataList: Omit<T, "serverId">[],
): Promise<(string | number)[]> {
return await this.table.bulkAdd(dataList as T[], { allKeys: true });
const processed = dataList.map(item => this.prepareDataForWrite(item));
return await this.table.bulkAdd(processed as T[], { allKeys: true });
}
// 批量创建数据(直接使用接口数据)
@@ -366,12 +407,14 @@ export class DatabaseService<T> {
return [];
}
const processedData = newData.map(item => ({
...item,
serverId: item.id, // 使用接口的id作为serverId主键
phone: item.phone ?? "",
region: item.region ?? "",
}));
const processedData = newData.map(item =>
this.prepareDataForWrite({
...item,
serverId: item.id, // 使用接口的id作为serverId主键
phone: item.phone ?? "",
region: item.region ?? "",
}),
);
return await this.table.bulkAdd(processedData as T[], { allKeys: true });
}
@@ -545,6 +588,27 @@ export class DatabaseService<T> {
.equals(value)
.count();
}
private prepareDataForWrite(data: any) {
if (!data || typeof data !== "object") {
return data;
}
const prepared = { ...data };
if ("extendFields" in prepared) {
const value = prepared.extendFields;
if (typeof value === "string" && value.trim() !== "") {
prepared.extendFields = value;
} else if (value && typeof value === "object") {
prepared.extendFields = JSON.stringify(value);
} else {
prepared.extendFields = "{}";
}
}
return prepared;
}
}
// 创建统一表的服务实例