0
0

AuthService.cs 7.6 KB

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