/// <summary> /// 添加action节点 /// </summary> /// <param name="actioNode">action节点</param> /// <returns></returns> public MenuBuilder <T> AddAction(MenuNode <T> actioNode) { actioNode.Controller = actioNode.Controller.ToLowerIfNoNull(); actioNode.ActionName = actioNode.ActionName.ToLowerIfNoNull(); this.LastPage.AddChildNode(actioNode); return(this); }
/// <summary> /// 处理权限 /// 返回当前节点 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="routeData">路由数据</param> /// <param name="menuNode">菜单节点</param> /// <param name="failureAction">权限验证不通过</param> public static MenuNode <T> Process <T>(RouteData routeData, MenuNode <T> menuNode, Action <MenuNode <T> > failureAction) where T : struct { var node = menuNode.FindNode(routeData); if (node == null) { return(null); } if (node.IsActive == false) { node.SetActive(); } if (node.IsPageNode) { if (node.IsPermission == false) { failureAction(node); } return(node); } else if (node.IsAllow(node.ActionEnum) == false) { failureAction(node); } return(node); }
/// <summary> /// 设置node和所有父级 /// </summary> /// <param name="node"></param> /// <param name="action"></param> private void SetWithParents(MenuNode <T> node, Action <MenuNode <T> > action) { while (node != null) { action.Invoke(node); node = node.ParentNode; } }
/// <summary> /// 添加子系统 /// </summary> /// <param name="name">系统名</param> /// <param name="icon">图标</param> /// <returns></returns> public MenuBuilder <T> System(string name, string icon) { var system = new MenuNode <T> { Name = name, Icon = icon }; return(this.AddSystem(system)); }
/// <summary> /// 添加菜单节点 /// </summary> /// <param name="pageNode">page节点</param> /// <returns></returns> public MenuBuilder <T> AddPage(MenuNode <T> pageNode) { pageNode.Controller = pageNode.Controller.ToLowerIfNoNull(); pageNode.ActionName = pageNode.ActionName.ToLowerIfNoNull(); pageNode.IsPageNode = true; this.LastModule.AddChildNode(pageNode); return(this); }
/// <summary> /// 添加行为节点 /// </summary> /// <param name="controller">控制器</param> /// <param name="actionName">Action名</param> /// <param name="actionEnum">行为枚举</param> /// <returns></returns> public MenuBuilder <T> Action(string controller, string actionName, T actionEnum) { var mainMenu = new MenuNode <T> { Controller = controller, ActionName = actionName, ActionEnum = actionEnum }; return(this.AddAction(mainMenu)); }
/// <summary> /// 添加模块节点 /// </summary> /// <param name="name">模块名</param> /// <param name="icon">图标</param> /// <returns></returns> public MenuBuilder <T> Module(string name, string icon) { var module = new MenuNode <T> { Name = name, Icon = icon }; return(this.AddModule(module)); }
/// <summary> /// 添加菜单节点 /// </summary> /// <param name="name">菜单名称</param> /// <param name="controller">控制器</param> /// <param name="actionName">Action名</param> /// <param name="icon">图标</param> /// <returns></returns> public MenuBuilder <T> Page(string name, string controller, string actionName, string icon) { var menu = new MenuNode <T> { Name = name, Controller = controller, ActionName = actionName, Icon = icon }; return(this.AddPage(menu)); }
/// <summary> /// 添加子系统 /// </summary> /// <param name="name">系统名</param> /// <param name="icon">图标</param> /// <param name="tag">附加数据</param> /// <returns></returns> public MenuBuilder <T> System(string name, string icon, object tag) { var system = new MenuNode <T> { Name = name, Icon = icon, Tag = tag }; return(this.AddSystem(system)); }
/// <summary> /// 添加模块节点 /// </summary> /// <param name="name">模块名</param> /// <returns></returns> public MenuBuilder <T> Module(string name) { var module = new MenuNode <T> { Name = name }; return(this.AddModule(module)); }
/// <summary> /// 添加子系统 /// </summary> /// <param name="name">系统名</param> /// <returns></returns> public MenuBuilder <T> System(string name) { var system = new MenuNode <T> { Name = name }; return(this.AddSystem(system)); }
/// <summary> /// 将节点添加到列表中 /// </summary> /// <param name="node"></param> /// <param name="list"></param> private void AddNodeToList(MenuNode <T> node, List <MenuNode <T> > list) { if (node.IsPageNode) { list.Add(node); } foreach (var item in node.ChildNodes) { this.AddNodeToList(item, list); } }
/// <summary> /// 过滤模块的子节点 /// </summary> /// <param name="module">模块</param> /// <param name="nodeMd5">保留的节点</param> /// <returns></returns> private IEnumerable <MenuNode <T> > FilterChildNodes(MenuNode <T> module, IEnumerable <FilterNode <T> > filterNodes) { foreach (var node in module.ChildNodes) { var filterNode = filterNodes.FirstOrDefault(item => item.HashMd5 == node.GetHashMd5()); if (filterNode != null) { node.ActionEnum = filterNode.ActionEnum; yield return(node); } } }
/// <summary> /// 添加子节点 /// </summary> /// <param name="nodes">节点</param> public MenuNode <T> AddChildNode(MenuNode <T> node) { node.ParentNode = this; if (this.IsPageNode == true) { var actionEnum = this.ActionEnum.GetHashCode() + node.ActionEnum.GetHashCode(); this.ActionEnum = (T)Enum.Parse(typeof(T), actionEnum.ToString()); } this.ChildNodes.Add(node); return(node); }
/// <summary> /// 过滤page节点 /// </summary> /// <param name="parentNode"></param> /// <param name="filterNodes"></param> /// <returns>返回没更新的节点</returns> private IEnumerable <MenuNode <T> > UpdatePageNodes(MenuNode <T> parentNode, IEnumerable <FilterNode <T> > filterNodes) { foreach (var node in parentNode.ChildNodes) { var md5 = node.GetHashMd5(); var filterNode = filterNodes.FirstOrDefault(item => item.HashMd5 == md5); if (filterNode != null) { node.ActionEnum = filterNode.ActionEnum; } else { yield return(node); } } }
/// <summary> /// 过滤模块 /// </summary> /// <param name="module">模块</param> /// <param name="filterNodes">保留的菜单节点</param> /// <returns></returns> private IEnumerable <MenuNode <T> > FilterModule(MenuNode <T> module, IEnumerable <FilterNode <T> > filterNodes) { var childNodes = this.FilterChildNodes(module, filterNodes).ToArray(); if (childNodes.Length == 0) { yield break; } module.ChildNodes = childNodes; yield return(module); foreach (var node in childNodes) { yield return(node); } }
/// <summary> /// 添加module节点 /// </summary> /// <param name="moduleNode">module节点</param> /// <returns></returns> public MenuBuilder <T> AddModule(MenuNode <T> moduleNode) { this.LastSystem.AddChildNode(moduleNode); return(this); }
/// <summary> /// 添加子system节点 /// </summary> /// <param name="systemNode">system节点</param> /// <returns></returns> public MenuBuilder <T> AddSystem(MenuNode <T> systemNode) { this.topNode.AddChildNode(systemNode); return(this); }