AuthService.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 authUserInfoOutput = new AuthUserInfoOutput { };
  52. //用户信息
  53. authUserInfoOutput.User = await _userRepository.GetAsync<AuthUserProfileDto>(User.Id);
  54. //用户菜单
  55. authUserInfoOutput.Menus = await _permissionRepository.Select
  56. .Where(a => new[] { PermissionType.Group, PermissionType.Menu }.Contains(a.Type))
  57. .Where(a =>
  58. _permissionRepository.Orm.Select<RolePermissionEntity>()
  59. .InnerJoin<UserRoleEntity>((b, c) => b.RoleId == c.RoleId && c.UserId == User.Id)
  60. .Where(b => b.PermissionId == a.Id)
  61. .Any()
  62. )
  63. .OrderBy(a => a.ParentId)
  64. .OrderBy(a => a.Sort)
  65. .ToListAsync(a => new AuthUserMenuDto { ViewPath = a.View.Path });
  66. //用户权限点
  67. authUserInfoOutput.Permissions = await _permissionRepository.Select
  68. .Where(a => a.Type == PermissionType.Dot)
  69. .Where(a =>
  70. _permissionRepository.Orm.Select<RolePermissionEntity>()
  71. .InnerJoin<UserRoleEntity>((b, c) => b.RoleId == c.RoleId && c.UserId == User.Id)
  72. .Where(b => b.PermissionId == a.Id)
  73. .Any()
  74. )
  75. .ToListAsync(a => a.Code);
  76. return ResponseOutput.Ok(authUserInfoOutput);
  77. }
  78. public async Task<IResponseOutput> GetVerifyCodeAsync(string lastKey)
  79. {
  80. var img = _verifyCodeHelper.GetBase64String(out string code);
  81. //删除上次缓存的验证码
  82. if (lastKey.NotNull())
  83. {
  84. await Cache.DelAsync(lastKey);
  85. }
  86. //写入Redis
  87. var guid = Guid.NewGuid().ToString("N");
  88. var key = string.Format(CacheKey.VerifyCodeKey, guid);
  89. await Cache.SetAsync(key, code, TimeSpan.FromMinutes(5));
  90. var data = new AuthGetVerifyCodeOutput { Key = guid, Img = img };
  91. return ResponseOutput.Ok(data);
  92. }
  93. public async Task<IResponseOutput> LoginAsync(AuthLoginInput input)
  94. {
  95. #region 验证码校验
  96. if (_appConfig.VarifyCode.Enable)
  97. {
  98. var verifyCodeKey = string.Format(CacheKey.VerifyCodeKey, input.VerifyCodeKey);
  99. var exists = await Cache.ExistsAsync(verifyCodeKey);
  100. if (exists)
  101. {
  102. var verifyCode = await Cache.GetAsync(verifyCodeKey);
  103. if (string.IsNullOrEmpty(verifyCode))
  104. {
  105. return ResponseOutput.NotOk("验证码已过期!", 1);
  106. }
  107. if (verifyCode.ToLower() != input.VerifyCode.ToLower())
  108. {
  109. return ResponseOutput.NotOk("验证码输入有误!", 2);
  110. }
  111. await Cache.DelAsync(verifyCodeKey);
  112. }
  113. else
  114. {
  115. return ResponseOutput.NotOk("验证码已过期!", 1);
  116. }
  117. }
  118. #endregion 验证码校验
  119. UserEntity user = null;
  120. user = await _userRepository.Select.DisableGlobalFilter("Tenant").Where(a => a.UserName == input.UserName).ToOneAsync();
  121. //user = (await _userRepository.GetAsync(a => a.UserName == input.UserName));
  122. if (!(user?.Id > 0))
  123. {
  124. return ResponseOutput.NotOk("账号输入有误!", 3);
  125. }
  126. #region 解密
  127. if (input.PasswordKey.NotNull())
  128. {
  129. var passwordEncryptKey = string.Format(CacheKey.PassWordEncryptKey, input.PasswordKey);
  130. var existsPasswordKey = await Cache.ExistsAsync(passwordEncryptKey);
  131. if (existsPasswordKey)
  132. {
  133. var secretKey = await Cache.GetAsync(passwordEncryptKey);
  134. if (secretKey.IsNull())
  135. {
  136. return ResponseOutput.NotOk("解密失败!", 1);
  137. }
  138. input.Password = DesEncrypt.Decrypt(input.Password, secretKey);
  139. await Cache.DelAsync(passwordEncryptKey);
  140. }
  141. else
  142. {
  143. return ResponseOutput.NotOk("解密失败!", 1);
  144. }
  145. }
  146. #endregion 解密
  147. var password = MD5Encrypt.Encrypt32(input.Password);
  148. if (user.Password != password)
  149. {
  150. return ResponseOutput.NotOk("密码输入有误!", 4);
  151. }
  152. var authLoginOutput = Mapper.Map<AuthLoginOutput>(user);
  153. if (_appConfig.Tenant)
  154. {
  155. var tenant = await _tenantRepository.Select.DisableGlobalFilter("Tenant").WhereDynamic(user.TenantId).ToOneAsync(a => new { a.TenantType, a.DataIsolationType });
  156. authLoginOutput.TenantType = tenant.TenantType;
  157. authLoginOutput.DataIsolationType = tenant.DataIsolationType;
  158. }
  159. return ResponseOutput.Ok(authLoginOutput);
  160. }
  161. }
  162. }