# -*- 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", "") # 移除 标签,仅保留内容 match = re.search(r']*>(.*?)', 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 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