AdminController.cs 5.0 KB

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