SysUserService.cs 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Furion.DatabaseAccessor.Extensions;
  2. using Microsoft.Extensions.Options;
  3. using YBEE.EQM.Core;
  4. namespace YBEE.EQM.Application;
  5. /// <summary>
  6. /// 系统用户服务
  7. /// </summary>
  8. public class SysUserService(IRepository<SysUser> userRep, IOptions<AuthOptions> options) : ISysUserService, ITransient
  9. {
  10. private readonly AuthOptions _authOptions = options.Value;
  11. /// <summary>
  12. /// 获取所有用户列表
  13. /// </summary>
  14. /// <returns></returns>
  15. public async Task<List<SysUserLiteOutput>> GetAllList()
  16. {
  17. var items = await userRep.DetachedEntities.Where(u => u.Status == CommonStatus.ENABLE)
  18. .ProjectToType<SysUserLiteOutput>()
  19. .ToListAsync();
  20. return items;
  21. }
  22. /// <summary>
  23. /// 修改密码
  24. /// </summary>
  25. /// <param name="input"></param>
  26. /// <returns></returns>
  27. public async Task ChangePassword(ChangeSysUserPasswordInput input)
  28. {
  29. string oldPwd = RSAEncryption.Decrypt(input.OldPassword, _authOptions.RsaPrivateKey);
  30. string newPwd = RSAEncryption.Decrypt(input.NewPassword, _authOptions.RsaPrivateKey);
  31. if (newPwd.Length < 6 || newPwd.Length > 32)
  32. {
  33. throw Oops.Oh(ErrorCode.E1006);
  34. }
  35. //if ((await _userRep.DetachedEntities.AsQueryable().ToListAsync()).Any(t => t.Id == CurrentSysUserInfo.SysUserId && AESEncryption.Decrypt(t.Password, _authOptions.AesPassword).Equals(oldPwd)))
  36. if ((await userRep.DetachedEntities.AsQueryable().ToListAsync()).Any(t => t.Id == CurrentSysUserInfo.SysUserId && PBKDF2Encryption.Compare(oldPwd, t.Password)))
  37. {
  38. var user = await userRep.FirstOrDefaultAsync(t => t.Id == CurrentSysUserInfo.SysUserId);
  39. //user.Password = AESEncryption.Encrypt(newPwd, _authOptions.AesPassword);
  40. user.Password = PBKDF2Encryption.Encrypt(newPwd);
  41. await user.UpdateIncludeNowAsync(["Password"]);
  42. }
  43. else
  44. {
  45. throw Oops.Oh(ErrorCode.E1005);
  46. }
  47. }
  48. /// <summary>
  49. /// 查询简要用户列表
  50. /// </summary>
  51. /// <param name="input"></param>
  52. /// <returns></returns>
  53. public async Task<PageResult<SysUserSimpleOutput>> QueryUserSimplePageList(SysUserPageInput input)
  54. {
  55. var query = GetQueryBase(input);
  56. var ret = await query.OrderBy(t => t.Name).ProjectToType<SysUserSimpleOutput>().ToADPagedListAsync(input.PageIndex, input.PageSize);
  57. return ret;
  58. }
  59. ///// <summary>
  60. ///// 更换密码加密方式
  61. ///// </summary>
  62. ///// <returns></returns>
  63. //public async Task UpdatePassword()
  64. //{
  65. // var items = await userRep.Where(t => t.Id > 1).ToListAsync();
  66. // foreach (var item in items)
  67. // {
  68. // string pwd = AESEncryption.Decrypt(item.Password, _authOptions.AesPassword);
  69. // item.Password = PBKDF2Encryption.Encrypt(pwd);
  70. // await item.UpdateIncludeAsync(["Password"]);
  71. // }
  72. //}
  73. #region 私有方法
  74. private IQueryable<SysUser> GetQueryBase(SysUserPageInput input)
  75. {
  76. var name = !string.IsNullOrEmpty(input.Name?.Trim());
  77. var mobile = !string.IsNullOrEmpty(input.Mobile?.Trim());
  78. var account = !string.IsNullOrEmpty(input.Account?.Trim());
  79. var email = !string.IsNullOrEmpty(input.Email?.Trim());
  80. var searchValue = !string.IsNullOrEmpty(input.SearchValue?.Trim());
  81. var query = userRep.DetachedEntities.Where(t => t.IsDeleted == false)
  82. .Where((searchValue, u => EF.Functions.Like(u.Name, $"%{input.SearchValue.Trim()}%") || EF.Functions.Like(u.Account, $"%{input.SearchValue.Trim()}%")))
  83. .Where(name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%"))
  84. .Where(mobile, u => EF.Functions.Like(u.Mobile, $"%{input.Mobile.Trim()}%"))
  85. .Where(account, u => EF.Functions.Like(u.Account, $"%{input.Account.Trim()}%"))
  86. .Where(email, u => EF.Functions.Like(u.Email, $"%{input.Email.Trim()}%"))
  87. .Where(input.SysOrgId.HasValue, t => t.SysOrgId == input.SysOrgId);
  88. return query;
  89. }
  90. #endregion
  91. }