using System.Text;
using Attribute;
using Base;
using Common;
using Dto;
using Extensions;
using Infrastructure;
using Infrastructure.Model;
using Microsoft.AspNetCore.Identity;
using Model;
using Model.Base;
using Repository;
using Service;
using UAParser;
using Util;
using Vo;
namespace Services
{
///
/// 登录
///
[AppService(ServiceType = typeof(ISysLoginService), ServiceLifetime = LifeTime.Transient)]
public class SysLoginService : BaseService, ISysLoginService
{
private readonly ISysUserService SysUserService;
private readonly ISysOauthClientDetailsService SysOauthClientDetailsService;
private readonly IHttpContextAccessor httpContextAccessor;
public SysLoginService(ISysUserService sysUserService, ISysOauthClientDetailsService sysOauthClientDetailsService, IHttpContextAccessor httpContextAccessor)
{
SysUserService = sysUserService;
SysOauthClientDetailsService = sysOauthClientDetailsService;
this.httpContextAccessor = httpContextAccessor;
}
///
/// 验证登录接口
///
///
///
///
///
public bool CheckLogin(string scope, string grantType, string token)
{
token = Encoding.UTF8.GetString(Convert.FromBase64String(token));
string[] checkUserData = token.Split(':');
string clienId = checkUserData[0];
string clienSecret = checkUserData[1];
SysOauthClientDetails oauth = SysOauthClientDetailsService.GetFirst(m => m.ClientId == clienId && m.ClientSecret == clienSecret);
if(oauth != null)
{
if(oauth.Scope == scope && oauth.AuthorizedGrantTypes.EndsWith(grantType))
{
return true;
}
}
return false;
}
///
/// 登录成功返回数据
///
///
///
public OAuthVo GetLoginInfo(TokenModel loginUser)
{
OAuthVo info = new ();
info.access_token = JwtUtil.GenerateJwtToken(JwtUtil.AddClaims(loginUser));
info.refresh_token = Guid.NewGuid().ToString();
info.user_id = loginUser.userId;
info.username = loginUser.username;
JwtSettings jwtSettings = new();
AppSettings.Bind("JwtSettings", jwtSettings);
info.exp = DateTime.Now.AddMinutes(jwtSettings.Expire);
return info;
}
///
/// 登录验证
///
///
///
///
public SysUser Login(LoginBodyDto loginBody, SysLogininfor logininfor)
{
SysUser user = SysUserService.GetFirst(m => m.username == loginBody.username);
loginBody.password = Function.MD532(loginBody.password + user.salt);
user = SysUserService.Login(loginBody);
logininfor.UserName = loginBody.username;
logininfor.Status = "1";
logininfor.LoginTime = DateTime.Now;
logininfor.Ipaddr = loginBody.loginIP;
ClientInfo clientInfo = httpContextAccessor.HttpContext.GetClientInfo();
logininfor.Browser = clientInfo.ToString();
logininfor.Os = clientInfo.OS.ToString();
if (user == null || user.userId <= 0)
{
logininfor.Msg = "用户名或密码错误";
AddLoginInfo(logininfor);
throw new CustomException(ResultCode.LOGIN_ERROR, logininfor.Msg, false);
}
if (user.LockFlag == "9")
{
logininfor.Msg = "该用户已禁用";
AddLoginInfo(logininfor);
throw new CustomException(ResultCode.LOGIN_ERROR, logininfor.Msg, false);
}
logininfor.Status = "0";
logininfor.Msg = "登录成功";
AddLoginInfo(logininfor);
SysUserService.UpdateLoginInfo(loginBody.loginIP, user.userId);
return user;
}
///
/// 登录验证
///
///
///
///
///
public SysUser PhoneLogin(PhoneLoginDto loginBody, SysLogininfor logininfor, SysUser user)
{
logininfor.UserName = user.username;
logininfor.Status = "1";
logininfor.LoginTime = DateTime.Now;
logininfor.Ipaddr = loginBody.LoginIP;
ClientInfo clientInfo = httpContextAccessor.HttpContext.GetClientInfo();
logininfor.Browser = clientInfo.ToString();
logininfor.Os = clientInfo.OS.ToString();
if (user.LockFlag == "9")
{
logininfor.Msg = "该用户已禁用";
AddLoginInfo(logininfor);
throw new CustomException(ResultCode.LOGIN_ERROR, logininfor.Msg, false);
}
logininfor.Status = "0";
logininfor.Msg = "登录成功";
AddLoginInfo(logininfor);
SysUserService.UpdateLoginInfo(loginBody.LoginIP, user.userId);
return user;
}
///
/// 查询登录日志
///
///
/// 分页
///
public PagedInfo GetLoginLog(SysLogininfor logininfoDto, PagerInfo pager)
{
//logininfoDto.BeginTime = DateTimeHelper.GetBeginTime(logininfoDto.BeginTime, -1);
//logininfoDto.EndTime = DateTimeHelper.GetBeginTime(logininfoDto.EndTime, 1);
var exp = Expressionable.Create();
exp.AndIF(logininfoDto.BeginTime == null, it => it.LoginTime >= DateTime.Now.ToShortDateString().ParseToDateTime());
exp.AndIF(logininfoDto.BeginTime != null, it => it.LoginTime >= logininfoDto.BeginTime && it.LoginTime <= logininfoDto.EndTime);
exp.AndIF(logininfoDto.Ipaddr.IfNotEmpty(), f => f.Ipaddr == logininfoDto.Ipaddr);
exp.AndIF(logininfoDto.UserName.IfNotEmpty(), f => f.UserName.Contains(logininfoDto.UserName));
exp.AndIF(logininfoDto.Status.IfNotEmpty(), f => f.Status == logininfoDto.Status);
var query = Queryable().Where(exp.ToExpression())
.OrderBy(it => it.InfoId, OrderByType.Desc);
return query.ToPage(pager);
}
///
/// 记录登录日志
///
///
///
public void AddLoginInfo(SysLogininfor sysLogininfor)
{
// Insert(sysLogininfor);
}
///
/// 清空登录日志
///
public void TruncateLogininfo()
{
Truncate();
}
///
/// 删除登录日志
///
///
///
public int DeleteLogininforByIds(long[] ids)
{
return Delete(ids);
}
public void CheckLockUser(string userName)
{
var lockTimeStamp = CacheService.GetLockUser(userName);
var lockTime = DateTimeHelper.ToLocalTimeDateBySeconds(lockTimeStamp);
var ts = lockTime - DateTime.Now;
if (lockTimeStamp > 0 && ts.TotalSeconds > 0)
{
throw new CustomException(ResultCode.LOGIN_ERROR, $"你的账号已被锁,剩余{Math.Round(ts.TotalMinutes, 0)}分钟");
}
}
}
}