AuthService.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. if(_appConfig.TenantDbType == TenantDbType.Share)
  99. {
  100. authLoginOutput.TenantType = await _tenantRepository.Select.WhereDynamic(user.TenantId).ToOneAsync(a => a.TenantType);
  101. }
  102. return ResponseOutput.Ok(authLoginOutput);
  103. }
  104. public async Task<IResponseOutput> GetUserInfoAsync()
  105. {
  106. if (!(User?.Id > 0))
  107. {
  108. return ResponseOutput.NotOk("未登录!");
  109. }
  110. var key = string.Format(CacheKey.UserInfo, User.Id);
  111. var output = await _cache.GetOrSetAsync(key, async () =>
  112. {
  113. var authUserInfoOutput = new AuthUserInfoOutput { };
  114. //用户信息
  115. authUserInfoOutput.User = await _userRepository.Select.WhereDynamic(User.Id)
  116. .ToOneAsync<AuthUserProfileDto>();
  117. //用户菜单
  118. authUserInfoOutput.Menus = await _permissionRepository.Select
  119. .Where(a => new[] { PermissionType.Group, PermissionType.Menu }.Contains(a.Type))
  120. .Where(a =>
  121. _permissionRepository.Orm.Select<RolePermissionEntity>()
  122. .InnerJoin<UserRoleEntity>((b, c) => b.RoleId == c.RoleId && c.UserId == User.Id)
  123. .Where(b => b.PermissionId == a.Id)
  124. .Any()
  125. )
  126. .OrderBy(a => a.ParentId)
  127. .OrderBy(a => a.Sort)
  128. .ToListAsync(a => new AuthUserMenuDto { ViewPath = a.View.Path });
  129. //用户权限点
  130. authUserInfoOutput.Permissions = await _permissionRepository.Select
  131. .Where(a => new[] { PermissionType.Api, PermissionType.Dot }.Contains(a.Type))
  132. .Where(a =>
  133. _permissionRepository.Orm.Select<RolePermissionEntity>()
  134. .InnerJoin<UserRoleEntity>((b, c) => b.RoleId == c.RoleId && c.UserId == User.Id)
  135. .Where(b => b.PermissionId == a.Id)
  136. .Any()
  137. )
  138. .ToListAsync(a => a.Code);
  139. return authUserInfoOutput;
  140. });
  141. return ResponseOutput.Ok(output);
  142. }
  143. public async Task<IResponseOutput> GetVerifyCodeAsync(string lastKey)
  144. {
  145. var img = _verifyCodeHelper.GetBase64String(out string code);
  146. //删除上次缓存的验证码
  147. if (lastKey.NotNull())
  148. {
  149. await _cache.DelAsync(lastKey);
  150. }
  151. //写入Redis
  152. var guid = Guid.NewGuid().ToString("N");
  153. var key = string.Format(CacheKey.VerifyCodeKey, guid);
  154. await _cache.SetAsync(key, code, TimeSpan.FromMinutes(5));
  155. var data = new AuthGetVerifyCodeOutput { Key = guid, Img = img };
  156. return ResponseOutput.Ok(data);
  157. }
  158. public async Task<IResponseOutput> GetPassWordEncryptKeyAsync()
  159. {
  160. //写入Redis
  161. var guid = Guid.NewGuid().ToString("N");
  162. var key = string.Format(CacheKey.PassWordEncryptKey, guid);
  163. var encyptKey = StringHelper.GenerateRandom(8);
  164. await _cache.SetAsync(key, encyptKey, TimeSpan.FromMinutes(5));
  165. var data = new { key = guid, encyptKey };
  166. return ResponseOutput.Ok(data);
  167. }
  168. }
  169. }