Browse Source

新增根据OpenId刷新令牌接口

qinchaoyue 4 months ago
parent
commit
b0ae0821e5

+ 8 - 0
src/Hotline.Api/Controllers/IdentityController.cs

@@ -101,6 +101,14 @@ jxrWXHbT1FB6DqkdOnBbQqS1Azqz5HxLlSyEK3F60e3SgB5iZsDZ
     public async Task<TokenOutDto> GetThirdTokenAsync([FromBody] ThirdTokenInDto dto)
     public async Task<TokenOutDto> GetThirdTokenAsync([FromBody] ThirdTokenInDto dto)
         => await _identityAppService.GetThredTokenAsync(dto);
         => await _identityAppService.GetThredTokenAsync(dto);
 
 
+    /// <summary>
+    /// 根据OpenId刷新令牌
+    /// </summary>
+    /// <param name="openId"></param>
+    /// <returns></returns>
+    public async Task<TokenOutDto> RefreshTokenAsync(string openId)
+        => await _identityAppService.RefreshTokenAsync(openId);
+
     [AllowAnonymous]
     [AllowAnonymous]
     [ApiExplorerSettings(IgnoreApi = true)]
     [ApiExplorerSettings(IgnoreApi = true)]
     [HttpPost("token")]
     [HttpPost("token")]

+ 17 - 1
src/Hotline.Api/Controllers/Snapshot/SnapshotController.cs

@@ -1,6 +1,7 @@
 using Hotline.Api.Filter;
 using Hotline.Api.Filter;
 using Hotline.Application.Orders;
 using Hotline.Application.Orders;
 using Hotline.Application.Snapshot;
 using Hotline.Application.Snapshot;
+using Hotline.Caching.Interfaces;
 using Hotline.File;
 using Hotline.File;
 using Hotline.Orders;
 using Hotline.Orders;
 using Hotline.Settings;
 using Hotline.Settings;
@@ -38,8 +39,9 @@ public class SnapshotController : BaseController
     private readonly IOrderDomainService _orderDomainService;
     private readonly IOrderDomainService _orderDomainService;
     private readonly IFileRepository _fileRepository;
     private readonly IFileRepository _fileRepository;
     private readonly IOrderSnapshotRepository _orderSnapshotRepository;
     private readonly IOrderSnapshotRepository _orderSnapshotRepository;
+    private readonly ISystemDicDataCacheManager _systemDicDataCacheManager;
 
 
-    public SnapshotController(IRepository<Order> orderRepository, ISnapshotApplication snapshotApplication, ISystemAreaDomainService systemAreaDomainService, IIndustryRepository industryRepository, IOrderDomainService orderDomainService, IFileRepository fileRepository, IOrderSnapshotRepository orderSnapshotRepository)
+    public SnapshotController(IRepository<Order> orderRepository, ISnapshotApplication snapshotApplication, ISystemAreaDomainService systemAreaDomainService, IIndustryRepository industryRepository, IOrderDomainService orderDomainService, IFileRepository fileRepository, IOrderSnapshotRepository orderSnapshotRepository, ISystemDicDataCacheManager systemDicDataCacheManager)
     {
     {
         _orderRepository = orderRepository;
         _orderRepository = orderRepository;
         _snapshotApplication = snapshotApplication;
         _snapshotApplication = snapshotApplication;
@@ -48,6 +50,7 @@ public class SnapshotController : BaseController
         _orderDomainService = orderDomainService;
         _orderDomainService = orderDomainService;
         _fileRepository = fileRepository;
         _fileRepository = fileRepository;
         _orderSnapshotRepository = orderSnapshotRepository;
         _orderSnapshotRepository = orderSnapshotRepository;
+        _systemDicDataCacheManager = systemDicDataCacheManager;
     }
     }
 
 
     /// <summary>
     /// <summary>
