0
0

AuthService.cs 7.5 KB

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