Fix kdocs upload status restore
This commit is contained in:
@@ -647,68 +647,82 @@ class KDocsUploader:
|
|||||||
if not image_path or not os.path.exists(image_path):
|
if not image_path or not os.path.exists(image_path):
|
||||||
return
|
return
|
||||||
|
|
||||||
if not self._ensure_playwright():
|
|
||||||
self._notify_admin(unit, name, image_path, self._last_error or "浏览器不可用")
|
|
||||||
return
|
|
||||||
|
|
||||||
if not self._open_document(doc_url):
|
|
||||||
self._notify_admin(unit, name, image_path, self._last_error or "打开文档失败")
|
|
||||||
return
|
|
||||||
|
|
||||||
if not self._is_logged_in():
|
|
||||||
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
|
account = None
|
||||||
|
prev_status = None
|
||||||
|
status_tracked = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
account = safe_get_account(user_id, account_id)
|
if not self._ensure_playwright():
|
||||||
if account and not getattr(account, "is_running", False):
|
self._notify_admin(unit, name, image_path, self._last_error or "浏览器不可用")
|
||||||
prev_status = getattr(account, "status", None)
|
return
|
||||||
account.status = "上传截图"
|
|
||||||
self._emit_account_update(user_id, account)
|
|
||||||
except Exception:
|
|
||||||
prev_status = None
|
|
||||||
|
|
||||||
sheet_name = (cfg.get("kdocs_sheet_name") or "").strip()
|
if not self._open_document(doc_url):
|
||||||
sheet_index = int(cfg.get("kdocs_sheet_index") or 0)
|
self._notify_admin(unit, name, image_path, self._last_error or "打开文档失败")
|
||||||
unit_col = (cfg.get("kdocs_unit_column") or "A").strip().upper()
|
return
|
||||||
image_col = (cfg.get("kdocs_image_column") or "D").strip().upper()
|
|
||||||
|
if not self._is_logged_in():
|
||||||
|
self._login_required = True
|
||||||
|
self._last_login_ok = False
|
||||||
|
self._notify_admin(unit, name, image_path, "登录已失效,请管理员重新扫码登录")
|
||||||
|
try:
|
||||||
|
log_to_client("表格上传失败: 登录已失效,请管理员重新扫码登录", user_id, account_id)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return
|
||||||
|
self._login_required = False
|
||||||
|
self._last_login_ok = True
|
||||||
|
|
||||||
success = False
|
|
||||||
error_msg = ""
|
|
||||||
for attempt in range(2):
|
|
||||||
try:
|
try:
|
||||||
if sheet_name or sheet_index:
|
account = safe_get_account(user_id, account_id)
|
||||||
self._select_sheet(sheet_name, sheet_index)
|
if account and not getattr(account, "is_running", False):
|
||||||
row_num = self._find_person_with_unit(unit, name, unit_col)
|
prev_status = getattr(account, "status", None)
|
||||||
if row_num < 0:
|
account.status = "上传截图"
|
||||||
error_msg = f"未找到人员: {unit}-{name}"
|
self._emit_account_update(user_id, account)
|
||||||
break
|
status_tracked = True
|
||||||
success = self._upload_image_to_cell(row_num, image_path, image_col)
|
except Exception:
|
||||||
if success:
|
prev_status = None
|
||||||
break
|
|
||||||
except Exception as e:
|
|
||||||
error_msg = str(e)
|
|
||||||
|
|
||||||
if success:
|
sheet_name = (cfg.get("kdocs_sheet_name") or "").strip()
|
||||||
self._last_success_at = time.time()
|
sheet_index = int(cfg.get("kdocs_sheet_index") or 0)
|
||||||
self._last_error = None
|
unit_col = (cfg.get("kdocs_unit_column") or "A").strip().upper()
|
||||||
log_to_client(f"已上传表格截图: {unit}-{name}", user_id, account_id)
|
image_col = (cfg.get("kdocs_image_column") or "D").strip().upper()
|
||||||
self._restore_account_status(user_id, account, prev_status)
|
|
||||||
return
|
|
||||||
|
|
||||||
if not error_msg:
|
success = False
|
||||||
error_msg = "上传失败"
|
error_msg = ""
|
||||||
self._last_error = error_msg
|
for attempt in range(2):
|
||||||
self._notify_admin(unit, name, image_path, error_msg)
|
try:
|
||||||
log_to_client(f"表格上传失败: {error_msg}", user_id, account_id)
|
if sheet_name or sheet_index:
|
||||||
self._restore_account_status(user_id, account, prev_status)
|
self._select_sheet(sheet_name, sheet_index)
|
||||||
|
row_num = self._find_person_with_unit(unit, name, unit_col)
|
||||||
|
if row_num < 0:
|
||||||
|
error_msg = f"未找到人员: {unit}-{name}"
|
||||||
|
break
|
||||||
|
success = self._upload_image_to_cell(row_num, image_path, image_col)
|
||||||
|
if success:
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = str(e)
|
||||||
|
|
||||||
|
if success:
|
||||||
|
self._last_success_at = time.time()
|
||||||
|
self._last_error = None
|
||||||
|
try:
|
||||||
|
log_to_client(f"已上传表格截图: {unit}-{name}", user_id, account_id)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return
|
||||||
|
|
||||||
|
if not error_msg:
|
||||||
|
error_msg = "上传失败"
|
||||||
|
self._last_error = error_msg
|
||||||
|
self._notify_admin(unit, name, image_path, error_msg)
|
||||||
|
try:
|
||||||
|
log_to_client(f"表格上传失败: {error_msg}", user_id, account_id)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
if status_tracked:
|
||||||
|
self._restore_account_status(user_id, account, prev_status)
|
||||||
|
|
||||||
def _notify_admin(self, unit: str, name: str, image_path: str, error: str) -> None:
|
def _notify_admin(self, unit: str, name: str, image_path: str, error: str) -> None:
|
||||||
cfg = self._load_system_config()
|
cfg = self._load_system_config()
|
||||||
|
|||||||
Reference in New Issue
Block a user