@@ -251,4 +254,17 @@ public class SnapshotController : BaseController
     public async Task<AddVolunteerReportOutDto> AddVolunteerReportAsync([FromBody] AddVolunteerReportInDto dto)
     public async Task<AddVolunteerReportOutDto> AddVolunteerReportAsync([FromBody] AddVolunteerReportInDto dto)
         => await _snapshotApplication.AddVolunteerReportAsync(dto, HttpContext.RequestAborted);
         => await _snapshotApplication.AddVolunteerReportAsync(dto, HttpContext.RequestAborted);
 
 
+    /// <summary>
+    /// 志愿者上报页面基础数据
+    /// </summary>
+    /// <param name="id">行业Id</param>
+    /// <returns></returns>
+    [HttpGet("report/base")]
+    public Dictionary<string, dynamic> GetReportBaseAsync()
+    { 
+        return new Dictionary<string, dynamic>
+        {
+            { "jobType", _systemDicDataCacheManager.JobType },
+        };
+    }
 }
 }

+ 11 - 0
src/Hotline.Application.Tests/Application/SnapshotApplicationTest.cs

@@ -162,10 +162,21 @@ public class SnapshotApplicationTest : TestBase
         result.PhoneNumberMask.Contains("***").ShouldBeTrue();
         result.PhoneNumberMask.Contains("***").ShouldBeTrue();
     }
     }
 
 
+    [Fact]
+    public async Task RefreshTokenAsync()
+    { 
+        var token = await _identityAppService.GetThredTokenAsync(new ThirdTokenInDto());
+        var newToken = await _identityAppService.RefreshTokenAsync(token.OpenId);
+        newToken.ShouldNotBeNull();
+        newToken.OpenId.ShouldBe(token.OpenId);
+        newToken.PhoneNumber.ShouldNotBeNullOrEmpty();
+    }
+
     [Fact]
     [Fact]
     public async Task GetThirdToken_Test()
     public async Task GetThirdToken_Test()
     {
     {
         var result = await _identityAppService.GetThredTokenAsync(new ThirdTokenInDto { LoginCode = "0c3Adhll2zDMBe413rnl2KvEym2AdhlH" });
         var result = await _identityAppService.GetThredTokenAsync(new ThirdTokenInDto { LoginCode = "0c3Adhll2zDMBe413rnl2KvEym2AdhlH" });
+        result.PhoneNumber.ShouldNotBeNullOrEmpty();
     }
     }
 
 
     [Theory]
     [Theory]

+ 7 - 0
src/Hotline.Application/Identity/IIdentityAppService.cs

@@ -21,6 +21,13 @@ namespace Hotline.Application.Identity
         /// <exception cref="UserFriendlyException"></exception>
         /// <exception cref="UserFriendlyException"></exception>
         Task<TokenOutDto> GetThredTokenAsync(ThirdTokenInDto dto);
         Task<TokenOutDto> GetThredTokenAsync(ThirdTokenInDto dto);
 
 
+        /// <summary>
+        /// 根据OpenId刷新令牌
+        /// </summary>
+        /// <param name="openId"></param>
+        /// <returns></returns>
+        Task<TokenOutDto> RefreshTokenAsync(string openId);
+
         Task<string> LoginAsync(LoginDto dto, CancellationToken cancellationToken);
         Task<string> LoginAsync(LoginDto dto, CancellationToken cancellationToken);
 
 
         Task<string> OldToNewLoginAsync(HotlineLoginOldToNewDto dto, CancellationToken cancellationToken);
         Task<string> OldToNewLoginAsync(HotlineLoginOldToNewDto dto, CancellationToken cancellationToken);

+ 39 - 5
src/Hotline.Application/Identity/IdentityAppService.cs

@@ -50,7 +50,7 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
     private readonly ISystemSettingCacheManager _systemSettingCacheManager;
     private readonly ISystemSettingCacheManager _systemSettingCacheManager;
     private readonly IThirdIdentiyService _thirdIdentiyService;
     private readonly IThirdIdentiyService _thirdIdentiyService;
     private readonly IThirdAccountRepository _thirdAccountRepository;
     private readonly IThirdAccountRepository _thirdAccountRepository;
