From 312f77131324742672e7770c3329d248d8c17f18 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Wed, 20 Jul 2022 14:05:07 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E5=88=B0=E6=9C=80=E6=96=B0=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Bootstrap.Admin/Bootstrap.Admin.csproj | 10 +- .../Extensions/TasksExtensions.cs | 3 +- .../Bootstrap.DataAccess.MongoDB.csproj | 2 +- .../Bootstrap.DataAccess.csproj | 12 +-- .../Helper/ExceptionsHelper.cs | 15 ++- .../Bootstrap.DataAccess/Helper/MenuHelper.cs | 93 ++++++++++++------- ...Bootstrap.Client.DataAccess.MongoDB.csproj | 2 +- .../Bootstrap.Client.DataAccess.csproj | 6 +- .../Bootstrap.Client/Bootstrap.Client.csproj | 6 +- 9 files changed, 94 insertions(+), 55 deletions(-) diff --git a/src/mvc/admin/Bootstrap.Admin/Bootstrap.Admin.csproj b/src/mvc/admin/Bootstrap.Admin/Bootstrap.Admin.csproj index 802b60b5..9ebe924a 100644 --- a/src/mvc/admin/Bootstrap.Admin/Bootstrap.Admin.csproj +++ b/src/mvc/admin/Bootstrap.Admin/Bootstrap.Admin.csproj @@ -9,12 +9,12 @@ - - - + + + - - + + diff --git a/src/mvc/admin/Bootstrap.Admin/Extensions/TasksExtensions.cs b/src/mvc/admin/Bootstrap.Admin/Extensions/TasksExtensions.cs index 1f7d1e3d..3bfdbf43 100644 --- a/src/mvc/admin/Bootstrap.Admin/Extensions/TasksExtensions.cs +++ b/src/mvc/admin/Bootstrap.Admin/Extensions/TasksExtensions.cs @@ -23,7 +23,8 @@ namespace Microsoft.Extensions.DependencyInjection /// public static IServiceCollection AddBootstrapAdminBackgroundTask(this IServiceCollection services) { - services.AddTaskServices(builder => builder.AddFileStorage()); + //services.AddTaskServices(builder => builder.AddFileStorage()); + services.AddTaskServices(); services.AddHostedService(); return services; } diff --git a/src/mvc/admin/Bootstrap.DataAccess.MongoDB/Bootstrap.DataAccess.MongoDB.csproj b/src/mvc/admin/Bootstrap.DataAccess.MongoDB/Bootstrap.DataAccess.MongoDB.csproj index 778491fe..354d1e47 100644 --- a/src/mvc/admin/Bootstrap.DataAccess.MongoDB/Bootstrap.DataAccess.MongoDB.csproj +++ b/src/mvc/admin/Bootstrap.DataAccess.MongoDB/Bootstrap.DataAccess.MongoDB.csproj @@ -2,7 +2,7 @@ - + diff --git a/src/mvc/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/src/mvc/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index 5df00e1f..ba3beca2 100644 --- a/src/mvc/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/src/mvc/admin/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -2,23 +2,23 @@ - + - + - - + + - - + + diff --git a/src/mvc/admin/Bootstrap.DataAccess/Helper/ExceptionsHelper.cs b/src/mvc/admin/Bootstrap.DataAccess/Helper/ExceptionsHelper.cs index 669f1d83..2760c9aa 100644 --- a/src/mvc/admin/Bootstrap.DataAccess/Helper/ExceptionsHelper.cs +++ b/src/mvc/admin/Bootstrap.DataAccess/Helper/ExceptionsHelper.cs @@ -4,6 +4,7 @@ using Longbow.Cache; using Longbow.Web.Mvc; +using Microsoft.Extensions.Logging; using PetaPoco; using System; using System.Collections.Generic; @@ -25,13 +26,21 @@ namespace Bootstrap.DataAccess /// /// /// + /// + /// /// /// /// - public static void Log(Exception ex, NameValueCollection additionalInfo) + public static void Log(IServiceProvider provider, EventId eventId, Exception? ex, NameValueCollection additionalInfo) { - var ret = DbContextManager.Create()?.Log(ex, additionalInfo) ?? false; - if (ret) CacheManager.Clear(RetrieveExceptionsDataKey); + if (ex != null) + { + var ret = DbContextManager.Create()?.Log(ex, additionalInfo) ?? false; + if (ret) + { + CacheManager.Clear(RetrieveExceptionsDataKey); + } + } } /// diff --git a/src/mvc/admin/Bootstrap.DataAccess/Helper/MenuHelper.cs b/src/mvc/admin/Bootstrap.DataAccess/Helper/MenuHelper.cs index c3a7547c..0bcf53cd 100644 --- a/src/mvc/admin/Bootstrap.DataAccess/Helper/MenuHelper.cs +++ b/src/mvc/admin/Bootstrap.DataAccess/Helper/MenuHelper.cs @@ -35,30 +35,40 @@ namespace Bootstrap.DataAccess /// public static bool Save(BootstrapMenu p) { - // 不允许保存系统菜单与前台演示系统的默认菜单 + var ret = false; + if (DictHelper.RetrieveSystemModel()) { - if (p.Category == "0") return true; - - // 查找原有数据比对是否为系统菜单与演示菜单 - if (!string.IsNullOrEmpty(p.Id)) + // 不允许保存系统菜单与前台演示系统的默认菜单 + if (p.Category == "0") { - - // 系统菜单 + ret = true; + } + else if (!string.IsNullOrEmpty(p.Id)) + { + // 查找原有数据比对是否为系统菜单与演示菜单 var menus = RetrieveAllMenus("Admin"); -#pragma warning disable CS8602 // 取消引用可能出现的空引用。 - var menu = menus.FirstOrDefault(m => m.Id.Equals(p.Id, System.StringComparison.OrdinalIgnoreCase)); -#pragma warning restore CS8602 // 取消引用可能出现的空引用。 - if (menu != null && menu.Category == "0") return true; - - // 演示系统 - var appMenus = BootstrapAppContext.Configuration.GetSection("AppMenus").Get>(); - if (appMenus.Any(m => m.Equals(menu?.Name, StringComparison.OrdinalIgnoreCase))) return true; + var menu = menus.FirstOrDefault(m => m.Id?.Equals(p.Id, System.StringComparison.OrdinalIgnoreCase) ?? false); + if (menu != null && menu.Category == "0") + { + // 系统菜单 + ret = true; + } + else if (BootstrapAppContext.Configuration != null) + { + var appMenus = BootstrapAppContext.Configuration.GetSection("AppMenus").Get>(); + if (appMenus.Any(m => m.Equals(menu?.Name, StringComparison.OrdinalIgnoreCase))) + { + ret = true; + } + } } } - - var ret = DbContextManager.Create()?.Save(p) ?? false; - if (ret) CacheCleanUtility.ClearCache(menuIds: string.IsNullOrEmpty(p.Id) ? new List() : new List() { p.Id }); + else + { + ret = DbContextManager.Create()?.Save(p) ?? false; + if (ret) CacheCleanUtility.ClearCache(menuIds: string.IsNullOrEmpty(p.Id) ? new List() : new List() { p.Id }); + } return ret; } @@ -69,23 +79,38 @@ namespace Bootstrap.DataAccess /// public static bool Delete(IEnumerable value) { + var ret = false; if (DictHelper.RetrieveSystemModel()) { // 不允许删除系统菜单与前台演示系统的默认菜单 var systemMenus = RetrieveAllMenus("Admin").Where(m => m.Category == "0"); value = value.Where(v => !systemMenus.Any(m => m.Id == v)); - if (!value.Any()) return true; - - // 演示系统 - var appMenus = BootstrapAppContext.Configuration.GetSection("AppMenus").Get>(); - var appIds = RetrieveAllMenus("Admin").Where(m => appMenus.Any(app => m.Name.Equals(app, System.StringComparison.OrdinalIgnoreCase))).Select(m => m.Id); -#pragma warning disable CS8602 // 取消引用可能出现的空引用。 - value = value.Where(m => !appIds.Any(app => app.Equals(m, StringComparison.OrdinalIgnoreCase))); -#pragma warning restore CS8602 // 取消引用可能出现的空引用。 - if (!value.Any()) return true; + if (!value.Any()) + { + ret = true; + } + else if (BootstrapAppContext.Configuration != null) + { + // 演示系统 + var appMenus = BootstrapAppContext.Configuration.GetSection("AppMenus").Get>(); + var appIds = RetrieveAllMenus("Admin") + .Where(m => appMenus.Any(app => m.Name.Equals(app, System.StringComparison.OrdinalIgnoreCase))) + .Select(m => m.Id); + value = value.Where(m => !appIds.Any(app => app?.Equals(m, StringComparison.OrdinalIgnoreCase) ?? false)); + if (!value.Any()) + { + ret = true; + } + } + } + else + { + ret = DbContextManager.Create()?.Delete(value) ?? false; + if (ret) + { + CacheCleanUtility.ClearCache(menuIds: value); + } } - var ret = DbContextManager.Create()?.Delete(value) ?? false; - if (ret) CacheCleanUtility.ClearCache(menuIds: value); return ret; } @@ -101,7 +126,7 @@ namespace Bootstrap.DataAccess /// /// /// - public static IEnumerable RetrieveMenusByRoleId(string roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbContextManager.Create()?.RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey) ?? new string[0]; + public static IEnumerable RetrieveMenusByRoleId(string roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbContextManager.Create()?.RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey) ?? Array.Empty(); /// /// 保存指定角色的所有菜单 @@ -125,7 +150,7 @@ namespace Bootstrap.DataAccess /// public static IEnumerable RetrieveAppMenus(string? appId, string? userName, string? activeUrl) { - if (string.IsNullOrEmpty(appId) || string.IsNullOrEmpty(userName)) return new BootstrapMenu[0]; + if (string.IsNullOrEmpty(appId) || string.IsNullOrEmpty(userName)) return Array.Empty(); var menus = RetrieveAllMenus(userName).Where(m => m.Category == "1" && m.IsResource == 0); menus = menus.Where(m => m.Application.Equals(appId, StringComparison.OrdinalIgnoreCase)); @@ -162,7 +187,11 @@ namespace Bootstrap.DataAccess /// /// /// - public static IEnumerable RetrieveAllMenus(string? userName) => string.IsNullOrEmpty(userName) ? new BootstrapMenu[0] : CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbContextManager.Create()?.RetrieveAllMenus(userName), RetrieveMenusAll) ?? Array.Empty(); + public static IEnumerable RetrieveAllMenus(string? userName) => string.IsNullOrEmpty(userName) + ? Array.Empty() + : CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbContextManager.Create() + ?.RetrieveAllMenus(userName), RetrieveMenusAll) + ?? Array.Empty(); /// /// 通过当前用户名与指定菜单路径获取此菜单下所有授权按钮集合 (userName, url, auths) => bool diff --git a/src/mvc/client/Bootstrap.Client.DataAccess.MongoDB/Bootstrap.Client.DataAccess.MongoDB.csproj b/src/mvc/client/Bootstrap.Client.DataAccess.MongoDB/Bootstrap.Client.DataAccess.MongoDB.csproj index 8fb752e2..96f6b2a1 100644 --- a/src/mvc/client/Bootstrap.Client.DataAccess.MongoDB/Bootstrap.Client.DataAccess.MongoDB.csproj +++ b/src/mvc/client/Bootstrap.Client.DataAccess.MongoDB/Bootstrap.Client.DataAccess.MongoDB.csproj @@ -1,7 +1,7 @@  - + diff --git a/src/mvc/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj b/src/mvc/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj index 616ef9c1..42c59222 100644 --- a/src/mvc/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj +++ b/src/mvc/client/Bootstrap.Client.DataAccess/Bootstrap.Client.DataAccess.csproj @@ -2,12 +2,12 @@ - + - + - + diff --git a/src/mvc/client/Bootstrap.Client/Bootstrap.Client.csproj b/src/mvc/client/Bootstrap.Client/Bootstrap.Client.csproj index defe1c13..1b91b262 100644 --- a/src/mvc/client/Bootstrap.Client/Bootstrap.Client.csproj +++ b/src/mvc/client/Bootstrap.Client/Bootstrap.Client.csproj @@ -5,9 +5,9 @@ - - - + + +