SnapshotThirdAccountSupplier.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Hotline.Caching.Interfaces;
  2. using Hotline.Orders;
  3. using Hotline.Repository.SqlSugar.Snapshot;
  4. using Hotline.Settings;
  5. using Hotline.Share.Dtos.Snapshot;
  6. using Hotline.Share.Enums.Snapshot;
  7. using Hotline.Share.Enums.User;
  8. using Hotline.Share.Tools;
  9. using Hotline.Snapshot;
  10. using Hotline.Snapshot.IRepository;
  11. using Hotline.ThirdAccountDomainServices;
  12. using Hotline.ThirdAccountDomainServices.Interfaces;
  13. using IdentityModel;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Security.Claims;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using XF.Domain.Dependency;
  21. namespace Hotline.Application.Snapshot;
  22. public class SnapshotThirdAccountSupplier : IThirdAccountSupplier, IScopeDependency
  23. {
  24. private readonly IThirdAccountRepository _thirdAccountRepository;
  25. private readonly IThirdAccountDomainService _thirdAccountDomainService;
  26. private readonly IVolunteerRepository _volunteerRepository;
  27. private readonly ISystemSettingCacheManager _systemSettingCacheManager;
  28. private readonly ISystemLogRepository _systemLog;
  29. private readonly ICitizenRepository _citizenRepository;
  30. public SnapshotThirdAccountSupplier(IThirdAccountRepository thirdAccountRepository, IVolunteerRepository volunteerRepository, ISystemSettingCacheManager systemSettingCacheManager, ISystemLogRepository systemLog, IThirdAccountDomainService thirdAccountDomainService, ICitizenRepository citizenRepository)
  31. {
  32. _thirdAccountRepository = thirdAccountRepository;
  33. _volunteerRepository = volunteerRepository;
  34. _systemSettingCacheManager = systemSettingCacheManager;
  35. _systemLog = systemLog;
  36. _thirdAccountDomainService = thirdAccountDomainService;
  37. _citizenRepository = citizenRepository;
  38. }
  39. public async Task<string> GetClaimAsync(ThirdAccount account, List<Claim> claims, CancellationToken token)
  40. {
  41. var userInfo = await _citizenRepository.GetAsync(account.ExternalId);
  42. if (userInfo == null)
  43. return string.Empty;
  44. claims.Add(new(JwtClaimTypes.Subject, userInfo.Id));
  45. return userInfo.Id;
  46. }
  47. public async Task<Dictionary<string, object>> GetLoginOutDataAsync(ThirdAccount thirdAccount, Dictionary<string, object> dicOutData, CancellationToken cancel)
  48. {
  49. var isVolunteer = await _volunteerRepository.IsVolunteerAsync(thirdAccount.PhoneNumber);
  50. var user = await _citizenRepository.GetAsync(thirdAccount.ExternalId);
  51. if (user == null || thirdAccount.ExternalId.IsNullOrEmpty())
  52. {
  53. user = new Citizen
  54. {
  55. CitizenType = EReadPackUserType.Citizen,
  56. Name = thirdAccount.UserName,
  57. PhoneNumber = thirdAccount.PhoneNumber,
  58. };
  59. user.Id = await _citizenRepository.AddAsync(user);
  60. await _thirdAccountDomainService.UpdateExternalIdAsync(thirdAccount.Id, thirdAccount.ExternalId, user.Id, cancel);
  61. }
  62. dicOutData.Add("userType", user.CitizenType);
  63. dicOutData.Add("isVolunteer", isVolunteer);
  64. dicOutData.Add("invitationCode", user.InvitationCode);
  65. return dicOutData;
  66. }
  67. public async Task<ThirdTokenDto> GetThirdParameterAsync(ThirdTokenDto thirdDto, CancellationToken token)
  68. {
  69. if (thirdDto.ThirdType == EThirdType.WeChat)
  70. {
  71. thirdDto.AppId = _systemSettingCacheManager.WxOpenAppId;
  72. thirdDto.Secret = _systemSettingCacheManager.WxOpenAppSecret;
  73. }
  74. return await Task.FromResult(thirdDto);
  75. }
  76. public async Task RegisterAsync(ThirdAccount thirdAccount, CancellationToken token)
  77. {
  78. var userInfo = await _citizenRepository.GetByPhoneNumberAsync(thirdAccount.PhoneNumber);
  79. if (userInfo != null)
  80. {
  81. userInfo.CitizenType = EReadPackUserType.Guider;
  82. await _citizenRepository.UpdateAsync(userInfo);
  83. await _thirdAccountDomainService.UpdateExternalIdAsync(thirdAccount.Id, thirdAccount.ExternalId, userInfo.Id, token);
  84. }
  85. else
  86. {
  87. userInfo = new Citizen
  88. {
  89. PhoneNumber = thirdAccount.PhoneNumber,
  90. CitizenType = EReadPackUserType.Citizen,
  91. };
  92. userInfo.Id = await _citizenRepository.AddAsync(userInfo);
  93. await _thirdAccountDomainService.UpdateExternalIdAsync(thirdAccount.Id, thirdAccount.ExternalId, userInfo.Id, token);
  94. }
  95. }
  96. }