# -*- coding: utf-8 -*- """示例场景插件:证明「后端 run + 前端通用表单 + 实时日志/进度」的插件化链路可用。 后续真实场景(如考勤月报封存检查)按同样方式接入即可。 """ import time from webhost.backend.plugins.base import PLUGINS, RunContext, ScenePlugin class DemoPlugin(ScenePlugin): id = "demo" name = "示例检查" version = "1.0.0" description = "跑一个带进度与日志的假任务,用于验证插件链路。" fields = [ {"key": "steps", "label": "步数", "type": "int", "default": 3}, {"key": "mode", "label": "模式", "type": "select", "options": ["正常", "中途报错"], "default": "正常"}, ] def run(self, fields: dict, ctx: RunContext) -> dict: steps = max(1, min(50, int(fields.get("steps", 3)))) mode = fields.get("mode", "正常") ctx.log(f"开始示例检查:{steps} 步,模式={mode}") for i in range(1, steps + 1): time.sleep(0.6) ctx.progress(int(i / steps * 90)) ctx.log(f"第 {i}/{steps} 步完成") if mode == "中途报错": raise RuntimeError("按模式设置的报错,用于演示失败处理") ctx.progress(100) ctx.log("示例检查完成") return { "summary": f"共执行 {steps} 步,全部正常", "rows": [[i, f"结果 {i}"] for i in range(1, steps + 1)], } PLUGINS.append(DemoPlugin())