From 1e0203472ee862c11465cf4510548613ed54e758 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Sun, 2 Jan 2022 11:24:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=20Extensions=20?= =?UTF-8?q?=E5=B7=A5=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BootstrapAdmin.Extensions.csproj | 11 ---- .../Extensions}/MenuExtensions.cs | 3 +- .../Shared/MainLayout.razor.cs | 2 +- .../Extensions/MenuExtensions.cs | 61 +++++++++++++++++++ .../Shared/MainLayout.razor.cs | 2 +- 5 files changed, 64 insertions(+), 15 deletions(-) delete mode 100644 src/blazor/admin/BootstrapAdmin.Extensions/BootstrapAdmin.Extensions.csproj rename src/blazor/admin/{BootstrapAdmin.Extensions => BootstrapAdmin.Web/Extensions}/MenuExtensions.cs (97%) create mode 100644 src/blazor/client/BootstrapClient.Shared/Extensions/MenuExtensions.cs diff --git a/src/blazor/admin/BootstrapAdmin.Extensions/BootstrapAdmin.Extensions.csproj b/src/blazor/admin/BootstrapAdmin.Extensions/BootstrapAdmin.Extensions.csproj deleted file mode 100644 index aa74cd55..00000000 --- a/src/blazor/admin/BootstrapAdmin.Extensions/BootstrapAdmin.Extensions.csproj +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/src/blazor/admin/BootstrapAdmin.Extensions/MenuExtensions.cs b/src/blazor/admin/BootstrapAdmin.Web/Extensions/MenuExtensions.cs similarity index 97% rename from src/blazor/admin/BootstrapAdmin.Extensions/MenuExtensions.cs rename to src/blazor/admin/BootstrapAdmin.Web/Extensions/MenuExtensions.cs index a39b75d6..ec9f96c0 100644 --- a/src/blazor/admin/BootstrapAdmin.Extensions/MenuExtensions.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Extensions/MenuExtensions.cs @@ -1,8 +1,7 @@ using BootstrapAdmin.DataAccess.Models; -using BootstrapBlazor.Components; using Microsoft.AspNetCore.Components.Routing; -namespace BootstrapAdmin.Extensions +namespace BootstrapAdmin.Web.Extensions { /// /// diff --git a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs index 9c7b4068..43de6e2c 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs @@ -1,6 +1,6 @@ using Bootstrap.Security.Blazor; -using BootstrapAdmin.Extensions; using BootstrapAdmin.Web.Core; +using BootstrapAdmin.Web.Extensions; using BootstrapAdmin.Web.Services; using Microsoft.AspNetCore.Components.Authorization; diff --git a/src/blazor/client/BootstrapClient.Shared/Extensions/MenuExtensions.cs b/src/blazor/client/BootstrapClient.Shared/Extensions/MenuExtensions.cs new file mode 100644 index 00000000..3ded5acb --- /dev/null +++ b/src/blazor/client/BootstrapClient.Shared/Extensions/MenuExtensions.cs @@ -0,0 +1,61 @@ +using BootstrapAdmin.DataAccess.Models; +using BootstrapBlazor.Components; +using Microsoft.AspNetCore.Components.Routing; + +namespace BootstrapClient.Web.Shared.Extensions; + +/// +/// +/// +public static class MenuExtensions +{ + /// + /// + /// + /// + /// + public static MenuItem Parse(this Navigation menu) => new() + { + Text = menu.Name, + Url = menu.Url.Replace("~", ""), + Icon = menu.Icon, + Match = NavLinkMatch.All, + Target = menu.Target, + Id = menu.Id, + ParentId = menu.ParentId + }; + + /// + /// 获取前台菜单 + /// + /// + public static IEnumerable ToClientMenus(this List navigations) + { + var menus = navigations.Where(m => m.Category == EnumNavigationCategory.Customer && m.IsResource == 0); + return CascadeMenus(menus); + } + + /// + /// 获得带层次关系的菜单集合 + /// + /// 未层次化菜单集合 + /// 带层次化的菜单集合 + public static IEnumerable CascadeMenus(IEnumerable navigations) + { + var root = navigations.Where(m => m.ParentId == "0") + .OrderBy(m => m.Category).ThenBy(m => m.Application).ThenBy(m => m.Order) + .Select(m => m.Parse()) + .ToList(); + CascadeMenus(navigations, root); + return root; + } + + private static void CascadeMenus(IEnumerable navigations, List level) + { + level.ForEach(m => + { + m.Items = navigations.Where(sub => sub.ParentId == m.Id).OrderBy(sub => sub.Order).Select(sub => sub.Parse()).ToList(); + CascadeMenus(navigations, m.Items.ToList()); + }); + } +} diff --git a/src/blazor/client/BootstrapClient.Shared/Shared/MainLayout.razor.cs b/src/blazor/client/BootstrapClient.Shared/Shared/MainLayout.razor.cs index 93fe56a9..2cf2f6c3 100644 --- a/src/blazor/client/BootstrapClient.Shared/Shared/MainLayout.razor.cs +++ b/src/blazor/client/BootstrapClient.Shared/Shared/MainLayout.razor.cs @@ -1,7 +1,7 @@ using Bootstrap.Security.Blazor; -using BootstrapAdmin.Extensions; using BootstrapAdmin.Web.Core; using BootstrapBlazor.Components; +using BootstrapClient.Web.Shared.Extensions; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization;