From c6381bbec2b69e4604a79dde4ce6d245bdec954f Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 7 May 2025 17:03:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E5=A4=B4=E5=83=8F=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/contacts_db.py | 24 +++++++++++++++++++++++- robot.py | 23 ++++++++++++++++++++--- utils/wechat/contact_manager.py | 3 ++- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/db/contacts_db.py b/db/contacts_db.py index e347f10..7264e31 100644 --- a/db/contacts_db.py +++ b/db/contacts_db.py @@ -651,4 +651,26 @@ class ContactsDBOperator(BaseDBOperator): return True except Exception as e: self.LOG.error(f"删除所有联系人信息失败: {e}") - return False \ No newline at end of file + return False + + #新增获取所有联系人头像信息接口 + def get_all_contacts_avatar(self) -> Dict[str, str]: + """获取所有联系人头像信息""" + try: + sql = """ + SELECT user_name, small_head_img_url + FROM t_wechat_contacts + union all + SELECT wxid as user_name, small_head_img_url + FROM t_chatroom_member + union all + SELECT chatroom_id as user_name, small_head_img_url + FROM t_chatrooms + """ + results = self.execute_query(sql) + #返回DICT + results = {result['user_name']: result['small_head_img_url'] for result in results} + return results + except Exception as e: + self.LOG.error(f"获取所有联系人头像信息失败: {e}") + return [] \ No newline at end of file diff --git a/robot.py b/robot.py index c3d6b84..d896971 100644 --- a/robot.py +++ b/robot.py @@ -179,7 +179,8 @@ class Robot(Job): # 登录成功后加载联系人信息 self.allContacts = self.get_all_contacts() - self.contact_manager.set_contacts(self.allContacts) + self.head_images =self.get_all_head_images() + self.contact_manager.set_contacts(self.allContacts,self.head_images) self.message_storage = MessageStorage(self.ipad_bot) @@ -371,13 +372,16 @@ class Robot(Job): wxid = member.get("UserName", "") nick_name = member.get("NickName", "") displayName = member.get("DisplayName", "") + small_head_img_url = member.get("SmallHeadImgUrl", "") #如果displayName不为空,使用displayName if displayName: nick_name = displayName if wxid: self.allContacts[wxid] = nick_name - self.contact_manager.set_contacts(self.allContacts) + self.head_images[wxid] = small_head_img_url + + self.contact_manager.set_contacts(self.allContacts,self.head_images) self.LOG.info(f"已更新群 {group_id} 的成员信息") except Exception as e: self.LOG.error(f"获取群成员信息失败: {e}") @@ -508,6 +512,16 @@ class Robot(Job): self.LOG.error(f"获取联系人信息失败: {e}") return {} + def get_all_head_images(self) -> dict: + """获取所有的联系人头像信息""" + try: + # 从数据库获取所有联系人的头像信息 + head_images = self.contacts_db.get_all_contacts_avatar() + return head_images + except Exception as e: + self.LOG.error(f"获取所有联系人头像信息失败: {e}") + return {} + async def refresh_contacts_db(self): """刷新联系人信息""" # 获取用户所有的联系人,并保存到数据库 @@ -551,13 +565,16 @@ class Robot(Job): wxid = member.get("UserName", "") nick_name = member.get("NickName", "") displayName = member.get("DisplayName", "") + small_head_img_url = member.get("SmallHeadImgUrl", "") #如果displayName不为空,使用displayName if displayName: nick_name = displayName if wxid: self.allContacts[wxid] = nick_name - self.contact_manager.set_contacts(self.allContacts) + self.head_images[wxid] = small_head_img_url + + self.contact_manager.set_contacts(self.allContacts,self.head_images) self.LOG.info(f"已更新群 {group_id} 的成员信息") else: self.LOG.error(f"获取群 {group_id} 信息失败,证明用户无该群信息,删除群的相关资料。") diff --git a/utils/wechat/contact_manager.py b/utils/wechat/contact_manager.py index 165589f..b77b3c2 100644 --- a/utils/wechat/contact_manager.py +++ b/utils/wechat/contact_manager.py @@ -51,7 +51,7 @@ class ContactManager: cls._instance = ContactManager() return cls._instance - def set_contacts(self, contacts: Dict[str, str]) -> None: + def set_contacts(self, contacts: Dict[str, str], head_imgs: Dict[str, str]) -> None: """设置联系人字典 Args: @@ -69,6 +69,7 @@ class ContactManager: """ self._contacts = contacts self._friends = contacts + self._head_images = head_imgs self._logger.info(f"联系人信息已更新,共 {len(contacts)} 个联系人") # 分类联系人 self._classify_contacts()