AdminController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Security.Claims;
  2. using System.Threading.Tasks;
  3. using GxPress.Api.Tools;
  4. using GxPress.Auth;
  5. using GxPress.Common.Page;
  6. using GxPress.Entity;
  7. using GxPress.EnumConst;
  8. using GxPress.Repository.Interface;
  9. using GxPress.Request.Admin;
  10. using GxPress.Result.Admin;
  11. using GxPress.Service.Interface.Role;
  12. using Microsoft.AspNetCore.Authorization;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Microsoft.Extensions.Logging;
  15. using Microsoft.Extensions.Options;
  16. namespace GxPress.Api.AdminControllers
  17. {
  18. /// <summary>
  19. /// 管理员
  20. /// </summary>
  21. [Route("api/admin/admin")]
  22. [ApiController]
  23. [Authorize(Roles = Roles.Admin)]
  24. public class AdminController : ControllerBase
  25. {
  26. private readonly JwtOptions _jwtOptions;
  27. private readonly ILogger<AdminController> _logger;
  28. private readonly IAdminRepository _adminRepository;
  29. private readonly IRoleService roleService;
  30. public AdminController(IOptions<JwtOptions> jwtOptions, ILogger<AdminController> logger, IAdminRepository adminRepository, IRoleService roleService)
  31. {
  32. _jwtOptions = jwtOptions.Value;
  33. _logger = logger;
  34. _adminRepository = adminRepository;
  35. this.roleService = roleService;
  36. }
  37. /// <summary>
  38. /// 登录
  39. /// </summary>
  40. /// <param name="request"></param>
  41. /// <returns></returns>
  42. [HttpPost("signin")]
  43. [AllowAnonymous]
  44. public async Task<AdminSignInResult> SignIn(AdminSignInRequest request)
  45. {
  46. var result = await _adminRepository.SignInAsync(request);
  47. //获取菜单
  48. result.MenuInResult = await roleService.GetMenusAllAsync(result.RoleId);
  49. var claims = new[]
  50. {
  51. new Claim(ClaimTypes.NameIdentifier,result.AdminId.ToString()),
  52. new Claim(ClaimTypes.Role,AccountTypeConst.Admin.ToString()),
  53. new Claim(ClaimTypes.Actor,result.Name)
  54. };
  55. result.Token = TokenHelper.BuildToken(_jwtOptions, claims);
  56. _logger.LogInformation("admin token:{token}", result.Token);
  57. return result;
  58. }
  59. /// <summary>
  60. /// 注册
  61. /// </summary>
  62. /// <param name="request"></param>
  63. /// <returns></returns>
  64. [HttpPost("signup")]
  65. public async Task<int> SignUp([FromBody] AdminSignUpRequest request)
  66. {
  67. return await _adminRepository.SignUpAsync(request);
  68. }
  69. /// <summary>
  70. /// 删除
  71. /// </summary>
  72. /// <param name="id"></param>
  73. /// <returns></returns>
  74. [HttpDelete("{id}")]
  75. public async Task<bool> Delete(int id)
  76. {
  77. return await _adminRepository.DeleteAsync(id);
  78. }
  79. /// <summary>
  80. /// 详情
  81. /// </summary>
  82. /// <param name="id"></param>
  83. /// <returns></returns>
  84. [HttpGet("{id}")]
  85. public async Task<AdminDetailResult> Get(int id)
  86. {
  87. return await _adminRepository.GetDetailAsync(id);
  88. }
  89. /// <summary>
  90. /// 修改
  91. /// </summary>
  92. /// <param name="id"></param>
  93. /// <param name="request"></param>
  94. /// <returns></returns>
  95. [HttpPut("{id}")]
  96. public async Task<bool> Update(int id, [FromBody] Admin request)
  97. {
  98. return await _adminRepository.UpdateAsync(id, request);
  99. }
  100. /// <summary>
  101. /// 修改
  102. /// </summary>
  103. /// <param name="request"></param>
  104. /// <returns></returns>
  105. [HttpPut("updatemenu")]
  106. public async Task<bool> UpdateMenu([FromBody] Admin request)
  107. {
  108. return await _adminRepository.UpdateQAsync(request);
  109. }
  110. /// <summary>
  111. /// 分页列表
  112. /// </summary>
  113. /// <param name="request"></param>
  114. /// <returns></returns>
  115. [HttpPost("page")]
  116. public async Task<PagedList<AdminPageResult>> GetPagedList([FromBody] AdminPageSearchRequest request)
  117. {
  118. var result = await _adminRepository.GetPagedList(request);
  119. foreach (var item in result.Items)
  120. {
  121. //获取菜单
  122. item.MenuInResult = await roleService.GetMenusAllAsync(item.RoleId);
  123. }
  124. return result;
  125. }
  126. /// <summary>
  127. /// 修改密码
  128. /// </summary>
  129. /// <param name="request"></param>
  130. /// <returns></returns>
  131. [HttpPut("updatepassword")]
  132. public async Task<bool> UpdatePassWord([FromBody] AdminUpdatePassWordRequest request)
  133. {
  134. return await _adminRepository.UpdatePassWord(request);
  135. }
  136. /// <summary>
  137. /// 修改密码
  138. /// </summary>
  139. /// <param name="request"></param>
  140. /// <returns></returns>
  141. [HttpPut("admin-update-password")]
  142. public async Task<bool> AdminUpdatePassWord([FromBody] AdminUpdatePassWordRequest request)
  143. {
  144. return await _adminRepository.AdminUpdatePassWord(request);
  145. }
  146. }
  147. }