SysLoginService.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Text;
  2. using Attribute;
  3. using Base;
  4. using Common;
  5. using Dto;
  6. using Extensions;
  7. using Infrastructure;
  8. using Infrastructure.Model;
  9. using Microsoft.AspNetCore.Identity;
  10. using Model;
  11. using Model.Base;
  12. using Repository;
  13. using Service;
  14. using UAParser;
  15. using Util;
  16. using Vo;
  17. namespace Services
  18. {
  19. /// <summary>
  20. /// 登录
  21. /// </summary>
  22. [AppService(ServiceType = typeof(ISysLoginService), ServiceLifetime = LifeTime.Transient)]
  23. public class SysLoginService : BaseService<SysLogininfor>, ISysLoginService
  24. {
  25. private readonly ISysUserService SysUserService;
  26. private readonly ISysOauthClientDetailsService SysOauthClientDetailsService;
  27. private readonly IHttpContextAccessor httpContextAccessor;
  28. public SysLoginService(ISysUserService sysUserService, ISysOauthClientDetailsService sysOauthClientDetailsService, IHttpContextAccessor httpContextAccessor)
  29. {
  30. SysUserService = sysUserService;
  31. SysOauthClientDetailsService = sysOauthClientDetailsService;
  32. this.httpContextAccessor = httpContextAccessor;
  33. }
  34. /// <summary>
  35. /// 验证登录接口
  36. /// </summary>
  37. /// <param name="scope"></param>
  38. /// <param name="grantType"></param>
  39. /// <param name="token"></param>
  40. /// <returns></returns>
  41. public bool CheckLogin(string scope, string grantType, string token)
  42. {
  43. token = Encoding.UTF8.GetString(Convert.FromBase64String(token));
  44. string[] checkUserData = token.Split(':');
  45. string clienId = checkUserData[0];
  46. string clienSecret = checkUserData[1];
  47. SysOauthClientDetails oauth = SysOauthClientDetailsService.GetFirst(m => m.ClientId == clienId && m.ClientSecret == clienSecret);
  48. if(oauth != null)
  49. {
  50. if(oauth.Scope == scope && oauth.AuthorizedGrantTypes.EndsWith(grantType))
  51. {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. /// <summary>
  58. /// 登录成功返回数据
  59. /// </summary>
  60. /// <param name="loginUser"></param>
  61. /// <returns></returns>
  62. public OAuthVo GetLoginInfo(TokenModel loginUser)
  63. {
  64. OAuthVo info = new ();
  65. info.access_token = JwtUtil.GenerateJwtToken(JwtUtil.AddClaims(loginUser));
  66. info.refresh_token = Guid.NewGuid().ToString();
  67. info.user_id = loginUser.userId;
  68. info.username = loginUser.username;
  69. JwtSettings jwtSettings = new();
  70. AppSettings.Bind("JwtSettings", jwtSettings);
  71. info.exp = DateTime.Now.AddMinutes(jwtSettings.Expire);
  72. return info;
  73. }
  74. /// <summary>
  75. /// 登录验证
  76. /// </summary>
  77. /// <param name="logininfor"></param>
  78. /// <param name="loginBody"></param>
  79. /// <returns></returns>
  80. public SysUser Login(LoginBodyDto loginBody, SysLogininfor logininfor)
  81. {
  82. SysUser user = SysUserService.GetFirst(m => m.username == loginBody.username);
  83. loginBody.password = Function.MD532(loginBody.password + user.salt);
  84. user = SysUserService.Login(loginBody);
  85. logininfor.UserName = loginBody.username;
  86. logininfor.Status = "1";
  87. logininfor.LoginTime = DateTime.Now;
  88. logininfor.Ipaddr = loginBody.loginIP;
  89. ClientInfo clientInfo = httpContextAccessor.HttpContext.GetClientInfo();
  90. logininfor.Browser = clientInfo.ToString();
  91. logininfor.Os = clientInfo.OS.ToString();
  92. if (user == null || user.userId <= 0)
  93. {
  94. logininfor.Msg = "用户名或密码错误";
  95. AddLoginInfo(logininfor);
  96. throw new CustomException(ResultCode.LOGIN_ERROR, logininfor.Msg, false);
  97. }
  98. if (user.LockFlag == "9")
  99. {
  100. logininfor.Msg = "该用户已禁用";
  101. AddLoginInfo(logininfor);
  102. throw new CustomException(ResultCode.LOGIN_ERROR, logininfor.Msg, false);
  103. }
  104. logininfor.Status = "0";
  105. logininfor.Msg = "登录成功";
  106. AddLoginInfo(logininfor);
  107. SysUserService.UpdateLoginInfo(loginBody.loginIP, user.userId);
  108. return user;
  109. }
  110. /// <summary>
  111. /// 登录验证
  112. /// </summary>
  113. /// <param name="logininfor"></param>
  114. /// <param name="loginBody"></param>
  115. /// <param name="user"></param>
  116. /// <returns></returns>
  117. public SysUser PhoneLogin(PhoneLoginDto loginBody, SysLogininfor logininfor, SysUser user)
  118. {
  119. logininfor.UserName = user.username;
  120. logininfor.Status = "1";
  121. logininfor.LoginTime = DateTime.Now;
  122. logininfor.Ipaddr = loginBody.LoginIP;
  123. ClientInfo clientInfo = httpContextAccessor.HttpContext.GetClientInfo();
  124. logininfor.Browser = clientInfo.ToString();
  125. logininfor.Os = clientInfo.OS.ToString();
  126. if (user.LockFlag == "9")
  127. {
  128. logininfor.Msg = "该用户已禁用";
  129. AddLoginInfo(logininfor);
  130. throw new CustomException(ResultCode.LOGIN_ERROR, logininfor.Msg, false);
  131. }
  132. logininfor.Status = "0";
  133. logininfor.Msg = "登录成功";
  134. AddLoginInfo(logininfor);
  135. SysUserService.UpdateLoginInfo(loginBody.LoginIP, user.userId);
  136. return user;
  137. }
  138. /// <summary>
  139. /// 查询登录日志
  140. /// </summary>
  141. /// <param name="logininfoDto"></param>
  142. /// <param name="pager">分页</param>
  143. /// <returns></returns>
  144. public PagedInfo<SysLogininfor> GetLoginLog(SysLogininfor logininfoDto, PagerInfo pager)
  145. {
  146. //logininfoDto.BeginTime = DateTimeHelper.GetBeginTime(logininfoDto.BeginTime, -1);
  147. //logininfoDto.EndTime = DateTimeHelper.GetBeginTime(logininfoDto.EndTime, 1);
  148. var exp = Expressionable.Create<SysLogininfor>();
  149. exp.AndIF(logininfoDto.BeginTime == null, it => it.LoginTime >= DateTime.Now.ToShortDateString().ParseToDateTime());
  150. exp.AndIF(logininfoDto.BeginTime != null, it => it.LoginTime >= logininfoDto.BeginTime && it.LoginTime <= logininfoDto.EndTime);
  151. exp.AndIF(logininfoDto.Ipaddr.IfNotEmpty(), f => f.Ipaddr == logininfoDto.Ipaddr);
  152. exp.AndIF(logininfoDto.UserName.IfNotEmpty(), f => f.UserName.Contains(logininfoDto.UserName));
  153. exp.AndIF(logininfoDto.Status.IfNotEmpty(), f => f.Status == logininfoDto.Status);
  154. var query = Queryable().Where(exp.ToExpression())
  155. .OrderBy(it => it.InfoId, OrderByType.Desc);
  156. return query.ToPage(pager);
  157. }
  158. /// <summary>
  159. /// 记录登录日志
  160. /// </summary>
  161. /// <param name="sysLogininfor"></param>
  162. /// <returns></returns>
  163. public void AddLoginInfo(SysLogininfor sysLogininfor)
  164. {
  165. // Insert(sysLogininfor);
  166. }
  167. /// <summary>
  168. /// 清空登录日志
  169. /// </summary>
  170. public void TruncateLogininfo()
  171. {
  172. Truncate();
  173. }
  174. /// <summary>
  175. /// 删除登录日志
  176. /// </summary>
  177. /// <param name="ids"></param>
  178. /// <returns></returns>
  179. public int DeleteLogininforByIds(long[] ids)
  180. {
  181. return Delete(ids);
  182. }
  183. public void CheckLockUser(string userName)
  184. {
  185. var lockTimeStamp = CacheService.GetLockUser(userName);
  186. var lockTime = DateTimeHelper.ToLocalTimeDateBySeconds(lockTimeStamp);
  187. var ts = lockTime - DateTime.Now;
  188. if (lockTimeStamp > 0 && ts.TotalSeconds > 0)
  189. {
  190. throw new CustomException(ResultCode.LOGIN_ERROR, $"你的账号已被锁,剩余{Math.Round(ts.TotalMinutes, 0)}分钟");
  191. }
  192. }
  193. }
  194. }