Files
37duAutomation/app/service/hrbp/oa/common/work_communication.py
T
puddy 3862bbfab2 feat: 新增OA工作沟通推送模块,优化考勤异常查询逻辑与钉钉推送能力
1. 新增OA工作沟通推送全流程模块:
   - 实现人员搜索、多附件上传、工作沟通提交完整链路
   - 提供一键推送入口send_work_communication,支持多接收人、富文本与多类型沟通
   - 补充配套配置常量与接口封装
   - 添加抓包调试脚本与完整需求/开发文档

2. 考勤异常处理模块重构:
   - 将默认查询范围从「当月」改为「上周(周一~周日)」
   - 统一app/影刀版的日期范围计算逻辑
   - 新增班段合成、请假日期修复逻辑,优化导出报表字段
   - 更新需求文档与开发文档,同步功能变更

3. 钉钉推送工具优化:
   - 修复窗口激活逻辑,避免批量推送时抢焦点导致窗口失焦
   - 新增安全点击与鼠标状态兜底,防止左键卡死
   - 优化搜索与会话打开流程,提升稳定性
2026-08-25 17:08:44 +08:00

136 lines
5.1 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.work_communication
@FileName: work_communication.py
@Author: 小布丁
@CreateTime: 2026-08-25
@Description: OA 工作沟通推送模块(app 版公共模块)。
对外提供 send_work_communication 一键推送入口:
搜索接收人 -> 上传附件 -> 提交沟通,三个步骤全部成功才算推送成功;
任一接收人解析失败或任一附件上传失败即整体失败,不发送部分数据。
底层接口见 oa_api/work_communication.py(search_person / upload_attachment / submit_work_communication),
可单独调用。需求文档见 aa.md。
@Version: 1.0.0
使用示例:
from app.service.hrbp.oa.common.session_manager import load_saved_session
from app.service.hrbp.oa.common.work_communication import send_work_communication
session = load_saved_session() # 复用「37duOA登录」写入的共享 cookies
doc_id = send_work_communication(
session=session,
receiver_keywords=["37036801", "夏一平"], # 工号或姓名,单个或列表
subject="【通知】...",
content="<p>正文支持 HTML</p>",
attachment_paths=[r"D:\\资料\\附件1.xlsx"], # 可选
creator_id="1342173e36cf416088266b17d7d084a4", # 当前操作人 ID(必填)
category="工作安排", # 沟通类型(可选):工作交流/信息传递/工作安排/项目沟通/温馨提示/其他
)
"""
from typing import List, Optional, Union
from app.utils.logger import get_logger
try:
from DrissionPage import SessionPage
except ImportError:
SessionPage = None
from app.service.hrbp.oa.common.oa_api.work_communication import (
search_person,
upload_attachment,
submit_work_communication,
)
logger = get_logger(__name__)
def _to_list(value: Union[str, List[str], None]) -> List[str]:
"""把单个字符串或列表统一成列表(空值返回空列表)。"""
if not value:
return []
return [value] if isinstance(value, str) else list(value)
def send_work_communication(
session: SessionPage,
receiver_keywords: Union[str, List[str]],
subject: str,
content: str,
attachment_paths: Union[str, List[str]] = None,
creator_id: str = None,
category: str = None,
) -> str:
"""
发送工作沟通(一键推送入口)。
完整流程:解析接收人(工号/姓名 -> ID)-> 上传附件 -> 提交沟通。
- 接收人支持多个,用工号或姓名(姓名同名时建议用工号保证准确);
- 附件支持多个,任一上传失败即整体失败(不发送部分数据);
- 所有步骤成功才返回沟通文档 ID(fdId)。
Args:
session: 已登录的 SessionPage 对象(复用共享 cookies)
receiver_keywords: 接收人工号或姓名,单个字符串或列表
subject: 沟通标题
content: 沟通内容(支持 HTML 富文本)
attachment_paths: 附件文件路径,单个字符串或列表,可空
creator_id: 创建人(当前操作人)用户 ID,必填
category: 沟通类型,可传中文名(如「工作安排」「信息传递」)或类型 ID,
默认「工作安排」(config.FD_CATEGORY_DEFAULT)
Returns:
沟通文档 ID(fdId,字符串)
Raises:
ValueError: 缺少必填参数
RuntimeError: 接收人解析失败 / 附件上传失败 / 提交失败(含具体原因)
"""
if not subject or not subject.strip():
raise ValueError("subject(标题) 不能为空")
if not content or not content.strip():
raise ValueError("content(内容) 不能为空")
if not creator_id:
raise ValueError("creator_id(创建人ID) 必填,请传入当前操作人的用户 ID")
# ---- 1. 解析接收人 ----
receiver_ids, receiver_names = [], []
unresolved = []
for kw in _to_list(receiver_keywords):
kw = str(kw).strip()
if not kw:
continue
result = search_person(session, kw)
if result:
pid, pname = result
receiver_ids.append(pid)
receiver_names.append(pname)
logger.info("接收人[%s] -> %s (ID: %s)", kw, pname, pid)
else:
unresolved.append(kw)
if not receiver_ids:
raise RuntimeError("未解析到任何接收人,请检查传入的工号/姓名")
if unresolved:
raise RuntimeError(f"以下接收人解析失败,已中止发送: {', '.join(unresolved)}")
# ---- 2. 上传附件(任一失败即整体失败) ----
attachment_ids = []
for path in _to_list(attachment_paths):
attid = upload_attachment(session, path)
attachment_ids.append(attid)
logger.info("附件[%s] -> attid: %s", path, attid)
# ---- 3. 提交沟通 ----
return submit_work_communication(
session=session,
receiver_ids=receiver_ids,
receiver_names=receiver_names,
subject=subject,
content=content,
attachment_ids=attachment_ids or None,
creator_id=creator_id,
category_id=category,
)