refactor: 统一所有缓存/会话文件的存储路径

1.  重构会话管理模块,新增app.common.app_paths统一管理缓存路径,所有登录态与缓存统一迁移至%LOCALAPPDATA%\37duAutomation\.sessions
2.  更新所有涉及文件存储路径的代码,包括考勤、工资卡、社保、审批等模块
3.  新增银行字典缓存功能,统一管理银行编码映射
4.  添加雇主险业务模块,实现花名册筛选复制功能
5.  更新.gitignore,将所有.sessions目录纳入忽略列表
6.  删除影刀脚本旧的本地缓存文件
7.  优化加班审批校验逻辑,新增提前到岗型加班校验规则
8.  完善工资卡核对功能,补充开卡银行信息展示
This commit is contained in:
2026-08-13 21:20:55 +08:00
parent c749d951a0
commit 186636a8e4
45 changed files with 4507 additions and 393 deletions
+114
View File
@@ -8,9 +8,12 @@
@Version: 2.0.0
"""
import json
import os
import time
from typing import Any, Dict, List
from app.common.app_paths import get_sessions_dir
from app.utils.logger import get_logger
from app.service.hrbp.ehr.common.config import (
WAGE_CARD_URL,
@@ -21,7 +24,11 @@ from app.service.hrbp.ehr.common.config import (
SHIFT_URL,
LEAVE_URL,
OVERTIME_URL,
BANK_DICT_URL,
BANK_DICT_CODE_TYPE,
BANK_DICT_EXPIRE_SEC,
)
from app.service.hrbp.ehr.common.session_manager import build_hr_session
logger = get_logger(__name__)
@@ -149,6 +156,113 @@ def fetch_all_wage_card(
return all_rows
# ---------- 银行字典接口(aa.md 提供,按 BANK_TYPE 匹配 code -> 开卡银行) ----------
# 缓存文件统一放在 AppData\Local\37duAutomation\.sessions 目录
_BANK_DICT_CACHE_DIR = get_sessions_dir()
BANK_DICT_LIST_FILE = os.path.join(_BANK_DICT_CACHE_DIR, "bank_dict_list.json")
BANK_DICT_MAP_FILE = os.path.join(_BANK_DICT_CACHE_DIR, "bank_dict_map.json")
def _fetch_bank_dict(session=None):
"""远程调用接口获取银行字典,并写入本地缓存。
Returns:
(list[dict], dict[code->name])
"""
if session is None:
session = build_hr_session()
payload = {
"codeType": BANK_DICT_CODE_TYPE,
"inUse": "0,1,2",
"platformMode": "redseaPlatform",
}
resp = session.post(BANK_DICT_URL, data=payload, timeout=30)
if not resp:
raise RuntimeError("银行字典请求失败,未获取到响应")
if resp.status_code != 200:
raise RuntimeError(f"银行字典接口状态码异常: {resp.status_code}")
if _is_expired(resp.text):
raise HRSessionExpiredError("HR 登录态已过期,银行字典接口返回非 JSON 内容")
resp_json = resp.json()
raw_list = resp_json.get(BANK_DICT_CODE_TYPE) or []
result_list = [{"code": str(item.get("code", "")), "name": item.get("name", "")}
for item in raw_list]
result_map = {item["code"]: item["name"] for item in result_list}
os.makedirs(_BANK_DICT_CACHE_DIR, exist_ok=True)
with open(BANK_DICT_LIST_FILE, "w", encoding="utf-8") as f:
json.dump(result_list, f, ensure_ascii=False, indent=2)
with open(BANK_DICT_MAP_FILE, "w", encoding="utf-8") as f:
json.dump(result_map, f, ensure_ascii=False, indent=2)
logger.info("银行字典远程拉取完成,共 %d 条", len(result_list))
return result_list, result_map
def _load_bank_dict_cache():
"""读取本地银行字典缓存,失败返回 (None, None)。"""
try:
with open(BANK_DICT_LIST_FILE, "r", encoding="utf-8") as f:
lst = json.load(f)
with open(BANK_DICT_MAP_FILE, "r", encoding="utf-8") as f:
mp = json.load(f)
return lst, mp
except Exception as e:
logger.warning("银行字典本地缓存读取失败:%s", e)
return None, None
def get_bank_dict(session=None, force_refresh: bool = False):
"""获取银行字典(优先本地缓存,过期/强制刷新才请求接口)。
Args:
session: 可选的已登录 requests.Session;未提供时自动构建
force_refresh: True 强制重新调用接口
Returns:
(list版本, dict[code->name]版本)
"""
need_refresh = force_refresh
if not (os.path.exists(BANK_DICT_LIST_FILE) and os.path.exists(BANK_DICT_MAP_FILE)):
need_refresh = True
else:
mtime = os.path.getmtime(BANK_DICT_LIST_FILE)
if time.time() - mtime > BANK_DICT_EXPIRE_SEC:
need_refresh = True
if need_refresh:
try:
return _fetch_bank_dict(session)
except Exception as e:
logger.error("银行字典远程拉取失败:%s", e)
lst, mp = _load_bank_dict_cache()
if lst is not None:
logger.warning("降级使用本地银行字典缓存,共 %d 条", len(lst))
return lst, mp
return [], {}
lst, mp = _load_bank_dict_cache()
if lst is None:
# 缓存异常时重新拉取
return _fetch_bank_dict(session)
logger.info("读取本地银行字典缓存,共 %d 条", len(lst))
return lst, mp
def bank_type_to_name(bank_type, bank_map) -> str:
"""按银行编码(BANK_TYPE)匹配字典 code,返回开卡银行;未命中返回空串。"""
if bank_type is None:
return ""
if isinstance(bank_type, float):
if bank_type != bank_type: # NaN
return ""
bank_type = str(int(bank_type))
elif isinstance(bank_type, int):
bank_type = str(bank_type)
else:
bank_type = str(bank_type).strip()
return bank_map.get(bank_type, "")
def adjust_abnormal(
session,
abnormal_ids,