refactor: 重构审批流程与依赖,新增社保异动模块
1. 新增社保增减名单模块,支持参保/停保名单导出 2. 重构钉钉推送逻辑,先推摘要再按需推送完整报告 3. 优化假期/加班校验的天数比对逻辑,适配半天请假场景 4. 重写排班HTML解析器,兼容新的页面结构并提取班次全称 5. 重构审批报告生成逻辑,新增全量明细展示 6. 重构影刀脚本的日志与依赖管理,新增UI自动化聚焦输入框 7. 更新依赖包,新增uiautomation库支持
This commit is contained in:
+124
-18
@@ -10,7 +10,7 @@
|
||||
@Version: 1.0.0
|
||||
|
||||
依赖安装:
|
||||
pip install pyautogui pywin32 pyperclip
|
||||
pip install pyautogui pywin32 pyperclip uiautomation
|
||||
|
||||
使用前提:
|
||||
1. Windows 系统
|
||||
@@ -59,6 +59,11 @@ try:
|
||||
except ImportError:
|
||||
pyperclip = None
|
||||
|
||||
try:
|
||||
import uiautomation as ua
|
||||
except ImportError:
|
||||
ua = None
|
||||
|
||||
|
||||
# ==================== 默认参数 ====================
|
||||
DEFAULT_ACTIVATE_METHOD = "auto" # auto | hotkey | win32
|
||||
@@ -147,29 +152,50 @@ def _is_dingtalk_foreground():
|
||||
|
||||
|
||||
def _activate(method=DEFAULT_ACTIVATE_METHOD):
|
||||
"""按指定方式激活钉钉窗口。auto = 先 Ctrl+D,失败回退 win32。
|
||||
"""激活钉钉窗口:先找窗口,托盘中则 Ctrl+D 唤起,win32 置前,点击确保焦点。
|
||||
|
||||
若钉钉已在前台则直接返回——Ctrl+D 是 toggle 热键,窗口已显示时再按会把它隐藏,
|
||||
若钉钉已在前台则直接返回--Ctrl+D 是 toggle 热键,窗口已显示时再按会把它隐藏,
|
||||
导致后续发送失败。连续发送时第二次起会命中此分支。
|
||||
"""
|
||||
if _is_dingtalk_foreground():
|
||||
logger.info("钉钉窗口已在前台,跳过激活")
|
||||
return
|
||||
|
||||
if method == "hotkey":
|
||||
_activate_by_hotkey()
|
||||
elif method == "win32":
|
||||
_activate_by_win32()
|
||||
elif method == "auto":
|
||||
# 先尝试找到已打开的钉钉窗口
|
||||
hwnd = None
|
||||
try:
|
||||
hwnd = _find_dingtalk_hwnd()
|
||||
except RuntimeError:
|
||||
pass
|
||||
|
||||
if hwnd is None:
|
||||
# 钉钉在系统托盘,用 Ctrl+D 全局热键唤起
|
||||
pyautogui.hotkey("ctrl", "d")
|
||||
time.sleep(1.5)
|
||||
try:
|
||||
_activate_by_hotkey()
|
||||
logger.info("通过 Ctrl+D 激活钉钉窗口")
|
||||
except Exception as e:
|
||||
logger.warning("Ctrl+D 激活失败(%s),回退到 win32 查窗口激活", e)
|
||||
_activate_by_win32()
|
||||
logger.info("通过 win32 激活钉钉窗口")
|
||||
hwnd = _find_dingtalk_hwnd()
|
||||
except RuntimeError:
|
||||
raise RuntimeError("Ctrl+D 未能唤起钉钉窗口,请确认钉钉已启动并配置了 Ctrl+D 热键")
|
||||
|
||||
# win32 置前台
|
||||
if win32gui.IsIconic(hwnd):
|
||||
win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
|
||||
time.sleep(0.3)
|
||||
pyautogui.press("alt")
|
||||
win32gui.SetForegroundWindow(hwnd)
|
||||
time.sleep(DEFAULT_WAIT_AFTER_ACTIVATE)
|
||||
|
||||
# 点击窗口中心确保键盘焦点落在钉钉上(SetForegroundWindow 只切前台,不保证键盘焦点)
|
||||
rect = win32gui.GetWindowRect(hwnd)
|
||||
cx = (rect[0] + rect[2]) // 2
|
||||
cy = (rect[1] + rect[3]) // 2
|
||||
pyautogui.click(cx, cy)
|
||||
time.sleep(0.5)
|
||||
|
||||
if _is_dingtalk_foreground():
|
||||
logger.info("钉钉窗口已激活")
|
||||
else:
|
||||
raise ValueError(f"不支持的 activate_method: {method!r},可选: auto/hotkey/win32")
|
||||
raise RuntimeError("无法激活钉钉窗口,请确认钉钉桌面端已启动并登录")
|
||||
|
||||
|
||||
# ==================== 剪贴板 ====================
|
||||
@@ -205,11 +231,90 @@ def _set_file_to_clipboard(file_path):
|
||||
|
||||
|
||||
# ==================== 搜索 & 发送 ====================
|
||||
def _find_editable_controls(control, result, depth=0, max_depth=15):
|
||||
"""递归遍历控件树,收集所有可编辑控件(Edit/Document)。"""
|
||||
if depth > max_depth or len(result) > 20:
|
||||
return
|
||||
try:
|
||||
ct = control.ControlType
|
||||
if ct in (ua.ControlType.EditControl, ua.ControlType.DocumentControl):
|
||||
result.append(control)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
for child in control.GetChildren():
|
||||
_find_editable_controls(child, result, depth + 1, max_depth)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _rect_area(rect):
|
||||
"""计算矩形面积,无效矩形返回 0。"""
|
||||
try:
|
||||
return max(0, rect.right - rect.left) * max(0, rect.bottom - rect.top)
|
||||
except Exception:
|
||||
return 0
|
||||
|
||||
|
||||
def _focus_message_input():
|
||||
"""聚焦消息输入框(分辨率无关)。
|
||||
|
||||
优先使用 UI Automation 遍历钉钉控件树,定位底部区域面积最大的可编辑控件
|
||||
(即消息输入框)并点击;若 uiautomation 不可用或未找到控件,回退到
|
||||
Escape 键关闭搜索面板,依赖钉钉将焦点自动移至消息输入框。
|
||||
"""
|
||||
if ua is not None:
|
||||
try:
|
||||
hwnd = _find_dingtalk_hwnd()
|
||||
window = ua.ControlFromHandle(hwnd)
|
||||
if window is not None:
|
||||
edits = []
|
||||
_find_editable_controls(window, edits)
|
||||
valid = [e for e in edits if e.BoundingRectangle and _rect_area(e.BoundingRectangle) > 0]
|
||||
if valid:
|
||||
wleft, wtop, wright, wbottom = win32gui.GetWindowRect(hwnd)
|
||||
wh = wbottom - wtop
|
||||
# 优先选择窗口底部 40% 区域的可编辑控件
|
||||
bottom_edits = [
|
||||
e for e in valid
|
||||
if e.BoundingRectangle.bottom > wtop + wh * 0.6
|
||||
]
|
||||
candidates = bottom_edits if bottom_edits else valid
|
||||
# 取面积最大的(消息输入框通常是最大的可编辑区域)
|
||||
target = max(candidates, key=lambda e: _rect_area(e.BoundingRectangle))
|
||||
rect = target.BoundingRectangle
|
||||
cx = (rect.left + rect.right) // 2
|
||||
cy = (rect.top + rect.bottom) // 2
|
||||
pyautogui.click(cx, cy)
|
||||
time.sleep(0.3)
|
||||
logger.debug("UI Automation 定位到消息输入框: (%d, %d)", cx, cy)
|
||||
return
|
||||
except Exception as e:
|
||||
logger.debug("UI Automation 聚焦失败: %s", e)
|
||||
|
||||
# 回退:钉钉可能失焦,重新确认前台并点击窗口底部消息输入框区域
|
||||
if not _is_dingtalk_foreground():
|
||||
_activate()
|
||||
try:
|
||||
hwnd = _find_dingtalk_hwnd()
|
||||
rect = win32gui.GetWindowRect(hwnd)
|
||||
cx = (rect[0] + rect[2]) // 2
|
||||
cy = rect[1] + int((rect[3] - rect[1]) * 0.85) # 窗口底部 15%,消息输入框区域
|
||||
pyautogui.click(cx, cy)
|
||||
time.sleep(0.3)
|
||||
logger.debug("回退方案:点击窗口底部聚焦消息输入框: (%d, %d)", cx, cy)
|
||||
except Exception:
|
||||
pyautogui.press("escape")
|
||||
time.sleep(0.3)
|
||||
logger.debug("最终回退:通过 Escape 键尝试聚焦")
|
||||
|
||||
|
||||
def _search_and_open(target, wait_search=DEFAULT_WAIT_SEARCH):
|
||||
"""Ctrl+Shift+F 打开搜索 → 粘贴目标名 → 回车进入会话。
|
||||
"""Ctrl+Shift+F 打开搜索 → 粘贴目标名 → 回车进入会话 → 点击输入框获取焦点。
|
||||
|
||||
时序很关键:搜索结果加载、会话跳转都需要时间,等待不足会把消息内容
|
||||
误贴到搜索框。这里分段等待,确保回车后会话已加载、焦点已落到消息输入框。
|
||||
误贴到搜索框。这里分段等待,确保回车后会话已加载;最后点击消息输入框
|
||||
确保焦点落到输入框,避免粘贴时内容进入搜索框或其他控件。
|
||||
"""
|
||||
pyautogui.hotkey("ctrl", "shift", "f")
|
||||
time.sleep(DEFAULT_WAIT_SEARCH_BOX) # 搜索框打开
|
||||
@@ -218,7 +323,8 @@ def _search_and_open(target, wait_search=DEFAULT_WAIT_SEARCH):
|
||||
time.sleep(DEFAULT_WAIT_SEARCH_RESULT) # 搜索结果加载
|
||||
pyautogui.press("enter")
|
||||
time.sleep(DEFAULT_WAIT_CONVERSATION_LOAD) # 会话开始加载
|
||||
time.sleep(wait_search) # 焦点稳定到消息输入框
|
||||
time.sleep(wait_search) # 会话加载完成
|
||||
_focus_message_input() # 聚焦消息输入框
|
||||
logger.info("已搜索并进入会话: %s", target)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user