0
0

AuthService.cs 7.5 KB

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