// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
using Furion.SpecificationDocument;
using Lazy.Captcha.Core;
namespace Admin.NET.Core.Service;
///
/// 系统登录授权服务
///
[ApiDescriptionSettings(Order = 500)]
public class SysAuthService : IDynamicApiController, ITransient
{
private readonly UserManager _userManager;
private readonly SqlSugarRepository _sysUserRep;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly SysMenuService _sysMenuService;
private readonly SysOnlineUserService _sysOnlineUserService;
private readonly SysConfigService _sysConfigService;
private readonly ICaptcha _captcha;
private readonly SysCacheService _sysCacheService;
public SysAuthService(UserManager userManager,
SqlSugarRepository sysUserRep,
IHttpContextAccessor httpContextAccessor,
SysMenuService sysMenuService,
SysOnlineUserService sysOnlineUserService,
SysConfigService sysConfigService,
ICaptcha captcha,
SysCacheService sysCacheService)
{
_userManager = userManager;
_sysUserRep = sysUserRep;
_httpContextAccessor = httpContextAccessor;
_sysMenuService = sysMenuService;
_sysOnlineUserService = sysOnlineUserService;
_sysConfigService = sysConfigService;
_captcha = captcha;
_sysCacheService = sysCacheService;
}
///
/// 账号密码登录
///
///
/// 用户名/密码:superadmin/123456
///
[AllowAnonymous]
[DisplayName("账号密码登录")]
public async Task Login([Required] LoginInput input)
{
//// 可以根据域名获取具体租户
//var host = _httpContextAccessor.HttpContext.Request.Host;
// 判断密码错误次数(默认5次,缓存30分钟)
var keyErrorPasswordCount = $"{CacheConst.KeyErrorPasswordCount}{input.Account}";
var errorPasswordCount = _sysCacheService.Get(keyErrorPasswordCount);
if (errorPasswordCount >= 5)
throw Oops.Oh(ErrorCodeEnum.D1027);
// 是否开启验证码
if (await _sysConfigService.GetConfigValue(CommonConst.SysCaptcha))
{
// 判断验证码
if (!_captcha.Validate(input.CodeId.ToString(), input.Code))
throw Oops.Oh(ErrorCodeEnum.D0008);
}
// 账号是否存在
var user = await _sysUserRep.AsQueryable().Includes(t => t.SysOrg).ClearFilter().FirstAsync(u => u.Account.Equals(input.Account));
_ = user ?? throw Oops.Oh(ErrorCodeEnum.D0009);
// 账号是否被冻结
if (user.Status == StatusEnum.Disable)
throw Oops.Oh(ErrorCodeEnum.D1017);
// 租户是否被禁用
var tenant = await _sysUserRep.ChangeRepository>().GetFirstAsync(u => u.Id == user.TenantId);
if (tenant != null && tenant.Status == StatusEnum.Disable)
throw Oops.Oh(ErrorCodeEnum.Z1003);
// 国密SM2解密(前端密码传输SM2加密后的)
input.Password = CryptogramUtil.SM2Decrypt(input.Password);
// 密码是否正确
if (CryptogramUtil.CryptoType == CryptogramEnum.MD5.ToString())
{
if (!user.Password.Equals(MD5Encryption.Encrypt(input.Password)))
{
_sysCacheService.Set(keyErrorPasswordCount, ++errorPasswordCount, TimeSpan.FromMinutes(30));
throw Oops.Oh(ErrorCodeEnum.D1000);
}
}
else
{
if (!CryptogramUtil.Decrypt(user.Password).Equals(input.Password))
{
_sysCacheService.Set(keyErrorPasswordCount, ++errorPasswordCount, TimeSpan.FromMinutes(30));
throw Oops.Oh(ErrorCodeEnum.D1000);
}
}
// 登录成功则清空密码错误次数
_sysCacheService.Remove(keyErrorPasswordCount);
return await CreateToken(user);
}
///
/// 验证锁屏密码
///
///
///
[DisplayName("验证锁屏密码")]
public async Task UnLockScreen([Required, FromQuery] string password)
{
// 账号是否存在
var user = await _sysUserRep.GetFirstAsync(u => u.Id == _userManager.UserId);
_ = user ?? throw Oops.Oh(ErrorCodeEnum.D0009);
// 国密SM2解密(前端密码传输SM2加密后的)
password = CryptogramUtil.SM2Decrypt(password);
// 密码是否正确
if (CryptogramUtil.CryptoType == CryptogramEnum.MD5.ToString())
{
if (!user.Password.Equals(MD5Encryption.Encrypt(password)))
throw Oops.Oh(ErrorCodeEnum.D1000);
}
else
{
if (!CryptogramUtil.Decrypt(user.Password).Equals(password))
throw Oops.Oh(ErrorCodeEnum.D1000);
}
return true;
}
///
/// 手机号登录
///
///
///
[AllowAnonymous]
[DisplayName("手机号登录")]
public async Task LoginPhone([Required] LoginPhoneInput input)
{
var verifyCode = _sysCacheService.Get($"{CacheConst.KeyPhoneVerCode}{input.Phone}");
if (string.IsNullOrWhiteSpace(verifyCode))
throw Oops.Oh("验证码不存在或已失效,请重新获取!");
if (verifyCode != input.Code)
throw Oops.Oh("验证码错误!");
// 账号是否存在
var user = await _sysUserRep.AsQueryable().Includes(t => t.SysOrg).ClearFilter().FirstAsync(u => u.Phone.Equals(input.Phone));
_ = user ?? throw Oops.Oh(ErrorCodeEnum.D0009);
return await CreateToken(user);
}
///
/// 生成Token令牌
///
///
///
[NonAction]
public async Task CreateToken(SysUser user)
{
// 单用户登录
await _sysOnlineUserService.SingleLogin(user.Id);
// 生成Token令牌
var tokenExpire = await _sysConfigService.GetTokenExpire();
var accessToken = JWTEncryption.Encrypt(new Dictionary
{
{ ClaimConst.UserId, user.Id },
{ ClaimConst.TenantId, user.TenantId },
{ ClaimConst.Account, user.Account },
{ ClaimConst.RealName, user.RealName },
{ ClaimConst.AccountType, user.AccountType },
{ ClaimConst.OrgId, user.OrgId },
{ ClaimConst.OrgName, user.SysOrg?.Name },
{ ClaimConst.OrgType, user.SysOrg?.Type },
}, tokenExpire);
// 生成刷新Token令牌
var refreshTokenExpire = await _sysConfigService.GetRefreshTokenExpire();
var refreshToken = JWTEncryption.GenerateRefreshToken(accessToken, refreshTokenExpire);
// 设置响应报文头
_httpContextAccessor.HttpContext.SetTokensOfResponseHeaders(accessToken, refreshToken);
// Swagger Knife4UI-AfterScript登录脚本
// ke.global.setAllHeader('Authorization', 'Bearer ' + ke.response.headers['access-token']);
return new LoginOutput
{
AccessToken = accessToken,
RefreshToken = refreshToken
};
}
///
/// 获取登录账号
///
///
[DisplayName("获取登录账号")]
public async Task GetUserInfo()
{
var user = await _sysUserRep.GetFirstAsync(u => u.Id == _userManager.UserId) ?? throw Oops.Oh(ErrorCodeEnum.D1011).StatusCode(401);
// 获取机构
var org = await _sysUserRep.ChangeRepository>().GetFirstAsync(u => u.Id == user.OrgId);
// 获取职位
var pos = await _sysUserRep.ChangeRepository>().GetFirstAsync(u => u.Id == user.PosId);
// 获取拥有按钮权限集合
var buttons = await _sysMenuService.GetOwnBtnPermList();
// 获取权限集合
var roleIds = await _sysUserRep.ChangeRepository>().AsQueryable()
.Where(u => u.UserId == user.Id).Select(u => u.RoleId).ToListAsync();
return new LoginUserOutput
{
Id = user.Id,
Account = user.Account,
RealName = user.RealName,
Phone = user.Phone,
IdCardNum = user.IdCardNum,
Email = user.Email,
AccountType = user.AccountType,
Avatar = user.Avatar,
Address = user.Address,
Signature = user.Signature,
OrgId = user.OrgId,
OrgName = org?.Name,
OrgType = org?.Type,
PosName = pos?.Name,
Buttons = buttons,
RoleIds = roleIds
};
}
///
/// 获取刷新Token
///
///
///
[DisplayName("获取刷新Token")]
public string GetRefreshToken([FromQuery] string accessToken)
{
var refreshTokenExpire = _sysConfigService.GetRefreshTokenExpire().GetAwaiter().GetResult();
return JWTEncryption.GenerateRefreshToken(accessToken, refreshTokenExpire);
}
///
/// 退出系统
///
[DisplayName("退出系统")]
public void Logout()
{
if (string.IsNullOrWhiteSpace(_userManager.Account))
throw Oops.Oh(ErrorCodeEnum.D1011);
_httpContextAccessor.HttpContext.SignoutToSwagger();
}
///
/// 获取登录配置
///
///
[AllowAnonymous]
[SuppressMonitor]
[DisplayName("获取登录配置")]
public async Task GetLoginConfig()
{
var secondVerEnabled = await _sysConfigService.GetConfigValue(CommonConst.SysSecondVer);
var captchaEnabled = await _sysConfigService.GetConfigValue(CommonConst.SysCaptcha);
return new { SecondVerEnabled = secondVerEnabled, CaptchaEnabled = captchaEnabled };
}
///
/// 获取水印配置
///
///
[SuppressMonitor]
[DisplayName("获取水印配置")]
public async Task GetWatermarkConfig()
{
var watermarkEnabled = await _sysConfigService.GetConfigValue(CommonConst.SysWatermark);
return new { WatermarkEnabled = watermarkEnabled };
}
///
/// 获取验证码
///
///
[AllowAnonymous]
[SuppressMonitor]
[DisplayName("获取验证码")]
public dynamic GetCaptcha()
{
var codeId = YitIdHelper.NextId().ToString();
var captcha = _captcha.Generate(codeId);
return new { Id = codeId, Img = captcha.Base64 };
}
///
/// Swagger登录检查
///
///
[AllowAnonymous]
[HttpPost("/api/swagger/checkUrl"), NonUnify]
[DisplayName("Swagger登录检查")]
public int SwaggerCheckUrl()
{
return _httpContextAccessor.HttpContext.User.Identity.IsAuthenticated ? 200 : 401;
}
///
/// Swagger登录提交
///
///
///
[AllowAnonymous]
[HttpPost("/api/swagger/submitUrl"), NonUnify]
[DisplayName("Swagger登录提交")]
public async Task SwaggerSubmitUrl([FromForm] SpecificationAuth auth)
{
try
{
_sysCacheService.Set(CommonConst.SysCaptcha, false);
await Login(new LoginInput
{
Account = auth.UserName,
Password = CryptogramUtil.SM2Encrypt(auth.Password),
});
_sysCacheService.Remove(CommonConst.SysCaptcha);
return 200;
}
catch (Exception)
{
return 401;
}
}
}