using Learn.Archives.API.Controllers.Dto; using Learn.Archives.API.Expand; using Learn.Archives.Core.Common; using Learn.Archives.Core.Model; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using System.Security.Claims; namespace Learn.Archives.API.Controllers { public class AdminController : BackController { readonly Repository baseService; public AdminController(Repository baseService) : base(baseService) { this.baseService = baseService; } /// /// 后台管理员登录 /// /// /// [HttpPost, AllowAnonymous] [HttpLogEnable] public async Task Login([FromBody] AdminLoginReq model) { if (string.IsNullOrWhiteSpace(model.Account)) Oh.Error("登录失败,用户名不能为空"); if (string.IsNullOrWhiteSpace(model.Password)) Oh.Error("登录失败,密码不能为空"); var admin = await baseService.GetFirstAsync(x => x.Account == model.Account); if (admin == null) Oh.Error("登录失败,用户不存在!"); if (!admin!.Enable) Oh.Error("登录失败,用户已锁定!"); if (admin.Password != model.Password) Oh.Error("登录失败,密码错误"); // 获取租户信息 //获取 return JwtHelper.GetToken(AppCommon.Config.AuthKey, [ new Claim(ClaimEnum.Role,admin.RoleId.ToString()), new Claim(ClaimEnum.Id, admin.Id.ToString()), new Claim(ClaimEnum.Name, admin.Name), ]); } } }