-    private readonly IRepository<GuiderInfo> _guiderInfoRepository;
+    private readonly IGuiderInfoRepository _guiderInfoRepository;
     private readonly IVolunteerRepository _volunteerRepository;
     private readonly IVolunteerRepository _volunteerRepository;
 
 
     public IdentityAppService(
     public IdentityAppService(
@@ -68,7 +68,7 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
         IThirdAccountRepository thirdAccountRepository,
         IThirdAccountRepository thirdAccountRepository,
         ISessionContext sessionContext,
         ISessionContext sessionContext,
         IRepository<Citizen> citizenRepository,
         IRepository<Citizen> citizenRepository,
-        IRepository<GuiderInfo> guiderInfoRepository,
+        IGuiderInfoRepository guiderInfoRepository,
         IVolunteerRepository volunteerRepository)
         IVolunteerRepository volunteerRepository)
     {
     {
         _accountRepository = accountRepository;
         _accountRepository = accountRepository;
@@ -322,16 +322,41 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
         }
         }
         var thirdToken = await _thirdIdentiyService.GetTokenAsync(thirdDto);
         var thirdToken = await _thirdIdentiyService.GetTokenAsync(thirdDto);
         var phone = await _thirdIdentiyService.GetPhoneNumberAsync(thirdDto);
         var phone = await _thirdIdentiyService.GetPhoneNumberAsync(thirdDto);
-        var thirdAccount = await _thirdAccountRepository.QueryByOpenIdAsync(thirdToken.OpenId);
+        var thirdAccount = await _thirdAccountRepository.GetByOpenIdAsync(thirdToken.OpenId);
 
 
         // 新用户注册
         // 新用户注册
         if (thirdAccount is null)
         if (thirdAccount is null)
         {
         {
             thirdAccount = thirdToken.Adapt<ThirdAccount>();
             thirdAccount = thirdToken.Adapt<ThirdAccount>();
+            thirdAccount.CitizenType = EReadPackUserType.Citizen;
             thirdAccount.PhoneNumber = phone.PhoneNumber;
             thirdAccount.PhoneNumber = phone.PhoneNumber;
+            var guider =  await _guiderInfoRepository.GetByPhoneNumberAsync(phone.PhoneNumber);
+            if (guider != null)
+            {
+                thirdAccount.CitizenType = EReadPackUserType.Guider;
+                thirdAccount.UserId = guider.Id;
+            }
+            else
+            {
+                var citizen = await _citizenRepository.Queryable().Where(m => m.PhoneNumber == phone.PhoneNumber).FirstAsync();
+                thirdAccount.UserId = citizen.Id;
+            }
             thirdAccount.Id = await _thirdAccountRepository.AddAsync(thirdAccount);
             thirdAccount.Id = await _thirdAccountRepository.AddAsync(thirdAccount);
         }
         }
 
 
+        return await GetJwtToken(thirdAccount);
+    }
+
+    public async Task<TokenOutDto> RefreshTokenAsync(string openId)
+    {
+        var thirdAccount = await _thirdAccountRepository.GetByOpenIdAsync(openId)
+            ?? throw UserFriendlyException.SameMessage("未找到用户信息");
+
+        return await GetJwtToken(thirdAccount);
+    }
+
+    private async Task<TokenOutDto> GetJwtToken(ThirdAccount thirdAccount)
+    {
         var jwtOptions = _identityOptionsAccessor.Value.Jwt;
         var jwtOptions = _identityOptionsAccessor.Value.Jwt;
         var claims = new List<Claim>
         var claims = new List<Claim>
         {
         {
@@ -345,6 +370,15 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
         await _cacheAudience.SetAsync(audience.Id, audience, TimeSpan.FromSeconds(expiredSeconds));
         await _cacheAudience.SetAsync(audience.Id, audience, TimeSpan.FromSeconds(expiredSeconds));
         var token = _jwtSecurity.EncodeJwtToken(claims, audience.Ticket);
         var token = _jwtSecurity.EncodeJwtToken(claims, audience.Ticket);
         var isVolunteer = await _volunteerRepository.IsVolunteerAsync(thirdAccount.PhoneNumber);
         var isVolunteer = await _volunteerRepository.IsVolunteerAsync(thirdAccount.PhoneNumber);
-        return new TokenOutDto(thirdAccount.CitizenType, token, isVolunteer);
+        return new TokenOutDto()
+        {
+            UserType = thirdAccount.CitizenType,
+            Token = token,
+            IsVolunteer = isVolunteer,
+            OpenId = thirdAccount.OpenId,
+            PhoneNumber = thirdAccount.PhoneNumber,
+            InvitationCode = thirdAccount.InvitationCode,
+            UserName = thirdAccount.UserName
+        };
     }
     }
-  }
+}

