diff --git a/src/blazor/Bootstrap.Client.Blazor.Shared/Shared/MainLayout.razor.cs b/src/blazor/Bootstrap.Client.Blazor.Shared/Shared/MainLayout.razor.cs index e853b53c..7ef89af9 100644 --- a/src/blazor/Bootstrap.Client.Blazor.Shared/Shared/MainLayout.razor.cs +++ b/src/blazor/Bootstrap.Client.Blazor.Shared/Shared/MainLayout.razor.cs @@ -1,4 +1,6 @@ using Bootstrap.Client.DataAccess; +using Bootstrap.Security; +using Bootstrap.Security.Mvc; using BootstrapBlazor.Components; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; @@ -87,17 +89,35 @@ namespace Bootstrap.Client.Blazor.Shared.Shared if (OperatingSystem.IsBrowser()) { - + // 需要调用 webapi 获取菜单数据 暂未实现 } 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 CascadeMenu(IEnumerable datasource) + { + var menus = new List(); + foreach (var m in datasource) + { + var item = new MenuItem() { Text = m.Name, Url = m.Url.TrimStart('~'), Target = m.Target, Icon = m.Icon - }).ToList(); + }; + menus.Add(item); + + if (m.Menus.Any()) + { + item.Items = CascadeMenu(m.Menus); + } } return menus; } diff --git a/src/client/Bootstrap.Client.DataAccess/Helper/MenuHelper.cs b/src/client/Bootstrap.Client.DataAccess/Helper/MenuHelper.cs index 5f917db0..9e059504 100644 --- a/src/client/Bootstrap.Client.DataAccess/Helper/MenuHelper.cs +++ b/src/client/Bootstrap.Client.DataAccess/Helper/MenuHelper.cs @@ -36,7 +36,7 @@ namespace Bootstrap.Client.DataAccess /// /// /// - public static IEnumerable RetrieveAllMenus(string userName) => CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbContextManager.Create()?.RetrieveAllMenus(userName), RetrieveMenusAll) ?? new BootstrapMenu[0]; + public static IEnumerable RetrieveAllMenus(string userName) => CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbContextManager.Create()?.RetrieveAllMenus(userName), RetrieveMenusAll) ?? System.Array.Empty(); /// /// 通过当前用户名与指定菜单路径获取此菜单下所有授权按钮集合 (userName, url, auths) => bool