# -*- coding: utf-8 -*- """ 项目配置文件 统一管理所有配置参数,避免硬编码 """ # 用户访问令牌(默认空字符串) import os # 用于路径与目录操作(写入用户配置目录) import json # 用于将令牌保存为 JSON 格式 # 后端服务器配置 # BACKEND_HOST = "192.168.5.233" # BACKEND_HOST = "192.168.5.12" BACKEND_HOST = "shuidrop.com" # BACKEND_HOST = "test.shuidrop.com" # BACKEND_PORT = "8000" BACKEND_PORT = "" # BACKEND_WS_URL = f"ws://{BACKEND_HOST}:{BACKEND_PORT}" BACKEND_WS_URL = f"wss://{BACKEND_HOST}" # WebSocket配置 WS_CONNECT_TIMEOUT = 16.0 WS_MESSAGE_TIMEOUT = 30.0 WS_PING_INTERVAL = 10 # 10秒ping间隔(提高检测频率) WS_PING_TIMEOUT = 5 # 5秒ping超时(更快检测断线) WS_ENABLE_PING = True # 是否启用WebSocket原生ping心跳 WS_ENABLE_APP_PING = False # 禁用应用层ping心跳(避免重复) # AI处理超时配置 AI_PROCESS_TIMEOUT = 30 # AI处理超时时间(秒) AI_LONG_PROCESS_THRESHOLD = 10 # AI长时间处理阈值(秒) # 内存管理配置 MAX_PENDING_REPLIES = 100 CLEANUP_INTERVAL = 300 # 5分钟 FUTURE_TIMEOUT = 300 # 5分钟 # 终端日志配置(简化) LOG_LEVEL = "INFO" VERSION = "1.0" # GUI配置 WINDOW_TITLE = "AI回复连接入口-V1.0" # 平台特定配置 PLATFORMS = { "JD": { "name": "京东", "ws_url": "wss://dongdong.jd.com/workbench/websocket" }, "DOUYIN": { "name": "抖音", "ws_url": None # 动态获取 }, "QIANNIU": { "name": "千牛(淘宝)", "ws_url": "ws://127.0.0.1:3030" }, "PDD": { "name": "拼多多", "ws_url": None # 动态获取 } } def get_backend_ws_url(platform: str, store_id: str) -> str: """获取旧版后端WebSocket URL(按店铺建连接,保留兼容)""" return f"{BACKEND_WS_URL}/ws/platform/{platform.lower()}/{store_id}/" def get_gui_ws_url(exe_token: str) -> str: """获取新版单连接GUI专用WebSocket URL(按用户token建一条连接)""" return f"{BACKEND_WS_URL}/ws/gui/{exe_token}/" def get_config(): """获取所有配置""" return { 'backend_host': BACKEND_HOST, 'backend_port': BACKEND_PORT, 'backend_ws_url': BACKEND_WS_URL, 'ws_connect_timeout': WS_CONNECT_TIMEOUT, 'ws_message_timeout': WS_MESSAGE_TIMEOUT, 'max_pending_replies': MAX_PENDING_REPLIES, 'cleanup_interval': CLEANUP_INTERVAL, 'platforms': PLATFORMS } APP_NAME = "ShuidropGUI" # 应用名称(作为配置目录名) API_TOKEN = 'sd_acF0TisgfFOtsBm4ytqb17MQbcxuX9Vp' # 默认回退令牌(仅当未找到外部配置时使用) def _get_config_paths(): """返回(配置目录, 配置文件路径),位于 %APPDATA%/ShuidropGUI/config.json""" base_dir = os.getenv('APPDATA') or os.path.expanduser('~') # 优先使用 APPDATA,其次使用用户主目录 cfg_dir = os.path.join(base_dir, APP_NAME) # 组合配置目录路径 cfg_file = os.path.join(cfg_dir, 'config.json') # 组合配置文件路径 return cfg_dir, cfg_file def get_saved_token() -> str: """优先从外部 JSON 配置读取令牌,不存在时回退到内置 API_TOKEN""" try: cfg_dir, cfg_file = _get_config_paths() # 获取目录与文件路径 if os.path.exists(cfg_file): # 如果配置文件存在 with open(cfg_file, 'r', encoding='utf-8') as f: # 以 UTF-8 读取 data = json.load(f) # 解析 JSON 内容 token = data.get('token', '') # 读取 token 字段 if token: # 如果有效 return token # 返回读取到的令牌 except Exception: pass # 读取失败时静默回退 return API_TOKEN # 回退为内置的默认值 def set_saved_token(new_token: str) -> bool: """将访问令牌写入外部 JSON 配置,并更新内存中的值 - new_token: 新的访问令牌字符串 返回: True 表示写入成功,False 表示失败 """ try: cfg_dir, cfg_file = _get_config_paths() os.makedirs(cfg_dir, exist_ok=True) data = {'token': new_token} with open(cfg_file, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=2) # 同步更新内存变量,保证运行期可立即生效 global API_TOKEN API_TOKEN = new_token return True except Exception as e: # 发生异常时打印提示并返回失败 print(f"写入令牌失败: {e}") return False