From 11a4ec8c676bb918fce001001d6e8ea5cba155cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B9=98=E9=A3=8E?= Date: Mon, 1 Dec 2025 15:09:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8MessageList=E7=BB=84=E4=BB=B6=E4=B8=AD?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9C=AA=E7=9F=A5=E8=81=94=E7=B3=BB=E4=BA=BA?= =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E6=8B=89=E5=8F=96=E7=BC=BA=E5=A4=B1=E7=9A=84=E5=A4=B4=E5=83=8F?= =?UTF-8?q?=E5=92=8C=E6=98=B5=E7=A7=B0=E4=BF=A1=E6=81=AF=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BC=9A=E8=AF=9D=E5=92=8C=E8=81=94=E7=B3=BB?= =?UTF-8?q?=E4=BA=BA=E6=95=B0=E6=8D=AE=E5=BA=93=EF=BC=8C=E4=BB=A5=E6=8F=90?= =?UTF-8?q?=E5=8D=87=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E7=9A=84=E5=87=86?= =?UTF-8?q?=E7=A1=AE=E6=80=A7=E5=92=8C=E5=AE=8C=E6=95=B4=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SidebarMenu/MessageList/index.tsx | 181 ++++++++++++------ 1 file changed, 127 insertions(+), 54 deletions(-) diff --git a/Touchkebao/src/pages/pc/ckbox/weChat/components/SidebarMenu/MessageList/index.tsx b/Touchkebao/src/pages/pc/ckbox/weChat/components/SidebarMenu/MessageList/index.tsx index 1e08ff0a..efbcbad8 100644 --- a/Touchkebao/src/pages/pc/ckbox/weChat/components/SidebarMenu/MessageList/index.tsx +++ b/Touchkebao/src/pages/pc/ckbox/weChat/components/SidebarMenu/MessageList/index.tsx @@ -47,6 +47,7 @@ const MessageList: React.FC = () => { } = useMessageStore(); const [filteredSessions, setFilteredSessions] = useState([]); const [syncing, setSyncing] = useState(false); // 同步状态 + const hasEnrichedRef = useRef(false); // 是否已做过未知联系人补充 // 右键菜单相关状态 const [contextMenu, setContextMenu] = useState<{ @@ -296,7 +297,130 @@ const MessageList: React.FC = () => { }; }, [contextMenu.visible]); - // ==================== 数据加载 ==================== + // ==================== 数据加载 & 未知联系人补充 ==================== + + // 同步完成后,检查是否存在“未知联系人”或缺失头像/昵称的会话,并异步补充详情 + const enrichUnknownContacts = async () => { + if (!currentUserId) return; + if (hasEnrichedRef.current) return; // 避免重复执行 + + // 只在会话有数据时执行 + if (!sessions || sessions.length === 0) return; + + const needEnrich = sessions.filter(s => { + const noName = !s.conRemark && !s.nickname && !s.wechatId; + const isUnknownNickname = s.nickname === "未知联系人"; + const noAvatar = !s.avatar; + return noName || isUnknownNickname || noAvatar; + }); + + if (needEnrich.length === 0) { + hasEnrichedRef.current = true; + return; + } + + hasEnrichedRef.current = true; + + // 逐个异步拉取详情,失败不打断整体流程 + for (const session of needEnrich) { + try { + let detailResult: any = null; + if (session.type === "friend") { + detailResult = await getWechatFriendDetail({ id: session.id }); + } else { + detailResult = await getWechatChatroomDetail({ id: session.id }); + } + + const detail = detailResult?.detail; + if (!detail) continue; + + // 更新会话列表 UI + setSessionState(prev => + prev.map(s => + s.id === session.id && s.type === session.type + ? { + ...s, + avatar: + session.type === "group" + ? detail.chatroomAvatar || s.avatar + : detail.avatar || s.avatar, + nickname: detail.nickname || s.nickname, + conRemark: detail.conRemark || s.conRemark, + wechatId: detail.wechatId || s.wechatId, + } + : s, + ), + ); + + // 同步到会话数据库 + await MessageManager.updateSession({ + userId: currentUserId, + id: session.id, + type: session.type, + avatar: + session.type === "group" + ? detail.chatroomAvatar || session.avatar + : detail.avatar || session.avatar, + nickname: detail.nickname || session.nickname, + conRemark: detail.conRemark || session.conRemark, + wechatId: detail.wechatId || session.wechatId, + }); + + // 同步到联系人数据库(方便后续搜索、其它页面使用) + const contactBase: any = { + serverId: `${session.type}_${session.id}`, + userId: currentUserId, + id: session.id, + type: session.type, + wechatAccountId: detail.wechatAccountId, + nickname: detail.nickname || "", + conRemark: detail.conRemark || "", + avatar: + session.type === "group" + ? detail.chatroomAvatar || "" + : detail.avatar || "", + lastUpdateTime: new Date().toISOString(), + sortKey: "", + searchKey: (detail.conRemark || detail.nickname || "").toLowerCase(), + }; + + if (session.type === "group") { + Object.assign(contactBase, { + chatroomId: detail.chatroomId, + chatroomOwner: detail.chatroomOwner, + selfDisplayName: detail.selfDisplyName, + notice: detail.notice, + }); + } else { + Object.assign(contactBase, { + 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, + }); + } + + // 使用 upsert 逻辑:如果已存在就更新,不存在则新增 + const existContact = await ContactManager.getContactByIdAndType( + currentUserId, + session.id, + session.type, + ); + if (existContact) { + await ContactManager.updateContact(contactBase); + } else { + await ContactManager.addContact(contactBase); + } + } catch (error) { + console.error("补拉未知联系人详情失败:", error, session); + } + } + }; // 与服务器同步数据(优化版:逐页同步,立即更新UI) const syncWithServer = async () => { @@ -379,6 +503,8 @@ const MessageList: React.FC = () => { console.log( `会话同步完成: 成功${successCount}页, 失败${failCount}页, 共处理${totalProcessed}条数据`, ); + // 同步完成后,异步补充未知联系人信息 + enrichUnknownContacts(); } catch (error) { console.error("同步服务器数据失败:", error); } finally { @@ -805,59 +931,6 @@ const MessageList: React.FC = () => { // 设置当前会话 setCurrentContact(session as any); - // 如果头像或昵称缺失,则尝试拉取详情并更新列表 - const needFetchDetail = - !session.avatar || - !(session.conRemark || session.nickname || session.wechatId); - - if (needFetchDetail) { - try { - let detailResult: any = null; - if (session.type === "friend") { - detailResult = await getWechatFriendDetail({ id: session.id }); - } else { - detailResult = await getWechatChatroomDetail({ id: session.id }); - } - - const detail = detailResult?.detail; - if (detail) { - // 1. 更新会话列表中的该条记录(驱动 UI 立即更新头像和昵称) - setSessionState(prev => - prev.map(s => - s.id === session.id && s.type === session.type - ? { - ...s, - avatar: - session.type === "group" - ? detail.chatroomAvatar || s.avatar - : detail.avatar || s.avatar, - nickname: detail.nickname || s.nickname, - conRemark: detail.conRemark || s.conRemark, - wechatId: detail.wechatId || s.wechatId, - } - : s, - ), - ); - - // 2. 同步到会话数据库,保证刷新后数据仍然是最新的 - await MessageManager.updateSession({ - userId: currentUserId, - id: session.id, - type: session.type, - avatar: - session.type === "group" - ? detail.chatroomAvatar || session.avatar - : detail.avatar || session.avatar, - nickname: detail.nickname || session.nickname, - conRemark: detail.conRemark || session.conRemark, - wechatId: detail.wechatId || session.wechatId, - }); - } - } catch (e) { - console.error("点击会话补拉详情失败:", e); - } - } - // 标记为已读(不更新时间和排序) if (session.config.unreadCount > 0) { // 立即更新UI(只更新未读数量)