feat: 优化整体项目
This commit is contained in:
@@ -19,6 +19,7 @@ import pymysql
|
||||
from loguru import logger
|
||||
from utils.plugin_base import PluginBase
|
||||
from utils.decorators import on_text_message
|
||||
from utils.redis_cache import get_cache
|
||||
from WechatHook import WechatHookClient
|
||||
|
||||
try:
|
||||
@@ -49,14 +50,14 @@ class SignInPlugin(PluginBase):
|
||||
self.config = tomllib.load(f)
|
||||
|
||||
self.db_config = self.config["database"]
|
||||
|
||||
|
||||
# 创建临时文件夹
|
||||
self.temp_dir = Path(__file__).parent / "temp"
|
||||
self.temp_dir.mkdir(exist_ok=True)
|
||||
|
||||
|
||||
# 图片文件夹
|
||||
self.images_dir = Path(__file__).parent / "images"
|
||||
|
||||
|
||||
logger.success("签到插件初始化完成")
|
||||
|
||||
def get_db_connection(self):
|
||||
@@ -149,29 +150,42 @@ class SignInPlugin(PluginBase):
|
||||
logger.error(f"更新用户昵称失败: {e}")
|
||||
return False
|
||||
|
||||
async def get_user_nickname_from_group(self, client: WechatHookClient,
|
||||
async def get_user_nickname_from_group(self, client: WechatHookClient,
|
||||
group_wxid: str, user_wxid: str) -> str:
|
||||
"""从群聊中获取用户昵称"""
|
||||
"""从群聊中获取用户昵称(优先使用缓存)"""
|
||||
try:
|
||||
logger.debug(f"尝试获取用户 {user_wxid} 在群 {group_wxid} 中的昵称")
|
||||
|
||||
# 使用11174 API获取单个用户的详细信息
|
||||
# 动态获取缓存实例(由 MessageLogger 初始化)
|
||||
redis_cache = get_cache()
|
||||
|
||||
# 1. 先尝试从 Redis 缓存获取
|
||||
if redis_cache and redis_cache.enabled:
|
||||
cached_info = redis_cache.get_user_basic_info(group_wxid, user_wxid)
|
||||
if cached_info and cached_info.get("nickname"):
|
||||
logger.debug(f"[缓存命中] {user_wxid}: {cached_info['nickname']}")
|
||||
return cached_info["nickname"]
|
||||
|
||||
# 2. 缓存未命中,调用 API 获取
|
||||
logger.debug(f"[缓存未命中] 调用API获取用户昵称: {user_wxid}")
|
||||
user_info = await client.get_user_info_in_chatroom(group_wxid, user_wxid)
|
||||
|
||||
|
||||
if user_info:
|
||||
# 从返回的详细信息中提取昵称
|
||||
nickname = user_info.get("nickName", {}).get("string", "")
|
||||
|
||||
|
||||
if nickname:
|
||||
logger.success(f"获取到用户昵称: {user_wxid} -> {nickname}")
|
||||
logger.success(f"API获取用户昵称成功: {user_wxid} -> {nickname}")
|
||||
# 3. 将用户信息存入缓存
|
||||
if redis_cache and redis_cache.enabled:
|
||||
redis_cache.set_user_info(group_wxid, user_wxid, user_info)
|
||||
logger.debug(f"[已缓存] {user_wxid}: {nickname}")
|
||||
return nickname
|
||||
else:
|
||||
logger.warning(f"用户 {user_wxid} 的昵称字段为空")
|
||||
else:
|
||||
logger.warning(f"未找到用户 {user_wxid} 在群 {group_wxid} 中的信息")
|
||||
|
||||
|
||||
return ""
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"获取群成员昵称失败: {e}")
|
||||
return ""
|
||||
@@ -770,13 +784,24 @@ class SignInPlugin(PluginBase):
|
||||
current_points = updated_user["points"] if updated_user else total_earned
|
||||
updated_user["points"] = current_points
|
||||
|
||||
# 尝试获取用户头像
|
||||
# 尝试获取用户头像(优先使用缓存)
|
||||
avatar_url = None
|
||||
if is_group:
|
||||
try:
|
||||
user_detail = await client.get_user_info_in_chatroom(from_wxid, user_wxid)
|
||||
if user_detail:
|
||||
avatar_url = user_detail.get("bigHeadImgUrl", "")
|
||||
redis_cache = get_cache()
|
||||
# 先从缓存获取
|
||||
if redis_cache and redis_cache.enabled:
|
||||
cached_info = redis_cache.get_user_basic_info(from_wxid, user_wxid)
|
||||
if cached_info:
|
||||
avatar_url = cached_info.get("avatar_url", "")
|
||||
# 缓存未命中则调用 API
|
||||
if not avatar_url:
|
||||
user_detail = await client.get_user_info_in_chatroom(from_wxid, user_wxid)
|
||||
if user_detail:
|
||||
avatar_url = user_detail.get("bigHeadImgUrl", "")
|
||||
# 存入缓存
|
||||
if redis_cache and redis_cache.enabled:
|
||||
redis_cache.set_user_info(from_wxid, user_wxid, user_detail)
|
||||
except Exception as e:
|
||||
logger.warning(f"获取用户头像失败: {e}")
|
||||
|
||||
@@ -864,13 +889,24 @@ class SignInPlugin(PluginBase):
|
||||
self.update_user_nickname(user_wxid, nickname)
|
||||
user_info["nickname"] = nickname
|
||||
|
||||
# 尝试获取用户头像
|
||||
# 尝试获取用户头像(优先使用缓存)
|
||||
avatar_url = None
|
||||
if is_group:
|
||||
try:
|
||||
user_detail = await client.get_user_info_in_chatroom(from_wxid, user_wxid)
|
||||
if user_detail:
|
||||
avatar_url = user_detail.get("bigHeadImgUrl", "")
|
||||
redis_cache = get_cache()
|
||||
# 先从缓存获取
|
||||
if redis_cache and redis_cache.enabled:
|
||||
cached_info = redis_cache.get_user_basic_info(from_wxid, user_wxid)
|
||||
if cached_info:
|
||||
avatar_url = cached_info.get("avatar_url", "")
|
||||
# 缓存未命中则调用 API
|
||||
if not avatar_url:
|
||||
user_detail = await client.get_user_info_in_chatroom(from_wxid, user_wxid)
|
||||
if user_detail:
|
||||
avatar_url = user_detail.get("bigHeadImgUrl", "")
|
||||
# 存入缓存
|
||||
if redis_cache and redis_cache.enabled:
|
||||
redis_cache.set_user_info(from_wxid, user_wxid, user_detail)
|
||||
except Exception as e:
|
||||
logger.warning(f"获取用户头像失败: {e}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user