feat: 多层菜单支持

This commit is contained in:
Argo-Tianyi 2021-05-25 20:00:52 +08:00
parent d9a80affcf
commit d31b056292
2 changed files with 24 additions and 4 deletions

View File

@ -1,4 +1,6 @@
using Bootstrap.Client.DataAccess; using Bootstrap.Client.DataAccess;
using Bootstrap.Security;
using Bootstrap.Security.Mvc;
using BootstrapBlazor.Components; using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
@ -87,17 +89,35 @@ namespace Bootstrap.Client.Blazor.Shared.Shared
if (OperatingSystem.IsBrowser()) if (OperatingSystem.IsBrowser())
{ {
// 需要调用 webapi 获取菜单数据 暂未实现
} }
else else
{ {
menus = MenuHelper.RetrieveAppMenus(UserName, "").Select(m => new MenuItem() var appId = BootstrapAppContext.AppId;
var data = MenuHelper.RetrieveAppMenus(UserName, "");
menus = CascadeMenu(data);
}
return menus;
}
private List<MenuItem> CascadeMenu(IEnumerable<BootstrapMenu> datasource)
{
var menus = new List<MenuItem>();
foreach (var m in datasource)
{
var item = new MenuItem()
{ {
Text = m.Name, Text = m.Name,
Url = m.Url.TrimStart('~'), Url = m.Url.TrimStart('~'),
Target = m.Target, Target = m.Target,
Icon = m.Icon Icon = m.Icon
}).ToList(); };
menus.Add(item);
if (m.Menus.Any())
{
item.Items = CascadeMenu(m.Menus);
}
} }
return menus; return menus;
} }

View File

@ -36,7 +36,7 @@ namespace Bootstrap.Client.DataAccess
/// </summary> /// </summary>
/// <param name="userName"></param> /// <param name="userName"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName) => CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbContextManager.Create<Menu>()?.RetrieveAllMenus(userName), RetrieveMenusAll) ?? new BootstrapMenu[0]; public static IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName) => CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbContextManager.Create<Menu>()?.RetrieveAllMenus(userName), RetrieveMenusAll) ?? System.Array.Empty<BootstrapMenu>();
/// <summary> /// <summary>
/// 通过当前用户名与指定菜单路径获取此菜单下所有授权按钮集合 (userName, url, auths) => bool /// 通过当前用户名与指定菜单路径获取此菜单下所有授权按钮集合 (userName, url, auths) => bool