1
0

AuthService.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. //用户信息
  110. var user = await _userRepository.Select.WhereDynamic(_user.Id)
  111. .ToOneAsync(m => new {
  112. m.NickName,
  113. m.UserName,
  114. m.Avatar
  115. });
  116. //用户菜单
  117. var menus = await _permissionRepository.Select
  118. .Where(a => new[] { PermissionType.Group, PermissionType.Menu }.Contains(a.Type))
  119. .Where(a =>
  120. _permissionRepository.Orm.Select<RolePermissionEntity>()
  121. .InnerJoin<UserRoleEntity>((b, c) => b.RoleId == c.RoleId && c.UserId == _user.Id)
  122. .Where(b => b.PermissionId == a.Id)
  123. .Any()
  124. )
  125. .OrderBy(a => a.ParentId)
  126. .OrderBy(a => a.Sort)
  127. .ToListAsync(a => new
  128. {
  129. a.Id,
  130. a.ParentId,
  131. a.Path,
  132. ViewPath = a.View.Path,
  133. a.Label,
  134. a.Icon,
  135. a.Opened,
  136. a.Closable,
  137. a.Hidden,
  138. a.NewWindow,
  139. a.External
  140. });
  141. //用户权限点
  142. var permissions = await _permissionRepository.Select
  143. .Where(a => new[] { PermissionType.Api, PermissionType.Dot }.Contains(a.Type))
  144. .Where(a =>
  145. _permissionRepository.Orm.Select<RolePermissionEntity>()
  146. .InnerJoin<UserRoleEntity>((b, c) => b.RoleId == c.RoleId && c.UserId == _user.Id)
  147. .Where(b => b.PermissionId == a.Id)
  148. .Any()
  149. )
  150. .ToListAsync(a => a.Code);
  151. return ResponseOutput.Ok(new { user, menus, permissions });
  152. }
  153. public async Task<IResponseOutput> GetVerifyCodeAsync(string lastKey)
  154. {
  155. var img = _verifyCodeHelper.GetBase64String(out string code);
  156. //删除上次缓存的验证码
  157. if (lastKey.NotNull())
  158. {
  159. await _cache.DelAsync(lastKey);
  160. }
  161. //写入Redis
  162. var guid = Guid.NewGuid().ToString("N");
  163. var key = string.Format(CacheKey.VerifyCodeKey, guid);
  164. await _cache.SetAsync(key, code, TimeSpan.FromMinutes(5));
  165. var data = new AuthGetVerifyCodeOutput { Key = guid, Img = img };
  166. return ResponseOutput.Ok(data);
  167. }
  168. public async Task<IResponseOutput> GetPassWordEncryptKeyAsync()
  169. {
  170. //写入Redis
  171. var guid = Guid.NewGuid().ToString("N");
  172. var key = string.Format(CacheKey.PassWordEncryptKey, guid);
  173. var encyptKey = StringHelper.GenerateRandom(8);
  174. await _cache.SetAsync(key, encyptKey, TimeSpan.FromMinutes(5));
  175. var data = new { key = guid, encyptKey };
  176. return ResponseOutput.Ok(data);
  177. }
  178. }
  179. }