拼多多实现加密参数登录+京东客服转接

This commit is contained in:
jjz
2025-09-13 15:19:39 +08:00
parent e9361b4c87
commit 22122660df
4 changed files with 319 additions and 8 deletions

View File

@@ -657,6 +657,67 @@ class BackendClient:
except Exception as e:
print(f"[PDD Transfer] 拼多多转接失败: {e}")
def _transfer_to_jd(self, customer_service_id: str, user_id: str, store_id: str):
"""执行京东平台转接操作"""
try:
from Utils.JD.JdUtils import WebsocketManager as JDWSManager
jd_mgr = JDWSManager()
shop_key = f"京东:{store_id}"
entry = jd_mgr.get_connection(shop_key)
if not entry:
print(f"[JD Transfer] 未找到京东连接: {shop_key}")
return
platform_info = entry.get('platform', {})
ws = platform_info.get('ws')
aid = platform_info.get('aid')
pin_zj = platform_info.get('pin_zj')
loop = platform_info.get('loop')
print(f"[JD Transfer] 找到京东连接,准备执行转接: user_id={user_id}, cs_id={customer_service_id}")
print(f"[JD Transfer] 连接参数: has_ws={bool(ws)}, aid={aid}, pin_zj={pin_zj}, has_loop={bool(loop)}")
if ws and aid and pin_zj and loop:
# 在事件循环中执行转接
def transfer_in_loop():
try:
# 导入FixJdCookie类来调用转接方法
from Utils.JD.JdUtils import FixJdCookie
jd_utils = FixJdCookie()
# 在事件循环中执行转接
future = asyncio.run_coroutine_threadsafe(
jd_utils.transfer_customer(ws, aid, user_id, pin_zj, customer_service_id),
loop
)
# 等待转接结果
try:
result = future.result(timeout=10) # 京东转接超时时间
if result:
print(f"[JD Transfer] ✅ 转接成功: user_id={user_id} -> cs_id={customer_service_id}")
else:
print(f"[JD Transfer] ❌ 转接失败: user_id={user_id}")
except Exception as fe:
print(f"[JD Transfer] 转接执行失败: {fe}")
except Exception as e:
print(f"[JD Transfer] 转接过程异常: {e}")
# 在新线程中执行转接操作
import threading
transfer_thread = threading.Thread(target=transfer_in_loop, daemon=True)
transfer_thread.start()
else:
print("[JD Transfer] 条件不足,未转接:",
{
'has_ws': bool(ws), 'has_aid': bool(aid), 'has_pin_zj': bool(pin_zj),
'has_loop': bool(loop)
})
except Exception as e:
print(f"[JD Transfer] 京东转接失败: {e}")
def _handle_transfer(self, message: Dict[str, Any]):
"""处理转接消息"""
# 新版转接消息格式: {"type": "transfer", "content": "客服ID", "receiver": {"id": "用户ID"}, "store_id": "店铺ID"}
@@ -672,8 +733,7 @@ class BackendClient:
platform_type = self._get_platform_by_store_id(store_id)
if platform_type == "京东":
# 京东转接逻辑 - 待实现
print(f"[JD Transfer] 京东平台转接功能待实现")
self._transfer_to_jd(customer_service_id, user_id, store_id)
elif platform_type == "抖音":
# 抖音转接逻辑 - 待实现
print(f"[DY Transfer] 抖音平台转接功能待实现")
@@ -711,13 +771,26 @@ class BackendClient:
self.get_store()
def _handle_login(self, message: Dict[str, Any]):
"""处理平台登录消息新版type=login, cookies, store_id, platform_name"""
"""处理平台登录消息新版type=login, cookies/login_params, store_id, platform_name"""
cookies = message.get('cookies', '')
store_id = message.get('store_id', '')
platform_name = message.get('platform_name', '')
print(f"收到登录指令: 平台={platform_name}, 店铺={store_id}, cookies_len={len(cookies) if cookies else 0}")
if self.login_callback:
self.login_callback(platform_name, store_id, cookies)
content = message.get('content', '')
data = message.get('data', {})
# 判断是拼多多登录参数还是普通Cookie
if platform_name == "拼多多" and content == "pdd_login_params" and data.get('login_params'):
# 拼多多登录参数模式
import json
login_params_str = json.dumps({"data": data})
print(f"收到拼多多登录参数: 平台={platform_name}, 店铺={store_id}, params_len={len(login_params_str)}")
if self.login_callback:
self.login_callback(platform_name, store_id, login_params_str)
else:
# 普通Cookie模式
print(f"收到登录指令: 平台={platform_name}, 店铺={store_id}, cookies_len={len(cookies) if cookies else 0}")
if self.login_callback:
self.login_callback(platform_name, store_id, cookies)
def _handle_error_message(self, message: Dict[str, Any]):
"""处理错误消息"""

View File

@@ -257,13 +257,19 @@ class WebSocketManager:
except Exception as e:
self._log(f"启动千牛平台监听失败: {e}", "ERROR")
def _start_pdd_listener(self, store_id: str, cookies: str):
def _start_pdd_listener(self, store_id: str, data: str):
"""启动拼多多平台监听"""
try:
def _runner():
try:
listener = PDDListenerForGUI_WS(log_callback=self._log)
result = asyncio.run(listener.start_with_cookies(store_id=store_id, cookies=cookies))
# 判断是登录参数还是Cookie
if self._is_pdd_login_params(data):
# 使用登录参数启动
result = asyncio.run(listener.start_with_login_params(store_id=store_id, login_params=data))
else:
# 使用Cookie启动兼容旧方式
result = asyncio.run(listener.start_with_cookies(store_id=store_id, cookies=data))
return result
except Exception as e:
self._log(f"拼多多监听器运行异常: {e}", "ERROR")
@@ -312,6 +318,15 @@ class WebSocketManager:
except Exception as send_e:
self._log(f"失败状态下报拼多多平台连接状态也失败: {send_e}", "ERROR")
def _is_pdd_login_params(self, data: str) -> bool:
"""判断是否为拼多多登录参数"""
try:
import json
parsed_data = json.loads(data)
return (parsed_data.get("data", {}).get("login_params") is not None)
except:
return False
def send_message(self, message: dict):
"""发送消息到后端"""
if self.backend_client and self.backend_client.is_connected: