Show upload status and log KDocs skips

This commit is contained in:
2026-01-07 14:28:58 +08:00
parent 703a62b6ad
commit 45cbdc51b4
3 changed files with 43 additions and 3 deletions

View File

@@ -862,6 +862,8 @@ def get_kdocs_status_api():
status["last_error"] = live_status.get("error")
else:
status["logged_in"] = True if status.get("last_login_ok") else False if status.get("last_login_ok") is False else None
if status.get("last_login_ok") is True and status.get("last_error") == "操作超时":
status["last_error"] = None
return jsonify(status)
except Exception as e:
return jsonify({"error": f"获取状态失败: {e}"}), 500

View File

@@ -14,7 +14,8 @@ import database
import email_service
from app_config import get_config
from services.client_log import log_to_client
from services.runtime import get_logger
from services.runtime import get_logger, get_socketio
from services.state import safe_get_account
try:
from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeoutError
@@ -657,10 +658,22 @@ class KDocsUploader:
self._login_required = True
self._last_login_ok = False
self._notify_admin(unit, name, image_path, "登录已失效,请管理员重新扫码登录")
log_to_client("表格上传失败: 登录已失效,请管理员重新扫码登录", user_id, account_id)
return
self._login_required = False
self._last_login_ok = True
prev_status = None
account = None
try:
account = safe_get_account(user_id, account_id)
if account and not getattr(account, "is_running", False):
prev_status = getattr(account, "status", None)
account.status = "上传截图"
self._emit_account_update(user_id, account)
except Exception:
prev_status = None
sheet_name = (cfg.get("kdocs_sheet_name") or "").strip()
sheet_index = int(cfg.get("kdocs_sheet_index") or 0)
unit_col = (cfg.get("kdocs_unit_column") or "A").strip().upper()
@@ -686,6 +699,7 @@ class KDocsUploader:
self._last_success_at = time.time()
self._last_error = None
log_to_client(f"已上传表格截图: {unit}-{name}", user_id, account_id)
self._restore_account_status(user_id, account, prev_status)
return
if not error_msg:
@@ -693,6 +707,7 @@ class KDocsUploader:
self._last_error = error_msg
self._notify_admin(unit, name, image_path, error_msg)
log_to_client(f"表格上传失败: {error_msg}", user_id, account_id)
self._restore_account_status(user_id, account, prev_status)
def _notify_admin(self, unit: str, name: str, image_path: str, error: str) -> None:
cfg = self._load_system_config()
@@ -719,6 +734,23 @@ class KDocsUploader:
except Exception as e:
logger.warning(f"[KDocs] 发送管理员邮件失败: {e}")
def _emit_account_update(self, user_id: int, account: Any) -> None:
try:
socketio = get_socketio()
socketio.emit("account_update", account.to_dict(), room=f"user_{user_id}")
except Exception:
pass
def _restore_account_status(self, user_id: int, account: Any, prev_status: Optional[str]) -> None:
if not account or not user_id:
return
if getattr(account, "is_running", False):
return
if getattr(account, "status", "") != "上传截图":
return
account.status = prev_status or "未开始"
self._emit_account_update(user_id, account)
def _select_sheet(self, sheet_name: str, sheet_index: int) -> None:
if sheet_name:
candidates = [

View File

@@ -400,14 +400,20 @@ def take_screenshot_for_account(
name = (account.remark or "").strip()
if unit and name:
from services.kdocs_uploader import get_kdocs_uploader
get_kdocs_uploader().enqueue_upload(
ok = get_kdocs_uploader().enqueue_upload(
user_id=user_id,
account_id=account_id,
unit=unit,
name=name,
image_path=screenshot_path,
)
if not ok:
log_to_client("表格上传排队失败: 队列已满", user_id, account_id)
else:
if not unit:
log_to_client("表格上传跳过: 未配置县区", user_id, account_id)
if not name:
log_to_client("表格上传跳过: 账号备注为空", user_id, account_id)
except Exception as kdocs_error:
logger.warning(f"表格上传任务提交失败: {kdocs_error}")