You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.9 KiB

2 years ago
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Shentun.Peis.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Volo.Abp.Application.Services;
  10. using Volo.Abp.Domain.Repositories;
  11. namespace Shentun.Peis.RoleMenuInfos
  12. {
  13. /// <summary>
  14. /// 角色对菜单的权限控制
  15. /// </summary>
  16. [ApiExplorerSettings(GroupName = "Work")]
  17. [Authorize]
  18. public class RoleMenuInfoAppService : ApplicationService
  19. {
  20. private readonly IRepository<RoleMenuInfo> _roleMenuInfoRepository;
  21. private readonly IRepository<MenuInfo> _menuInfoRepository;
  22. public RoleMenuInfoAppService(
  23. IRepository<RoleMenuInfo> roleMenuInfoRepository,
  24. IRepository<MenuInfo> menuInfoRepository)
  25. {
  26. this._roleMenuInfoRepository = roleMenuInfoRepository;
  27. this._menuInfoRepository = menuInfoRepository;
  28. }
  29. /// <summary>
  30. /// 获取角色对应的菜单选项
  31. /// </summary>
  32. /// <param name="RoleId">角色ID</param>
  33. /// <returns></returns>
  34. [HttpGet("api/app/rolemenuinfo/getrolemenuinfolist")]
  35. public async Task<List<GetRoleMenuInfoIdsDto>> GetRoleMenuInfoListAsync(Guid RoleId)
  36. {
  37. var query = from a in await _roleMenuInfoRepository.GetQueryableAsync()
  38. join b in await _menuInfoRepository.GetQueryableAsync() on a.MenuInfoId equals b.Id into bb
  39. from ab in bb.DefaultIfEmpty()
  40. where a.RoleId == RoleId
  41. orderby ab.DisplayOrder ascending
  42. select ab;
  43. var entlistdto = query.Select(s => new GetRoleMenuInfoIdsDto
  44. {
  45. DisplayName = s.DisplayName,
  46. MenuInfoId = s.Id,
  47. ParentId = s.ParentId
  48. }).ToList();
  49. return entlistdto;
  50. }
  51. /// <summary>
  52. /// 设置角色的菜单权限
  53. /// </summary>
  54. /// <param name="input"></param>
  55. /// <returns></returns>
  56. [HttpPost("api/app/rolemenuinfo/setrolemenuinfo")]
  57. public async Task SetRoleMenuInfoAsync(SetRoleMenuInfoRequestDto input)
  58. {
  59. //删除原有配置
  60. await _roleMenuInfoRepository.DeleteAsync(m => m.RoleId == input.RoleId);
  61. List<RoleMenuInfo> roleMenuInfoList = new List<RoleMenuInfo>();
  62. if (input.MenuInfoIds.Any())
  63. {
  64. //增加新配置
  65. foreach (var item in input.MenuInfoIds)
  66. {
  67. roleMenuInfoList.Add(new RoleMenuInfo
  68. {
  69. RoleId = input.RoleId,
  70. MenuInfoId = item
  71. });
  72. }
  73. await _roleMenuInfoRepository.InsertManyAsync(roleMenuInfoList);
  74. }
  75. }
  76. }
  77. }