feat(hrbp-ehr): 新增HR薪资卡与花名册自动化模块

1.  新增hrbp.ehr业务模块目录结构与基础配置
2.  新增工资卡数据抓取、核对功能,支持在职/离职员工数据导出
3.  新增员工花名册自定义导出自动化流程
4.  添加pandas依赖到requirements.txt
5.  清理调试用的验证码图片文件
This commit is contained in:
2026-08-08 23:39:04 +08:00
parent de02d1953f
commit 93fe40f4ed
32 changed files with 2223 additions and 110 deletions
+5
View File
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
"""
@Module: hrbp.ehr.common
@Description: HR 系统公共模块(登录、接口调用、会话管理、配置)
"""
Binary file not shown.

Before

Width:  |  Height:  |  Size: 95 KiB

After

Width:  |  Height:  |  Size: 142 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 109 KiB

After

Width:  |  Height:  |  Size: 166 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 193 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 224 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 134 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 155 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 123 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 176 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 202 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 184 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 212 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 9.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

+23
View File
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
"""
@Module: hrbp.ehr.common
@FileName: config.py
@Author: 小布丁
@CreateTime: 2026-08-08 10:40
@Description: HR 系统配置,集中管理接口地址、分页大小等常量(供各业务模块复用)
@Version: 1.0.0
"""
# ---------- 银行卡(工资卡)信息查询接口 ----------
# 分页查询工资卡信息(aa.md 提供),返回 JSON
WAGE_CARD_URL = "https://hr.37du.co/RedseaPlatform/getPageAjax/HrStaffWageCard.mc"
# 单页最大抓取条数
PAGE_MAX_SIZE = 500
# ---------- 员工花名册(自定义导出)页面 ----------
# 花名册自定义导出页面(aa.md 提供),基于 DrissionPage 浏览器自动化操作
ROSTER_URL = "https://hr.37du.co/RedseaPlatform/PtPortal.mc?method=classic#ajax&1&nworktoday-40175-24311-77128-52242&nworktoday-40175-24311|/RedseaPlatform/hrRoster/1116da64-b094-11ea-b588-14187743d052/getCustomCommonList.mc?clickFlag=true&treePermitType=permit&useSealTreeData=0&menuName=%E5%91%98%E5%B7%A5%E8%8A%B1%E5%90%8D%E5%86%8C"
# 花名册默认导出的自定义模板名称
ROSTER_TEMPLATE_NAME = "员工基础信息"
+135 -3
View File
@@ -1,8 +1,140 @@
# -*- coding: utf-8 -*-
"""
@Module: hrbp.ehr.common
@FileName: hr_api.py
@Author: 小布丁
@CreateTime: 2026-08-05 23:52
@Description: HR 系统 API 接口,集中管理流程引擎、排单、考勤等接口地址
@Version: 1.2.0
"""
@Description: HR 系统 API 模块,负责调用系统接口(工资卡等),供各业务模块复用
@Version: 2.0.0
"""
import time
from typing import Any, Dict, List
from app.utils.logger import get_logger
from app.service.hrbp.ehr.common.config import WAGE_CARD_URL, PAGE_MAX_SIZE
logger = get_logger(__name__)
class HRSessionExpiredError(Exception):
"""HR 登录态过期:接口返回登录页等非 JSON 内容。"""
def _is_expired(text) -> bool:
"""接口正常情况下返回 JSON;返回登录页/错误页等非 JSON 即视为登录态过期。"""
return bool(text) and not text.lstrip().startswith(("{", "["))
# ---------- 银行卡(工资卡)信息查询接口 ----------
def fetch_wage_card_page(
session,
status: int,
page_no: int = 1,
page_size: int = PAGE_MAX_SIZE,
leave_start: str = None,
leave_end: str = None,
) -> Dict[str, Any]:
"""
请求单页工资卡数据。
Args:
session: 已登录的 requests.Session 对象
status: 人员状态,1=在职 / 2=离职
page_no: 页码(从 1 开始)
page_size: 每页条数
leave_start / leave_end: 离职筛选时间范围 "YYYY-MM-DD"(status=2 时必填)
Returns:
接口返回的 JSON 字典
Raises:
RuntimeError: 请求失败或响应异常
HRSessionExpiredError: 登录态过期(接口返回非 JSON)
"""
params = {
"start": (page_no - 1) * page_size,
"pageNo": page_no,
"pageSize": page_size,
"ftl_list_STRU_TREE_CODE": "cq37du",
"sortName": "hr_staff_wage_card.STAFF_NAME",
"sortType": "asc",
"commonQuery": "[]",
"person_status": status,
}
# 在职、离职参数区分
if status == 1:
params["leaveOffice_date"] = ""
elif leave_start and leave_end:
params["leaveOffice_date[0]"] = leave_start
params["leaveOffice_date[1]"] = leave_end
params["leaveOffice_date_start"] = leave_start
params["leaveOffice_date_end"] = leave_end
resp = session.post(WAGE_CARD_URL, data=params, timeout=20)
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 内容")
try:
return resp.json()
except Exception:
body = (resp.text or "")[:200]
raise RuntimeError(f"工资卡接口返回非 JSON 数据(状态码 {resp.status_code}): {body}")
def fetch_all_wage_card(
session,
status: int,
status_name: str,
leave_start: str = None,
leave_end: str = None,
) -> List[Dict[str, Any]]:
"""
分页抓取指定人员类型(在职/离职)的全部工资卡数据。
Args:
session: 已登录的 requests.Session 对象
status: 人员状态,1=在职 / 2=离职
status_name: 人员类型文字(用于日志与数据标记)
leave_start / leave_end: 离职筛选时间范围(status=2 时必填)
Returns:
数据行列表(每条已附加「人员类型」字段)
"""
all_rows: List[Dict[str, Any]] = []
page_no = 1
while True:
try:
data = fetch_wage_card_page(session, status, page_no, PAGE_MAX_SIZE, leave_start, leave_end)
except HRSessionExpiredError:
logger.warning("【%s】HR 登录态过期,重新登录后续抓", status_name)
from app.service.hrbp.ehr.common.session_manager import refresh_hr_session
session = refresh_hr_session()
data = fetch_wage_card_page(session, status, page_no, PAGE_MAX_SIZE, leave_start, leave_end)
if data.get("state") != "1":
logger.error("【%s】第%d页请求失败:%s", status_name, page_no, data.get("meg"))
break
page_list = data.get("datas", []) or []
if not page_list:
logger.info("【%s】无更多数据", status_name)
break
# 给每条数据增加人员类型标识
for item in page_list:
item["人员类型"] = status_name
all_rows.extend(page_list)
total = data.get("count", 0)
total_page = data.get("pageCount", 0)
logger.info("【%s】已抓取 %d/%d 页,累计 %d/%d 条",
status_name, page_no, total_page, len(all_rows), total)
if page_no >= total_page:
break
page_no += 1
time.sleep(0.2) # 轻微限速,避免触发风控
return all_rows
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
"""
@Module: hrbp.ehr.common
@FileName: session_manager.py
@Author: 小布丁
@CreateTime: 2026-08-08 10:45
@Description: 从 login.py 持久化的 HR cookies 构建 requests 会话;支持会话过期重新登录刷新(供各 HR 业务模块复用)
@Version: 1.0.0
"""
import os
import json
from app.utils.logger import get_logger
try:
import requests
except ImportError:
requests = None
logger = get_logger(__name__)
# HR 登录 cookies 持久化目录(login.py 登录成功后保存)
_SESSIONS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', '..', '.sessions')
HR_COOKIE_FILE = os.path.join(_SESSIONS_DIR, 'hr_cookies.json')
def build_hr_session() -> "requests.Session":
"""用本地缓存的 HR cookies 构造 requests 会话(仅取 hr.37du.co 域)。"""
if requests is None:
raise RuntimeError("缺少 requests 依赖,请先安装")
if not os.path.exists(HR_COOKIE_FILE):
logger.info("未找到 HR cookies 缓存,先执行 HR 登录")
refresh_hr_session()
if not os.path.exists(HR_COOKIE_FILE):
raise RuntimeError(f"HR 登录后仍未生成 cookies 文件:{HR_COOKIE_FILE}")
with open(HR_COOKIE_FILE, encoding='utf-8') as f:
cookies = json.load(f)
session = requests.Session()
session.headers.update({
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0.0.0 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded",
})
for c in cookies or []:
if c.get("domain") and "37du.co" in c["domain"]:
session.cookies.set(c["name"], c["value"], domain=c["domain"], path=c.get("path", "/"))
return session
def refresh_hr_session() -> "requests.Session":
"""HR 登录态过期后重新执行 HR 登录刷新 cookies,并重建会话。"""
from app.service.hrbp.ehr.common.login import login as hr_login
if not hr_login():
raise RuntimeError("HR 登录态过期后重新登录失败")
return build_hr_session()