feat: 新增轻量会话校验能力,优化登录态失效处理流程

1. 新增TODO_COUNT_URL轻量接口用于快速校验会话有效性
2. 定义OALoginExpiredError统一标识登录态失效异常
3. 重构会话校验逻辑,使用轻量接口替代拉取待办页校验
4. 为重试装饰器添加不重试异常配置,登录失效直接上抛
5. 全流程增加登录态失效自动重登机制,最多重试2次
6. 新增通用钉钉机器人推送工具类
This commit is contained in:
2026-08-15 12:45:41 +08:00
parent c1c54e61fc
commit d468d15032
11 changed files with 895 additions and 410 deletions
@@ -217,7 +217,8 @@ def _get_cookies_with_domain(session: SessionPage) -> list:
def get_fd_id_by_listening(session: SessionPage, browser: Optional[ChromiumPage] = None) -> str:
# 优先复用外部传入的浏览器;仅在没有可用浏览器时才自行创建(如复用已保存会话的场景)
# 优先复用外部传入的浏览器;仅在没有可用浏览器时才自行创建(如复用已保存会话的场景)。
# 复用场景下 get_session 已用轻量接口 is_session_valid 校验过会话,无需在此重复校验
own_browser = browser is None
if own_browser:
cookies = _get_cookies_with_domain(session)
@@ -236,6 +237,10 @@ def get_fd_id_by_listening(session: SessionPage, browser: Optional[ChromiumPage]
browser.get(HOME_URL)
time.sleep(5)
# 兜底:轻量接口校验通过后,若新浏览器 cookies 提取不全仍可能停在登录页,快速失败避免空等
if "login.jsp" in browser.url or browser.ele('tag:input@name=j_username', timeout=2):
raise RuntimeError("登录态已失效,页面停留在登录页,请重新登录")
deadline = time.time() + 10
target_request = None
while time.time() < deadline:
@@ -620,73 +625,77 @@ def export_employee_change_list(
base_params = {**DEFAULT_FIXED_PARAMS, **filter_params}
browser, session = get_session()
# 登录态失效时自动删除 cookie、重新登录并重跑一遍(最多 2 轮)
for login_attempt in range(2):
browser, session = get_session()
try:
# 1. 动态获取 fdId(复用登录浏览器,避免重复创建导致浏览器被提前关闭)
fd_id = get_fd_id_by_listening(session, browser)
# 2. 从菜单获取 searchId
search_id = get_search_id_from_menu(session, fd_id, menu_name)
# 3. 生成一个固定的时间戳(毫秒)
timestamp = str(int(time.time() * 1000))
logger.info("使用时间戳: %s", timestamp)
# 4. 查询获取总条数
total = get_total_count(session, base_params, search_id, timestamp)
if total == 0:
logger.warning("查询结果为 0 条,请检查筛选条件或无数据")
return ""
# 5. 导出数据
logger.info("开始导出 %d 条记录...", total)
file_content = export_all_data(session, base_params, total, columns,
search_id, timestamp)
output_filename = f"{time.strftime('%Y%m')}_{menu_name}表_{time.strftime('%H%M%S')}.xls"
output_path = os.path.abspath(os.path.join(output_dir, output_filename))
with open(output_path, "wb") as f:
f.write(file_content)
logger.info("导出完成,文件保存为:%s", output_path)
# 6. 读取原始表并内存清洗(去『废弃』+按工号去重),不落中间表
headers, rows, emp_col = _read_and_clean(output_path)
# 7. 调用 HR 参保记录接口核对,追加『社保记录查询结果』列(一次性完成)
mode = "suspension" if menu_name == MENU_NAME_SUSPEND else "participation"
final_path = _write_annotated(
headers, rows, emp_col, mode,
os.path.splitext(os.path.basename(output_path))[0], output_dir)
# 8. 清理中间原始导出文件,只保留最终标注表
try:
os.remove(output_path)
logger.info("已清理中间原始导出文件:%s", output_path)
except Exception:
pass
# 1. 动态获取 fdId(复用登录浏览器,避免重复创建导致浏览器被提前关闭)
fd_id = get_fd_id_by_listening(session, browser)
if browser is not None:
save_browser_cookies(browser)
return final_path
# 2. 从菜单获取 searchId
search_id = get_search_id_from_menu(session, fd_id, menu_name)
except Exception as e:
logger.error("导出过程出错: %s", e, exc_info=True)
if "登录" in str(e) or "cookie" in str(e).lower():
# 3. 生成一个固定的时间戳(毫秒)
timestamp = str(int(time.time() * 1000))
logger.info("使用时间戳: %s", timestamp)
# 4. 查询获取总条数
total = get_total_count(session, base_params, search_id, timestamp)
if total == 0:
logger.warning("查询结果为 0 条,请检查筛选条件或无数据")
return ""
# 5. 导出数据
logger.info("开始导出 %d 条记录...", total)
file_content = export_all_data(session, base_params, total, columns,
search_id, timestamp)
output_filename = f"{time.strftime('%Y%m')}_{menu_name}表_{time.strftime('%H%M%S')}.xls"
output_path = os.path.abspath(os.path.join(output_dir, output_filename))
with open(output_path, "wb") as f:
f.write(file_content)
logger.info("导出完成,文件保存为:%s", output_path)
# 6. 读取原始表并内存清洗(去『废弃』+按工号去重),不落中间表
headers, rows, emp_col = _read_and_clean(output_path)
# 7. 调用 HR 参保记录接口核对,追加『社保记录查询结果』列(一次性完成)
mode = "suspension" if menu_name == MENU_NAME_SUSPEND else "participation"
final_path = _write_annotated(
headers, rows, emp_col, mode,
os.path.splitext(os.path.basename(output_path))[0], output_dir)
# 8. 清理中间原始导出文件,只保留最终标注表
try:
if os.path.exists(COOKIE_FILE):
os.remove(COOKIE_FILE)
logger.info("已删除失效的 cookie 文件")
except Exception:
pass
raise
finally:
if browser is not None:
try:
browser.quit()
os.remove(output_path)
logger.info("已清理中间原始导出文件:%s", output_path)
except Exception:
pass
if browser is not None:
save_browser_cookies(browser)
return final_path
except Exception as e:
logger.error("导出过程出错: %s", e, exc_info=True)
is_login_fail = "登录" in str(e) or "cookie" in str(e).lower()
if is_login_fail and login_attempt == 0:
try:
if os.path.exists(COOKIE_FILE):
os.remove(COOKIE_FILE)
logger.info("检测到登录态失效,已删除 cookie,自动重新登录后重跑")
except Exception:
pass
continue
raise
finally:
if browser is not None:
try:
browser.quit()
except Exception:
pass
def export_participation(
filter_params: Optional[Dict] = None,