namespace Admin.NET.Core.Service; /// /// 系统菜单服务 /// [ApiDescriptionSettings(Order = 450)] public class SysMenuService : IDynamicApiController, ITransient { private readonly UserManager _userManager; private readonly SqlSugarRepository _sysMenuRep; private readonly SysRoleMenuService _sysRoleMenuService; private readonly SysUserRoleService _sysUserRoleService; private readonly SysCacheService _sysCacheService; public SysMenuService(UserManager userManager, SqlSugarRepository sysMenuRep, SysRoleMenuService sysRoleMenuService, SysUserRoleService sysUserRoleService, SysCacheService sysCacheService) { _userManager = userManager; _sysMenuRep = sysMenuRep; _sysRoleMenuService = sysRoleMenuService; _sysUserRoleService = sysUserRoleService; _sysCacheService = sysCacheService; } /// /// 获取登录菜单树 /// /// [DisplayName("获取登录菜单树")] public async Task> GetLoginMenuTree() { if (_userManager.SuperAdmin) { var menuList = await _sysMenuRep.AsQueryable() .Where(u => u.Type != MenuTypeEnum.Btn && u.Status == StatusEnum.Enable) .OrderBy(u => u.OrderNo).ToTreeAsync(u => u.Children, u => u.Pid, 0); return menuList.Adapt>(); } else { var menuIdList = await GetMenuIdList(); var menuTree = await _sysMenuRep.AsQueryable() .Where(u => u.Status == StatusEnum.Enable) .OrderBy(u => u.OrderNo).ToTreeAsync(u => u.Children, u => u.Pid, 0, menuIdList.Select(d => (object)d).ToArray()); DeleteBtnFromMenuTree(menuTree); return menuTree.Adapt>(); } } /// /// 删除登录菜单树里面的按钮 /// private void DeleteBtnFromMenuTree(List menuList) { if (menuList == null) return; for (var i = menuList.Count - 1; i >= 0; i--) { var menu = menuList[i]; if (menu.Type == MenuTypeEnum.Btn) menuList.Remove(menu); else if (menu.Children.Count > 0) DeleteBtnFromMenuTree(menu.Children); } } /// /// 获取菜单列表 /// /// [AllowAnonymous] [DisplayName("获取菜单列表")] public async Task> GetList([FromQuery] MenuInput input) { var menuIdList = _userManager.SuperAdmin ? new List() : await GetMenuIdList(); // 有筛选条件时返回list列表(防止构造不出树) if (!string.IsNullOrWhiteSpace(input.Title) || input.Type is > 0) { return await _sysMenuRep.AsQueryable() .WhereIF(!string.IsNullOrWhiteSpace(input.Title), u => u.Title.Contains(input.Title)) .WhereIF(input.Type is > 0, u => u.Type == input.Type) .WhereIF(menuIdList.Count > 1, u => menuIdList.Contains(u.Id)) .OrderBy(u => u.OrderNo).ToListAsync(); } return _userManager.SuperAdmin ? await _sysMenuRep.AsQueryable().OrderBy(u => u.OrderNo).ToTreeAsync(u => u.Children, u => u.Pid, 0) : await _sysMenuRep.AsQueryable() .OrderBy(u => u.OrderNo).ToTreeAsync(u => u.Children, u => u.Pid, 0, menuIdList.Select(d => (object)d).ToArray()); // 角色菜单授权时 } /// /// 增加菜单 /// /// /// [ApiDescriptionSettings(Name = "Add"), HttpPost] [DisplayName("增加菜单")] public async Task AddMenu(AddMenuInput input) { var isExist = input.Type != MenuTypeEnum.Btn ? await _sysMenuRep.IsAnyAsync(u => u.Title == input.Title) : await _sysMenuRep.IsAnyAsync(u => u.Permission == input.Permission); if (isExist) throw Oops.Oh(ErrorCodeEnum.D4000); // 校验菜单参数 var sysMenu = input.Adapt(); CheckMenuParam(sysMenu); await _sysMenuRep.InsertAsync(sysMenu); // 清除缓存 DeleteMenuCache(); } /// /// 更新菜单 /// /// /// [ApiDescriptionSettings(Name = "Update"), HttpPost] [DisplayName("更新菜单")] public async Task UpdateMenu(UpdateMenuInput input) { var isExist = input.Type != MenuTypeEnum.Btn ? await _sysMenuRep.IsAnyAsync(u => u.Title == input.Title && u.Type == input.Type && u.Id != input.Id) : await _sysMenuRep.IsAnyAsync(u => u.Permission == input.Permission && u.Type == input.Type && u.Id != input.Id); if (isExist) throw Oops.Oh(ErrorCodeEnum.D4000); // 校验菜单参数 var sysMenu = input.Adapt(); CheckMenuParam(sysMenu); await _sysMenuRep.AsUpdateable(sysMenu).ExecuteCommandAsync(); // 清除缓存 DeleteMenuCache(); } /// /// 删除菜单 /// /// /// [UnitOfWork] [ApiDescriptionSettings(Name = "Delete"), HttpPost] [DisplayName("删除菜单")] public async Task DeleteMenu(DeleteMenuInput input) { var menuTreeList = await _sysMenuRep.AsQueryable().ToChildListAsync(u => u.Pid, input.Id, true); var menuIdList = menuTreeList.Select(u => u.Id).ToList(); await _sysMenuRep.DeleteAsync(u => menuIdList.Contains(u.Id)); // 级联删除角色菜单数据 await _sysRoleMenuService.DeleteRoleMenuByMenuIdList(menuIdList); // 清除缓存 DeleteMenuCache(); } /// /// 增加和编辑时检查菜单数据 /// /// private static void CheckMenuParam(SysMenu menu) { var permission = menu.Permission; if (menu.Type == MenuTypeEnum.Btn) { menu.Name = null; menu.Path = null; menu.Component = null; menu.Icon = null; menu.Redirect = null; menu.OutLink = null; menu.IsHide = false; menu.IsKeepAlive = true; menu.IsAffix = false; menu.IsIframe = false; if (string.IsNullOrEmpty(permission)) throw Oops.Oh(ErrorCodeEnum.D4003); if (!permission.Contains(':')) throw Oops.Oh(ErrorCodeEnum.D4004); } else { menu.Permission = null; } } /// /// 获取用户拥有按钮权限集合(缓存) /// /// [DisplayName("获取按钮权限集合")] public async Task> GetOwnBtnPermList() { var userId = _userManager.UserId; var permissions = _sysCacheService.Get>(CacheConst.KeyPermission + userId); if (permissions == null || permissions.Count == 0) { var menuIdList = _userManager.SuperAdmin ? new List() : await GetMenuIdList(); permissions = await _sysMenuRep.AsQueryable() .Where(u => u.Type == MenuTypeEnum.Btn) .WhereIF(menuIdList.Count > 0, u => menuIdList.Contains(u.Id)) .Select(u => u.Permission).ToListAsync(); _sysCacheService.Set(CacheConst.KeyPermission + userId, permissions); } return permissions; } /// /// 获取系统所有按钮权限集合(缓存) /// /// [ApiDescriptionSettings(false)] public async Task> GetAllBtnPermList() { var permissions = _sysCacheService.Get>(CacheConst.KeyPermission + 0); if (permissions == null || permissions.Count == 0) { permissions = await _sysMenuRep.AsQueryable() .Where(u => u.Type == MenuTypeEnum.Btn) .Select(u => u.Permission).ToListAsync(); _sysCacheService.Set(CacheConst.KeyPermission + 0, permissions); } return permissions; } /// /// 清除菜单和按钮缓存 /// private void DeleteMenuCache() { _sysCacheService.RemoveByPrefixKey(CacheConst.KeyMenu); _sysCacheService.RemoveByPrefixKey(CacheConst.KeyPermission); } /// /// 获取当前用户菜单Id集合 /// /// private async Task> GetMenuIdList() { var roleIdList = await _sysUserRoleService.GetUserRoleIdList(_userManager.UserId); return await _sysRoleMenuService.GetRoleMenuIdList(roleIdList); } }