通讯录群详情新增手动同步群公告按钮

变更项:1) 新增POST接口用于手动同步群公告,仅在手动触发时调用Group/GetChatRoomInfoDetail。2) 同步逻辑采用基础群信息与Detail信息合并后再落库,确保公告可更新且不破坏原有群资料。3) 群详情页公告区域新增同步按钮和加载态,避免重复点击。4) 同步成功后自动刷新当前群资料。5) 补充中文注释说明手动同步链路。
This commit is contained in:
liuwei
2026-04-16 17:24:49 +08:00
parent da1cf5bd02
commit 09eff21761
2 changed files with 102 additions and 1 deletions

View File

@@ -444,6 +444,73 @@ def api_group_profile(roomid):
return jsonify({"success": False, "error": str(e)}), 500
@contacts_bp.route('/api/group_profile/<roomid>/sync_announcement', methods=['POST'])
@login_required
def api_sync_group_announcement(roomid):
"""手动同步指定群公告(调用 /Group/GetChatRoomInfoDetail"""
try:
server = current_app.dashboard_server
if not roomid:
return jsonify({"success": False, "error": "缺少群ID"}), 400
if not getattr(server, "robot", None) or not getattr(server.robot, "ipad_bot", None):
return jsonify({"success": False, "error": "机器人实例未初始化"}), 503
async def fetch_and_merge():
# 先拉基础群信息,再拉 Detail 信息,最后合并,避免只用 Detail 导致字段不完整。
base_info = await server.robot.ipad_bot.get_chatroom_info(roomid)
detail_info = await server.robot.ipad_bot.get_chatroom_announce(roomid)
merged_info = dict(base_info or {})
detail_contact = None
if isinstance(detail_info, dict):
contact_list = detail_info.get("ContactList")
if isinstance(contact_list, list) and contact_list:
first = contact_list[0]
if isinstance(first, dict):
detail_contact = first
if detail_contact:
merged_info.update(detail_contact)
if isinstance(detail_info, dict):
merged_info.update(detail_info)
# 统一公告字段命名,供 contacts_db.save_chatroom_info 直接提取入库。
announcement = (
merged_info.get("ChatRoomAnnouncement")
or merged_info.get("Announcement")
or merged_info.get("Annoucement")
or merged_info.get("AnnouncementContent")
or merged_info.get("chatRoomAnnouncement")
)
if announcement:
merged_info["ChatRoomAnnouncement"] = announcement
# 保底补上群ID避免少数字段缺失导致无法更新到对应群。
if not merged_info.get("UserName"):
merged_info["UserName"] = roomid
return merged_info
merged_info = asyncio.run(fetch_and_merge())
if not merged_info:
return jsonify({"success": False, "error": "获取群详情失败"}), 500
save_ok = server.contact_db.save_chatroom_info(merged_info)
if not save_ok:
return jsonify({"success": False, "error": "保存群公告失败"}), 500
profile = server.contact_db.get_chatroom_profile(roomid)
return jsonify({
"success": True,
"message": "群公告同步成功",
"data": {
"profile": profile
}
})
except Exception as e:
logger.error(f"手动同步群公告失败: {e}")
return jsonify({"success": False, "error": str(e)}), 500
@contacts_bp.route('/api/group_member_context/<roomid>/<wxid>', methods=['GET'])
@login_required
def api_group_member_context(roomid, wxid):