From 9a2d9f3f12e251d2b66ebc6cb5a1d9f7817b8ce0 Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 7 May 2025 10:37:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E9=95=BF=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=BF=83=E8=B7=B3=E5=A4=B1=E8=B4=A5=EF=BC=8C=E5=88=99=E5=B0=9D?= =?UTF-8?q?=E8=AF=95=E4=BA=8C=E6=AC=A1=E7=99=BB=E5=BD=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robot.py | 5 +++++ wechat_ipad/client/login.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/robot.py b/robot.py index ee4857a..71518d4 100644 --- a/robot.py +++ b/robot.py @@ -321,6 +321,11 @@ class Robot(Job): self.LOG.warning("长连接心跳失败") except Exception as e: self.LOG.error(f"wechat_ipad heartbeat long: {e}") + success = await self.ipad_bot.twice_auto_auth() + if success: + self.LOG.debug("尝试二次登录成功") + else: + self.LOG.warning("尝试二次登录失败") await asyncio.sleep(10) async def _process_ipad_message(self, message: WxMessage): diff --git a/wechat_ipad/client/login.py b/wechat_ipad/client/login.py index fa45262..e8fd719 100644 --- a/wechat_ipad/client/login.py +++ b/wechat_ipad/client/login.py @@ -207,6 +207,27 @@ class LoginMixin(WechatAPIClientBase): else: self.error_handler(json_resp) + async def twice_auto_auth(self) -> bool: + """二次登录。 + + Returns: + bool: 成功返回True,否则返回False + + Raises: + UserLoggedOut: 如果未登录时调用 + 根据error_handler处理错误 + """ + if not self.wxid: + raise UserLoggedOut("请先登录") + + async with aiohttp.ClientSession() as session: + response = await session.post(f'http://{self.ip}:{self.port}/api/Login/TwiceAutoAuth?wxid={self.wxid}') + json_resp = await response.json() + if json_resp.get("Success"): + return True + else: + self.error_handler(json_resp) + @staticmethod def create_device_name() -> str: """生成一个随机的设备名。