from datetime import datetime from typing import Any from zhenxun.configs.config import Config from zhenxun.services.log import logger from ...utils import config, getJsonManager, getToolManager from ..dbService import g_pDBService class CSignInManager: @classmethod async def signInByUid(cls, uid: str) -> str: toDay = getToolManager().dateTime().date().today() message = "" status = await g_pDBService.userSign.sign(uid, toDay.strftime("%Y-%m-%d")) # 如果完成签到 if status == 1 or status == 2: # 获取签到总天数 signDay = await g_pDBService.userSign.getUserSignCountByDate( uid, toDay.strftime("%Y-%m") ) exp, point = await g_pDBService.userSign.getUserSignRewardByDate( uid, toDay.strftime("%Y-%m-%d") ) message += config.g_sTranslation["signIn"]["success"].format( day=signDay, exp=exp, num=point ) jSign = await getJsonManager().getSign() reward = jSign["continuou"].get(f"{signDay}", None) signType = Config.get_config("zhenxun_plugin_farm", "签到图片样式") if reward: extraPoint = reward.get("point", 0) extraExp = reward.get("exp", 0) plant = reward.get("plant", {}) message += config.g_sTranslation["signIn"]["grandTotal"].format( exp=extraExp, num=extraPoint ) vipPoint = reward.get("vipPoint", 0) if vipPoint > 0: message += config.g_sTranslation["signIn"]["grandTotal1"].format( num=vipPoint ) if plant: for key, value in plant.items(): message += config.g_sTranslation["signIn"][ "grandTotal2" ].format(name=key, num=value) else: message = config.g_sTranslation["signIn"]["error1"] pass return message @classmethod def generateSignData( cls, todayRewards: list[dict[str, Any]], cumulativeRewards: list[dict[str, Any]], currentProgress: int, totalMilestones: int, consecutiveDays: int = 0, ) -> dict[str, Any]: """生成签到页面所需的数据 Args: todayRewards (list[dict[str, Any]]): 今日奖励列表 cumulativeRewards (list[dict[str, Any]]): 累计奖励列表 currentProgress (int): 当前进度 totalMilestones (int): 总里程碑数 consecutiveDays (int, optional): 连续签到天数 Returns: dict[str, Any]: 签到数据字典 """ # 计算进度百分比 progressPercentage = min(100, int((currentProgress / totalMilestones) * 100)) # 计算已达成里程碑数量 achievedMilestones = 0 for reward in cumulativeRewards: if reward.get("achieved", False): achievedMilestones += 1 return { "currentDate": datetime.now().strftime("%Y年%m月%d日"), "todayRewards": todayRewards, "cumulativeRewards": cumulativeRewards, "currentProgress": currentProgress, "totalMilestones": totalMilestones, "progressPercentage": progressPercentage, "achievedMilestones": achievedMilestones, "consecutiveDays": consecutiveDays, } @classmethod async def createSignImage( cls, todayRewards: list[dict[str, Any]], cumulativeRewards: list[dict[str, Any]], currentProgress: int, totalMilestones: int, consecutiveDays: int = 0, ): """创建签到图片的完整流程 Args: todayRewards (list[dict[str, Any]]): 今日奖励列表 cumulativeRewards (list[dict[str, Any]]): 累计奖励列表 currentProgress (int): 当前进度 totalMilestones (int): 总里程碑数 consecutiveDays (int, optional): 连续签到天数 """ try: templatePath = config.g_sResourcePath / "html/sign_in.html" outputPath = config.g_sResourcePath / "temp_html/sign_in.html" savePath = config.DATA_PATH / "farm_res/html/sign_in.png" # 1. 生成渲染数据 context = cls.generateSignData( todayRewards=todayRewards, cumulativeRewards=cumulativeRewards, currentProgress=currentProgress, totalMilestones=totalMilestones, consecutiveDays=consecutiveDays, ) # 2. 渲染HTML文件 getToolManager().renderHtmlToFile(templatePath, context, outputPath) # 3. 生成图片字节数据 # imageBytes = await getToolManager().screenshotHtmlToBytes(outputPath) # 4. 保存图片文件 await getToolManager().screenshotSave( str(outputPath), str(savePath), 600, 800 ) except Exception as e: logger.error(f"❌ 生成签到图片时出错: {e}") raise