[patch] 修改逻辑: 在用户误触或开了多个GUI程序的时候不能同时建立多个连接 确保一个账号只能建立一个与后端的连接 友好提示的集成review 修改powershell语法问题

This commit is contained in:
2025-10-13 13:02:24 +08:00
parent 0abac8518c
commit aabd450ea3
2 changed files with 31 additions and 28 deletions

View File

@@ -18,6 +18,7 @@ class PlatformConnectionSignals(QObject):
"""平台连接信号(线程安全)"""
platform_connected = pyqtSignal(str, str) # (platform_name, store_id)
_backend_client = None
@@ -43,11 +44,11 @@ class WebSocketManager:
self.gui_update_callback = None
self.platform_listeners = {} # 存储各平台的监听器
self.connected_platforms = [] # 存储已连接的平台列表 # <- 新增
# 平台连接信号(线程安全)
self.platform_signals = PlatformConnectionSignals()
self.platform_signals.platform_connected.connect(self._on_platform_signal_received)
self.callbacks = {
'log': None,
'success': None,
@@ -87,7 +88,7 @@ class WebSocketManager:
self._notify_platform_connected(platform_name)
except Exception as e:
self._log(f"处理平台连接信号失败: {e}", "ERROR")
def _notify_platform_connected(self, platform_name: str):
"""通知GUI平台连接成功仅在主线程中调用"""
try:

52
main.py
View File

@@ -11,6 +11,8 @@ import config
from WebSocket.backend_singleton import get_websocket_manager
from windows_taskbar_fix import setup_windows_taskbar_icon
import os
# ===================== 文件日志系统 - 生产环境启用 =====================
# 重定向所有输出到文件,确保有日志记录
# from exe_file_logger import setup_file_logging, log_to_file # 生产环境禁用
@@ -463,7 +465,7 @@ class LoginWindow(QMainWindow):
"""显示断开连接提示框(信号槽函数,始终在主线程中执行)"""
try:
self.add_log(f"🎯 主线程收到断开信号: {disconnect_msg}", "INFO")
# 在状态标签显示警告信息
self.status_label.setText(f"⚠️ 账号在其他设备登录")
self.status_label.setStyleSheet(
@@ -783,7 +785,7 @@ class LoginWindow(QMainWindow):
self.add_log("⚠️ WebSocket管理器未初始化", "WARNING")
except Exception as e:
self.add_log(f"❌ 设置版本检查器失败: {e}", "ERROR")
def emit_update_signal(self, latest_version, download_url):
"""发射更新信号(可以在任何线程中调用)"""
try:
@@ -795,7 +797,7 @@ class LoginWindow(QMainWindow):
self.add_log(f"❌ 发射更新信号失败: {e}", "ERROR")
import traceback
self.add_log(f"详细错误: {traceback.format_exc()}", "ERROR")
def _show_update_dialog(self, latest_version, download_url):
"""显示更新对话框(信号槽函数,始终在主线程中执行)"""
try:
@@ -811,10 +813,10 @@ class LoginWindow(QMainWindow):
try:
self.add_log(f"🔔 准备显示更新通知: v{latest_version}", "INFO")
self.add_log(f" 下载地址: {download_url if download_url else '(空)'}", "INFO")
# 读取更新内容
update_content = self._get_update_content(latest_version)
# 检查下载地址
if not download_url or download_url.strip() == "":
# 下载地址为空,只显示通知,不提供下载
@@ -822,7 +824,7 @@ class LoginWindow(QMainWindow):
if update_content:
message += f"更新内容:\n{update_content}\n\n"
message += "下载地址暂未配置,请稍后再试或联系管理员。"
QMessageBox.information(
self,
"版本更新",
@@ -831,15 +833,15 @@ class LoginWindow(QMainWindow):
)
self.add_log(f"⚠️ 新版本 {latest_version} 的下载地址为空,已通知用户", "WARNING")
return # 安全返回,不崩溃
# 下载地址有效,显示更新对话框
message = f"发现新版本 {latest_version},是否立即更新?\n\n"
if update_content:
message += f"更新内容:\n{update_content}\n\n"
message += "点击确定将打开下载页面。"
reply = QMessageBox.question(
self,
"版本更新",
@@ -847,31 +849,31 @@ class LoginWindow(QMainWindow):
QMessageBox.Yes | QMessageBox.No,
QMessageBox.Yes
)
if reply == QMessageBox.Yes:
self.add_log("用户选择立即更新", "INFO")
self.trigger_update(download_url, latest_version)
else:
self.add_log("用户选择稍后更新", "INFO")
# 用户点击"否",不做任何操作,程序继续运行
except Exception as e:
self.add_log(f"❌ 显示更新通知失败: {e}", "ERROR")
import traceback
self.add_log(f"详细错误: {traceback.format_exc()}", "ERROR")
def _get_update_content(self, version):
"""获取版本更新内容"""
try:
import json
version_file = "version_history.json"
if not os.path.exists(version_file):
return ""
with open(version_file, 'r', encoding='utf-8') as f:
history = json.load(f)
# 查找对应版本
for record in history:
if record.get('version') == version:
@@ -883,9 +885,9 @@ class LoginWindow(QMainWindow):
if len(content) > 150:
content = content[:150] + "..."
return content
return ""
except Exception as e:
self.add_log(f"⚠️ 读取更新内容失败: {e}", "DEBUG")
return ""
@@ -894,7 +896,7 @@ class LoginWindow(QMainWindow):
"""触发更新下载(不阻塞主程序)"""
import webbrowser
import threading
# 检查下载地址是否有效
if not download_url or download_url.strip() == "":
self.add_log("⚠️ 下载地址为空,无法打开更新页面", "WARNING")
@@ -905,7 +907,7 @@ class LoginWindow(QMainWindow):
QMessageBox.Ok
)
return
# 明确提示用户即将下载
reply = QMessageBox.information(
self,
@@ -919,13 +921,13 @@ class LoginWindow(QMainWindow):
QMessageBox.Ok | QMessageBox.Cancel,
QMessageBox.Ok
)
if reply == QMessageBox.Cancel:
self.add_log("用户取消下载", "INFO")
return
self.add_log(f"📂 准备打开下载页面: {download_url}", "INFO")
# 在独立线程中打开浏览器确保不阻塞GUI主线程
def open_browser_thread():
try:
@@ -933,10 +935,10 @@ class LoginWindow(QMainWindow):
self.add_log("✅ 浏览器已打开,下载即将开始", "SUCCESS")
except Exception as e:
self.add_log(f"❌ 打开浏览器失败: {e}", "ERROR")
thread = threading.Thread(target=open_browser_thread, daemon=True)
thread.start()
self.add_log("✅ 已启动下载,程序继续运行", "INFO")