简化信息存储
This commit is contained in:
@@ -322,7 +322,74 @@ class ContactsDBOperator(BaseDBOperator):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"获取所有联系人名称映射失败: {e}")
|
self.logger.error(f"获取所有联系人名称映射失败: {e}")
|
||||||
return {}
|
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:
|
def save_chatroom_member_detail(self, chatroom_id: str, member_details: List[Dict]) -> bool:
|
||||||
"""保存群成员详细信息到数据库
|
"""保存群成员详细信息到数据库
|
||||||
|
|
||||||
|
|||||||
5
robot.py
5
robot.py
@@ -170,15 +170,14 @@ class Robot(Job):
|
|||||||
self.LOG.info(f"添加新的群信息到数据库成功:{chatroom_info}")
|
self.LOG.info(f"添加新的群信息到数据库成功:{chatroom_info}")
|
||||||
# 添加 memberList 到群组信息表中
|
# 添加 memberList 到群组信息表中
|
||||||
member_list = chatroom_info.get('data', {}).get('memberList', [])
|
member_list = chatroom_info.get('data', {}).get('memberList', [])
|
||||||
self.contacts_db.save_chatroom_member_detail(msg.roomid, member_list)
|
self.contacts_db.save_chatroom_member_simple(msg.roomid, member_list)
|
||||||
for info in chatroom_info.get('data', {}).get('memberList', []):
|
for info in member_list:
|
||||||
wxid = info.get("wxid", "")
|
wxid = info.get("wxid", "")
|
||||||
self.LOG.info(f"已添加新用户信息到数据库: {wxid}")
|
self.LOG.info(f"已添加新用户信息到数据库: {wxid}")
|
||||||
# 更新缓存
|
# 更新缓存
|
||||||
self.allContacts[wxid] = info.get("nickName", "nickName")
|
self.allContacts[wxid] = info.get("nickName", "nickName")
|
||||||
self.LOG.info(f"已维护新用户信息到缓存: {wxid}")
|
self.LOG.info(f"已维护新用户信息到缓存: {wxid}")
|
||||||
self.contact_manager.set_contacts(self.allContacts)
|
self.contact_manager.set_contacts(self.allContacts)
|
||||||
self.contacts_db.save_chatroom_member_detail(msg.roomid, member_list)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.LOG.error(f"chatroom_info save error: {e}")
|
self.LOG.error(f"chatroom_info save error: {e}")
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user