加入群成员列表功能

This commit is contained in:
liuwei
2025-04-08 15:10:46 +08:00
parent 65ef82d044
commit e33e0e5263
4 changed files with 163 additions and 23 deletions

View File

@@ -5,6 +5,8 @@
import logging
from typing import Dict, Optional, List, Tuple
from wcferry import Wcf
class ContactManager:
"""联系人管理器单例类"""
@@ -17,6 +19,7 @@ class ContactManager:
_initialized = False
_logger = logging.getLogger("ContactManager")
_friends: List[Dict] = []
_group_contacts_friends: Dict[str, Dict[str, str]] = {}
# 定义公共好友列表
_PUBLIC_FRIENDS = {
'fmessage': '朋友推荐消息',
@@ -46,7 +49,7 @@ class ContactManager:
cls._instance = ContactManager()
return cls._instance
def set_contacts(self, contacts: Dict[str, str], friends: List[Dict]) -> None:
def set_contacts(self, contacts: Dict[str, str], wcf: Wcf) -> None:
"""设置联系人字典
Args:
@@ -62,12 +65,12 @@ class ContactManager:
"gender": gender}
"""
self._contacts = contacts
self._friends = friends
self._friends = wcf.get_friends()
self._logger.info(f"联系人信息已更新,共 {len(contacts)} 个联系人")
# 分类联系人
self._classify_contacts()
self._classify_contacts(wcf)
def _classify_contacts(self) -> None:
def _classify_contacts(self, wcf: Wcf) -> None:
"""将联系人分类为群组、个人联系人、公共好友和公众号"""
self._group_contacts = {}
self._personal_contacts = {}
@@ -84,6 +87,9 @@ class ContactManager:
# 判断是否为群组wxid以@chatroom结尾
elif wxid.endswith('@chatroom'):
self._group_contacts[wxid] = nickname
# 如果是群,这处理群列表内容
self._group_contacts_friends[wxid] = wcf.get_chatroom_members(wxid)
# # 其他为普通好友和群成员
# else:
# self._personal_contacts[wxid] = nickname
@@ -147,9 +153,31 @@ class ContactManager:
"""
return self._contacts.get(wxid, wxid)
def get_group_name(self, roomid: str, wxid: str) -> str:
"""
Args:
roomid: 群ID
wxid: 微信ID
Returns:
对应的昵称如果不存在则返回wxid本身
"""
return self._group_contacts_friends.get(roomid, "").get(wxid, "未知昵称")
def get_group_members(self, roomid: str) -> Dict[str, str]:
"""获取指定群的成员列表
Args:
roomid: 群ID
Returns:
群成员字典,格式为 {"wxid": "NickName"}
"""
return self._group_contacts_friends.get(roomid, {})
def update_contact(self, wxid: str, nickname: str) -> None:
"""更新单个联系人信息
Args:
wxid: 微信ID
nickname: 昵称
@@ -166,30 +194,32 @@ class ContactManager:
self._personal_contacts[wxid] = nickname
self._logger.debug(f"已更新联系人: {wxid} -> {nickname}")
def refresh_contacts(self, new_contacts: Dict[str, str], friends: List[Dict]) -> None:
def refresh_contacts(self, new_contacts: Dict[str, str], wcf: Wcf) -> None:
"""刷新联系人信息
Args:
new_contacts: 新的联系人字典
friends: 好友清单 contact = {
"wxid": cnt.get("wxid", ""),
"code": cnt.get("code", ""),
"remark": cnt.get("remark", ""),
"name": cnt.get("name", ""),
"country": cnt.get("country", ""),
"province": cnt.get("province", ""),
"city": cnt.get("city", ""),
"gender": gender}
wcf :wcf
"""
self._contacts = new_contacts
self._friends = friends
# friends: 好友清单 contact = {
# "wxid": cnt.get("wxid", ""),
# "code": cnt.get("code", ""),
# "remark": cnt.get("remark", ""),
# "name": cnt.get("name", ""),
# "country": cnt.get("country", ""),
# "province": cnt.get("province", ""),
# "city": cnt.get("city", ""),
# "gender": gender}
self._friends = wcf.get_friends()
self._logger.info(f"联系人信息已刷新,共 {len(new_contacts)} 个联系人")
# 重新分类联系人
self._classify_contacts()
self._classify_contacts(wcf)
def get_contact_statistics(self) -> Tuple[int, int, int, int, int]:
"""获取联系人统计信息
Returns:
包含总联系人数、群组数、个人联系人数、公共好友数和公众号数的元组
"""