from __future__ import annotations import tkinter as tk from tkinter import ttk from tkinter.constants import BOTH, LEFT, X from typing import Callable from douyin.license.license_manager import ( get_default_activation_url, get_license_status_payload, install_license_or_activate, ) from gui.config_io import read_config def _format_status(payload: dict) -> str: state = "有效" if payload.get("ok") else "无效" reason = str(payload.get("reason") or "") customer = str(payload.get("customer") or "") expires = str(payload.get("expires_at") or "") parts = [f"状态:{state}", f"原因:{reason}"] if customer: parts.append(f"客户:{customer}") if expires: parts.append(f"到期:{expires}") return " | ".join(parts) def _activation_settings() -> tuple[str, int]: try: cfg = read_config() except Exception: cfg = {} section = cfg.get("license") if isinstance(cfg.get("license"), dict) else {} activation_url = str(section.get("activation_url") or "").strip() or get_default_activation_url() try: timeout_seconds = int(section.get("timeout_seconds") or 10) except (TypeError, ValueError): timeout_seconds = 10 return activation_url, max(1, timeout_seconds) def setup_license_tab( parent: ttk.Notebook, *, on_line: Callable[[str], None], ) -> ttk.Frame: tab = ttk.Frame(parent) parent.add(tab, text="授权") box = ttk.LabelFrame(tab, text="卡密授权") box.pack(fill=BOTH, expand=True, padx=10, pady=10) status_var = tk.StringVar(value="") machine_var = tk.StringVar(value="") path_var = tk.StringVar(value="") status_row = ttk.Frame(box) status_row.pack(fill=X, padx=10, pady=(10, 6)) ttk.Label(status_row, textvariable=status_var, foreground="#0f766e").pack( side=LEFT ) machine_row = ttk.Frame(box) machine_row.pack(fill=X, padx=10, pady=(0, 6)) ttk.Label(machine_row, text="本机机器码:", width=12).pack(side=LEFT) machine_entry = ttk.Entry(machine_row, textvariable=machine_var) machine_entry.pack(side=LEFT, fill=X, expand=True) path_row = ttk.Frame(box) path_row.pack(fill=X, padx=10, pady=(0, 10)) ttk.Label(path_row, text="授权文件:", width=12).pack(side=LEFT) ttk.Entry(path_row, textvariable=path_var).pack(side=LEFT, fill=X, expand=True) ttk.Label(box, text="输入卡密:").pack(anchor="w", padx=10) text = tk.Text(box, height=8, wrap="word") text.pack(fill=BOTH, expand=True, padx=10, pady=(4, 8)) hint = ttk.Label( box, text="只需要输入服务器发放的 DG- 卡密,程序会自动联网验证。", foreground="#666666", ) hint.pack(anchor="w", padx=10, pady=(0, 8)) buttons = ttk.Frame(box) buttons.pack(fill=X, padx=10, pady=(0, 10)) def refresh_status() -> None: payload = get_license_status_payload() status_var.set(_format_status(payload)) machine_var.set(str(payload.get("machine_fingerprint") or "")) path_var.set(str(payload.get("license_path") or "")) def install_from_text() -> None: raw = text.get("1.0", "end").strip() if not raw: on_line("[授权] 请输入卡密") return activation_url, timeout_seconds = _activation_settings() status = install_license_or_activate( raw, activation_url=activation_url, timeout_seconds=timeout_seconds, ) refresh_status() if status.ok: text.delete("1.0", "end") on_line("[授权] 卡密验证成功") else: on_line(f"[授权] 卡密验证失败:{status.reason} - {status.message}") ttk.Button(buttons, text="验证卡密", command=install_from_text).pack( side=LEFT, padx=(0, 8) ) ttk.Button(buttons, text="刷新状态", command=refresh_status).pack(side=LEFT) refresh_status() return tab