diff --git a/Bootstrap.Admin/Controllers/Api/MenusController.cs b/Bootstrap.Admin/Controllers/Api/MenusController.cs index 3f7bcfd4..25f3658f 100644 --- a/Bootstrap.Admin/Controllers/Api/MenusController.cs +++ b/Bootstrap.Admin/Controllers/Api/MenusController.cs @@ -58,7 +58,7 @@ namespace Bootstrap.Admin.Controllers.Api ret = MenuHelper.RetrieveMenusByRoleId(id).ToList(); break; case "user": - ret = MenuHelper.RetrieveAllMenus(User.Identity.Name).ToList(); + ret = MenuHelper.RetrieveMenus(User.Identity.Name).ToList(); break; default: break; diff --git a/Bootstrap.Admin/appsettings.json b/Bootstrap.Admin/appsettings.json index d030e312..e293ea64 100644 --- a/Bootstrap.Admin/appsettings.json +++ b/Bootstrap.Admin/appsettings.json @@ -93,7 +93,7 @@ }, { "Enabled": true, - "Key": "BootstrapMenu-RetrieveMenusByUserName", + "Key": "BootstrapMenu-RetrieveMenus", "Interval": 600, "SlidingExpiration": true, "Desc": "用户所有菜单数据缓存" diff --git a/Bootstrap.DataAccess/CacheCleanUtility.cs b/Bootstrap.DataAccess/CacheCleanUtility.cs index 7a4b50b7..d29ee3a1 100644 --- a/Bootstrap.DataAccess/CacheCleanUtility.cs +++ b/Bootstrap.DataAccess/CacheCleanUtility.cs @@ -32,9 +32,9 @@ namespace Bootstrap.DataAccess cacheKeys.Add(string.Format("{0}-{1}", MenuHelper.RetrieveMenusByRoleIdDataKey, id)); }); cacheKeys.Add(RoleHelper.RetrieveRolesDataKey + "*"); - cacheKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); + cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*"); cacheKeys.Add(RetrieveAllRolesDataKey + "*"); - corsKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); + corsKeys.Add(MenuHelper.RetrieveMenusAll + "*"); } if (userIds != null) { @@ -42,8 +42,8 @@ namespace Bootstrap.DataAccess { cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByUserIdDataKey, id)); cacheKeys.Add(string.Format("{0}-{1}", GroupHelper.RetrieveGroupsByUserIdDataKey, id)); - cacheKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); - corsKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); + cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*"); + corsKeys.Add(MenuHelper.RetrieveMenusAll + "*"); }); cacheKeys.Add(UserHelper.RetrieveNewUsersDataKey + "*"); cacheKeys.Add(UserHelper.RetrieveUsersDataKey + "*"); @@ -57,8 +57,8 @@ namespace Bootstrap.DataAccess cacheKeys.Add(string.Format("{0}-{1}", UserHelper.RetrieveUsersByGroupIdDataKey, id)); }); cacheKeys.Add(GroupHelper.RetrieveGroupsDataKey + "*"); - cacheKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); - corsKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); + cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*"); + corsKeys.Add(MenuHelper.RetrieveMenusAll + "*"); cacheKeys.Add(RetrieveAllRolesDataKey + "*"); } if (menuIds != null) @@ -68,8 +68,8 @@ namespace Bootstrap.DataAccess cacheKeys.Add(string.Format("{0}-{1}", RoleHelper.RetrieveRolesByMenuIdDataKey, id)); }); cacheKeys.Add(MenuHelper.RetrieveMenusByRoleIdDataKey + "*"); - cacheKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); - corsKeys.Add(MenuHelper.RetrieveMenusDataKey + "*"); + cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*"); + corsKeys.Add(MenuHelper.RetrieveMenusAll + "*"); } if (dictIds != null) { diff --git a/Bootstrap.DataAccess/Helper/MenuHelper.cs b/Bootstrap.DataAccess/Helper/MenuHelper.cs index 9eec85aa..cec4f37c 100644 --- a/Bootstrap.DataAccess/Helper/MenuHelper.cs +++ b/Bootstrap.DataAccess/Helper/MenuHelper.cs @@ -1,7 +1,9 @@ using Bootstrap.Security; +using Bootstrap.Security.SQLServer; using Longbow.Cache; using Longbow.Data; using System.Collections.Generic; +using System.Linq; namespace Bootstrap.DataAccess { @@ -17,10 +19,6 @@ namespace Bootstrap.DataAccess /// /// /// - public const string RetrieveMenusDataKey = "BootstrapMenu-RetrieveMenusByUserName"; - /// - /// - /// public const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus"; /// /// @@ -35,20 +33,17 @@ namespace Bootstrap.DataAccess /// public static bool DeleteMenu(IEnumerable value) => DbAdapterManager.Create().DeleteMenu(value); /// - /// + /// 通过用户名获得所有菜单 /// /// /// - public static IEnumerable RetrieveMenusByUserName(string userName) => DbAdapterManager.Create().RetrieveMenusByUserName(userName); + public static IEnumerable RetrieveMenusByUserName(string userName) => RetrieveAllMenus(userName); /// /// /// /// /// - public static IEnumerable RetrieveMenusByRoleId(int roleId) - { - return CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbAdapterManager.Create().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey); - } + public static IEnumerable RetrieveMenusByRoleId(int roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbAdapterManager.Create().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey); /// /// /// @@ -60,21 +55,50 @@ namespace Bootstrap.DataAccess /// /// /// - /// - /// - public static IEnumerable RetrieveAppMenus(string appId, string userName, string url) => DbAdapterManager.Create().RetrieveAppMenus(appId, userName, url); - /// - /// - /// - /// /// /// - public static IEnumerable RetrieveSystemMenus(string userName, string activeUrl = null) => DbAdapterManager.Create().RetrieveSystemMenus(userName, activeUrl); + public static IEnumerable RetrieveAppMenus(string appId, string userName, string activeUrl) + { + var menus = RetrieveAllMenus(userName).Where(m => m.Category == "1" && m.IsResource == 0); + if (appId != "0") menus = menus.Where(m => m.ApplicationCode == appId); + BASQLHelper.ActiveMenu(null, menus, activeUrl); + var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order); + BASQLHelper.CascadeMenus(menus, root); + return root; + } /// - /// + /// 通过当前用户名获得后台菜单,层次化后集合 /// - /// + /// + /// 当前登陆的用户名 + /// 当前访问菜单 + /// 连接字符串名称,默认为ba /// - public static IEnumerable RetrieveAllMenus(string name) => DbAdapterManager.Create().RetrieveAllMenus(name); + public static IEnumerable RetrieveSystemMenus(string userName, string activeUrl = null) + { + var menus = RetrieveAllMenus(userName).Where(m => m.Category == "0" && m.IsResource == 0); + BASQLHelper.ActiveMenu(null, menus, activeUrl); + var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order); + BASQLHelper.CascadeMenus(menus, root); + return root; + } + /// + /// 通过当前用户名获得所有菜单,层次化后集合 + /// + /// + /// + public static IEnumerable RetrieveMenus(string userName) + { + var menus = RetrieveAllMenus(userName); + var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order); + BASQLHelper.CascadeMenus(menus, root); + return root; + } + /// + /// 通过用户获得所有菜单 + /// + /// + /// + private static IEnumerable RetrieveAllMenus(string userName) => CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbAdapterManager.Create().RetrieveAllMenus(userName), RetrieveMenusAll); } } diff --git a/Bootstrap.DataAccess/Menu.cs b/Bootstrap.DataAccess/Menu.cs index 68ade37b..d0aaf3b6 100644 --- a/Bootstrap.DataAccess/Menu.cs +++ b/Bootstrap.DataAccess/Menu.cs @@ -136,45 +136,7 @@ namespace Bootstrap.DataAccess /// 通过当前用户名获得所有菜单,层次化后集合 /// /// 当前登陆的用户名 - /// 当前访问菜单 /// - public virtual IEnumerable RetrieveAllMenus(string userName, string activeUrl = null) - { - var menus = RetrieveMenusByUserName(userName, activeUrl); - var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.Category).ThenBy(m => m.ApplicationCode).ThenBy(m => m.Order); - BASQLHelper.CascadeMenus(menus, root); - return root; - } - /// - /// 通过当前用户名获得前台菜单,层次化后集合 - /// - /// - /// 当前登陆的用户名 - /// 当前访问菜单 - /// - public virtual IEnumerable RetrieveAppMenus(string appId, string userName, string activeUrl = null) => BASQLHelper.RetrieveAppMenus(appId, userName, activeUrl); - /// - /// 通过当前用户名获得后台菜单,层次化后集合 - /// - /// - /// 当前登陆的用户名 - /// 当前访问菜单 - /// 连接字符串名称,默认为ba - /// - public virtual IEnumerable RetrieveSystemMenus(string userName, string activeUrl = null) - { - var menus = RetrieveMenusByUserName(userName, activeUrl).Where(m => m.Category == "0" && m.IsResource == 0); - var root = menus.Where(m => m.ParentId == 0).OrderBy(m => m.ApplicationCode).ThenBy(m => m.Order); - BASQLHelper.CascadeMenus(menus, root); - return root; - } - /// - /// 通过当前用户名获得所有菜单 - /// - /// 当前登陆的用户名 - /// 当前访问菜单 - /// - /// - public virtual IEnumerable RetrieveMenusByUserName(string userName, string activeUrl = null, string appId = "0") => BASQLHelper.RetrieveMenusByUserName(userName, activeUrl, appId); + public virtual IEnumerable RetrieveAllMenus(string userName) => BASQLHelper.RetrieveAllMenus(userName); } }