# -*- coding: utf-8 -*- """首次激活对话框:服务器地址 + 激活码。""" from __future__ import annotations import tkinter as tk from tkinter import messagebox, ttk import hub_client from device_store import is_activated, load_device, machine_fingerprint, save_device def ensure_activated(root: tk.Tk) -> bool: """已激活且能 ping 通则通过;否则弹出激活窗。返回是否可继续。""" device = load_device() if device.get("token") and device.get("server"): if hub_client.ping(): return True # token 失效时允许重新激活 if not messagebox.askyesno( "激活校验", "无法连接中控或令牌失效,是否重新激活?\n选「否」将以离线模式继续(不上报复盘)。", parent=root, ): return True win = tk.Toplevel(root) win.title("激活黄金辅助") win.geometry("460x280") win.transient(root) win.grab_set() result = {"ok": False} frm = ttk.Frame(win, padding=16) frm.pack(fill=tk.BOTH, expand=True) ttk.Label(frm, text="首次使用请输入中控地址与激活码(网页下载中心生成)").pack(anchor="w") ttk.Label(frm, text="服务器地址(例:http://192.168.1.10:5800)").pack(anchor="w", pady=(12, 4)) var_server = tk.StringVar(value=device.get("server") or "http://127.0.0.1:5800") ttk.Entry(frm, textvariable=var_server, width=48).pack(fill=tk.X) ttk.Label(frm, text="激活码").pack(anchor="w", pady=(12, 4)) var_code = tk.StringVar() ttk.Entry(frm, textvariable=var_code, width=48).pack(fill=tk.X) ttk.Label(frm, text="本机名称(可改)").pack(anchor="w", pady=(12, 4)) var_name = tk.StringVar(value=device.get("machine_name") or "") ttk.Entry(frm, textvariable=var_name, width=48).pack(fill=tk.X) status = ttk.Label(frm, text="", foreground="#888") status.pack(anchor="w", pady=(10, 0)) def do_skip(): result["ok"] = True win.destroy() def do_ok(): server = var_server.get().strip() code = var_code.get().strip() if not server or not code: status.configure(text="请填写服务器与激活码", foreground="#f6465d") return try: hub_client.activate(server, code, machine_fingerprint(), var_name.get().strip() or "PC") result["ok"] = True messagebox.showinfo("激活成功", "已绑定账户与机器,成交将上报复盘中心。", parent=win) win.destroy() except hub_client.HubError as exc: status.configure(text=str(exc), foreground="#f6465d") btns = ttk.Frame(frm) btns.pack(fill=tk.X, pady=(16, 0)) ttk.Button(btns, text="离线使用", command=do_skip).pack(side=tk.LEFT) ttk.Button(btns, text="激活", command=do_ok).pack(side=tk.RIGHT) win.protocol("WM_DELETE_WINDOW", do_skip) root.wait_window(win) return result["ok"]