Files
37duAutomation/app/service/hrbp/oa/common/oa_api/todo.py
T
puddy 5422dc8e3f refactor: 替换排班/打卡接口为考勤逐日接口,清理旧依赖
1. 重构考勤相关接口:将get_attendance_html、get_attendance_records替换为get_attendance_daily,使用新的ATTENDANCE_DAILY_URL接口替代旧接口
2. 更新请假校验逻辑:使用考勤逐日接口统计上班天数,替换原跨月排班HTML请求逻辑
3. 升级测试用例:同步更新test_03_attendance.py和test_06_leave_online.py适配新接口
4. 新增/完善文档:新增多份流程与需求文档,删除旧版冗余文档,整理docs目录结构
5. 清理废弃文件:删除原周考勤异常、门店请休假异常相关旧需求文档
6. 配置更新:更新config.py中的考勤相关URL常量,补充注释说明
7. 待办优化:更新aa.md内容,同步加班审批规则说明
2026-08-18 12:00:53 +08:00

173 lines
5.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
@Module: hrbp.oa.common.oa_api
@FileName: todo.py
@Description: OA 待办列表接口:分页查询待办单据、解析、去重。
@Version: 1.0.0
"""
import time
import re
from typing import List, Dict, Any
from app.utils.logger import get_logger
try:
from DrissionPage import SessionPage
except ImportError:
SessionPage = None
from app.service.hrbp.oa.common.config import BASE_URL, TODO_LIST_URL, DEFAULT_PAGE_SIZE, FD_TYPE
from app.service.hrbp.oa.common.oa_api.base import oa_request_json
logger = get_logger(__name__)
def fetch_todo_page(
session: SessionPage,
subject: str,
page_no: int = 1,
page_size: int = DEFAULT_PAGE_SIZE
) -> Dict[str, Any]:
"""
请求单页待办列表数据。
Args:
session: 已登录的 SessionPage 对象
subject: 待办标题关键字,如 "请假" / "加班"
page_no: 页码(从1开始)
page_size: 每页条数
Returns:
接口返回的 JSON 字典
Raises:
RuntimeError: 请求失败或状态码异常
"""
params = {
"method": "list",
"dataType": "todo",
"fdType": FD_TYPE, # 待办类型(请假/加班)
"fdCateId": "",
"q.fdSubject": subject, # 按标题关键字过滤(请假/加班)
"orderby": "fdCreateTime",
"ordertype": "down",
"page.pageNo": page_no,
"page.pageSize": page_size,
"__seq": int(time.time() * 1000), # 防缓存
}
logger.debug("请求第 %d 页待办[%s]", page_no, subject)
return oa_request_json(session, "GET", TODO_LIST_URL, params, "待办列表")
def parse_todo_list(data: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
解析接口返回的 JSON,提取每张单据的关键信息。
Args:
data: 接口返回的 JSON
Returns:
单据列表,每项包含:
- title: 标题(去除 HTML 标签)
- creator: 发起人
- time: 接收时间(字符串)
- detail_url: SSO 跳转链接(完整 URL)
"""
datas = data.get("datas", [])
if not datas:
return []
docs = []
for row in datas:
# 1. 标题
title_cell = next((c for c in row if c["col"] == "todo.subject4View"), {})
title_raw = title_cell.get("value", "")
# 移除 <span class="subjecthead"> 标签,仅保留内容
match = re.search(r'<span class="subjecthead"[^>]*>(.*?)</span>', title_raw, re.DOTALL)
title = match.group(1).strip() if match else re.sub(r'<[^>]+>', '', title_raw).strip()
# 2. 发起人
creator_cell = next((c for c in row if c["col"] == "docCreatorName"), {})
creator = creator_cell.get("value", "")
# 3. 接收时间
time_cell = next((c for c in row if c["col"] == "fdCreateTime"), {})
time_str = time_cell.get("value", "")
# 4. SSO 跳转链接(补全为完整 URL)
href_cell = next((c for c in row if c["col"] == "tr_href"), {})
detail_url = href_cell.get("value", "")
if detail_url and not detail_url.startswith(("http://", "https://")):
detail_url = BASE_URL + detail_url
# 跳过"工作沟通/协作"类待办(如"请回复…与您的工作沟通:加班申请"):
# 链接指向协作模块 kmCollaborateMain.do,非审批工作流,SSO 后仅到
# unifyWorkFlow.jsp?fkId=… 拿不到 businessKey,永远无法解析,直接跳过避免无谓重试
if "kmCollaborateMain.do" in detail_url:
logger.warning("跳过非审批待办(工作沟通/协作): %s", title)
continue
docs.append({
"title": title,
"creator": creator,
"time": time_str,
"detail_url": detail_url,
})
return docs
def get_documents(
session: SessionPage,
subject: str,
page_size: int = DEFAULT_PAGE_SIZE
) -> List[Dict[str, Any]]:
"""
主入口:获取所有待处理单据(自动分页)。
请假与加班流程逻辑一致,仅通过 subject 关键字区分。
Args:
session: 已登录的 SessionPage 对象(已设置 cookies)
subject: 待办标题关键字,如 "请假" / "加班"
page_size: 每页条数(默认15)
Returns:
所有单据的列表
"""
all_docs = []
# 请求第一页,获取总条数
data = fetch_todo_page(session, subject, page_no=1, page_size=page_size)
total = int(data.get("page", {}).get("totalSize", 0))
logger.info("总待办[%s]条数: %d", subject, total)
# 解析第一页
docs = parse_todo_list(data)
all_docs.extend(docs)
logger.info("第 1 页获取 %d 条", len(docs))
# 若总条数超过单页,继续翻页
if total > page_size:
total_pages = (total + page_size - 1) // page_size
for p in range(2, total_pages + 1):
data = fetch_todo_page(session, subject, page_no=p, page_size=page_size)
docs = parse_todo_list(data)
all_docs.extend(docs)
logger.info("第 %d 页获取 %d 条,累计 %d 条", p, len(docs), len(all_docs))
# 去重:OA 待办接口分页可能返回重复单据(同一条在多次/多页重复出现),
# 以详情链接 detail_url(含唯一 taskId)为键去重,避免同一单重复审批、重复推送
seen = set()
unique_docs = []
for doc in all_docs:
key = doc.get("detail_url") or doc.get("title")
if key in seen:
continue
seen.add(key)
unique_docs.append(doc)
if len(unique_docs) != len(all_docs):
logger.warning("去重前 %d 条,去重后 %d 条(已过滤 %d 条重复单据)",
len(all_docs), len(unique_docs), len(all_docs) - len(unique_docs))
return unique_docs