Files
CKB-Touchkebao/msgManage_new_logic.ts

149 lines
5.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 新的消息处理逻辑 - 需要替换到 src/store/module/websocket/msgManage.ts 的第 150-335 行
// 更新新架构的SessionStore增量更新索引和缓存
try {
const userId = useCustomerStore.getState().currentCustomer?.userId || 0;
if (userId > 0) {
// 1. 先检查联系人是否存在于本地数据库
console.log("🔍 [新消息] 检查联系人是否存在:", {
sessionId,
type,
userId,
});
const existingContact = await ContactManager.getContactByIdAndType(
userId,
sessionId,
type,
);
// 2. 如果联系人不存在,先请求 API 补齐数据
if (!existingContact) {
console.log("⚠️ [新消息] 联系人不存在,先请求 API 补齐数据:", {
sessionId,
type,
});
try {
let detailResult: any = null;
if (type === "friend") {
detailResult = await getWechatFriendDetail({
id: sessionId,
});
} else {
detailResult = await getWechatChatroomDetail({
id: sessionId,
});
}
const detail = detailResult?.detail;
if (detail) {
console.log("✅ [新消息] 成功获取详情,创建联系人:", {
id: detail.id,
nickname: detail.nickname,
avatar: detail.avatar || detail.chatroomAvatar,
});
// 创建联系人数据
const newContact: any = {
serverId: `${type}_${sessionId}_${wechatAccountId}`,
userId,
id: sessionId,
type,
wechatAccountId: detail.wechatAccountId || wechatAccountId,
nickname: detail.nickname || "",
conRemark: detail.conRemark || "",
avatar:
type === "group"
? detail.chatroomAvatar || ""
: detail.avatar || "",
lastUpdateTime: new Date().toISOString(),
sortKey: "",
searchKey: (
detail.conRemark ||
detail.nickname ||
""
).toLowerCase(),
};
// 添加类型特定字段
if (type === "group") {
Object.assign(newContact, {
chatroomId: detail.chatroomId || "",
chatroomOwner: detail.chatroomOwner || "",
selfDisplayName:
detail.selfDisplyName || detail.selfDisplayName || "",
notice: detail.notice || "",
});
} else {
Object.assign(newContact, {
wechatFriendId: detail.id,
wechatId: detail.wechatId || "",
alias: detail.alias || "",
gender: detail.gender,
region: detail.region || "",
signature: detail.signature || "",
phone: detail.phone || "",
quanPin: detail.quanPin || "",
groupId: detail.groupId,
});
}
// 添加到联系人数据库
await ContactManager.addContact(newContact);
console.log("✅ [新消息] 联系人已添加到数据库");
} else {
console.warn("❌ [新消息] API 返回空数据,无法创建联系人");
}
} catch (error) {
console.error("❌ [新消息] 请求 API 补齐数据失败:", error);
}
} else {
console.log("✅ [新消息] 联系人已存在:", {
id: existingContact.id,
nickname: existingContact.nickname,
avatar: existingContact.avatar ? "有" : "无",
});
}
// 3. 从数据库获取或创建会话信息
const updatedSession = await Promise.race([
MessageManager.getSessionByContactId(userId, sessionId, type),
new Promise<null>(resolve => setTimeout(() => resolve(null), 5000)), // 5秒超时
]);
if (updatedSession) {
const messageStore = useMessageStore.getState();
// 增量更新索引
messageStore.addSession(updatedSession);
// 失效缓存,下次切换账号时会重新计算
messageStore.invalidateCache(wechatAccountId);
messageStore.invalidateCache(0); // 也失效"全部"的缓存
// 更新会话列表缓存(不阻塞主流程)
const cacheKey = `sessions_${wechatAccountId}`;
sessionListCache
.get<ChatSession[]>(cacheKey)
.then(cachedSessions => {
if (cachedSessions) {
// 更新缓存中的会话
const index = cachedSessions.findIndex(
s => s.id === updatedSession.id && s.type === updatedSession.type,
);
if (index >= 0) {
cachedSessions[index] = updatedSession;
} else {
cachedSessions.push(updatedSession);
}
return sessionListCache.set(cacheKey, cachedSessions);
}
})
.catch(error => {
console.error("更新会话缓存失败:", error);
});
}
}
} catch (error) {
console.error("更新SessionStore失败:", error);
}