feat: unify login UI and improve kdocs defaults

This commit is contained in:
2026-02-07 01:27:00 +08:00
parent bf29ac1924
commit 9991834ccd
22 changed files with 358 additions and 114 deletions

View File

@@ -3,6 +3,7 @@
from __future__ import annotations
import os
import re
import shutil
import subprocess
import time
@@ -13,7 +14,6 @@ import email_service
from api_browser import APIBrowser, get_cookie_jar_path, is_cookie_jar_fresh
from app_config import get_config
from app_logger import get_logger
from app_security import sanitize_filename
from browser_pool_worker import get_browser_worker_pool
from services.client_log import log_to_client
from services.runtime import get_socketio
@@ -264,12 +264,46 @@ def _build_screenshot_targets(browse_type: str) -> tuple[str, str, str]:
return index_url, target_url, run_script
def _normalize_screenshot_name(value: str, *, fallback: str = "账号") -> str:
"""规范化截图文件名中的账号名,尽量保留中文。"""
label = str(value or "").strip() or fallback
label = label.replace("/", "_").replace("\\", "_")
label = re.sub(r"\s+", "", label)
label = re.sub(r"[^0-9A-Za-z_\-.一-鿿]", "_", label)
label = re.sub(r"_+", "_", label).strip("._-")
return label or fallback
def _next_screenshot_sequence(username_prefix: str, account_label: str, date_str: str) -> int:
"""按“用户+账号+日期”计算下一个截图序号。"""
pattern = re.compile(
rf"^{re.escape(username_prefix)}_{re.escape(account_label)}_{date_str}_(\d{{3}})\.jpg$",
re.IGNORECASE,
)
max_seq = 0
try:
with os.scandir(SCREENSHOTS_DIR) as entries:
for entry in entries:
if not entry.is_file():
continue
matched = pattern.match(entry.name)
if not matched:
continue
max_seq = max(max_seq, int(matched.group(1)))
except FileNotFoundError:
pass
return max_seq + 1
def _build_screenshot_output_path(username_prefix: str, account, browse_type: str) -> tuple[str, str]:
"""构建截图输出文件名与路径。"""
timestamp = get_beijing_now().strftime("%Y%m%d_%H%M%S")
login_account = account.remark if account.remark else account.username
raw_filename = f"{username_prefix}_{login_account}_{browse_type}_{timestamp}.jpg"
screenshot_filename = sanitize_filename(raw_filename)
"""构建截图输出文件名与路径(账号备注 + 日期 + 序号)"""
del browse_type
date_str = get_beijing_now().strftime("%Y%m%d")
account_label = _normalize_screenshot_name(account.remark or account.username, fallback=account.username)
seq = _next_screenshot_sequence(username_prefix, account_label, date_str)
screenshot_filename = f"{username_prefix}_{account_label}_{date_str}_{seq:03d}.jpg"
return screenshot_filename, os.path.join(SCREENSHOTS_DIR, screenshot_filename)
@@ -379,11 +413,13 @@ def _enqueue_kdocs_upload_if_needed(user_id: int, account_id: str, account, scre
if int(user_cfg.get("kdocs_auto_upload", 0) or 0) != 1:
return
unit = (user_cfg.get("kdocs_unit") or cfg.get("kdocs_default_unit") or "").strip()
user_unit = (user_cfg.get("kdocs_unit") or "").strip()
default_unit = (cfg.get("kdocs_default_unit") or "").strip() or "道县"
unit = user_unit or default_unit
if not user_unit:
log_to_client(f"未配置县区,使用默认县区: {unit}", user_id, account_id)
name = (account.remark or "").strip()
if not unit:
log_to_client("表格上传跳过: 未配置县区", user_id, account_id)
return
if not name:
log_to_client("表格上传跳过: 账号备注为空", user_id, account_id)
return