using BaseService.Systems.UserRoleMenusManagement.Dto; using Microsoft.AspNetCore.Authorization; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; using Volo.Abp.Identity; namespace BaseService.Systems.UserMenusManagement { [Authorize] public class RoleMenusAppService : ApplicationService, IRoleMenusAppService { public IIdentityRoleRepository RoleRepository { get; } private readonly IRepository _menuRepository; private readonly IRepository _roleMenuRepository; public RoleMenusAppService( IIdentityRoleRepository roleRepository, IRepository menuRepository, IRepository roleMenuRepository) { RoleRepository = roleRepository; _menuRepository = menuRepository; _roleMenuRepository = roleMenuRepository; } [Authorize(IdentityPermissions.Roles.ManagePermissions)] public async Task Update(UpdateRoleMenuDto input) { var roleMenus = new List(); foreach (var menuId in input.MenuIds) { roleMenus.Add(new RoleMenu(input.RoleId, menuId)); } await _roleMenuRepository.DeleteAsync(_ => _.RoleId == input.RoleId); await _roleMenuRepository.InsertManyAsync(roleMenus); } [Authorize(IdentityPermissions.Roles.Default)] public async Task> GetMenusList() { var result = new List(); if (!CurrentTenant.Id.HasValue) result = await _menuRepository.GetListAsync(); else result = await _menuRepository.GetListAsync(_ => _.IsHost == false); var dtos = ObjectMapper.Map, List>(result); return new ListResultDto(dtos.OrderBy(_ => _.Sort).ToList()); } /// /// 获取当前角色菜单 /// /// public async Task> GetRoleMenus() { var roleIds = await (await RoleRepository.GetDbSetAsync()).Where(_ => CurrentUser.Roles.Contains(_.Name)).Select(_ => _.Id).ToListAsync(); var roleMenus = await (await _roleMenuRepository.GetQueryableAsync()).Where(_ => roleIds.Contains(_.RoleId)).Select(_ => _.MenuId).ToListAsync(); var menus = await _menuRepository.GetListAsync(_ => _.CategoryId == 1 && roleMenus.Contains(_.Id)); var root = menus.Where(_ => _.Pid == null).OrderBy(_ => _.Sort).ToList(); return new ListResultDto(LoadRoleMenusTree(root, menus)); } [Authorize(IdentityPermissions.Roles.Default)] public async Task> GetRoleMenuIds(Guid id) { var menuIds = await _roleMenuRepository.GetListAsync(_ => _.RoleId == id); var menus = await _menuRepository.GetListAsync(_ => menuIds.Select(m => m.MenuId).Contains(_.Id)); return new ListResultDto(menus.Select(_ => _.Id).ToList()); } private List LoadRoleMenusTree(List roots, List menus) { var result = new List(); foreach (var root in roots) { var menu = new RoleMenusDto { Path = root.Path, Name = root.Name, Component = root.Component, Meta = new MenuMeta { Icon = root.Icon, Title = root.Title ?? root.Name, IsAffix = root.IsAffix }, AlwaysShow = root.AlwaysShow, Hidden = root.Hidden }; if (menus.Where(_ => _.Pid == root.Id).Any()) { menu.Children = LoadRoleMenusTree(menus.Where(_ => _.Pid == root.Id).OrderBy(_ => _.Sort).ToList(), menus); } result.Add(menu); } return result; } } }