[patch] 设计PDD的处理消息架构为msg_type 通用模式 处理PDD支持图片类型和 商品卡片类型的消息的发送回复 修改GUI托盘内部样式

This commit is contained in:
2025-10-21 14:20:19 +08:00
parent f7606f09cf
commit 9b21287bd0
4 changed files with 375 additions and 31 deletions

View File

@@ -150,12 +150,22 @@ class BackendClient:
# 消息循环
async for message in self.websocket:
try:
# 打印原始文本帧与长度
# 🔍 添加心跳检测日志
try:
raw_len = len(message.encode('utf-8')) if isinstance(message, str) else len(message)
print(f"后端发送消息体内容:{message}")
# 解析消息类型
data_preview = json.loads(message)
msg_type = data_preview.get('type', 'unknown')
# 心跳相关消息用DEBUG级别其他消息用INFO级别
if msg_type in ['pong', 'connection_status_ack']:
self._log(f"💓 [心跳] 收到后端响应: {msg_type}", "DEBUG")
else:
self._log(f"📨 [后端] 收到消息: type={msg_type}, 长度={raw_len}字节", "DEBUG")
print(f"后端发送消息体内容:{message}")
except Exception:
pass
data = json.loads(message)
self.on_message_received(data)
except json.JSONDecodeError:
@@ -696,7 +706,8 @@ class BackendClient:
store_id = message.get('store_id', '')
data = message.get('data')
content = message.get('content', '')
print(f"[{store_id}] [{message.get('msg_type', 'unknown')}] : {content}")
msg_type = message.get('msg_type', 'text') # 获取消息类型,默认为text
print(f"[{store_id}] [{msg_type}] : {content}")
# 尝试将后端AI/客服回复转发到对应平台
try:
@@ -713,7 +724,8 @@ class BackendClient:
elif platform_type == "千牛":
self._forward_to_qianniu(store_id, recv_pin, content)
elif platform_type == "拼多多":
self._forward_to_pdd(store_id, recv_pin, content)
# 传递msg_type参数支持图片/视频等类型
self._forward_to_pdd(store_id, recv_pin, content, msg_type)
else:
print(f"[Forward] 未知平台类型或未找到店铺: {platform_type}, store_id={store_id}")
except Exception as e:
@@ -867,8 +879,15 @@ class BackendClient:
except Exception as e:
print(f"[QN Forward] 转发失败: {e}")
def _forward_to_pdd(self, store_id: str, recv_pin: str, content: str):
"""转发消息到拼多多平台"""
def _forward_to_pdd(self, store_id: str, recv_pin: str, content: str, msg_type: str = "text"):
"""转发消息到拼多多平台
Args:
store_id: 店铺ID
recv_pin: 接收者ID
content: 消息内容文本或图片URL
msg_type: 消息类型text/image/video
"""
try:
from Utils.Pdd.PddUtils import WebsocketManager as PDDWSManager
pdd_mgr = PDDWSManager()
@@ -884,22 +903,22 @@ class BackendClient:
loop = platform_info.get('loop')
print(
f"[PDD Forward] shop_key={shop_key} has_pdd_instance={bool(pdd_instance)} has_loop={bool(loop)} recv_pin={recv_pin}")
f"[PDD Forward] shop_key={shop_key} has_pdd_instance={bool(pdd_instance)} has_loop={bool(loop)} recv_pin={recv_pin} msg_type={msg_type}")
if pdd_instance and loop and content:
# 在拼多多实例的事件循环中发送消息
def send_in_loop():
try:
# 在事件循环中执行发送
# 在事件循环中执行发送传递msg_type参数
future = asyncio.run_coroutine_threadsafe(
pdd_instance.send_message_external(recv_pin, content),
pdd_instance.send_message_external(recv_pin, content, msg_type),
loop
)
# 等待结果
try:
result = future.result(timeout=10) # 拼多多可能需要更长时间
if result:
print(f"[PDD Forward] 已转发到平台: uid={recv_pin}, content_len={len(content)}")
print(f"[PDD Forward] 已转发到平台: uid={recv_pin}, type={msg_type}, content_len={len(content)}")
else:
print(f"[PDD Forward] 转发失败: 拼多多实例返回False")
except Exception as fe: