using Bootstrap.Security; using Bootstrap.Security.DataAccess; using Longbow.Cache; using Longbow.Data; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Linq; namespace Bootstrap.DataAccess { /// /// /// public static class MenuHelper { /// /// /// public const string RetrieveMenusByRoleIdDataKey = "MenuHelper-RetrieveMenusByRoleId"; /// /// /// public const string RetrieveMenusAll = "BootstrapMenu-RetrieveMenus"; /// /// /// /// /// public static bool Save(BootstrapMenu p) { // 不允许保存系统菜单与前台演示系统的默认菜单 if (DictHelper.RetrieveSystemModel() && (p.Category == "0" || p.Application == "2")) return true; if (p.Id == string.Empty) p.Id = null; var ret = DbContextManager.Create().Save(p); if (ret) CacheCleanUtility.ClearCache(menuIds: string.IsNullOrEmpty(p.Id) ? new List() : new List() { p.Id }); return ret; } /// /// /// /// /// public static bool Delete(IEnumerable value) { if (DictHelper.RetrieveSystemModel()) { // 不允许删除系统菜单与前台演示系统的默认菜单 var systemMenus = RetrieveAllMenus("Admin").Where(m => m.Category == "0" || m.Application == "2"); value = value.Where(v => !systemMenus.Any(m => m.Id == v)); if (!value.Any()) return true; } var ret = DbContextManager.Create().Delete(value); if (ret) CacheCleanUtility.ClearCache(menuIds: value); return ret; } /// /// 通过用户名获得所有菜单 /// /// /// public static IEnumerable RetrieveMenusByUserName(string userName) => RetrieveAllMenus(userName); /// /// /// /// /// public static IEnumerable RetrieveMenusByRoleId(string roleId) => CacheManager.GetOrAdd($"{RetrieveMenusByRoleIdDataKey}-{roleId}", k => DbContextManager.Create().RetrieveMenusByRoleId(roleId), RetrieveMenusByRoleIdDataKey); /// /// /// /// /// /// public static bool SaveMenusByRoleId(string roleId, IEnumerable menuIds) { var ret = DbContextManager.Create().SaveMenusByRoleId(roleId, menuIds); if (ret) CacheCleanUtility.ClearCache(menuIds: menuIds, roleIds: new List() { roleId }); return ret; } /// /// /// /// /// /// /// 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.Application == appId); return DbHelper.CascadeMenus(menus, activeUrl); } /// /// 通过当前用户名获得后台菜单,层次化后集合 /// /// 当前登陆的用户名 /// 当前访问菜单 /// public static IEnumerable RetrieveSystemMenus(string userName, string activeUrl = null) { var menus = RetrieveAllMenus(userName).Where(m => m.Category == "0" && m.IsResource == 0); return DbHelper.CascadeMenus(menus, activeUrl); } /// /// 通过当前用户名获得所有菜单,层次化后集合 /// /// /// public static IEnumerable RetrieveMenus(string userName) { var menus = RetrieveAllMenus(userName); return DbHelper.CascadeMenus(menus); } /// /// 通过用户获得所有菜单 /// /// /// public static IEnumerable RetrieveAllMenus(string userName) => CacheManager.GetOrAdd($"{RetrieveMenusAll}-{userName}", key => DbContextManager.Create().RetrieveAllMenus(userName), RetrieveMenusAll); } }