+ 8 - 1
src/Hotline.Application/Snapshot/SnapshotApplicationBase.cs

@@ -257,7 +257,7 @@ public abstract class SnapshotApplicationBase
     public async Task<SnapshotUserInfoOutDto> GetSnapshotUserInfoAsync()
     public async Task<SnapshotUserInfoOutDto> GetSnapshotUserInfoAsync()
     {
     {
         var openId = _sessionContext.OpenId;
         var openId = _sessionContext.OpenId;
-        var thirdAccount = await _thirdAccountRepository.QueryByOpenIdAsync(openId);
+        var thirdAccount = await _thirdAccountRepository.GetByOpenIdAsync(openId);
         var dayTime = DateTime.Now;
         var dayTime = DateTime.Now;
         var readPack = await _redPackRecordRepository.Queryable()
         var readPack = await _redPackRecordRepository.Queryable()
             .Where(m => m.WXOpenId == openId && m.PickupStatus == ERedPackPickupStatus.Received)
             .Where(m => m.WXOpenId == openId && m.PickupStatus == ERedPackPickupStatus.Received)
@@ -532,6 +532,13 @@ public abstract class SnapshotApplicationBase
     #endregion
     #endregion
 
 
     #region 志愿者
     #region 志愿者
+
+    public async Task<string> AddVolunteerAsync(AddVolunteerInDto dto, CancellationToken cancellationToken)
+    {
+        var entity = dto.Adapt<Volunteer>();
+        return entity.Id;
+    }
+
     /// <summary>
     /// <summary>
     /// 志愿者上报
     /// 志愿者上报
     /// </summary>
     /// </summary>

+ 23 - 0
src/Hotline.Repository.SqlSugar/Snapshot/GuiderInfoRepository.cs

@@ -0,0 +1,23 @@
+using Hotline.Repository.SqlSugar.DataPermissions;
+using Hotline.Snapshot;
+using Hotline.Snapshot.Interfaces;
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using XF.Domain.Dependency;
+
+namespace Hotline.Repository.SqlSugar.Snapshot;
+public class GuiderInfoRepository : BaseRepository<GuiderInfo>, IGuiderInfoRepository, IScopeDependency
+{
+    public GuiderInfoRepository(ISugarUnitOfWork<HotlineDbContext> uow, IDataPermissionFilterBuilder dataPermissionFilterBuilder) : base(uow, dataPermissionFilterBuilder)
+    {
+    }
+
+    public async Task<GuiderInfo> GetByPhoneNumberAsync(string phoneNumber)
+    {
+        return await Queryable().Where(m => m.PhoneNumber == phoneNumber).FirstAsync();
+    }
+}

+ 1 - 1
src/Hotline.Repository.SqlSugar/Snapshot/ThirdAccountRepository.cs

@@ -12,7 +12,7 @@ public class ThirdAccountRepository : BaseRepository<ThirdAccount>,  IThirdAccou
     {
     {
     }
     }
 
 
-    public async Task<ThirdAccount> QueryByOpenIdAsync(string openId)
+    public async Task<ThirdAccount> GetByOpenIdAsync(string openId)
         => await Queryable()
         => await Queryable()
         .Where(p => p.OpenId == openId)
         .Where(p => p.OpenId == openId)
         .FirstAsync();
         .FirstAsync();

+ 20 - 7
src/Hotline.Share/Dtos/Snapshot/ThirdTokenDto.cs

@@ -5,13 +5,6 @@ namespace Hotline.Share.Dtos.Snapshot;
 
 
 public class TokenOutDto
 public class TokenOutDto
 {
 {
-    public TokenOutDto(EReadPackUserType citizenType, string token, bool isVolunteer)
-    {
-        IsVolunteer = isVolunteer;
-        UserType = citizenType;
-        Token = token;
-    }
-
     /// <summary>
     /// <summary>
     /// 用户类型
     /// 用户类型
     /// </summary>
     /// </summary>
@@ -22,10 +15,30 @@ public class TokenOutDto
     /// </summary>
     /// </summary>
     public string Token { get; set; }
     public string Token { get; set; }
 
 
+    /// <summary>
+    /// 电话号码
+    /// </summary>
+    public string PhoneNumber { get; set; }
+
+    /// <summary>
+    /// 用户名字
+    /// </summary>
+    public string UserName { get; set; }
+
+    /// <summary>
+    /// OpenId
+    /// </summary>
+    public string OpenId { get; set; }
+
     /// <summary>
     /// <summary>
     /// 是否自愿者
     /// 是否自愿者
     /// </summary>
     /// </summary>
     public bool IsVolunteer { get; set; }
     public bool IsVolunteer { get; set; }
+
+    /// <summary>
+    /// 邀请码
+    /// </summary>
+    public string InvitationCode { get; set; }
 }
 }
 
 
 public class ThirdTokenInDto
 public class ThirdTokenInDto

+ 1 - 1
src/Hotline.Share/Enums/Snapshot/EReadPackUserType.cs

@@ -18,5 +18,5 @@ public enum EReadPackUserType
     /// 网络员
     /// 网络员
     /// </summary>
     /// </summary>
     [Description("网络员")]
     [Description("网络员")]
-    Gukder = 1
+    Guider = 1
 }
 }

