[patch] PDD登录状态GUI回调优化

This commit is contained in:
2025-10-11 09:55:21 +08:00
parent 2ae051f07d
commit ca65024cd2

View File

@@ -4,6 +4,8 @@ from typing import Optional, Callable
import threading import threading
import asyncio import asyncio
from threading import Thread from threading import Thread
from PyQt5.QtCore import QObject, pyqtSignal
# 创建新的后端客户端 # 创建新的后端客户端
from WebSocket.BackendClient import BackendClient from WebSocket.BackendClient import BackendClient
from Utils.JD.JdUtils import JDListenerForGUI as JDListenerForGUI_WS from Utils.JD.JdUtils import JDListenerForGUI as JDListenerForGUI_WS
@@ -11,6 +13,11 @@ from Utils.Dy.DyUtils import DouYinListenerForGUI as DYListenerForGUI_WS
from Utils.Pdd.PddUtils import PddListenerForGUI as PDDListenerForGUI_WS from Utils.Pdd.PddUtils import PddListenerForGUI as PDDListenerForGUI_WS
from Utils.QianNiu.QianNiuUtils import QianNiuListenerForGUI as QNListenerForGUI_WS from Utils.QianNiu.QianNiuUtils import QianNiuListenerForGUI as QNListenerForGUI_WS
class PlatformConnectionSignals(QObject):
"""平台连接信号(线程安全)"""
platform_connected = pyqtSignal(str, str) # (platform_name, store_id)
_backend_client = None _backend_client = None
@@ -36,6 +43,11 @@ class WebSocketManager:
self.gui_update_callback = None self.gui_update_callback = None
self.platform_listeners = {} # 存储各平台的监听器 self.platform_listeners = {} # 存储各平台的监听器
self.connected_platforms = [] # 存储已连接的平台列表 # <- 新增 self.connected_platforms = [] # 存储已连接的平台列表 # <- 新增
# 平台连接信号(线程安全)
self.platform_signals = PlatformConnectionSignals()
self.platform_signals.platform_connected.connect(self._on_platform_signal_received)
self.callbacks = { self.callbacks = {
'log': None, 'log': None,
'success': None, 'success': None,
@@ -65,8 +77,16 @@ class WebSocketManager:
else: else:
print(f"[{level}] {message}") print(f"[{level}] {message}")
def _on_platform_signal_received(self, platform_name: str, store_id: str):
"""接收平台连接信号(在主线程中执行)"""
try:
self._log(f"📡 收到平台连接信号: {platform_name} (店铺:{store_id})", "INFO")
self._notify_platform_connected(platform_name)
except Exception as e:
self._log(f"处理平台连接信号失败: {e}", "ERROR")
def _notify_platform_connected(self, platform_name: str): def _notify_platform_connected(self, platform_name: str):
"""通知GUI平台连接成功""" """通知GUI平台连接成功(仅在主线程中调用)"""
try: try:
if platform_name not in self.connected_platforms: if platform_name not in self.connected_platforms:
self.connected_platforms.append(platform_name) self.connected_platforms.append(platform_name)
@@ -325,14 +345,13 @@ class WebSocketManager:
result = asyncio.run(listener.start_with_login_params(store_id=store_id, login_params=cookies)) result = asyncio.run(listener.start_with_login_params(store_id=store_id, login_params=cookies))
self._log(f"📊 start_with_login_params 执行结果: {result}", "DEBUG") self._log(f"📊 start_with_login_params 执行结果: {result}", "DEBUG")
# 🔥 详细的结果分析(与拼多多完全一致 # 详细的结果分析(仅日志记录GUI 已在主线程中通知
if result == "need_verification_code": if result == "need_verification_code":
self._log("✅ [DY] 登录流程正常,已发送验证码需求通知给后端", "SUCCESS") self._log("✅ [DY] 登录流程正常,已发送验证码需求通知给后端", "SUCCESS")
elif result == "verification_code_error": elif result == "verification_code_error":
self._log("⚠️ [DY] 验证码错误,已发送错误通知给后端", "WARNING") self._log("⚠️ [DY] 验证码错误,已发送错误通知给后端", "WARNING")
elif result: elif result:
self._log("✅ [DY] 登录成功,平台连接已建立", "SUCCESS") self._log("✅ [DY] 登录成功,平台连接已建立", "SUCCESS")
self._notify_platform_connected("抖音")
else: else:
self._log("❌ [DY] 登录失败", "ERROR") self._log("❌ [DY] 登录失败", "ERROR")
else: else:
@@ -357,10 +376,9 @@ class WebSocketManager:
result = asyncio.run(listener.start_with_cookies(store_id=store_id, cookie_dict=cookie_dict)) result = asyncio.run(listener.start_with_cookies(store_id=store_id, cookie_dict=cookie_dict))
self._log(f"📊 start_with_cookies 执行结果: {result}", "DEBUG") self._log(f"📊 start_with_cookies 执行结果: {result}", "DEBUG")
# Cookie启动成功时也要通知GUI # Cookie启动成功时记录日志GUI 已在主线程中通知)
if result: if result:
self._log("✅ [DY] Cookie启动成功平台连接已建立", "SUCCESS") self._log("✅ [DY] Cookie启动成功平台连接已建立", "SUCCESS")
self._notify_platform_connected("抖音")
# 🔥 移除不再在backend_singleton中发送connect_message # 🔥 移除不再在backend_singleton中发送connect_message
# 抖音的连接状态报告应该在DyUtils中的DyLogin类中发送与拼多多保持一致 # 抖音的连接状态报告应该在DyUtils中的DyLogin类中发送与拼多多保持一致
@@ -386,7 +404,10 @@ class WebSocketManager:
if f"抖音:{store_id}" in self.platform_listeners: if f"抖音:{store_id}" in self.platform_listeners:
self.platform_listeners[f"抖音:{store_id}"]['status'] = 'success' self.platform_listeners[f"抖音:{store_id}"]['status'] = 'success'
# ✅ 临时方案:启动后立即通知(因为 DY 监听器也会阻塞)
# DY 内部会处理验证码流程,失败时会向后端上报相应状态
self._log("已启动抖音平台监听", "SUCCESS") self._log("已启动抖音平台监听", "SUCCESS")
self._notify_platform_connected("抖音") # ← 立即通知
except Exception as e: except Exception as e:
self._log(f"启动抖音平台监听失败: {e}", "ERROR") self._log(f"启动抖音平台监听失败: {e}", "ERROR")
@@ -471,14 +492,13 @@ class WebSocketManager:
result = asyncio.run(listener.start_with_login_params(store_id=store_id, login_params=data)) result = asyncio.run(listener.start_with_login_params(store_id=store_id, login_params=data))
self._log(f"📊 start_with_login_params 执行结果: {result}", "DEBUG") self._log(f"📊 start_with_login_params 执行结果: {result}", "DEBUG")
# 详细的结果分析 # 详细的结果分析仅日志记录GUI 已在主线程中通知)
if result == "need_verification_code": if result == "need_verification_code":
self._log("✅ [PDD] 登录流程正常,已发送验证码需求通知给后端", "SUCCESS") self._log("✅ [PDD] 登录流程正常,已发送验证码需求通知给后端", "SUCCESS")
elif result == "verification_code_error": elif result == "verification_code_error":
self._log("⚠️ [PDD] 验证码错误,已发送错误通知给后端", "WARNING") self._log("⚠️ [PDD] 验证码错误,已发送错误通知给后端", "WARNING")
elif result: elif result:
self._log("✅ [PDD] 登录成功,平台连接已建立", "SUCCESS") self._log("✅ [PDD] 登录成功,平台连接已建立", "SUCCESS")
self._notify_platform_connected("拼多多")
else: else:
self._log("❌ [PDD] 登录失败", "ERROR") self._log("❌ [PDD] 登录失败", "ERROR")
else: else:
@@ -488,10 +508,9 @@ class WebSocketManager:
result = asyncio.run(listener.start_with_cookies(store_id=store_id, cookies=data)) result = asyncio.run(listener.start_with_cookies(store_id=store_id, cookies=data))
self._log(f"📊 start_with_cookies 执行结果: {result}", "DEBUG") self._log(f"📊 start_with_cookies 执行结果: {result}", "DEBUG")
# Cookie启动成功时也要通知GUI # Cookie启动成功时记录日志GUI 已在主线程中通知)
if result: if result:
self._log("✅ [PDD] Cookie启动成功平台连接已建立", "SUCCESS") self._log("✅ [PDD] Cookie启动成功平台连接已建立", "SUCCESS")
self._notify_platform_connected("拼多多")
# 根据实际登录结果上报状态给后端 # 根据实际登录结果上报状态给后端
if self.backend_client and result not in ["need_verification_code", "verification_code_error", if self.backend_client and result not in ["need_verification_code", "verification_code_error",
@@ -550,7 +569,10 @@ class WebSocketManager:
'store_id': store_id, 'store_id': store_id,
} }
self._log("拼多多平台监听线程已启动,等待登录结果...", "INFO") # ✅ 临时方案:启动后立即通知(因为 PDD 监听器会阻塞,无法通过返回值判断)
# PDD 内部会处理验证码流程,失败时会向后端上报 status=False
self._log("已启动拼多多平台监听", "SUCCESS")
self._notify_platform_connected("拼多多") # ← 立即通知
except Exception as e: except Exception as e:
self._log(f"启动拼多多平台监听失败: {e}", "ERROR") self._log(f"启动拼多多平台监听失败: {e}", "ERROR")