Learn.Archives/Learn.Archives.API/Controllers/AdminController.cs

53 lines
1.8 KiB
C#

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<Admin>
{
readonly Repository<Admin> baseService;
public AdminController(Repository<Admin> baseService) : base(baseService)
{
this.baseService = baseService;
}
/// <summary>
/// 后台管理员登录
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost, AllowAnonymous]
[HttpLogEnable]
public async Task<string> 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),
]);
}
}
}