看板内容进行优化,同时加入了用户管理模块,将用户信息全局开放,
This commit is contained in:
79
utils/wechat/contact_manager.py
Normal file
79
utils/wechat/contact_manager.py
Normal file
@@ -0,0 +1,79 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
联系人管理器 - 提供全局访问联系人信息的单例类
|
||||
"""
|
||||
import logging
|
||||
from typing import Dict, Optional
|
||||
|
||||
|
||||
class ContactManager:
|
||||
"""联系人管理器单例类"""
|
||||
_instance = None
|
||||
_contacts: Dict[str, str] = {}
|
||||
_initialized = False
|
||||
_logger = logging.getLogger("ContactManager")
|
||||
|
||||
def __new__(cls):
|
||||
if cls._instance is None:
|
||||
cls._instance = super(ContactManager, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
# 确保初始化代码只执行一次
|
||||
if not ContactManager._initialized:
|
||||
self._logger.info("初始化联系人管理器")
|
||||
ContactManager._initialized = True
|
||||
|
||||
@classmethod
|
||||
def get_instance(cls):
|
||||
"""获取单例实例"""
|
||||
if cls._instance is None:
|
||||
cls._instance = ContactManager()
|
||||
return cls._instance
|
||||
|
||||
def set_contacts(self, contacts: Dict[str, str]) -> None:
|
||||
"""设置联系人字典
|
||||
|
||||
Args:
|
||||
contacts: 联系人字典,格式为 {"wxid": "NickName"}
|
||||
"""
|
||||
self._contacts = contacts
|
||||
self._logger.info(f"联系人信息已更新,共 {len(contacts)} 个联系人")
|
||||
|
||||
def get_contacts(self) -> Dict[str, str]:
|
||||
"""获取所有联系人
|
||||
|
||||
Returns:
|
||||
联系人字典,格式为 {"wxid": "NickName"}
|
||||
"""
|
||||
return self._contacts
|
||||
|
||||
def get_nickname(self, wxid: str) -> str:
|
||||
"""根据微信ID获取昵称
|
||||
|
||||
Args:
|
||||
wxid: 微信ID
|
||||
|
||||
Returns:
|
||||
对应的昵称,如果不存在则返回wxid本身
|
||||
"""
|
||||
return self._contacts.get(wxid, wxid)
|
||||
|
||||
def update_contact(self, wxid: str, nickname: str) -> None:
|
||||
"""更新单个联系人信息
|
||||
|
||||
Args:
|
||||
wxid: 微信ID
|
||||
nickname: 昵称
|
||||
"""
|
||||
self._contacts[wxid] = nickname
|
||||
self._logger.debug(f"已更新联系人: {wxid} -> {nickname}")
|
||||
|
||||
def refresh_contacts(self, new_contacts: Dict[str, str]) -> None:
|
||||
"""刷新联系人信息
|
||||
|
||||
Args:
|
||||
new_contacts: 新的联系人字典
|
||||
"""
|
||||
self._contacts = new_contacts
|
||||
self._logger.info(f"联系人信息已刷新,共 {len(new_contacts)} 个联系人")
|
||||
Reference in New Issue
Block a user