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_license_status_payload, install_license_text, ) 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 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="支持粘贴 DYGJ- 开头的卡密,或完整 license.dat 内容。卡密不会写入 config.json。", 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 status = install_license_text(raw) 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