优化联系人信息,解决总结时使用了其他群的备注信息

This commit is contained in:
liuwei
2025-05-28 15:33:23 +08:00
parent 3c460ce4a7
commit 6c9b99afcf
7 changed files with 91 additions and 72 deletions

View File

@@ -12,7 +12,6 @@ from db.base import BaseDBOperator
from db.connection import DBConnectionManager
class ContactsDBOperator(BaseDBOperator):
"""微信联系人数据库操作类"""
@@ -153,8 +152,8 @@ class ContactsDBOperator(BaseDBOperator):
'small_head_img_url': contact.get('SmallHeadImgUrl', ''),
'description': '', # 可根据需要补充
'card_img_url': '', # 可根据需要补充
'label_list': '', # 可根据需要补充
'phone_num_list': '',# 可根据需要补充
'label_list': '', # 可根据需要补充
'phone_num_list': '', # 可根据需要补充
'type': contact_type
}
@@ -289,9 +288,9 @@ class ContactsDBOperator(BaseDBOperator):
SELECT chatroom_id as user_name, nick_name, remark as remark
FROM t_chatrooms
"""
results = self.execute_query(sql)
contacts_dict = {}
for result in results:
user_name = result.get('user_name')
@@ -338,6 +337,7 @@ class ContactsDBOperator(BaseDBOperator):
except Exception as e:
self.LOG.error(f"获取所有联系人名称映射失败: {e}")
return {}
def save_chatroom_member_simple(self, chatroom_id: str, member_details: List[Dict]) -> bool:
"""
保存群成员简要信息到数据库,兼容不同数据结构
@@ -405,6 +405,7 @@ class ContactsDBOperator(BaseDBOperator):
except Exception as e:
self.LOG.error(f"保存群 {chatroom_id} 成员信息失败: {e}")
return False
def save_chatroom_member_detail(self, chatroom_id: str, member_details: List[Dict]) -> bool:
"""保存群成员详细信息到数据库
@@ -540,7 +541,8 @@ class ContactsDBOperator(BaseDBOperator):
'chat_room_owner': chatroom_data.get('ChatRoomOwner', ''),
'small_head_img_url': chatroom_data.get('SmallHeadImgUrl', ''),
# 成员列表可选存储为JSON字符串
'member_list': json.dumps(chatroom_data.get('NewChatroomData', {}).get('ChatRoomMember', []), ensure_ascii=False)
'member_list': json.dumps(chatroom_data.get('NewChatroomData', {}).get('ChatRoomMember', []),
ensure_ascii=False)
}
fields = ', '.join(data.keys())
@@ -598,7 +600,7 @@ class ContactsDBOperator(BaseDBOperator):
self.LOG.error(f"删除群聊{chatroom_id}信息失败: {e}")
return False
#新增获取群列表接口
# 新增获取群列表接口
def get_chatroom_list(self) -> List[dict]:
"""获取群列表"""
try:
@@ -612,7 +614,7 @@ class ContactsDBOperator(BaseDBOperator):
self.LOG.error(f"获取群列表失败: {e}")
return []
#新增获取群成员列表接口
# 新增获取群成员列表接口
def get_chatroom_member_list(self, chatroom_id: str) -> List[dict]:
"""获取群成员列表"""
try:
@@ -623,7 +625,18 @@ class ContactsDBOperator(BaseDBOperator):
self.LOG.error(f"获取群{chatroom_id}成员列表失败: {e}")
return []
#新增获取群成员信息接口
# 获取群成员的昵称信息
def get_chatroom_member_list_name_all(self) -> List[dict]:
"""获取群成员列表"""
try:
sql = "SELECT chatroom_id,wxid, COALESCE(NULLIF(display_name,''), nick_name, wxid) as nick_name FROM t_chatroom_member"
results = self.execute_query(sql)
return results
except Exception as e:
self.LOG.error(f"获取群成员列表失败: {e}")
return []
# 新增获取群成员信息接口
def get_chatroom_member_info(self, chatroom_id: str, wxid: str) -> Optional[dict]:
"""获取群成员信息"""
try:
@@ -634,7 +647,7 @@ class ContactsDBOperator(BaseDBOperator):
self.LOG.error(f"获取群{chatroom_id}成员{wxid}信息失败: {e}")
return None
#新增群信息删除功能
# 新增群信息删除功能
def delete_chatroom_all_info(self, chatroom_id: str) -> bool:
"""删除群成员信息"""
try:
@@ -649,7 +662,7 @@ class ContactsDBOperator(BaseDBOperator):
self.LOG.error(f"删除群{chatroom_id}信息失败: {e}")
return False
#新增删除所有联系人功能
# 新增删除所有联系人功能
def delete_all_contacts(self) -> bool:
"""删除所有联系人信息"""
try:
@@ -660,8 +673,8 @@ class ContactsDBOperator(BaseDBOperator):
except Exception as e:
self.LOG.error(f"删除所有联系人信息失败: {e}")
return False
#新增获取所有联系人头像信息接口
# 新增获取所有联系人头像信息接口
def get_all_contacts_avatar(self) -> Dict[str, str]:
"""获取所有联系人头像信息"""
try:
@@ -676,9 +689,9 @@ class ContactsDBOperator(BaseDBOperator):
FROM t_chatrooms
"""
results = self.execute_query(sql)
#返回DICT
# 返回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 []
return []