0
0

AuthService.cs 7.5 KB

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