refactor: 统一所有缓存/会话文件的存储路径
1. 重构会话管理模块,新增app.common.app_paths统一管理缓存路径,所有登录态与缓存统一迁移至%LOCALAPPDATA%\37duAutomation\.sessions 2. 更新所有涉及文件存储路径的代码,包括考勤、工资卡、社保、审批等模块 3. 新增银行字典缓存功能,统一管理银行编码映射 4. 添加雇主险业务模块,实现花名册筛选复制功能 5. 更新.gitignore,将所有.sessions目录纳入忽略列表 6. 删除影刀脚本旧的本地缓存文件 7. 优化加班审批校验逻辑,新增提前到岗型加班校验规则 8. 完善工资卡核对功能,补充开卡银行信息展示
This commit is contained in:
@@ -65,8 +65,8 @@ except ImportError:
|
||||
|
||||
|
||||
# ==================== 路径 & 常量配置 ====================
|
||||
# 数据目录固定放在 AppData,不依赖脚本位置,每台电脑都有
|
||||
_APP_DATA_DIR = os.path.join(os.getenv('LOCALAPPDATA') or os.getenv('APPDATA') or os.path.expanduser('~'), 'OvertimeApproval')
|
||||
# 数据目录固定放在 AppData\Local\37duAutomation,不依赖脚本位置,每台电脑都有(影刀版独立子目录)
|
||||
_APP_DATA_DIR = os.path.join(os.getenv('LOCALAPPDATA') or os.getenv('APPDATA') or os.path.expanduser('~'), '37duAutomation', 'OvertimeApproval')
|
||||
_SESSIONS_DIR = os.path.join(_APP_DATA_DIR, '.sessions')
|
||||
COOKIE_FILE = os.path.join(_SESSIONS_DIR, 'oa_cookies.json')
|
||||
|
||||
@@ -99,6 +99,12 @@ ZHINENG_HALF_MORNING_HOURS = 3.5
|
||||
ZHINENG_HALF_AFTERNOON_HOURS = 4.5
|
||||
# 职能排班上午/下午分界:申请加班开始时间早于该时刻(12点)视为上午半天
|
||||
ZHINENG_HALF_CUTOFF_HOUR = 12
|
||||
# 提前到岗型延时加班:加班开始时间早于当天正常上班时间(09:00)视为提前到岗型,
|
||||
# 常规"晚于规定下班时间18:00""当日有下班打卡"校验不适用,改以到岗打卡为区间合规证据。
|
||||
# 提前到岗型员工只会提前到、不会晚到:打卡须不晚于加班开始时间(晚到则无法证明开始时刻已到岗)。
|
||||
# 到岗打卡证据窗口:加班开始时间前1小时内(含开始时刻)的打卡视为到岗
|
||||
OT_PRE_SHIFT_ARRIVE_BEFORE_HOURS = 1
|
||||
OT_PRE_SHIFT_ARRIVE_AFTER_MINUTES = 0
|
||||
|
||||
# ---------- 分页 ----------
|
||||
DEFAULT_PAGE_SIZE = 15
|
||||
@@ -1028,6 +1034,73 @@ def push_report(report, subject=""):
|
||||
return False
|
||||
|
||||
|
||||
# ==================== 真实审批提交(默认关闭) ====================
|
||||
# 审批按钮:通过->同意(#chooseSubmit),驳回->退回申请人(#backApplySubmit)
|
||||
_APPROVE_BUTTONS = {
|
||||
"通过": ("#chooseSubmit", "同意"),
|
||||
"驳回": ("#backApplySubmit", "退回申请人"),
|
||||
}
|
||||
|
||||
|
||||
def _find_button(page, selector, timeout=3.0):
|
||||
"""在详情页查找审批按钮:优先主文档,找不到则遍历 iframe(部分详情页按钮在 iframe 内)。"""
|
||||
try:
|
||||
btn = page.ele(selector, timeout=timeout)
|
||||
if btn:
|
||||
return btn
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
frames = page.get_frames()
|
||||
except Exception:
|
||||
frames = []
|
||||
for frame in frames:
|
||||
try:
|
||||
btn = frame.ele(selector, timeout=1)
|
||||
if btn:
|
||||
return btn
|
||||
except Exception:
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def submit_approval(page, decision):
|
||||
"""
|
||||
按校验结论在流程详情页执行真实审批提交。
|
||||
|
||||
Args:
|
||||
page: 停留在详情页的 ChromiumPage
|
||||
decision: 校验决策(通过/驳回;其余决策不提交)
|
||||
|
||||
Returns:
|
||||
{"action": "同意"/"退回申请人"/"-", "ok": "true"/"false", "message": ...}
|
||||
"""
|
||||
if decision not in _APPROVE_BUTTONS:
|
||||
return {"action": "-", "ok": "true", "message": "无需提交"}
|
||||
selector, label = _APPROVE_BUTTONS[decision]
|
||||
try:
|
||||
btn = _find_button(page, selector)
|
||||
if btn is None:
|
||||
msg = f"未找到审批按钮 {selector}({label}),可能页面未加载完成或按钮在未识别的 iframe 中"
|
||||
logger.error(msg)
|
||||
return {"action": label, "ok": "false", "message": msg}
|
||||
logger.info("点击审批按钮: %s(%s)", selector, label)
|
||||
btn.click()
|
||||
# 点击弹出的确认框"确定"按钮(同意/驳回共用同一确认框,仅标题不同)
|
||||
confirm = _find_button(page, "#custom-confirm-modal-ok", timeout=3)
|
||||
if confirm is None:
|
||||
msg = "未找到确认框确定按钮 #custom-confirm-modal-ok,审批未提交"
|
||||
logger.error(msg)
|
||||
return {"action": label, "ok": "false", "message": msg}
|
||||
confirm.click()
|
||||
logger.info("已点击确认框确定按钮")
|
||||
time.sleep(1.0)
|
||||
return {"action": label, "ok": "true", "message": f"已执行审批提交[{label}]"}
|
||||
except Exception as e:
|
||||
logger.error("审批提交失败[%s]: %s", label, str(e))
|
||||
return {"action": label, "ok": "false", "message": f"审批提交[{label}]失败: {e}"}
|
||||
|
||||
|
||||
def run_flow(subject, validate_doc, generate_report):
|
||||
logger.info("%s审批流程开始", subject)
|
||||
|
||||
@@ -1041,8 +1114,6 @@ def run_flow(subject, validate_doc, generate_report):
|
||||
try:
|
||||
doc_detail = _fetch_doc_detail(browser, session, doc)
|
||||
result = validate_doc(doc_detail, session)
|
||||
results.append(result)
|
||||
logger.info("已校验: %s - %s -> %s", result["creator"], result["title"], result["decision"])
|
||||
# 弹出结果提示:通过=绿勾,其余(驳回/人工复核/失败)=红叉,停留便于演示
|
||||
try:
|
||||
show_rpa_result_popup(
|
||||
@@ -1052,6 +1123,14 @@ def run_flow(subject, validate_doc, generate_report):
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
# 真实审批提交:仅当 overtime_approve_enabled=true 时执行(默认关闭)
|
||||
if os.getenv("overtime_approve_enabled", "false").lower() == "true":
|
||||
submit = submit_approval(browser, result.get("decision"))
|
||||
result["审批动作"] = submit["action"]
|
||||
result["审批结果"] = f"{'成功' if submit['ok'] == 'true' else '失败'},{submit['message']}"
|
||||
logger.info("审批提交: %s %s", submit["action"], result["审批结果"])
|
||||
results.append(result)
|
||||
logger.info("已校验: %s - %s -> %s", result["creator"], result["title"], result["decision"])
|
||||
except Exception as e:
|
||||
logger.error("处理单据失败: %s, 错误: %s", doc.get("title"), str(e))
|
||||
results.append({
|
||||
@@ -1109,6 +1188,16 @@ def _next_day(date_str):
|
||||
return (d + timedelta(days=1)).strftime("%Y-%m-%d")
|
||||
|
||||
|
||||
def _is_within_arrival_window(punch_str, start_dt,
|
||||
before_hours=OT_PRE_SHIFT_ARRIVE_BEFORE_HOURS,
|
||||
after_minutes=OT_PRE_SHIFT_ARRIVE_AFTER_MINUTES):
|
||||
"""提前到岗型加班:判断打卡时间是否落在加班开始时间附近的到岗证据窗口内。"""
|
||||
p = _parse_dt(punch_str)
|
||||
if p is None:
|
||||
return False
|
||||
return (start_dt - timedelta(hours=before_hours)) <= p <= (start_dt + timedelta(minutes=after_minutes))
|
||||
|
||||
|
||||
def _is_spring_festival(date_str):
|
||||
try:
|
||||
import chinese_calendar as cc
|
||||
@@ -1163,29 +1252,33 @@ def _check_attendance(session, staff_id, ot_date):
|
||||
def _check_punch_records(session, staff_id, ot_date):
|
||||
if not ot_date or not staff_id:
|
||||
return {"当日最早打卡": None, "当日最晚打卡": None, "次日最早打卡": None,
|
||||
"打卡备注": "缺少加班日期或员工ID"}
|
||||
"前一天最晚打卡": None, "打卡备注": "缺少加班日期或员工ID"}
|
||||
|
||||
start = datetime.strptime(ot_date, "%Y-%m-%d")
|
||||
prev_day = (start - timedelta(days=1)).strftime("%Y-%m-%d")
|
||||
next_day = (start + timedelta(days=1)).strftime("%Y-%m-%d")
|
||||
|
||||
try:
|
||||
records = get_attendance_records(session, staff_id, ot_date, next_day)
|
||||
# 查询范围扩到前一天:提前到岗型加班(开始<=6点)的到岗打卡可能被系统归到前一天
|
||||
records = get_attendance_records(session, staff_id, prev_day, next_day)
|
||||
summary = summarize_attendance_records(records)
|
||||
prev_summary = next((r for r in summary if r["attend_date"] == prev_day), None)
|
||||
day_summary = next((r for r in summary if r["attend_date"] == ot_date), None)
|
||||
next_summary = next((r for r in summary if r["attend_date"] == next_day), None)
|
||||
if not day_summary and not next_summary:
|
||||
if not day_summary and not next_summary and not prev_summary:
|
||||
return {"当日最早打卡": None, "当日最晚打卡": None, "次日最早打卡": None,
|
||||
"打卡备注": "当天及次日无打卡记录"}
|
||||
"前一天最晚打卡": None, "打卡备注": "当天及次日无打卡记录"}
|
||||
return {
|
||||
"当日最早打卡": day_summary["first_check_in"] if day_summary else None,
|
||||
"当日最晚打卡": day_summary["last_check_out"] if day_summary else None,
|
||||
"次日最早打卡": next_summary["first_check_in"] if next_summary else None,
|
||||
"前一天最晚打卡": prev_summary["last_check_out"] if prev_summary else None,
|
||||
"打卡备注": "",
|
||||
}
|
||||
except Exception as e:
|
||||
logger.warning("获取打卡明细失败: %s", str(e))
|
||||
return {"当日最早打卡": None, "当日最晚打卡": None, "次日最早打卡": None,
|
||||
"打卡备注": f"获取失败: {e}"}
|
||||
"前一天最晚打卡": None, "打卡备注": f"获取失败: {e}"}
|
||||
|
||||
|
||||
def _check_leave_covers_morning(session, staff_id, date_str):
|
||||
@@ -1234,14 +1327,34 @@ def _validate_delayed(info, punch, session, staff_id):
|
||||
reasons.append(f"加班时长为{hours:g}小时,不足{OT_MIN_HOURS}小时")
|
||||
|
||||
# 3. 区间合规校验
|
||||
work_start = datetime.strptime(f"{ot_date} {WORK_START_TIME}", "%Y-%m-%d %H:%M")
|
||||
work_end = datetime.strptime(f"{ot_date} {WORK_END_TIME}", "%Y-%m-%d %H:%M")
|
||||
if start is not None and start < work_end:
|
||||
reasons.append(f"申请开始时间({start:%H:%M})早于规定下班时间({WORK_END_TIME})")
|
||||
if end is not None:
|
||||
if actual_checkout is None:
|
||||
reasons.append("当日无下班打卡记录,无法校验区间合规")
|
||||
elif end > actual_checkout:
|
||||
reasons.append(f"申请结束时间({end:%H:%M})晚于实际下班打卡时间({actual_checkout:%H:%M:%S})")
|
||||
# 提前到岗型延时加班:加班开始时间早于当天正常上班时间(09:00)。
|
||||
# 常规"晚于18:00""当日有下班打卡"校验不适用,改以加班开始时间附近的到岗打卡作为证据:
|
||||
# - 开始<=6点时,到岗打卡可能被系统归到前一天(跨天打卡),需回看前一天记录;
|
||||
# - 6点~9点开始的,到岗打卡归属当天,只看当天记录。
|
||||
is_pre_shift = start is not None and start < work_start
|
||||
if is_pre_shift:
|
||||
candidates = [punch.get("当日最早打卡")]
|
||||
cross_cutoff = start.replace(hour=CROSS_DAY_BEFORE_HOUR, minute=0, second=0, microsecond=0)
|
||||
if start <= cross_cutoff:
|
||||
candidates.append(punch.get("前一天最晚打卡"))
|
||||
arrival_punch = next(
|
||||
(c for c in candidates if _is_within_arrival_window(c, start)),
|
||||
None,
|
||||
)
|
||||
if arrival_punch is None:
|
||||
reasons.append(f"提前到岗型加班,未找到加班开始时间({start:%H:%M})附近的到岗打卡")
|
||||
else:
|
||||
info["到岗打卡"] = arrival_punch
|
||||
else:
|
||||
if start is not None and start < work_end:
|
||||
reasons.append(f"申请开始时间({start:%H:%M})早于规定下班时间({WORK_END_TIME})")
|
||||
if end is not None:
|
||||
if actual_checkout is None:
|
||||
reasons.append("当日无下班打卡记录,无法校验区间合规")
|
||||
elif end > actual_checkout:
|
||||
reasons.append(f"申请结束时间({end:%H:%M})晚于实际下班打卡时间({actual_checkout:%H:%M:%S})")
|
||||
|
||||
info["计算加班时长(小时)"] = hours
|
||||
if reasons:
|
||||
@@ -1503,6 +1616,8 @@ def _format_detail(info):
|
||||
("当日最早打卡", "最早打卡"),
|
||||
("当日最晚打卡", "最晚打卡"),
|
||||
("次日最早打卡", "次日最早打卡"),
|
||||
("前一天最晚打卡", "前一天最晚打卡"),
|
||||
("到岗打卡", "到岗打卡"),
|
||||
("计算加班时长(小时)", "计算时长"),
|
||||
("弹性扣减(小时)", "弹性扣减"),
|
||||
("有效加班时长(小时)", "有效时长"),
|
||||
@@ -1560,6 +1675,8 @@ def generate_report(results):
|
||||
lines.append(f" {detail}")
|
||||
if note:
|
||||
lines.append(f" 备注: {note}")
|
||||
if r.get("审批动作"):
|
||||
lines.append(f" 审批动作: {r['审批动作']}({r.get('审批结果', '')})")
|
||||
lines.append("")
|
||||
|
||||
# ----- 驳回清单 -----
|
||||
|
||||
Reference in New Issue
Block a user