Todo: 补充nsis集成需要的依赖 并提交uninstall logo的图片资源 并集成关于GUI版本控制显示代码

This commit is contained in:
2025-09-29 15:38:34 +08:00
parent c58cec750f
commit 1ac50b99a9
7 changed files with 1017 additions and 250 deletions

View File

@@ -30,6 +30,7 @@ class BackendClient:
self.login_callback: Optional[Callable] = None # 新增平台登录下发cookies回调
self.success_callback: Optional[Callable] = None # 新增:后端连接成功回调
self.token_error_callback: Optional[Callable] = None # 新增token错误回调
self.version_callback: Optional[Callable] = None # 新增:版本检查回调
self.is_connected = False
@@ -100,15 +101,15 @@ class BackendClient:
ping_timeout=WS_PING_TIMEOUT, # 可配置ping超时
close_timeout=10, # 10秒关闭超时
# 增加TCP keepalive配置
max_size=2**20, # 1MB最大消息大小
max_queue=32, # 最大队列大小
max_size=2 ** 20, # 1MB最大消息大小
max_queue=32, # 最大队列大小
compression=None # 禁用压缩以提高性能
)
print(f"[连接] 已启用心跳ping_interval={WS_PING_INTERVAL}s, ping_timeout={WS_PING_TIMEOUT}s")
else:
self.websocket = await websockets.connect(
self.url,
max_size=2**20,
max_size=2 ** 20,
max_queue=32,
compression=None
)
@@ -118,13 +119,13 @@ class BackendClient:
self.reconnect_attempts = 0 # 重置重连计数
self.is_reconnecting = False
print("后端WebSocket连接成功")
# 等待连接稳定后再发送状态通知
await asyncio.sleep(0.5)
# 发送连接状态通知给后端
self._notify_connection_status(True)
self.on_connected()
# 消息循环
@@ -144,7 +145,7 @@ class BackendClient:
except websockets.ConnectionClosed as e:
self.is_connected = False
self._notify_connection_status(False) # 通知断开
# 详细分析断开原因
if e.code == 1006:
print(f"[重连] WebSocket异常关闭 (1006): 可能是心跳超时或网络问题")
@@ -154,7 +155,7 @@ class BackendClient:
print(f"[重连] WebSocket关闭 (1001): 端点离开")
else:
print(f"[重连] WebSocket关闭 ({e.code}): {e.reason}")
self._handle_connection_closed(e)
if not await self._should_reconnect():
break
@@ -263,7 +264,8 @@ class BackendClient:
error: Callable = None,
login: Callable = None,
success: Callable = None,
token_error: Callable = None):
token_error: Callable = None,
version: Callable = None):
"""设置各种消息类型的回调函数"""
if store_list:
self.store_list_callback = store_list
@@ -283,6 +285,8 @@ class BackendClient:
self.success_callback = success
if token_error:
self.token_error_callback = token_error
if version:
self.version_callback = version
def on_connected(self):
"""连接成功时的处理"""
@@ -337,14 +341,14 @@ class BackendClient:
try:
if not self.loop:
return
# 获取当前活跃平台的store_id
active_store_id = None
try:
from Utils.JD.JdUtils import WebsocketManager as JdManager
from Utils.Dy.DyUtils import DouYinWebsocketManager as DyManager
from Utils.Dy.DyUtils import DouYinWebsocketManager as DyManager
from Utils.Pdd.PddUtils import WebsocketManager as PddManager
# 检查各平台是否有活跃连接
for mgr_class, platform_name in [(JdManager, "京东"), (DyManager, "抖音"), (PddManager, "拼多多")]:
try:
@@ -365,30 +369,30 @@ class BackendClient:
continue
except Exception as e:
print(f"[状态] 获取活跃平台信息失败: {e}")
status_message = {
"type": "connection_status",
"status": connected,
"timestamp": int(time.time()),
"client_uuid": self.uuid
}
# 如果有活跃平台添加store_id
if active_store_id:
status_message["store_id"] = active_store_id
print(f"[状态] 添加store_id到状态消息: {active_store_id}")
else:
print(f"[状态] 未检测到活跃平台不添加store_id")
# 异步发送状态通知
asyncio.run_coroutine_threadsafe(
self._send_to_backend(status_message),
self.loop
)
status_text = "连接" if connected else "断开"
print(f"[状态] 已通知后端GUI客户端{status_text}")
except Exception as e:
print(f"[状态] 发送状态通知失败: {e}")
import traceback
@@ -437,6 +441,8 @@ class BackendClient:
self._handle_token_error(message)
elif msg_type == 'staff_list':
self._handle_staff_list(message)
elif msg_type == 'version_response': # 新增:版本检查响应
self._handle_version_response(message)
else:
print(f"未知消息类型: {msg_type}")
@@ -1151,12 +1157,14 @@ class BackendClient:
content = message.get('content', '')
data = message.get('data', {})
# 判断是拼多多登录参数还是普通Cookie
if platform_name == "拼多多" and ("拼多多登录" in content) and data.get('login_params'):
# 拼多多登录参数模式 - 传递完整的消息JSON给处理器
print(f"收到拼多多登录参数: 平台={platform_name}, 店铺={store_id}, 类型={content}")
# 🔥 判断是登录参数模式还是普通Cookie模式(支持拼多多和抖音)
if (platform_name in ["拼多多", "抖音"] and
(("拼多多登录" in content and data.get('login_params')) or
("抖音登录" in content and data.get('login_flow')))):
# 登录参数模式 - 传递完整的消息JSON给处理器
print(f"收到{platform_name}登录参数: 平台={platform_name}, 店铺={store_id}, 类型={content}")
if self.login_callback:
# 传递完整的JSON消息拼多多处理器来解析login_params
# 传递完整的JSON消息让处理器来解析登录参数
import json
full_message = json.dumps(message)
self.login_callback(platform_name, store_id, full_message)
@@ -1170,13 +1178,13 @@ class BackendClient:
"""处理错误消息"""
error_msg = message.get('error', '未知错误')
content = message.get('content', '')
# 检查是否为token错误无论type是error还是error_token
if content == "无效的exe_token" or "无效的exe_token" in content:
print(f"[错误] 检测到token错误: {content}")
self._handle_token_error(message)
return
print(f"后端连接错误: {error_msg}")
if self.error_callback:
self.error_callback(error_msg, message)
@@ -1185,15 +1193,15 @@ class BackendClient:
"""处理token错误消息 - 无效token时停止重连并显示错误"""
error_content = message.get('content', '无效的exe_token')
print(f"[错误] Token验证失败: {error_content}")
# 停止重连机制
self.should_stop = True
self.is_reconnecting = False
# 触发token错误回调
if self.token_error_callback:
self.token_error_callback(error_content)
# 主动关闭连接
if self.websocket:
asyncio.run_coroutine_threadsafe(self.websocket.close(), self.loop)
@@ -1211,6 +1219,16 @@ class BackendClient:
if self.customers_callback: # 假设客服列表更新也触发客服列表回调
self.customers_callback(staff_list, store_id)
def _handle_version_response(self, message: Dict[str, Any]):
"""处理版本检查响应"""
latest_version = message.get('latest_version')
download_url = message.get('download_url')
print(f"收到版本检查响应: 最新版本={latest_version}, 下载地址={download_url}")
if self.version_callback:
self.version_callback(message)
# ==================== 辅助方法 ====================
def set_token(self, token: str):