From d175c5838002a6ede36b9415ad673bcb7a05af88 Mon Sep 17 00:00:00 2001 From: liuwei Date: Mon, 28 Apr 2025 10:53:10 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=E4=BF=A1=E6=81=AF=E5=AD=98?= =?UTF-8?q?=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/contacts_db.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++ robot.py | 5 ++-- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/db/contacts_db.py b/db/contacts_db.py index 6881791..17e8308 100644 --- a/db/contacts_db.py +++ b/db/contacts_db.py @@ -322,7 +322,74 @@ class ContactsDBOperator(BaseDBOperator): except Exception as e: self.logger.error(f"获取所有联系人名称映射失败: {e}") return {} + def save_chatroom_member_simple(self, chatroom_id: str, member_details: List[Dict]) -> bool: + """保存简化版的群成员信息到数据库 + + Args: + chatroom_id: 群聊ID + member_details: 群成员信息列表,格式为: + [{'wxid': str, 'nickName': str, 'inviterUserName': str, + 'memberFlag': int, 'displayName': str, + 'bigHeadImgUrl': str, 'smallHeadImgUrl': str}] + + Returns: + bool: 是否成功保存 + """ + if not member_details or not chatroom_id: + self.logger.warning(f"没有群聊{chatroom_id}的成员信息需要保存") + return False + try: + for member in member_details: + wxid = member.get('wxid', '') + if not wxid: + continue + + # 构建数据 + data = { + 'chatroom_id': chatroom_id, + 'wxid': wxid, + 'nick_name': member.get('nickName', ''), + 'display_name': member.get('displayName', ''), + 'inviter_user_name': member.get('inviterUserName', ''), + 'member_flag': member.get('memberFlag', 0), + 'big_head_img_url': member.get('bigHeadImgUrl', ''), + 'small_head_img_url': member.get('smallHeadImgUrl', ''), + 'is_owner': 0, # 默认值 + 'is_admin': 0, # 默认值 + # 其他字段使用默认值 + 'sex': 0, + 'signature': '', + 'alias': '', + 'country': '', + 'province': '', + 'city': '', + 'label_list': '', + 'phone_num_list': '', + 'py_initial': '', + 'quan_pin': '', + 'remark_py_initial': '', + 'remark_quan_pin': '' + } + + # 构建SQL语句 + fields = ', '.join(data.keys()) + placeholders = ', '.join(['%s'] * len(data)) + values = tuple(data.values()) + + sql = f""" + REPLACE INTO t_chatroom_member ({fields}) + VALUES ({placeholders}) + """ + + self.execute_update(sql, values) + + self.logger.info(f"成功保存群聊{chatroom_id}的{len(member_details)}个成员信息") + return True + + except Exception as e: + self.logger.error(f"保存群聊{chatroom_id}的成员信息失败: {e}") + return False def save_chatroom_member_detail(self, chatroom_id: str, member_details: List[Dict]) -> bool: """保存群成员详细信息到数据库 diff --git a/robot.py b/robot.py index 14971cb..b4dc4f4 100644 --- a/robot.py +++ b/robot.py @@ -170,15 +170,14 @@ class Robot(Job): self.LOG.info(f"添加新的群信息到数据库成功:{chatroom_info}") # 添加 memberList 到群组信息表中 member_list = chatroom_info.get('data', {}).get('memberList', []) - self.contacts_db.save_chatroom_member_detail(msg.roomid, member_list) - for info in chatroom_info.get('data', {}).get('memberList', []): + self.contacts_db.save_chatroom_member_simple(msg.roomid, member_list) + for info in member_list: wxid = info.get("wxid", "") self.LOG.info(f"已添加新用户信息到数据库: {wxid}") # 更新缓存 self.allContacts[wxid] = info.get("nickName", "nickName") self.LOG.info(f"已维护新用户信息到缓存: {wxid}") self.contact_manager.set_contacts(self.allContacts) - self.contacts_db.save_chatroom_member_detail(msg.roomid, member_list) except Exception as e: self.LOG.error(f"chatroom_info save error: {e}") return