AdminController.cs 4.5 KB

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