123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- 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
- {
- /// <summary>
- /// 登录
- /// </summary>
- [AppService(ServiceType = typeof(ISysLoginService), ServiceLifetime = LifeTime.Transient)]
- public class SysLoginService : BaseService<SysLogininfor>, 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;
- }
- /// <summary>
- /// 验证登录接口
- /// </summary>
- /// <param name="scope"></param>
- /// <param name="grantType"></param>
- /// <param name="token"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 登录成功返回数据
- /// </summary>
- /// <param name="loginUser"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 登录验证
- /// </summary>
- /// <param name="logininfor"></param>
- /// <param name="loginBody"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 登录验证
- /// </summary>
- /// <param name="logininfor"></param>
- /// <param name="loginBody"></param>
- /// <param name="user"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 查询登录日志
- /// </summary>
- /// <param name="logininfoDto"></param>
- /// <param name="pager">分页</param>
- /// <returns></returns>
- public PagedInfo<SysLogininfor> GetLoginLog(SysLogininfor logininfoDto, PagerInfo pager)
- {
- //logininfoDto.BeginTime = DateTimeHelper.GetBeginTime(logininfoDto.BeginTime, -1);
- //logininfoDto.EndTime = DateTimeHelper.GetBeginTime(logininfoDto.EndTime, 1);
- var exp = Expressionable.Create<SysLogininfor>();
- 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);
- }
- /// <summary>
- /// 记录登录日志
- /// </summary>
- /// <param name="sysLogininfor"></param>
- /// <returns></returns>
- public void AddLoginInfo(SysLogininfor sysLogininfor)
- {
- // Insert(sysLogininfor);
- }
- /// <summary>
- /// 清空登录日志
- /// </summary>
- public void TruncateLogininfo()
- {
- Truncate();
- }
- /// <summary>
- /// 删除登录日志
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- 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)}分钟");
- }
- }
- }
- }
|