824 lines
34 KiB
Python
824 lines
34 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import threading
|
|
import tkinter as tk
|
|
from tkinter import messagebox, ttk
|
|
|
|
from douyin.core.version import print_version_banner
|
|
from douyin.diagnostics.log_upload import upload_diagnostic_logs
|
|
from douyin.license.license_manager import (
|
|
check_license,
|
|
get_default_activation_url,
|
|
install_license_or_activate,
|
|
require_online_license_or_exit,
|
|
start_online_license_monitor,
|
|
)
|
|
from douyin.updater.force_update import (
|
|
UpdateError,
|
|
load_manual_update_settings,
|
|
perform_forced_update,
|
|
)
|
|
from gui.components.config_io import save_ui_to_config
|
|
from gui.components.config_vars import build_config_vars
|
|
from gui.components.mousewheel_guard import disable_spinbox_mousewheel
|
|
from gui.components.run_params_builder import get_params as build_run_params
|
|
from gui.components.screenshot_action import do_screenshot
|
|
from gui.components.update_progress import apply_update_progress_event
|
|
from gui.components.wifi_adb_dialog import open_wifi_adb_dialog
|
|
from gui.components.wifi_adb_one_click_scan import on_scan_one_click
|
|
from gui.components.wifi_adb_pick_dialog import open_scan_pick_dialog
|
|
from gui.components.wifi_adb_reconnect import try_reconnect_last_wifi
|
|
from gui.gui_core import Runner, find_adb_executable, list_adb_devices, read_config, write_config
|
|
|
|
from . import theme
|
|
from .data import load_dashboard_data
|
|
from .page_context import PageContext
|
|
from .page_registry import PAGES
|
|
from .widgets import ScrollFrame, SlimScrollbar, button, clear, nav_button, rounded_surface, status_pill
|
|
|
|
|
|
def enforce_dashboard_license_required() -> None:
|
|
os.environ["DOUYIN_LICENSE_REQUIRED"] = "1"
|
|
|
|
|
|
class DashboardApp:
|
|
def __init__(self, *, initial_page: str = "数据看板", license_locked: bool = False) -> None:
|
|
self.root = tk.Tk()
|
|
try:
|
|
self.root.tk.call("tk", "scaling", 1.12)
|
|
except Exception:
|
|
pass
|
|
self.root.title("抖音自动化助手 - 数据看板")
|
|
self.root.geometry("1360x900")
|
|
self.root.minsize(1160, 760)
|
|
theme.setup_styles(self.root)
|
|
disable_spinbox_mousewheel(self.root)
|
|
|
|
self.data = load_dashboard_data()
|
|
self.runner = Runner(self._on_runner_line)
|
|
self.config_vars = build_config_vars(self.data.cfg)
|
|
self.text_widgets: dict[str, tk.Text] = {}
|
|
self.text_cache = {
|
|
"want_text": self.data.cfg.get("contains_want", []) or [],
|
|
"not_want_text": self.data.cfg.get("contains_not_want", []) or [],
|
|
"yg_comment_texts": self.data.cfg.get("yanghao_comment_texts", []) or [],
|
|
"yg_like_keywords_text": self.data.cfg.get("yanghao_like_keywords", []) or [],
|
|
"yg_fav_keywords_text": self.data.cfg.get("yanghao_fav_keywords", []) or [],
|
|
}
|
|
self.license_locked = bool(license_locked)
|
|
self.current_page = "授权" if self.license_locked else (initial_page if initial_page in PAGES else "数据看板")
|
|
self._license_monitor_started = False
|
|
self.status_var = tk.StringVar(value="请先验证卡密" if self.license_locked else "就绪")
|
|
self.adb_exe = find_adb_executable()
|
|
self.device_var = tk.StringVar(value="")
|
|
self.y_var = self.config_vars.get("y_var", tk.IntVar(value=0))
|
|
self.image_var = tk.StringVar(value="screenshot.png")
|
|
self.ocr_results: list = []
|
|
self._scan_busy = {"flag": False}
|
|
self.nav_buttons: dict[str, tk.Widget] = {}
|
|
self._startup_progress: tk.Toplevel | None = None
|
|
|
|
self._build_shell()
|
|
self.status_var.trace_add("write", lambda *_: self._sync_status_pill())
|
|
self.refresh_devices(silent=True)
|
|
self.root.after(300, self._try_reconnect_last_wifi)
|
|
self.show_page(self.current_page)
|
|
|
|
def _ensure_license_ready(self) -> bool:
|
|
if not self.license_locked:
|
|
return True
|
|
self.status_var.set("请先验证卡密")
|
|
try:
|
|
self.show_page("授权")
|
|
except Exception:
|
|
pass
|
|
messagebox.showwarning("需要授权", "请先在“授权”页面输入卡密并验证成功。", parent=self.root)
|
|
return False
|
|
|
|
def _start_license_monitor_once(self) -> None:
|
|
if self._license_monitor_started:
|
|
return
|
|
start_online_license_monitor(on_line=self._on_runner_line)
|
|
self._license_monitor_started = True
|
|
|
|
def _build_shell(self) -> None:
|
|
shell = ttk.Frame(self.root, style="Dashboard.TFrame")
|
|
shell.pack(fill="both", expand=True)
|
|
|
|
top = rounded_surface(shell, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_LG)
|
|
top.pack(fill="x", padx=20, pady=(12, 6))
|
|
brand = tk.Frame(top, bg=theme.SURFACE)
|
|
brand.pack(fill="x", padx=20, pady=(10, 4))
|
|
brand.grid_columnconfigure(1, weight=1)
|
|
mark = tk.Frame(brand, bg=theme.TEAL, width=34, height=34)
|
|
mark.grid(row=0, column=0, sticky="w", padx=(0, 12))
|
|
mark.pack_propagate(False)
|
|
tk.Label(mark, text="抖", bg=theme.TEAL, fg="#ffffff", font=("Microsoft YaHei UI", 17, "bold")).pack(expand=True)
|
|
title_box = tk.Frame(brand, bg=theme.SURFACE)
|
|
title_box.grid(row=0, column=1, sticky="w")
|
|
tk.Label(title_box, text="抖音自动化助手", bg=theme.SURFACE, fg=theme.TEXT, font=("Microsoft YaHei UI", 20, "bold")).pack(anchor="w")
|
|
tk.Label(title_box, text="轻量数据看板入口", bg=theme.SURFACE, fg=theme.SUBTLE, font=theme.FONT_SMALL).pack(anchor="w")
|
|
quick_state = tk.Frame(brand, bg=theme.SURFACE)
|
|
quick_state.grid(row=0, column=2, sticky="e")
|
|
button(
|
|
quick_state,
|
|
"检查更新",
|
|
self.check_update,
|
|
width=124,
|
|
).pack(side="right", padx=(8, 0))
|
|
tk.Label(
|
|
quick_state,
|
|
textvariable=self.status_var,
|
|
bg=theme.TEAL_SOFT,
|
|
fg=theme.TEAL_DARK,
|
|
font=("Microsoft YaHei UI", 13, "bold"),
|
|
padx=16,
|
|
pady=8,
|
|
).pack(side="right")
|
|
tk.Label(
|
|
quick_state,
|
|
text="当前状态",
|
|
bg=theme.SURFACE,
|
|
fg=theme.MUTED,
|
|
font=theme.FONT_SMALL,
|
|
).pack(side="right", padx=(0, 8))
|
|
tool_row = tk.Frame(top, bg=theme.SURFACE)
|
|
tool_row.pack(fill="x", padx=20, pady=(4, 14))
|
|
tool_row.grid_columnconfigure(0, minsize=350)
|
|
tool_row.grid_columnconfigure(1, weight=1)
|
|
tool_row.grid_columnconfigure(2, minsize=318)
|
|
|
|
device_group = tk.Frame(tool_row, bg=theme.SURFACE)
|
|
device_group.grid(row=0, column=0, sticky="ew", padx=(0, 16))
|
|
tk.Label(
|
|
device_group,
|
|
text="设备",
|
|
bg=theme.SURFACE,
|
|
fg=theme.MUTED,
|
|
font=("Microsoft YaHei UI", 12, "bold"),
|
|
).pack(anchor="w", pady=(0, 7))
|
|
self.device_combo = ttk.Combobox(device_group, textvariable=self.device_var, values=[], width=38, state="readonly")
|
|
self.device_combo.pack(fill="x")
|
|
|
|
device_actions = tk.Frame(tool_row, bg=theme.SURFACE)
|
|
device_actions.grid(row=0, column=1, sticky="w", padx=(0, 12))
|
|
tk.Label(
|
|
device_actions,
|
|
text="设备工具",
|
|
bg=theme.SURFACE,
|
|
fg=theme.MUTED,
|
|
font=("Microsoft YaHei UI", 12, "bold"),
|
|
).pack(anchor="w", pady=(0, 7))
|
|
device_button_row = tk.Frame(device_actions, bg=theme.SURFACE)
|
|
device_button_row.pack(anchor="w")
|
|
button(device_button_row, "刷新设备", self.refresh_devices, width=120).pack(side="left", padx=(0, 6))
|
|
self.scan_button = button(device_button_row, "扫描 WiFi", self.scan_wifi, width=124)
|
|
self.scan_button.pack(side="left", padx=(0, 6))
|
|
button(device_button_row, "WiFi ADB", self.open_wifi_adb, width=112).pack(side="left", padx=(0, 6))
|
|
button(device_button_row, "上传日志", self.upload_logs, width=112).pack(side="left")
|
|
|
|
primary_actions = tk.Frame(tool_row, bg=theme.SURFACE)
|
|
primary_actions.grid(row=0, column=2, sticky="e")
|
|
tk.Label(
|
|
primary_actions,
|
|
text="主操作",
|
|
bg=theme.SURFACE,
|
|
fg=theme.MUTED,
|
|
font=("Microsoft YaHei UI", 12, "bold"),
|
|
).pack(anchor="e", pady=(0, 7))
|
|
primary_button_row = tk.Frame(primary_actions, bg=theme.SURFACE)
|
|
primary_button_row.pack(anchor="e")
|
|
button(primary_button_row, "保存配置", self.save_config, width=130).pack(side="left", padx=(0, 8))
|
|
button(primary_button_row, "截图+OCR", self.screenshot_ocr, kind="primary", width=148).pack(side="left")
|
|
|
|
main = ttk.Frame(shell, style="Dashboard.TFrame")
|
|
main.pack(fill="both", expand=True, padx=20, pady=(6, 6))
|
|
main.grid_columnconfigure(1, weight=1)
|
|
main.grid_rowconfigure(0, weight=1)
|
|
|
|
sidebar = rounded_surface(main, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_LG)
|
|
sidebar.grid(row=0, column=0, sticky="nsw", padx=(0, 18))
|
|
sidebar.configure(width=218)
|
|
sidebar.grid_propagate(False)
|
|
sidebar.pack_propagate(False)
|
|
nav_canvas = tk.Canvas(sidebar, bg=theme.SURFACE, highlightthickness=0, bd=0)
|
|
nav_scroll = SlimScrollbar(sidebar, command=lambda *args: nav_canvas.yview(*[a for a in args if a is not None]))
|
|
nav_inner = tk.Frame(nav_canvas, bg=theme.SURFACE)
|
|
nav_window = nav_canvas.create_window((0, 0), window=nav_inner, anchor="nw")
|
|
nav_canvas.configure(yscrollcommand=nav_scroll.set)
|
|
nav_scroll.pack(side="right", fill="y", padx=(0, 8), pady=12)
|
|
nav_canvas.pack(side="left", fill="both", expand=True, padx=(0, 4), pady=8)
|
|
nav_inner.bind("<Configure>", lambda _e: nav_canvas.configure(scrollregion=nav_canvas.bbox("all")))
|
|
nav_canvas.bind("<Configure>", lambda e: nav_canvas.itemconfigure(nav_window, width=e.width))
|
|
|
|
def _nav_wheel(event):
|
|
delta = getattr(event, "delta", 0)
|
|
if delta:
|
|
units = -1 if delta > 0 else 1
|
|
else:
|
|
units = -1 if getattr(event, "num", 0) == 4 else 1
|
|
nav_canvas.yview_scroll(units * 3, "units")
|
|
return "break"
|
|
|
|
def _bind_nav_wheel(widget: tk.Widget) -> None:
|
|
widget.bind("<MouseWheel>", _nav_wheel, add="+")
|
|
widget.bind("<Button-4>", _nav_wheel, add="+")
|
|
widget.bind("<Button-5>", _nav_wheel, add="+")
|
|
for child in widget.winfo_children():
|
|
_bind_nav_wheel(child)
|
|
|
|
for group_title, names in [
|
|
("总览", ["数据看板"]),
|
|
("配置", ["运行设置", "筛选设置", "高价值预约", "养号设置", "调试参数", "授权", "日志配置", "关键词"]),
|
|
("数据", ["运行日志", "行为日志", "实时状态", "截图预览", "统计信息", "预约队列", "高价值房间", "OCR结果", "中奖记录", "映射表"]),
|
|
("连接", ["WiFi ADB"]),
|
|
]:
|
|
ttk.Label(nav_inner, text=group_title, background=theme.SURFACE, foreground=theme.MUTED, font=("Microsoft YaHei UI", 10, "bold")).pack(anchor="w", padx=16, pady=(14, 6))
|
|
for name in names:
|
|
btn = nav_button(nav_inner, name, lambda n=name: self.show_page(n), active=name == self.current_page)
|
|
btn.pack(fill="x", padx=12, pady=2)
|
|
self.nav_buttons[name] = btn
|
|
_bind_nav_wheel(sidebar)
|
|
|
|
self.content_scroll = ScrollFrame(main)
|
|
self.content_scroll.grid(row=0, column=1, sticky="nsew")
|
|
self.content = self.content_scroll.inner
|
|
|
|
bottom = rounded_surface(shell, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_LG)
|
|
bottom.pack(fill="x", padx=20, pady=(6, 16))
|
|
inner_bottom = tk.Frame(bottom, bg=theme.SURFACE)
|
|
inner_bottom.pack(fill="x", padx=14, pady=11)
|
|
button(inner_bottom, "开始运行", self.start_run, kind="primary", width=132).pack(side="left", padx=(0, 8))
|
|
button(inner_bottom, "暂停/继续", self.pause_resume_run, width=124).pack(side="left", padx=4)
|
|
button(inner_bottom, "停止", self.stop_run, width=96).pack(side="left", padx=4)
|
|
button(inner_bottom, "强制停止", self.force_stop_run, kind="danger", width=132).pack(side="left", padx=4)
|
|
button(inner_bottom, "发送返回键", self.send_back_key, width=146).pack(side="left", padx=8)
|
|
self.status_pill = status_pill(inner_bottom, "就绪")
|
|
self.status_pill.pack(side="right")
|
|
|
|
def _sync_nav(self) -> None:
|
|
for name, btn in self.nav_buttons.items():
|
|
active = name == self.current_page
|
|
if hasattr(btn, "set_enabled"):
|
|
btn.set_enabled((not self.license_locked) or name == "授权")
|
|
if hasattr(btn, "set_active"):
|
|
btn.set_active(active)
|
|
|
|
def _sync_status_pill(self) -> None:
|
|
if not hasattr(self, "status_pill"):
|
|
return
|
|
text = self.status_var.get() or "就绪"
|
|
color = theme.GREEN
|
|
bg = theme.GREEN_SOFT
|
|
if "失败" in text or "异常" in text:
|
|
color, bg = theme.RED, theme.RED_SOFT
|
|
elif "运行" in text or "扫描" in text:
|
|
color, bg = theme.TEAL, theme.TEAL_SOFT
|
|
elif "暂停" in text:
|
|
color, bg = theme.AMBER, theme.AMBER_SOFT
|
|
elif "未" in text:
|
|
color, bg = theme.AMBER, theme.AMBER_SOFT
|
|
self.status_pill.configure(text=text, fg=color, bg=bg)
|
|
|
|
def _actions(self) -> dict:
|
|
return {
|
|
"refresh": self.refresh_data,
|
|
"refresh_devices": self.refresh_devices,
|
|
"save_config": self.save_config,
|
|
"screenshot_ocr": self.screenshot_ocr,
|
|
"scan_wifi": self.scan_wifi,
|
|
"wifi_adb": self.open_wifi_adb,
|
|
"verify_license": self.verify_license,
|
|
"upload_logs": self.upload_logs,
|
|
"check_update": self.check_update,
|
|
"show_run_log": lambda: self.show_page("运行日志"),
|
|
"show_actions_log": lambda: self.show_page("行为日志"),
|
|
}
|
|
|
|
def _selected_device(self) -> str:
|
|
dev = self.device_var.get().strip()
|
|
return "" if dev in {"", "自动选择设备"} else dev
|
|
|
|
def _on_runner_line(self, line: str) -> None:
|
|
try:
|
|
self.status_var.set((line or "").strip()[:120] or "就绪")
|
|
except Exception:
|
|
pass
|
|
|
|
def _params(self):
|
|
try:
|
|
self.config_vars = build_config_vars(read_config())
|
|
except Exception:
|
|
pass
|
|
self.config_vars["device_var"] = tk.StringVar(value=self._selected_device())
|
|
self.config_vars["y_var"].set(int(self.y_var.get() or 0))
|
|
return build_run_params(self.config_vars)
|
|
|
|
def _ensure_text_widgets(self) -> None:
|
|
for key, lines in self.text_cache.items():
|
|
widget = self.text_widgets.get(key)
|
|
if widget is not None:
|
|
try:
|
|
if widget.winfo_exists():
|
|
continue
|
|
except Exception:
|
|
pass
|
|
text = tk.Text(self.root)
|
|
text.insert("1.0", "\n".join(str(x) for x in lines))
|
|
self.text_widgets[key] = text
|
|
|
|
def _capture_text_widgets(self) -> None:
|
|
for key, widget in list(self.text_widgets.items()):
|
|
try:
|
|
if not widget.winfo_exists():
|
|
continue
|
|
lines = [x.strip() for x in widget.get("1.0", "end").splitlines() if x.strip()]
|
|
self.text_cache[key] = lines
|
|
except Exception:
|
|
continue
|
|
|
|
def _apply_text_cache_to_data(self) -> None:
|
|
self.data.cfg["contains_want"] = self.text_cache.get("want_text", [])
|
|
self.data.cfg["contains_not_want"] = self.text_cache.get("not_want_text", [])
|
|
self.data.cfg["yanghao_comment_texts"] = self.text_cache.get("yg_comment_texts", [])
|
|
self.data.cfg["yanghao_like_keywords"] = self.text_cache.get("yg_like_keywords_text", [])
|
|
self.data.cfg["yanghao_fav_keywords"] = self.text_cache.get("yg_fav_keywords_text", [])
|
|
|
|
def save_config(self) -> None:
|
|
try:
|
|
self._capture_text_widgets()
|
|
self._ensure_text_widgets()
|
|
self.config_vars["y_var"].set(int(self.y_var.get() or 0))
|
|
save_ui_to_config(
|
|
vars_=self.config_vars,
|
|
texts_=self.text_widgets,
|
|
cfg=self.data.cfg,
|
|
read_config=read_config,
|
|
write_config=write_config,
|
|
on_line=self._on_runner_line,
|
|
)
|
|
self.data = load_dashboard_data()
|
|
self.status_var.set("配置已保存")
|
|
except Exception as exc:
|
|
self.status_var.set(f"保存失败:{exc}")
|
|
|
|
def refresh_devices(self, silent: bool = False) -> None:
|
|
try:
|
|
self.adb_exe = find_adb_executable()
|
|
devices = list_adb_devices(self.adb_exe)
|
|
values = devices or ["自动选择设备"]
|
|
self.device_combo["values"] = values
|
|
if self.device_var.get() not in values:
|
|
self.device_var.set(devices[0] if devices else "自动选择设备")
|
|
if not silent:
|
|
self.status_var.set(f"已刷新设备:{len(devices)} 台" if devices else "未发现设备")
|
|
except Exception as exc:
|
|
if not silent:
|
|
self.status_var.set(f"刷新设备失败:{exc}")
|
|
|
|
def open_wifi_adb(self) -> None:
|
|
if not self._ensure_license_ready():
|
|
return
|
|
self.show_page("WiFi ADB")
|
|
open_wifi_adb_dialog(
|
|
self.root,
|
|
adb_exe=self.adb_exe,
|
|
on_line=self._on_runner_line,
|
|
refresh_devices=self.refresh_devices,
|
|
)
|
|
|
|
def _open_scan_pick_dialog(self, devs) -> None:
|
|
open_scan_pick_dialog(
|
|
self.root,
|
|
devs,
|
|
device_var=self.device_var,
|
|
on_line=self._on_runner_line,
|
|
refresh_devices=self.refresh_devices,
|
|
)
|
|
|
|
def scan_wifi(self) -> None:
|
|
if not self._ensure_license_ready():
|
|
return
|
|
on_scan_one_click(
|
|
adb_exe=self.adb_exe,
|
|
scan_btn=self.scan_button,
|
|
scan_busy=self._scan_busy,
|
|
root=self.root,
|
|
on_line=self._on_runner_line,
|
|
device_var=self.device_var,
|
|
refresh_devices=self.refresh_devices,
|
|
open_pick_dialog=self._open_scan_pick_dialog,
|
|
)
|
|
|
|
def _try_reconnect_last_wifi(self) -> None:
|
|
if self.license_locked:
|
|
return
|
|
try_reconnect_last_wifi(
|
|
self.root,
|
|
adb_exe=self.adb_exe,
|
|
device_var=self.device_var,
|
|
on_line=self._on_runner_line,
|
|
refresh_devices=self.refresh_devices,
|
|
)
|
|
|
|
def screenshot_ocr(self) -> None:
|
|
if not self._ensure_license_ready():
|
|
return
|
|
do_screenshot(
|
|
device_id=self._selected_device(),
|
|
y_offset=int(self.y_var.get() or 0),
|
|
image_var=self.image_var,
|
|
on_line=self._on_runner_line,
|
|
root=self.root,
|
|
on_ocr_results=self._set_ocr_results,
|
|
on_refresh_preview=self.refresh_data,
|
|
)
|
|
|
|
def _set_ocr_results(self, results: list) -> None:
|
|
self.ocr_results = results
|
|
self.status_var.set(f"OCR结果已更新:{len(results)} 项")
|
|
|
|
def start_run(self) -> None:
|
|
if not self._ensure_license_ready():
|
|
return
|
|
if self.runner.is_running():
|
|
self.status_var.set("已在运行中")
|
|
return
|
|
progress = self._show_startup_progress()
|
|
try:
|
|
self._update_startup_progress(progress, 12, "保存配置", "正在写入当前界面参数")
|
|
self.save_config()
|
|
self._update_startup_progress(progress, 38, "解析设备", "正在确认 ADB 与目标设备")
|
|
params = self._params()
|
|
self._update_startup_progress(progress, 68, "启动主进程", "正在加载运行核心")
|
|
self.root.update_idletasks()
|
|
self.runner.start(params)
|
|
self._update_startup_progress(progress, 90, "等待运行日志", "主进程已启动,正在等待初始化输出")
|
|
self.status_var.set("运行中")
|
|
self.data = load_dashboard_data()
|
|
except Exception as exc:
|
|
self._close_startup_progress(progress)
|
|
self.status_var.set(f"启动失败:{exc}")
|
|
return
|
|
self.root.after(600, lambda: self._finish_startup_progress(progress))
|
|
|
|
def _show_startup_progress(self) -> tk.Toplevel:
|
|
win = tk.Toplevel(self.root)
|
|
win.title("启动运行")
|
|
win.transient(self.root)
|
|
win.resizable(False, False)
|
|
win.configure(bg=theme.BG)
|
|
win.protocol("WM_DELETE_WINDOW", lambda: None)
|
|
frame = tk.Frame(win, bg=theme.BG, padx=24, pady=22)
|
|
frame.pack(fill="both", expand=True)
|
|
card = rounded_surface(frame, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_MD)
|
|
card.pack(fill="both", expand=True)
|
|
inner = tk.Frame(card, bg=theme.SURFACE, padx=24, pady=21)
|
|
inner.pack(fill="both", expand=True)
|
|
tk.Label(inner, text="正在初始化运行", bg=theme.SURFACE, fg=theme.TEXT, font=("Microsoft YaHei UI", 18, "bold")).pack(anchor="w")
|
|
stage = tk.Label(inner, text="准备中", bg=theme.SURFACE, fg=theme.TEXT, font=("Microsoft YaHei UI", 14, "bold"))
|
|
stage.pack(anchor="w", pady=(17, 4))
|
|
detail = tk.Label(inner, text="正在准备启动环境", bg=theme.SURFACE, fg=theme.MUTED, font=theme.FONT_SMALL)
|
|
detail.pack(anchor="w", pady=(0, 13))
|
|
pct = tk.Label(inner, text="0%", bg=theme.SURFACE, fg=theme.TEAL_DARK, font=("Microsoft YaHei UI", 20, "bold"))
|
|
pct.pack(anchor="e")
|
|
bar = ttk.Progressbar(inner, mode="determinate", length=420, maximum=100)
|
|
bar.pack(fill="x", pady=(3, 4), ipady=3)
|
|
win._startup_labels = {"stage": stage, "detail": detail, "pct": pct, "bar": bar} # type: ignore[attr-defined]
|
|
self._update_startup_progress(win, 5, "准备中", "正在准备启动环境")
|
|
try:
|
|
win.update_idletasks()
|
|
x = self.root.winfo_rootx() + (self.root.winfo_width() - win.winfo_width()) // 2
|
|
y = self.root.winfo_rooty() + (self.root.winfo_height() - win.winfo_height()) // 2
|
|
win.geometry(f"+{max(0, x)}+{max(0, y)}")
|
|
win.grab_set()
|
|
except Exception:
|
|
pass
|
|
self._startup_progress = win
|
|
return win
|
|
|
|
def _update_startup_progress(self, progress: tk.Toplevel, percent: int, stage: str, detail: str) -> None:
|
|
labels = getattr(progress, "_startup_labels", None)
|
|
if not labels:
|
|
return
|
|
value = max(0, min(100, int(percent)))
|
|
labels["stage"].configure(text=stage)
|
|
labels["detail"].configure(text=detail)
|
|
labels["pct"].configure(text=f"{value}%")
|
|
labels["bar"].configure(value=value)
|
|
try:
|
|
progress.update_idletasks()
|
|
except Exception:
|
|
pass
|
|
|
|
def _close_startup_progress(self, progress: tk.Toplevel | None) -> None:
|
|
if progress is None:
|
|
return
|
|
try:
|
|
progress.grab_release()
|
|
except Exception:
|
|
pass
|
|
try:
|
|
progress.destroy()
|
|
except Exception:
|
|
pass
|
|
if self._startup_progress is progress:
|
|
self._startup_progress = None
|
|
|
|
def _finish_startup_progress(self, progress: tk.Toplevel | None) -> None:
|
|
if progress is None:
|
|
return
|
|
try:
|
|
self._update_startup_progress(progress, 100, "启动完成", "运行进程已开始")
|
|
except Exception:
|
|
pass
|
|
self._close_startup_progress(progress)
|
|
|
|
def stop_run(self) -> None:
|
|
self.runner.stop()
|
|
self.status_var.set("已停止")
|
|
|
|
def pause_resume_run(self) -> None:
|
|
if not self.runner.is_running():
|
|
self.status_var.set("未运行")
|
|
return
|
|
if self.runner.is_paused():
|
|
ok = self.runner.resume()
|
|
self.status_var.set("运行中" if ok else "继续失败")
|
|
else:
|
|
ok = self.runner.pause()
|
|
self.status_var.set("已暂停" if ok else "暂停失败")
|
|
|
|
def force_stop_run(self) -> None:
|
|
self.runner.force_stop()
|
|
self.status_var.set("已停止(强制)")
|
|
|
|
def send_back_key(self) -> None:
|
|
if not self._ensure_license_ready():
|
|
return
|
|
ok = self.runner.send_back_key(self._selected_device())
|
|
self.status_var.set("返回键已发送" if ok else "发送返回键失败")
|
|
|
|
def verify_license(self) -> None:
|
|
var = self.config_vars.get("license_key_var")
|
|
raw = str(var.get() if var is not None else "").strip()
|
|
if not raw:
|
|
self.status_var.set("请输入卡密")
|
|
messagebox.showwarning("卡密验证", "请输入卡密后再验证。", parent=self.root)
|
|
return
|
|
try:
|
|
cfg = read_config()
|
|
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()
|
|
timeout_seconds = int(section.get("timeout_seconds") or 10)
|
|
except Exception:
|
|
activation_url = get_default_activation_url()
|
|
timeout_seconds = 10
|
|
status = install_license_or_activate(
|
|
raw,
|
|
activation_url=activation_url,
|
|
timeout_seconds=max(1, timeout_seconds),
|
|
)
|
|
if status.ok:
|
|
if var is not None:
|
|
var.set("")
|
|
self.license_locked = False
|
|
self.status_var.set("卡密验证成功")
|
|
self._start_license_monitor_once()
|
|
messagebox.showinfo("卡密验证", "验证成功,授权已写入本机。", parent=self.root)
|
|
self.show_page("数据看板")
|
|
else:
|
|
self.status_var.set(f"卡密验证失败:{status.message}")
|
|
messagebox.showerror(
|
|
"卡密验证失败",
|
|
f"{status.message}\n\n原因:{status.reason}",
|
|
parent=self.root,
|
|
)
|
|
|
|
def upload_logs(self) -> None:
|
|
if not self._ensure_license_ready():
|
|
return
|
|
self.status_var.set("正在上传诊断日志")
|
|
self.root.update_idletasks()
|
|
result = upload_diagnostic_logs()
|
|
if result.ok:
|
|
self.status_var.set("诊断日志上传成功")
|
|
messagebox.showinfo(
|
|
"上传诊断日志",
|
|
f"上传成功。\n大小:{result.size // 1024} KB",
|
|
parent=self.root,
|
|
)
|
|
else:
|
|
self.status_var.set(f"日志上传失败:{result.message}")
|
|
messagebox.showerror(
|
|
"上传诊断日志失败",
|
|
f"{result.message}\n\n原因:{result.reason}",
|
|
parent=self.root,
|
|
)
|
|
|
|
def check_update(self) -> None:
|
|
if not self._ensure_license_ready():
|
|
return
|
|
self.status_var.set("正在检查更新")
|
|
progress = self._show_update_progress("正在连接更新服务器")
|
|
|
|
def worker() -> None:
|
|
try:
|
|
result = perform_forced_update(
|
|
settings=load_manual_update_settings(),
|
|
progress_callback=lambda event: self.root.after(
|
|
0,
|
|
lambda event=event: self._update_progress_window(progress, event),
|
|
),
|
|
)
|
|
self.root.after(0, lambda: self._finish_update_progress(progress, lambda: self._show_update_result(result)))
|
|
except UpdateError as exc:
|
|
self.root.after(0, lambda: self._finish_update_progress(progress, lambda: self._show_update_error(str(exc))))
|
|
except Exception as exc:
|
|
self.root.after(0, lambda: self._finish_update_progress(progress, lambda: self._show_update_error(f"检查更新异常:{exc}")))
|
|
|
|
threading.Thread(target=worker, daemon=True).start()
|
|
|
|
def _show_update_progress(self, message: str) -> tk.Toplevel:
|
|
win = tk.Toplevel(self.root)
|
|
win.title("检查更新")
|
|
win.transient(self.root)
|
|
win.resizable(False, False)
|
|
win.configure(bg=theme.BG)
|
|
win.protocol("WM_DELETE_WINDOW", lambda: None)
|
|
frame = tk.Frame(win, bg=theme.BG, padx=26, pady=24)
|
|
frame.pack(fill="both", expand=True)
|
|
|
|
card = rounded_surface(frame, fill=theme.SURFACE, outline=theme.BORDER, radius=theme.RADIUS_MD)
|
|
card.pack(fill="both", expand=True)
|
|
inner = tk.Frame(card, bg=theme.SURFACE, padx=24, pady=22)
|
|
inner.pack(fill="both", expand=True)
|
|
|
|
top = tk.Frame(inner, bg=theme.SURFACE)
|
|
top.pack(fill="x")
|
|
tk.Label(
|
|
top,
|
|
text="软件更新",
|
|
bg=theme.SURFACE,
|
|
fg=theme.TEXT,
|
|
font=("Microsoft YaHei UI", 18, "bold"),
|
|
).pack(side="left")
|
|
mode = tk.Label(
|
|
top,
|
|
text="检查中",
|
|
bg=theme.TEAL_SOFT,
|
|
fg=theme.TEAL_DARK,
|
|
font=("Microsoft YaHei UI", 12, "bold"),
|
|
padx=12,
|
|
pady=5,
|
|
)
|
|
mode.pack(side="right")
|
|
|
|
stage = tk.Label(
|
|
inner,
|
|
text=message,
|
|
bg=theme.SURFACE,
|
|
fg=theme.TEXT,
|
|
font=("Microsoft YaHei UI", 14, "bold"),
|
|
)
|
|
stage.pack(anchor="w", pady=(18, 4))
|
|
detail = tk.Label(
|
|
inner,
|
|
text="优先尝试差量更新,失败后才回退完整安装包",
|
|
bg=theme.SURFACE,
|
|
fg=theme.MUTED,
|
|
font=theme.FONT_SMALL,
|
|
)
|
|
detail.pack(anchor="w", pady=(0, 14))
|
|
pct = tk.Label(
|
|
inner,
|
|
text="0%",
|
|
bg=theme.SURFACE,
|
|
fg=theme.TEAL_DARK,
|
|
font=("Microsoft YaHei UI", 22, "bold"),
|
|
)
|
|
pct.pack(anchor="e")
|
|
bar = ttk.Progressbar(inner, mode="determinate", length=420, maximum=100)
|
|
bar.pack(fill="x", pady=(3, 10), ipady=3)
|
|
sub = tk.Label(
|
|
inner,
|
|
text="准备中",
|
|
bg=theme.SURFACE,
|
|
fg=theme.SUBTLE,
|
|
font=("Microsoft YaHei UI", 11),
|
|
)
|
|
sub.pack(anchor="w")
|
|
|
|
win._update_labels = { # type: ignore[attr-defined]
|
|
"mode": mode,
|
|
"stage": stage,
|
|
"detail": detail,
|
|
"pct": pct,
|
|
"bar": bar,
|
|
"sub": sub,
|
|
}
|
|
self._update_progress_window(win, {"stage": "manifest_fetch", "message": message})
|
|
|
|
try:
|
|
win.update_idletasks()
|
|
x = self.root.winfo_rootx() + (self.root.winfo_width() - win.winfo_width()) // 2
|
|
y = self.root.winfo_rooty() + (self.root.winfo_height() - win.winfo_height()) // 2
|
|
win.geometry(f"+{max(0, x)}+{max(0, y)}")
|
|
win.grab_set()
|
|
except Exception:
|
|
pass
|
|
return win
|
|
|
|
def _update_progress_window(self, progress: tk.Toplevel, event: dict) -> None:
|
|
labels = getattr(progress, "_update_labels", None)
|
|
if not labels:
|
|
return
|
|
apply_update_progress_event(
|
|
event,
|
|
mode_label=labels["mode"],
|
|
stage_label=labels["stage"],
|
|
detail_label=labels["detail"],
|
|
percent_label=labels["pct"],
|
|
bar=labels["bar"],
|
|
sub_label=labels["sub"],
|
|
)
|
|
|
|
def _finish_update_progress(self, progress: tk.Toplevel, callback) -> None:
|
|
try:
|
|
progress.grab_release()
|
|
except Exception:
|
|
pass
|
|
try:
|
|
progress.destroy()
|
|
except Exception:
|
|
pass
|
|
callback()
|
|
|
|
def _show_update_result(self, result) -> None:
|
|
self.status_var.set(result.message or "更新检查完成")
|
|
if result.exit_required:
|
|
messagebox.showinfo(
|
|
"检查更新",
|
|
f"{result.message}\n\n程序会交给更新器处理,请按提示完成更新。",
|
|
parent=self.root,
|
|
)
|
|
try:
|
|
self.root.after(300, self.root.destroy)
|
|
except Exception:
|
|
pass
|
|
return
|
|
messagebox.showinfo("检查更新", result.message or "已是最新版本。", parent=self.root)
|
|
|
|
def _show_update_error(self, message: str) -> None:
|
|
self.status_var.set(f"检查更新失败:{message}")
|
|
messagebox.showerror("检查更新失败", message, parent=self.root)
|
|
|
|
def refresh_data(self) -> None:
|
|
self.data = load_dashboard_data()
|
|
self.status_var.set("已刷新")
|
|
self.show_page(self.current_page)
|
|
|
|
def show_page(self, name: str) -> None:
|
|
target = name if name in PAGES else "数据看板"
|
|
if self.license_locked and target != "授权":
|
|
target = "授权"
|
|
self.status_var.set("请先验证卡密")
|
|
self.current_page = target
|
|
self._sync_nav()
|
|
self._capture_text_widgets()
|
|
self._apply_text_cache_to_data()
|
|
self._ensure_text_widgets()
|
|
clear(self.content)
|
|
builder = PAGES.get(target, PAGES["数据看板"])
|
|
builder(self.content, self.data, PageContext(self.config_vars, self.text_widgets, self._actions(), self.status_var, self.runner))
|
|
try:
|
|
self.content_scroll.canvas.yview_moveto(0)
|
|
except Exception:
|
|
pass
|
|
|
|
def run(self) -> None:
|
|
self.root.mainloop()
|
|
|
|
|
|
def main() -> None:
|
|
enforce_dashboard_license_required()
|
|
local_status = check_license()
|
|
if local_status.ok:
|
|
require_online_license_or_exit()
|
|
try:
|
|
print_version_banner("gui_dashboard_app")
|
|
except Exception:
|
|
pass
|
|
app = DashboardApp(
|
|
initial_page="数据看板" if local_status.ok else "授权",
|
|
license_locked=not local_status.ok,
|
|
)
|
|
if local_status.ok:
|
|
app._start_license_monitor_once()
|
|
app.run()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|