using System.Threading.Tasks;
using System.Security.Claims;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Admin.Core.Attributes;
using Admin.Core.Model.Output;
using Admin.Core.Service.Admin.Auth;
using Admin.Core.Service.Admin.Auth.Input;
using Admin.Core.Service.Admin.Auth.Output;
using Admin.Core.Common.Auth;
using System.Diagnostics;
using Admin.Core.Service.Admin.LoginLog.Input;
using Admin.Core.Service.Admin.LoginLog;
namespace Admin.Core.Controllers.Admin
{
///
/// 授权管理
///
public class AuthController : AreaController
{
private readonly IUserToken _userToken;
private readonly IAuthService _authService;
private readonly ILoginLogService _loginLogService;
public AuthController(
IUserToken userToken,
IAuthService authServices,
ILoginLogService loginLogService
)
{
_userToken = userToken;
_authService = authServices;
_loginLogService = loginLogService;
}
///
/// 获取验证码
///
/// 上次验证码键
///
[HttpGet]
[AllowAnonymous]
[NoOprationLog]
public async Task GetVerifyCode(string lastKey)
{
return await _authService.GetVerifyCodeAsync(lastKey);
}
///
/// 获取密钥
///
///
[HttpGet]
[AllowAnonymous]
[NoOprationLog]
public async Task GetPassWordEncryptKey()
{
return await _authService.GetPassWordEncryptKeyAsync();
}
///
/// 查询用户信息
///
///
[HttpGet]
[Login]
public async Task GetUserInfo()
{
return await _authService.GetUserInfoAsync();
}
///
/// 用户登录
/// 根据登录信息生成Token
///
/// 登录信息
///
[HttpPost]
[AllowAnonymous]
[NoOprationLog]
public async Task Login(AuthLoginInput input)
{
var sw = new Stopwatch();
sw.Start();
var res = (await _authService.LoginAsync(input)) as IResponseOutput;
sw.Stop();
#region 添加登录日志
var loginLogAddInput = new LoginLogAddInput()
{
CreatedUserName = input.UserName,
ElapsedMilliseconds = sw.ElapsedMilliseconds,
Status = res.Success,
Msg = res.Msg
};
AuthLoginOutput user = null;
if (res.Success)
{
user = (res as IResponseOutput).Data;
loginLogAddInput.CreatedUserId = user.Id;
loginLogAddInput.RealName = user.Name;
}
await _loginLogService.AddAsync(loginLogAddInput);
#endregion
if (!res.Success)
{
return res;
}
#region 生成token信息
var token = _userToken.Build(new[]
{
new Claim(ClaimAttributes.UserId, user.Id.ToString()),
new Claim(ClaimAttributes.UserName, user.UserName),
new Claim(ClaimAttributes.UserRealName, user.Name)
});
#endregion
return ResponseOutput.Ok(new { token });
}
}
}