Files
37duAutomation/plugins/xq0003/data/hr_browser.py
T

125 lines
4.4 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 -*-
"""XQ0003 EHR系统请假流程审核 插件 - 浏览器审批层。
用途:表单数据全部走接口,仅在「通过」需要真实点击审批按钮时才启动浏览器。
登录态来自底座注入的 requests.Session(hr 平台),插件不自行读 cookies 文件、不自动补登。
"""
import time
from ..config import logger, APPROVE_BUTTONS
def _cookies_from_session(session):
"""把注入 session 的 cookies 转成 DrissionPage 可用的 cookie 字典列表。"""
cookies = []
if session is None:
return cookies
try:
jar = session.cookies
except Exception:
return cookies
for c in jar:
domain = str(c.domain or "")
if domain and "37du.co" not in domain:
continue
cookies.append({
"name": c.name,
"value": c.value,
"domain": domain or ".37du.co",
"path": c.path or "/",
})
return cookies
def build_browser(session):
"""创建 ChromiumPage 并注入 HR 登录态;cookies 为空时抛错(提示先登录 HR)。"""
from DrissionPage import ChromiumPage, ChromiumOptions
cookies = _cookies_from_session(session)
if not cookies:
raise RuntimeError("未获取到 HR 登录 cookies,请先在壳子(工作台)完成 HR 登录后再执行")
page = ChromiumPage(ChromiumOptions())
page.set.cookies(cookies)
logger.info("浏览器已启动并注入 HR cookies:%d 个", len(cookies))
return page
def _find_button(page, selector, timeout=3.0):
"""在详情页查找审批按钮:优先主文档,找不到则遍历 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()
def _visible(el):
try:
return el.states.is_displayed
except Exception:
return False
# 普通流程确认框 #custom-confirm-modal-ok 为主;选人弹窗 .wf-select-people 为特殊单据才触发
confirm = None
for _ in range(20):
c1 = page.ele("#custom-confirm-modal-ok", timeout=0.2)
if c1 is not None and _visible(c1):
confirm = c1
break
people = page.ele(".wf-select-people", timeout=0.2)
if people is not None and _visible(people):
try:
c2 = people.ele("css:.modal-footer button.btn.btn-primary", timeout=0.2)
except Exception:
c2 = None
if c2 is not None and _visible(c2):
confirm = c2
break
time.sleep(0.2)
if confirm is None:
msg = "未找到确认框确定按钮(#custom-confirm-modal-ok 或 .modal-footer .btn.btn-primary),审批未提交"
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}"}