From 7f9894908dc7222de17ca819430ed66fd81d2177 Mon Sep 17 00:00:00 2001 From: jjz <3082705704@qq.com> Date: Fri, 26 Sep 2025 15:38:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9B=9E=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Utils/Pdd/PddUtils.py | 33 +++++++++++++++++++++++---------- WebSocket/backend_singleton.py | 17 ++++++++--------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Utils/Pdd/PddUtils.py b/Utils/Pdd/PddUtils.py index cd7be49..4e7a6c4 100644 --- a/Utils/Pdd/PddUtils.py +++ b/Utils/Pdd/PddUtils.py @@ -1605,9 +1605,9 @@ class PddLogin: return result # 发送验证码通知给后端,并获取验证码 - def request_verification_code(self, username, store_id, backend_anti_content): + def request_verification_code(self, username, store_id, backend_anti_content, phone_number=None): """向后端请求获取手机验证码""" - self._log(f"开始请求验证码,用户名: {username}, 店铺ID: {store_id}", "INFO") + self._log(f"开始请求验证码,用户名: {username}, 店铺ID: {store_id}, 手机号: {phone_number}", "INFO") # 使用后端下发的anti_content anti_content = backend_anti_content @@ -1622,18 +1622,18 @@ class PddLogin: response = requests.post(url, headers=self.headers, json=payload, cookies=self.cookies) self._log(f"发送验证码请求结果: {response.text}") - # 发送消息给后端,告知需要验证码 + # 发送消息给后端,告知需要验证码(包含手机号) self._log("准备向后端发送验证码需求通知", "INFO") - self._send_verification_needed_message(store_id) + self._send_verification_needed_message(store_id, phone_number) # 这里需要等待后端重新下发包含验证码的登录参数 # 实际实现中这个方法会在接收到新的登录参数后被重新调用 return None - def _send_verification_needed_message(self, store_id): + def _send_verification_needed_message(self, store_id, phone_number=None): """向后端发送需要验证码的通知""" try: - self._log(f"开始发送验证码需求通知,店铺ID: {store_id}", "INFO") + self._log(f"开始发送验证码需求通知,店铺ID: {store_id}, 手机号: {phone_number}", "INFO") from WebSocket.backend_singleton import get_backend_client backend = get_backend_client() self._log(f"获取到后端客户端: {backend is not None}", "DEBUG") @@ -1643,11 +1643,12 @@ class PddLogin: "type": "connect_message", "store_id": store_id, "status": False, - "content": "需要验证码" + "content": "需要验证码", + "phone_number": phone_number } self._log(f"准备发送验证码通知消息: {message}", "DEBUG") backend.send_message(message) - self._log("✅ 成功向后端发送验证码需求通知", "SUCCESS") + self._log("✅ 成功向后端发送验证码需求通知(含手机号)", "SUCCESS") else: self._log("❌ 后端客户端为空,无法发送验证码需求通知", "ERROR") except Exception as e: @@ -1864,8 +1865,20 @@ class PddLogin: backend_anti_content = login_params.get("anti_content") self._log(f"为用户 {username} 发送验证码,使用后端anti_content", "INFO") - # 传递后端下发的anti_content - self.request_verification_code(username, store_id, backend_anti_content) + # 🔥 从响应中提取手机号 + phone_number = None + try: + response_json = response.json() + phone_number = response_json.get("result") # 手机号在result字段中 + if phone_number and isinstance(phone_number, str): + self._log(f"🔍 从登录响应中提取到手机号: {phone_number}", "SUCCESS") + else: + self._log("⚠️ 响应中的result字段不包含有效手机号", "WARNING") + except Exception as e: + self._log(f"❌ 提取手机号时出错: {e}", "DEBUG") + + # 传递后端下发的anti_content和手机号 + self.request_verification_code(username, store_id, backend_anti_content, phone_number) return "need_verification_code" else: diff --git a/WebSocket/backend_singleton.py b/WebSocket/backend_singleton.py index f895136..82032a5 100644 --- a/WebSocket/backend_singleton.py +++ b/WebSocket/backend_singleton.py @@ -71,9 +71,7 @@ class WebSocketManager: def connect_backend(self, token: str) -> bool: """连接后端WebSocket""" try: - # 🔥 重置清理标记,准备下次重连检测 - if hasattr(self, '_cleared'): - delattr(self, '_cleared') + # 1 保存token到配置 try: @@ -147,12 +145,13 @@ class WebSocketManager: def _handle_platform_login(self, platform_name: str, store_id: str, cookies: str): """处理平台登录请求""" try: - # 🔥 检查并清理旧连接(重连后首次登录时) - if not hasattr(self, '_cleared') and self.platform_listeners: - self._log(f"🔄 检测到后端重连后首次登录,清理 {len(self.platform_listeners)} 个旧连接", "INFO") - self.platform_listeners.clear() - self.connected_platforms.clear() - self._cleared = True + # 🔥 检查并清理当前店铺的旧连接 + store_key_pattern = f":{store_id}" # 匹配 "平台名:store_id" 格式 + keys_to_remove = [key for key in self.platform_listeners.keys() if key.endswith(store_key_pattern)] + if keys_to_remove: + self._log(f"🔄 检测到店铺 {store_id} 重连,清理 {len(keys_to_remove)} 个旧连接", "INFO") + for key in keys_to_remove: + self.platform_listeners.pop(key, None) # 平台名称映射 platform_map = {