+ 13 - 0
src/Hotline/Snapshot/Interfaces/IGuiderInfoRepository.cs

@@ -0,0 +1,13 @@
+using Hotline.Share.Dtos.Snapshot;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using XF.Domain.Repository;
+
+namespace Hotline.Snapshot.Interfaces;
+public interface IGuiderInfoRepository : IRepository<GuiderInfo>
+{
+    Task<GuiderInfo> GetByPhoneNumberAsync(string phoneNumber);
+}

+ 1 - 1
src/Hotline/Snapshot/Interfaces/IThirdAccountRepository.cs

@@ -3,5 +3,5 @@
 namespace Hotline.Snapshot.Interfaces;
 namespace Hotline.Snapshot.Interfaces;
 public interface IThirdAccountRepository : IRepository<ThirdAccount>
 public interface IThirdAccountRepository : IRepository<ThirdAccount>
 {
 {
-    Task<ThirdAccount> QueryByOpenIdAsync(string openId);
+    Task<ThirdAccount> GetByOpenIdAsync(string openId);
 }
 }

+ 12 - 0
src/Hotline/Snapshot/ThirdAccount.cs

@@ -64,4 +64,16 @@ public class ThirdAccount : CreationSoftDeleteEntity
     /// </summary>
     /// </summary>
     [SugarColumn(ColumnDescription = "用户头像Url")]
     [SugarColumn(ColumnDescription = "用户头像Url")]
     public string? HeadImgUrl { get; set; }
     public string? HeadImgUrl { get; set; }
+
+    /// <summary>
+    /// 用户自己填的邀请码
+    /// </summary>
+    [SugarColumn(ColumnDescription = "用户自己填的邀请码")]
+    public string? InvitationCode { get; set; }
+
+    /// <summary>
+    /// 用户昵称
+    /// </summary>
+    [SugarColumn(ColumnDescription = "用户昵称")]
+    public string? UserName { get; set; }
 }
 }