From f60b276916ce15b3201ce8e3db53b36d0d368706 Mon Sep 17 00:00:00 2001 From: aoxuan Date: Wed, 11 Jun 2025 20:54:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor(notify):=20=E9=87=8D=E6=9E=84=E4=BC=81?= =?UTF-8?q?=E4=B8=9A=E5=BE=AE=E4=BF=A1=E7=BE=A4=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E6=8E=A8=E9=80=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -将图片压缩、存在性检查、Base64编码和MD5计算移至 CompanyWebHookBotPushImage 方法内部 - 优化错误处理和日志记录 - 简化调用接口,提高代码可读性和维护性 --- app/models/MAA.py | 30 ++-------- app/services/notification.py | 103 ++++++++++++++++++----------------- 2 files changed, 59 insertions(+), 74 deletions(-) diff --git a/app/models/MAA.py b/app/models/MAA.py index d5e49ac..ebd0a2f 100644 --- a/app/models/MAA.py +++ b/app/models/MAA.py @@ -2000,34 +2000,14 @@ class MaaManager(QObject): Config.get(Config.notify_CompanyWebHookBotUrl), ) app_path = Config.app_path - image_path = app_path / "resources/images/notification/six_star.png" - - # 压缩后得到的仍然是字符串路径 - final_image_path = ImageUtils.compress_image_if_needed( - str(image_path) + image_path = ( + app_path / "resources/images/notification/six_star.png" ) - # 转回Path对象,方便exists判断 - final_image_path = Path( - final_image_path + Notify.CompanyWebHookBotPushImage( + image_path, + Config.get(Config.notify_CompanyWebHookBotUrl), ) - if final_image_path.exists(): - image_base64 = ImageUtils.get_base64_from_file( - str(final_image_path) - ) - image_md5 = ImageUtils.calculate_md5_from_file( - str(final_image_path) - ) - Notify.CompanyWebHookBotPushImage( - image_base64, - image_md5, - user_data["Notify"]["CompanyWebHookBotUrl"], - ) - else: - logger.error( - f"{self.name} | 图片不存在或者压缩失败,请检查图片路径是否正确" - ) - # 发送用户单独通知 if user_data["Notify"]["Enabled"] and user_data["Notify"]["IfSendSixStar"]: diff --git a/app/services/notification.py b/app/services/notification.py index 03c6775..7ab2211 100644 --- a/app/services/notification.py +++ b/app/services/notification.py @@ -273,38 +273,55 @@ class Notification(QObject): ) return f"使用企业微信群机器人推送通知时出错:{err}" - def CompanyWebHookBotPushImage(self, base64_content, md5_content, webhook_url): - """使用企业微信群机器人推送图片通知 - - Args: - base64_content (str): 图片的base64编码字符串 - md5_content (str): 图片的MD5值 - webhook_url (str): 群组机器人WebHook地址 - - Returns: - bool: 推送是否成功 - """ - if webhook_url == "": - logger.error("请正确设置企业微信群机器人的WebHook地址") - self.push_info_bar.emit( - "error", - "企业微信群机器人通知推送异常", - "请正确设置企业微信群机器人的WebHook地址", - -1, - ) - return False - + def CompanyWebHookBotPushImage(self, image_path: str, webhook_url: str) -> bool: + """使用企业微信群机器人推送图片通知""" try: - # 构造请求数据 + # 压缩图片 + final_image_path = ImageUtils.compress_image_if_needed(str(image_path)) + final_image_path = Path(final_image_path) + + # 检查图片是否存在 + if not final_image_path.exists(): + logger.error( + "图片推送异常 | 图片不存在或者压缩失败,请检查图片路径是否正确" + ) + self.push_info_bar.emit( + "error", + "企业微信群机器人通知推送异常", + "图片不存在或者压缩失败,请检查图片路径是否正确", + -1, + ) + return False + + if not webhook_url: + logger.error("请正确设置企业微信群机器人的WebHook地址") + self.push_info_bar.emit( + "error", + "企业微信群机器人通知推送异常", + "请正确设置企业微信群机器人的WebHook地址", + -1, + ) + return False + + # 获取图片base64和md5 + try: + image_base64 = ImageUtils.get_base64_from_file(str(final_image_path)) + image_md5 = ImageUtils.calculate_md5_from_file(str(final_image_path)) + except Exception as e: + logger.error(f"图片编码或MD5计算失败:{e}") + self.push_info_bar.emit( + "error", + "企业微信群机器人通知推送异常", + f"图片编码或MD5计算失败:{e}", + -1, + ) + return False + data = { "msgtype": "image", - "image": { - "base64": base64_content, - "md5": md5_content - } + "image": {"base64": image_base64, "md5": image_md5}, } - # 发送请求 for _ in range(3): try: response = requests.post( @@ -314,8 +331,9 @@ class Notification(QObject): ) info = response.json() break - except Exception as e: + except requests.RequestException as e: err = e + logger.warning(f"推送企业微信群机器人图片第{_+1}次失败:{e}") time.sleep(0.1) else: logger.error(f"推送企业微信群机器人图片时出错:{err}") @@ -327,7 +345,7 @@ class Notification(QObject): ) return False - if info["errcode"] == 0: + if info.get("errcode") == 0: logger.info("企业微信群机器人推送图片成功") return True else: @@ -341,11 +359,11 @@ class Notification(QObject): return False except Exception as e: - logger.error(f"推送企业微信群机器人图片时出错:{e}") + logger.error(f"推送企业微信群机器人图片时发生未知异常:{e}") self.push_info_bar.emit( "error", "企业微信群机器人图片推送失败", - f"使用企业微信群机器人推送图片时出错:{e}", + f"发生未知异常:{e}", -1, ) return False @@ -386,26 +404,13 @@ class Notification(QObject): "这是 AUTO_MAA 外部通知测试信息。如果你看到了这段内容,说明 AUTO_MAA 的通知功能已经正确配置且可以正常工作!", Config.get(Config.notify_CompanyWebHookBotUrl), ) + app_path = Config.app_path image_path = app_path / "resources/images/notification/test_notify.png" - - # 压缩后得到的仍然是字符串路径 - final_image_path = ImageUtils.compress_image_if_needed(str(image_path)) - # 转回Path对象,方便exists判断 - final_image_path = Path(final_image_path) - - if final_image_path.exists(): - image_base64 = ImageUtils.get_base64_from_file(str(final_image_path)) - image_md5 = ImageUtils.calculate_md5_from_file(str(final_image_path)) - Notify.CompanyWebHookBotPushImage( - image_base64, - image_md5, - Config.get(Config.notify_CompanyWebHookBotUrl), - ) - else: - logger.error( - f"通知测试 | 图片不存在或者压缩失败,请检查图片路径是否正确" - ) + Notify.CompanyWebHookBotPushImage( + image_path, + Config.get(Config.notify_CompanyWebHookBotUrl), + ) return True