Files
RuoYi-Vue3-FastAPI/ruoyi-fastapi-backend/plugins/core/manifest/menu_tree.py
T
insistence 2a055ba648 feat: 新增插件系统 (#112)
* feat: 初始化插件系统

* refactor: 收口插件系统运行时重构

* perf: 优化插件系统类型提示

* fix&perf: 修复和优化插件系统

* fix: 修复gitignore规则误忽略插件文件的问题

* fix: 修复运行时插件根路径算错的问题

* fix: 加强插件发现和路由注册的防护措施

* revert: 回滚定时任务白名单

* fix: 移除未使用的应用路由注册探测

* revert: 恢复部分代码

* perf: 优化插件系统

* docs: 新增插件开发文档

* perf: 优化插件管理模块

* perf: 提升插件系统核心能力

* refactor: 重构生命周期 step runner

* fix: 修复lint错误

* test: 清理测试用例

* test: 调整测试目录名称

* fix: 修复前后端目录硬编码的问题

* fix: 修复插件系统安全性缺口

* refactor: 重新设计插件生命周期 Migration 事务与回滚

* perf: 优化插件系统边界问题

* refactor: 重构当前插件系统的依赖体系设计

* perf: 优化代码

* perf: 优化代码

* fix: 修复代码合并问题

* fix: 修复bug

* perf: 优化代码

* perf&fix: 优化代码和修复bug

* docs: 优化文档格式

* feat: 适配Vue2版本

* docs: 更新README文档

* fix: 修复ruff lint错误

* chore: 更新后端依赖文件
2026-07-28 20:35:18 +08:00

102 lines
3.1 KiB
Python

from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from plugins.core.manifest.schema import PluginManifest, PluginMenuManifest
MIN_PLUGIN_COMPONENT_PARTS = 3
class PluginMenuTree:
"""
插件菜单树领域工具。
统一处理 manifest 菜单树的遍历、统计、权限和组件路径解析,避免校验、管理和
运行时负载构建各自维护递归逻辑。
"""
@classmethod
def flatten(cls, menus: list[PluginMenuManifest]) -> list[PluginMenuManifest]:
"""
展平插件菜单树。
:param menus: 插件菜单声明列表
:return: 展平后的插件菜单列表
"""
flattened_menus = []
for menu in menus:
flattened_menus.append(menu)
flattened_menus.extend(cls.flatten(menu.children))
return flattened_menus
@classmethod
def count(cls, menus: list[PluginMenuManifest]) -> int:
"""
统计插件菜单树节点数量。
:param menus: 插件菜单声明列表
:return: 菜单节点数量
"""
return len(cls.flatten(menus))
@classmethod
def collect_permissions(cls, menus: list[PluginMenuManifest]) -> set[str]:
"""
收集插件菜单树中的权限标识。
:param menus: 插件菜单声明列表
:return: 菜单权限标识集合
"""
return {menu.perms for menu in cls.flatten(menus) if menu.perms}
@classmethod
def collect_route_paths(
cls,
menus: list[PluginMenuManifest],
parent_path: str = '',
) -> list[str]:
"""
收集插件菜单树中的完整路由路径。
:param menus: 插件菜单声明列表
:param parent_path: 父级菜单路径
:return: 完整路由路径列表
"""
route_paths = []
for menu in menus:
current_path = f'{parent_path}/{menu.path}' if parent_path else menu.path
if menu.type != 'F':
route_paths.append(current_path)
route_paths.extend(cls.collect_route_paths(menu.children, current_path))
return route_paths
@staticmethod
def is_plugin_component(component: str) -> bool:
"""
判断菜单组件是否引用插件前端视图。
:param component: 菜单组件路径
:return: 是否为插件组件路径
"""
return component.startswith('plugin/')
@staticmethod
def resolve_plugin_view_path(manifest: PluginManifest, component: str) -> Path | None:
"""
将插件组件路径解析为前端插件内视图路径。
:param manifest: 插件清单
:param component: 菜单组件路径
:return: Vue 文件相对路径
"""
parts = component.split('/')
plugin_id = manifest.frontend.plugin_id or manifest.id
if len(parts) < MIN_PLUGIN_COMPONENT_PARTS or parts[0] != 'plugin' or parts[1] != plugin_id:
return None
return Path(manifest.frontend.views_path, *parts[2:]).with_suffix('.vue')