实现pdd抖音平台cookie登录

This commit is contained in:
jjz
2025-09-29 13:01:34 +08:00
parent 4f2706d8d9
commit d5d205e575
3 changed files with 107 additions and 37 deletions

View File

@@ -302,9 +302,19 @@ class WebSocketManager:
self._log("🍪 使用Cookie启动抖音监听器", "INFO")
self._log("🔄 开始执行 start_with_cookies", "DEBUG")
try:
cookie_dict = json.loads(cookies) if isinstance(cookies, str) else cookies
except json.JSONDecodeError as e:
self._log(f"❌ Cookie JSON解析失败: {e}", "ERROR")
# 🔥 修复尝试JSON解析失败时用ast.literal_eval解析Python字典字符串
if isinstance(cookies, str):
try:
cookie_dict = json.loads(cookies)
except json.JSONDecodeError:
# 后端发送的是Python字典字符串格式使用ast.literal_eval
import ast
cookie_dict = ast.literal_eval(cookies)
self._log("✅ 使用ast.literal_eval成功解析cookies", "DEBUG")
else:
cookie_dict = cookies
except (json.JSONDecodeError, ValueError, SyntaxError) as e:
self._log(f"❌ Cookie解析失败: {e}", "ERROR")
return False
result = asyncio.run(listener.start_with_cookies(store_id=store_id, cookie_dict=cookie_dict))
self._log(f"📊 start_with_cookies 执行结果: {result}", "DEBUG")
@@ -314,26 +324,9 @@ class WebSocketManager:
self._log("✅ [DY] Cookie启动成功平台连接已建立", "SUCCESS")
self._notify_platform_connected("抖音")
# 🔥 根据实际登录结果上报状态给后端(与拼多多完全一致)
if self.backend_client and result not in ["need_verification_code", "verification_code_error", "login_failure"]:
# 如果是特殊状态,说明通知已经在DyLogin中发送了不需要重复发送
try:
message = {
"type": "connect_message",
"store_id": store_id,
"status": bool(result)
}
self.backend_client.send_message(message)
status_text = "成功" if result else "失败"
self._log(f"上报抖音平台连接状态{status_text}: {message}", "SUCCESS" if result else "ERROR")
except Exception as send_e:
self._log(f"上报抖音平台连接状态失败: {send_e}", "ERROR")
elif result == "need_verification_code":
self._log("需要验证码验证码通知已由DyLogin发送等待后端重新下发登录参数", "INFO")
elif result == "verification_code_error":
self._log("验证码错误错误通知已由DyLogin发送等待后端处理", "INFO")
elif result == "login_failure":
self._log("登录失败失败通知已由DyLogin发送等待后端处理", "INFO")
# 🔥 移除不再在backend_singleton中发送connect_message
# 抖音的连接状态报告应该在DyUtils中的DyLogin类中发送与拼多多保持一致
# 所有特殊状态通知已经在DyLogin中发送了,这里不需要重复发送
return result
except Exception as e:
@@ -360,16 +353,8 @@ class WebSocketManager:
except Exception as e:
self._log(f"启动抖音平台监听失败: {e}", "ERROR")
# 确保失败时也上报状态
if self.backend_client:
try:
self.backend_client.send_message({
"type": "connect_message",
"store_id": store_id,
"status": False
})
except Exception as send_e:
self._log(f"失败状态下报抖音平台连接状态也失败: {send_e}", "ERROR")
# 🔥 移除:确保失败时也不在这里上报状态
# 失败状态应该在DyLogin中处理与拼多多保持一致
def _start_qianniu_listener(self, store_id: str, cookies: str):
"""启动千牛平台监听(单连接多店铺架构)"""