using Azure.Core; 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; readonly Repository menuRelationDB; readonly Repository menuDB; public AdminController(Repository baseService, Repository menuRelationDB, Repository menuDB) : base(baseService) { this.baseService = baseService; this.menuRelationDB = menuRelationDB; this.menuDB = menuDB; } /// /// 管理员登录 /// /// /// [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.GetMD5()) Oh.Error("登录失败,密码错误"); // 获取租户信息 var buttonRole = admin.RoleId==1 ? ["*:*:*"] : await menuRelationDB.AsQueryable() .LeftJoin((mr, m) => mr.MenuId == m.Id) .Where((mr, m) => mr.RoleId == admin.RoleId) .Select((mr, m) => m) .Where(m =>m.IsButton) .Select(m=>m.Auths) .ToArrayAsync(); //获取 return new { //按钮权限 Permissions = buttonRole, //用户名 UserName = admin.Name, NickName = admin.Name, AccessToken = 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), ]) }; } public override Task Edit([FromBody] Admin model) { //创建用户时 密码加密 if (model.Id == 0) model.Password = model.Password.GetMD5(); return base.Edit(model); } } }