1
0

AuthService.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using Admin.Core.Common.Cache;
  2. using Admin.Core.Common.Configs;
  3. using Admin.Core.Common.Helpers;
  4. using Admin.Core.Common.Output;
  5. using Admin.Core.Model.Admin;
  6. using Admin.Core.Repository.Admin;
  7. using Admin.Core.Service.Admin.Auth.Input;
  8. using Admin.Core.Service.Admin.Auth.Output;
  9. using Admin.Tools.Captcha;
  10. using System;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. namespace Admin.Core.Service.Admin.Auth
  14. {
  15. public class AuthService : BaseService, IAuthService
  16. {
  17. private readonly AppConfig _appConfig;
  18. private readonly IPermissionRepository _permissionRepository;
  19. private readonly IUserRepository _userRepository;
  20. private readonly VerifyCodeHelper _verifyCodeHelper;
  21. private readonly ITenantRepository _tenantRepository;
  22. private readonly ICaptcha _captcha;
  23. public AuthService(
  24. AppConfig appConfig,
  25. VerifyCodeHelper verifyCodeHelper,
  26. IUserRepository userRepository,
  27. IPermissionRepository permissionRepository,
  28. ITenantRepository tenantRepository,
  29. ICaptcha captcha
  30. )
  31. {
  32. _appConfig = appConfig;
  33. _verifyCodeHelper = verifyCodeHelper;
  34. _userRepository = userRepository;
  35. _permissionRepository = permissionRepository;
  36. _tenantRepository = tenantRepository;
  37. _captcha = captcha;
  38. }
  39. public async Task<IResponseOutput> GetPassWordEncryptKeyAsync()
  40. {
  41. //写入Redis
  42. var guid = Guid.NewGuid().ToString("N");
  43. var key = string.Format(CacheKey.PassWordEncryptKey, guid);
  44. var encyptKey = StringHelper.GenerateRandom(8);
  45. await Cache.SetAsync(key, encyptKey, TimeSpan.FromMinutes(5));
  46. var data = new { key = guid, encyptKey };
  47. return ResponseOutput.Ok(data);
  48. }
  49. public async Task<IResponseOutput> GetUserInfoAsync()
  50. {
  51. if (!(User?.Id > 0))
  52. {
  53. return ResponseOutput.NotOk("未登录!");
  54. }
  55. var authUserInfoOutput = new AuthUserInfoOutput { };
  56. //用户信息
  57. authUserInfoOutput.User = await _userRepository.GetAsync<AuthUserProfileDto>(User.Id);
  58. //用户菜单
  59. authUserInfoOutput.Menus = await _permissionRepository.Select
  60. .Where(a => new[] { PermissionType.Group, PermissionType.Menu }.Contains(a.Type))
  61. .Where(a =>
  62. _permissionRepository.Orm.Select<RolePermissionEntity>()
  63. .InnerJoin<UserRoleEntity>((b, c) => b.RoleId == c.RoleId && c.UserId == User.Id)
  64. .Where(b => b.PermissionId == a.Id)
  65. .Any()
  66. )
  67. .OrderBy(a => a.ParentId)
  68. .OrderBy(a => a.Sort)
  69. .ToListAsync(a => new AuthUserMenuDto { ViewPath = a.View.Path });
  70. //用户权限点
  71. authUserInfoOutput.Permissions = await _permissionRepository.Select
  72. .Where(a => a.Type == PermissionType.Dot)
  73. .Where(a =>
  74. _permissionRepository.Orm.Select<RolePermissionEntity>()
  75. .InnerJoin<UserRoleEntity>((b, c) => b.RoleId == c.RoleId && c.UserId == User.Id)
  76. .Where(b => b.PermissionId == a.Id)
  77. .Any()
  78. )
  79. .ToListAsync(a => a.Code);
  80. return ResponseOutput.Ok(authUserInfoOutput);
  81. }
  82. public async Task<IResponseOutput> GetVerifyCodeAsync(string lastKey)
  83. {
  84. var img = _verifyCodeHelper.GetBase64String(out string code);
  85. //删除上次缓存的验证码
  86. if (lastKey.NotNull())
  87. {
  88. await Cache.DelAsync(lastKey);
  89. }
  90. //写入Redis
  91. var guid = Guid.NewGuid().ToString("N");
  92. var key = string.Format(CacheKey.VerifyCodeKey, guid);
  93. await Cache.SetAsync(key, code, TimeSpan.FromMinutes(5));
  94. var data = new AuthGetVerifyCodeOutput { Key = guid, Img = img };
  95. return ResponseOutput.Ok(data);
  96. }
  97. public async Task<IResponseOutput> LoginAsync(AuthLoginInput input)
  98. {
  99. #region 验证码校验
  100. if (_appConfig.VarifyCode.Enable)
  101. {
  102. /*
  103. var verifyCodeKey = string.Format(CacheKey.VerifyCodeKey, input.VerifyCodeKey);
  104. var exists = await Cache.ExistsAsync(verifyCodeKey);
  105. if (exists)
  106. {
  107. var verifyCode = await Cache.GetAsync(verifyCodeKey);
  108. if (string.IsNullOrEmpty(verifyCode))
  109. {
  110. return ResponseOutput.NotOk("验证码已过期!", 1);
  111. }
  112. if (verifyCode.ToLower() != input.VerifyCode.ToLower())
  113. {
  114. return ResponseOutput.NotOk("验证码输入有误!", 2);
  115. }
  116. await Cache.DelAsync(verifyCodeKey);
  117. }
  118. else
  119. {
  120. return ResponseOutput.NotOk("验证码已过期!", 1);
  121. }
  122. */
  123. var isOk = await _captcha.CheckAsync(input.Captcha, true);
  124. if (!isOk)
  125. {
  126. return ResponseOutput.NotOk("安全验证不通过,请重新登录!");
  127. }
  128. }
  129. #endregion 验证码校验
  130. UserEntity user = null;
  131. user = await _userRepository.Select.DisableGlobalFilter("Tenant").Where(a => a.UserName == input.UserName).ToOneAsync();
  132. //user = (await _userRepository.GetAsync(a => a.UserName == input.UserName));
  133. if (!(user?.Id > 0))
  134. {
  135. return ResponseOutput.NotOk("账号输入有误!", 3);
  136. }
  137. #region 解密
  138. if (input.PasswordKey.NotNull())
  139. {
  140. var passwordEncryptKey = string.Format(CacheKey.PassWordEncryptKey, input.PasswordKey);
  141. var existsPasswordKey = await Cache.ExistsAsync(passwordEncryptKey);
  142. if (existsPasswordKey)
  143. {
  144. var secretKey = await Cache.GetAsync(passwordEncryptKey);
  145. if (secretKey.IsNull())
  146. {
  147. return ResponseOutput.NotOk("解密失败!", 1);
  148. }
  149. input.Password = DesEncrypt.Decrypt(input.Password, secretKey);
  150. await Cache.DelAsync(passwordEncryptKey);
  151. }
  152. else
  153. {
  154. return ResponseOutput.NotOk("解密失败!", 1);
  155. }
  156. }
  157. #endregion 解密
  158. var password = MD5Encrypt.Encrypt32(input.Password);
  159. if (user.Password != password)
  160. {
  161. return ResponseOutput.NotOk("密码输入有误!", 4);
  162. }
  163. var authLoginOutput = Mapper.Map<AuthLoginOutput>(user);
  164. if (_appConfig.Tenant)
  165. {
  166. var tenant = await _tenantRepository.Select.DisableGlobalFilter("Tenant").WhereDynamic(user.TenantId).ToOneAsync(a => new { a.TenantType, a.DataIsolationType });
  167. authLoginOutput.TenantType = tenant.TenantType;
  168. authLoginOutput.DataIsolationType = tenant.DataIsolationType;
  169. }
  170. return ResponseOutput.Ok(authLoginOutput);
  171. }
  172. }
  173. }