实现pdd抖音平台cookie登录
This commit is contained in:
@@ -142,7 +142,47 @@ class DyLogin:
|
|||||||
response = requests.post(url, headers=self.headers, cookies=self.cookies, params=params, data=data)
|
response = requests.post(url, headers=self.headers, cookies=self.cookies, params=params, data=data)
|
||||||
self.cookies.update(response.cookies.get_dict())
|
self.cookies.update(response.cookies.get_dict())
|
||||||
self._log(f"验证码验证响应: {response.text}", "DEBUG")
|
self._log(f"验证码验证响应: {response.text}", "DEBUG")
|
||||||
ticket = re.findall('ticket=(.*?)",', response.text)[0]
|
|
||||||
|
# 🔥 修复:增加错误处理,检查响应是否包含错误信息
|
||||||
|
ticket = None
|
||||||
|
try:
|
||||||
|
# 尝试解析JSON响应
|
||||||
|
response_data = response.json()
|
||||||
|
if "error_code" in response_data:
|
||||||
|
error_code = response_data.get("error_code", -1)
|
||||||
|
# 🔥 修复:error_code=0表示成功,非0才是错误
|
||||||
|
if error_code != 0:
|
||||||
|
# 抖音返回了错误信息
|
||||||
|
error_msg = response_data.get("description", "验证码验证失败")
|
||||||
|
self._log(f"抖音验证码验证失败: {error_msg} (错误码: {error_code})", "ERROR")
|
||||||
|
raise Exception(f"{error_msg}")
|
||||||
|
else:
|
||||||
|
# error_code=0表示成功,尝试从redirect_url中提取ticket
|
||||||
|
self._log(f"✅ 抖音验证码验证成功 (错误码: {error_code})", "SUCCESS")
|
||||||
|
redirect_url = response_data.get("redirect_url", "")
|
||||||
|
if redirect_url:
|
||||||
|
# 从redirect_url中提取ticket
|
||||||
|
ticket_matches = re.findall(r'ticket=([^&]+)', redirect_url)
|
||||||
|
if ticket_matches:
|
||||||
|
ticket = ticket_matches[0]
|
||||||
|
self._log(f"✅ 从redirect_url中提取到ticket: {ticket[:20]}...", "SUCCESS")
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
# 如果不是JSON响应,继续用原来的方式解析
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 🔥 修复:如果JSON中没有获取到ticket,尝试用原来的方式解析
|
||||||
|
if not ticket:
|
||||||
|
ticket_matches = re.findall('ticket=(.*?)",', response.text)
|
||||||
|
if ticket_matches:
|
||||||
|
ticket = ticket_matches[0]
|
||||||
|
self._log(f"✅ 从响应文本中提取到ticket: {ticket[:20]}...", "SUCCESS")
|
||||||
|
|
||||||
|
# 最终检查是否获取到ticket
|
||||||
|
if not ticket:
|
||||||
|
# 没有找到ticket,说明验证失败
|
||||||
|
self._log("抖音验证码验证失败:响应中未找到ticket信息", "ERROR")
|
||||||
|
raise Exception("验证码验证失败:服务器未返回有效的ticket")
|
||||||
|
|
||||||
return ticket
|
return ticket
|
||||||
|
|
||||||
def callback(self, ticket):
|
def callback(self, ticket):
|
||||||
@@ -351,7 +391,8 @@ class DyLogin:
|
|||||||
message = {
|
message = {
|
||||||
"type": "connect_message",
|
"type": "connect_message",
|
||||||
"store_id": store_id,
|
"store_id": store_id,
|
||||||
"status": True # 登录成功
|
"status": True, # 登录成功
|
||||||
|
"cookies": self.cookies # 🔥 新增:添加登录生成的cookie信息
|
||||||
}
|
}
|
||||||
self._log(f"准备发送登录成功消息: {message}", "DEBUG")
|
self._log(f"准备发送登录成功消息: {message}", "DEBUG")
|
||||||
backend.send_message(message)
|
backend.send_message(message)
|
||||||
@@ -449,7 +490,8 @@ class DyLogin:
|
|||||||
# 将配置信息添加到cookies中
|
# 将配置信息添加到cookies中
|
||||||
self.cookies.update(shop_config)
|
self.cookies.update(shop_config)
|
||||||
self._log("🎉 登录成功!配置信息已获取", "SUCCESS")
|
self._log("🎉 登录成功!配置信息已获取", "SUCCESS")
|
||||||
# 🔥 不在这里发送成功通知,让backend_singleton统一处理
|
# 🔥 发送登录成功通知给后端(与拼多多保持一致)
|
||||||
|
self._send_login_success_message(store_id)
|
||||||
return self.cookies
|
return self.cookies
|
||||||
else:
|
else:
|
||||||
error_msg = "获取抖音平台配置信息失败"
|
error_msg = "获取抖音平台配置信息失败"
|
||||||
@@ -2354,6 +2396,25 @@ class DouYinListenerForGUI:
|
|||||||
# 在后台启动监听任务
|
# 在后台启动监听任务
|
||||||
asyncio.create_task(keep_running())
|
asyncio.create_task(keep_running())
|
||||||
|
|
||||||
|
# 🔥 新增:Cookie登录成功后发送登录成功报告(与登录参数模式保持一致)
|
||||||
|
try:
|
||||||
|
from WebSocket.backend_singleton import get_backend_client
|
||||||
|
backend = get_backend_client()
|
||||||
|
|
||||||
|
if backend:
|
||||||
|
message = {
|
||||||
|
"type": "connect_message",
|
||||||
|
"store_id": store_id,
|
||||||
|
"status": True, # 登录成功
|
||||||
|
"cookies": cookie_dict # 添加cookie信息
|
||||||
|
}
|
||||||
|
backend.send_message(message)
|
||||||
|
self._log("✅ [DY] 已向后端发送Cookie登录成功报告", "SUCCESS")
|
||||||
|
else:
|
||||||
|
self._log("⚠️ [DY] 无法获取后端客户端,跳过状态报告", "WARNING")
|
||||||
|
except Exception as e:
|
||||||
|
self._log(f"⚠️ [DY] 发送登录成功报告失败: {e}", "WARNING")
|
||||||
|
|
||||||
self._log("✅ [DY] 抖音平台连接成功,开始监听消息", "SUCCESS")
|
self._log("✅ [DY] 抖音平台连接成功,开始监听消息", "SUCCESS")
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -1691,7 +1691,8 @@ class PddLogin:
|
|||||||
message = {
|
message = {
|
||||||
"type": "connect_message",
|
"type": "connect_message",
|
||||||
"store_id": store_id,
|
"store_id": store_id,
|
||||||
"status": True # 登录成功
|
"status": True, # 登录成功
|
||||||
|
"cookies": self.cookies # 🔥 新增:添加登录生成的cookie信息
|
||||||
}
|
}
|
||||||
self._log(f"准备发送登录成功消息: {message}", "DEBUG")
|
self._log(f"准备发送登录成功消息: {message}", "DEBUG")
|
||||||
backend.send_message(message)
|
backend.send_message(message)
|
||||||
@@ -2801,6 +2802,28 @@ class ChatPdd:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._log(f"❌ 获取或发送客服列表失败: {e}", "ERROR")
|
self._log(f"❌ 获取或发送客服列表失败: {e}", "ERROR")
|
||||||
|
|
||||||
|
# 🔥 新增:Cookie登录成功后发送登录成功报告(与登录参数模式保持一致)
|
||||||
|
try:
|
||||||
|
if self.backend_service and hasattr(self, 'store_id') and self.store_id:
|
||||||
|
# 构建cookie字典(从cookies_str解析)
|
||||||
|
cookie_dict = {}
|
||||||
|
if hasattr(self, 'cookie') and self.cookie:
|
||||||
|
cookie_dict = self.cookie
|
||||||
|
|
||||||
|
message = {
|
||||||
|
"type": "connect_message",
|
||||||
|
"store_id": self.store_id,
|
||||||
|
"status": True, # 登录成功
|
||||||
|
"cookies": cookie_dict # 添加cookie信息
|
||||||
|
}
|
||||||
|
# 🔥 修复:使用正确的方法名 send_message_to_backend
|
||||||
|
await self.backend_service.send_message_to_backend(message)
|
||||||
|
self._log("✅ [PDD] 已向后端发送Cookie登录成功报告", "SUCCESS")
|
||||||
|
else:
|
||||||
|
self._log("⚠️ [PDD] 无法发送登录成功报告:backend_service或store_id缺失", "WARNING")
|
||||||
|
except Exception as e:
|
||||||
|
self._log(f"⚠️ [PDD] 发送登录成功报告失败: {e}", "WARNING")
|
||||||
|
|
||||||
# 启动消息监听和心跳
|
# 启动消息监听和心跳
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
self.heartbeat(websocket),
|
self.heartbeat(websocket),
|
||||||
@@ -3080,6 +3103,7 @@ class PddListenerForGUI:
|
|||||||
cookies_str=cookies,
|
cookies_str=cookies,
|
||||||
store=store
|
store=store
|
||||||
)
|
)
|
||||||
|
|
||||||
self._log("✅ [PDD] 拼多多平台连接成功,开始监听消息", "SUCCESS")
|
self._log("✅ [PDD] 拼多多平台连接成功,开始监听消息", "SUCCESS")
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -302,9 +302,19 @@ class WebSocketManager:
|
|||||||
self._log("🍪 使用Cookie启动抖音监听器", "INFO")
|
self._log("🍪 使用Cookie启动抖音监听器", "INFO")
|
||||||
self._log("🔄 开始执行 start_with_cookies", "DEBUG")
|
self._log("🔄 开始执行 start_with_cookies", "DEBUG")
|
||||||
try:
|
try:
|
||||||
cookie_dict = json.loads(cookies) if isinstance(cookies, str) else cookies
|
# 🔥 修复:尝试JSON解析,失败时用ast.literal_eval解析Python字典字符串
|
||||||
except json.JSONDecodeError as e:
|
if isinstance(cookies, str):
|
||||||
self._log(f"❌ Cookie JSON解析失败: {e}", "ERROR")
|
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
|
return False
|
||||||
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")
|
||||||
@@ -314,26 +324,9 @@ class WebSocketManager:
|
|||||||
self._log("✅ [DY] Cookie启动成功,平台连接已建立", "SUCCESS")
|
self._log("✅ [DY] Cookie启动成功,平台连接已建立", "SUCCESS")
|
||||||
self._notify_platform_connected("抖音")
|
self._notify_platform_connected("抖音")
|
||||||
|
|
||||||
# 🔥 根据实际登录结果上报状态给后端(与拼多多完全一致)
|
# 🔥 移除:不再在backend_singleton中发送connect_message
|
||||||
if self.backend_client and result not in ["need_verification_code", "verification_code_error", "login_failure"]:
|
# 抖音的连接状态报告应该在DyUtils中的DyLogin类中发送,与拼多多保持一致
|
||||||
# 如果是特殊状态,说明通知已经在DyLogin中发送了,不需要重复发送
|
# 所有特殊状态通知都已经在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")
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -360,16 +353,8 @@ class WebSocketManager:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._log(f"启动抖音平台监听失败: {e}", "ERROR")
|
self._log(f"启动抖音平台监听失败: {e}", "ERROR")
|
||||||
|
|
||||||
# 确保失败时也上报状态
|
# 🔥 移除:确保失败时也不在这里上报状态
|
||||||
if self.backend_client:
|
# 失败状态应该在DyLogin中处理,与拼多多保持一致
|
||||||
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")
|
|
||||||
|
|
||||||
def _start_qianniu_listener(self, store_id: str, cookies: str):
|
def _start_qianniu_listener(self, store_id: str, cookies: str):
|
||||||
"""启动千牛平台监听(单连接多店铺架构)"""
|
"""启动千牛平台监听(单连接多店铺架构)"""
|
||||||
|
|||||||
Reference in New Issue
Block a user