Files
37duAutomation/webhost/backend/plugins/base.py
T
puddy 53913f0c7d refactor(workflows): 统一替换 yingdao 目录引用为 workflows
1.  将所有文档、代码中对 `yingdao/` 目录的引用替换为 `workflows/`,统一影刀版代码的命名空间
2.  调整 workflows/share 模块内的路径引用与文档注释,确保一致性
3.  同步更新测试代码与配置文件中的旧目录引用
2026-09-08 16:16:35 +08:00

70 lines
2.6 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 -*-
"""场景插件抽象:后端 `run` 承载真实逻辑(浏览器自动化/登录态等),
前端按 `fields` 渲染通用表单,执行过程经 `RunContext.log/progress` 推送到 WebSocket。
"""
from __future__ import annotations
from typing import List
class RunContext:
"""传给插件 `run` 的执行上下文:把日志/进度实时推给前端,并提供共享登录态访问。"""
def __init__(self, hub, run_id: str, plugin_id: str, auth=None):
self._hub = hub
self._auth = auth # webhost.backend.auth 模块(宿主注入的共享登录态)
self.run_id = run_id
self.plugin_id = plugin_id
def log(self, msg: str, level: str = "INFO"):
self._hub.publish({
"t": "log", "run_id": self.run_id, "plugin_id": self.plugin_id,
"level": level, "msg": msg,
})
def progress(self, pct: int):
self._hub.publish({
"t": "progress", "run_id": self.run_id,
"plugin_id": self.plugin_id, "pct": int(max(0, min(100, pct))),
})
# ---------- 共享登录态 ----------
def get_session(self, platform: str) -> "requests.Session":
"""获取指定平台的共享登录 session,可直接传给 shared 数据模块。"""
if not self._auth:
raise RuntimeError("宿主未注入登录态服务,无法构建 session")
return self._auth.build_session(platform)
def get_user_id(self, platform: str) -> str:
"""获取指定平台的用户 ID(HR 数据接口大多需要 user_id 参数)。"""
if not self._auth:
raise RuntimeError("宿主未注入登录态服务,无法获取 user_id")
return self._auth.get_user_id(platform)
class ScenePlugin:
"""场景插件基类。新增场景:子类填 id/name/fields/run 并注册到 plugins 列表。"""
id: str = ""
name: str = ""
version: str = "1.0.0"
description: str = ""
platform: str = "" # 所需平台登录态:hr / oa / ""(无需登录)
uses_month: bool = False # 是否需要底座页眉的“执行月份”(前端据此注入 ref_month 字段)
# 表单字段定义:[{key,label,type,default,options}]
# type ∈ int | text | select
fields: List[dict] = []
def run(self, fields: dict, ctx: RunContext) -> dict:
raise NotImplementedError(f"插件 {self.id} 未实现 run")
class _Registry(list):
def find(self, pid: str) -> ScenePlugin:
for p in self:
if p.id == pid:
return p
raise KeyError(pid)
PLUGINS: _Registry = _Registry()