using System.Security.Claims; using System.Threading.Tasks; using GxPress.Api.Tools; using GxPress.Auth; using GxPress.Common.Page; using GxPress.Entity; using GxPress.EnumConst; using GxPress.Repository.Interface; using GxPress.Request.Admin; using GxPress.Result.Admin; using GxPress.Service.Interface.Role; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace GxPress.Api.AdminControllers { /// /// 管理员 /// [Route("api/admin/admin")] [ApiController] [Authorize(Roles = Roles.Admin)] public class AdminController : ControllerBase { private readonly JwtOptions _jwtOptions; private readonly ILogger _logger; private readonly IAdminRepository _adminRepository; private readonly IRoleService roleService; private readonly IHttpContextAccessor httpContextAccessor; public AdminController(IOptions jwtOptions, ILogger logger, IAdminRepository adminRepository, IRoleService roleService, IHttpContextAccessor httpContextAccessor) { _jwtOptions = jwtOptions.Value; _logger = logger; _adminRepository = adminRepository; this.roleService = roleService; this.httpContextAccessor=httpContextAccessor; } /// /// 登录 /// /// /// [HttpPost("signin")] [AllowAnonymous] public async Task SignIn(AdminSignInRequest request) { request.Ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString(); var result = await _adminRepository.SignInAsync(request); //获取菜单 result.MenuInResult = await roleService.GetMenusAllAsync(result.RoleId); var claims = new[] { new Claim(ClaimTypes.NameIdentifier,result.AdminId.ToString()), new Claim(ClaimTypes.Role,AccountTypeConst.Admin.ToString()), new Claim(ClaimTypes.Actor,result.Name) }; result.Token = TokenHelper.BuildToken(_jwtOptions, claims); _logger.LogInformation("admin token:{token}", result.Token); return result; } /// /// 注册 /// /// /// [HttpPost("signup")] public async Task SignUp([FromBody] AdminSignUpRequest request) { return await _adminRepository.SignUpAsync(request); } /// /// 删除 /// /// /// [HttpDelete("{id}")] public async Task Delete(int id) { return await _adminRepository.DeleteAsync(id); } /// /// 详情 /// /// /// [HttpGet("{id}")] public async Task Get(int id) { return await _adminRepository.GetDetailAsync(id); } /// /// 修改 /// /// /// /// [HttpPut("{id}")] public async Task Update(int id, [FromBody] Admin request) { return await _adminRepository.UpdateAsync(id, request); } /// /// 分页列表 /// /// /// [HttpPost("page")] public async Task> GetPagedList([FromBody] AdminPageSearchRequest request) { var result = await _adminRepository.GetPagedList(request); foreach (var item in result.Items) { //获取菜单 item.MenuInResult = await roleService.GetMenusAllAsync(item.RoleId); } return result; } /// /// 修改密码 /// /// /// [HttpPut("updatepassword")] public async Task UpdatePassWord([FromBody] AdminUpdatePassWordRequest request) { return await _adminRepository.UpdatePassWord(request); } /// /// 修改密码 /// /// /// [HttpPut("admin-update-password")] public async Task AdminUpdatePassWord([FromBody] AdminUpdatePassWordRequest request) { return await _adminRepository.AdminUpdatePassWord(request); } } }