AuthService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. namespace Admin.Core.Service.Admin.Auth
  15. {
  16. public class AuthService : IAuthService
  17. {
  18. private readonly IUser _user;
  19. private readonly ICache _cache;
  20. private readonly IMapper _mapper;
  21. private readonly AppConfig _appConfig;
  22. private readonly VerifyCodeHelper _verifyCodeHelper;
  23. private readonly IUserRepository _userRepository;
  24. private readonly IPermissionRepository _permissionRepository;
  25. public AuthService(
  26. IUser user,
  27. ICache cache,
  28. IMapper mapper,
  29. AppConfig appConfig,
  30. VerifyCodeHelper verifyCodeHelper,
  31. IUserRepository userRepository,
  32. IPermissionRepository permissionRepository
  33. )
  34. {
  35. _user = user;
  36. _cache = cache;
  37. _mapper = mapper;
  38. _appConfig = appConfig;
  39. _verifyCodeHelper = verifyCodeHelper;
  40. _userRepository = userRepository;
  41. _permissionRepository = permissionRepository;
  42. }
  43. public async Task<IResponseOutput> LoginAsync(AuthLoginInput input)
  44. {
  45. #region 验证码校验
  46. if (_appConfig.VarifyCode.Enabled)
  47. {
  48. var verifyCodeKey = string.Format(CacheKey.VerifyCodeKey, input.VerifyCodeKey);
  49. var exists = await _cache.ExistsAsync(verifyCodeKey);
  50. if (exists)
  51. {
  52. var verifyCode = await _cache.GetAsync(verifyCodeKey);
  53. if (string.IsNullOrEmpty(verifyCode))
  54. {
  55. return ResponseOutput.NotOk("验证码已过期!", 1);
  56. }
  57. if (verifyCode.ToLower() != input.VerifyCode.ToLower())
  58. {
  59. return ResponseOutput.NotOk("验证码输入有误!", 2);
  60. }
  61. await _cache.DelAsync(verifyCodeKey);
  62. }
  63. else
  64. {
  65. return ResponseOutput.NotOk("验证码已过期!", 1);
  66. }
  67. }
  68. #endregion
  69. var 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. return ResponseOutput.Ok(authLoginOutput);
  102. }
  103. public async Task<IResponseOutput> GetUserInfoAsync()
  104. {
  105. if (!(_user?.Id > 0))
  106. {
  107. return ResponseOutput.NotOk("未登录!");
  108. }
  109. var user = await _userRepository.Select.WhereDynamic(_user.Id)
  110. .ToOneAsync(m => new {
  111. m.NickName,
  112. m.UserName,
  113. m.Avatar
  114. });
  115. //获取菜单
  116. var menus = await _permissionRepository.Select
  117. .Where(a => new[] { PermissionType.Group, PermissionType.Menu }.Contains(a.Type))
  118. .Where(a =>
  119. _permissionRepository.Orm.Select<RolePermissionEntity>()
  120. .InnerJoin<UserRoleEntity>((b, c) => b.RoleId == c.RoleId && c.UserId == _user.Id)
  121. .Where(b => b.PermissionId == a.Id)
  122. .Any()
  123. )
  124. .OrderBy(a => a.ParentId)
  125. .OrderBy(a => a.Sort)
  126. .ToListAsync(a => new
  127. {
  128. a.Id,
  129. a.ParentId,
  130. a.Path,
  131. ViewPath = a.View.Path,
  132. a.Label,
  133. a.Icon,
  134. a.Opened,
  135. a.Closable,
  136. a.Hidden,
  137. a.NewWindow,
  138. a.External
  139. });
  140. var permissions = await _permissionRepository.Select
  141. .Where(a => a.Type == PermissionType.Api)
  142. .Where(a =>
  143. _permissionRepository.Orm.Select<RolePermissionEntity>()
  144. .InnerJoin<UserRoleEntity>((b, c) => b.RoleId == c.RoleId && c.UserId == _user.Id)
  145. .Where(b => b.PermissionId == a.Id)
  146. .Any()
  147. )
  148. .ToListAsync(a => a.Code);
  149. return ResponseOutput.Ok(new { user, menus, permissions });
  150. }
  151. public async Task<IResponseOutput> GetVerifyCodeAsync(string lastKey)
  152. {
  153. var img = _verifyCodeHelper.GetBase64String(out string code);
  154. //删除上次缓存的验证码
  155. if (lastKey.NotNull())
  156. {
  157. await _cache.DelAsync(lastKey);
  158. }
  159. //写入Redis
  160. var guid = Guid.NewGuid().ToString("N");
  161. var key = string.Format(CacheKey.VerifyCodeKey, guid);
  162. await _cache.SetAsync(key, code, TimeSpan.FromMinutes(5));
  163. var data = new AuthGetVerifyCodeOutput { Key = guid, Img = img };
  164. return ResponseOutput.Ok(data);
  165. }
  166. public async Task<IResponseOutput> GetPassWordEncryptKeyAsync()
  167. {
  168. //写入Redis
  169. var guid = Guid.NewGuid().ToString("N");
  170. var key = string.Format(CacheKey.PassWordEncryptKey, guid);
  171. var encyptKey = StringHelper.GenerateRandom(8);
  172. await _cache.SetAsync(key, encyptKey, TimeSpan.FromMinutes(5));
  173. var data = new { key = guid, encyptKey };
  174. return ResponseOutput.Ok(data);
  175. }
  176. }
  177. }