qinchaoyue 5 kuukautta sitten
vanhempi
commit
ad35b47daf

+ 2 - 10
src/Hotline.Api/Controllers/IdentityController.cs

@@ -98,16 +98,8 @@ jxrWXHbT1FB6DqkdOnBbQqS1Azqz5HxLlSyEK3F60e3SgB5iZsDZ
     /// <returns>登录成功, 用户是新用户, 需要调用获取用户手机号码 third/phone 接口</returns>
     [AllowAnonymous]
     [HttpPost("third/token")]
-    public async Task<IActionResult> GetThirdTokenAsync([FromBody] ThirdTokenInDto dto)
-        => Ok(await _identityAppService.GetThredTokenAsync(dto));
-
-    /// <summary>
-    /// 获取微信用户手机号码
-    /// </summary>
-    /// <param name="dto">微信小程序Code</param>
-    [HttpPost("third/phone")]
-    public async Task GetThirdPhoneAsync([FromBody]ThirdPhoneInDto dto)
-        => await _identityAppService.GetThirdPhoneAsync(dto);
+    public async Task<TokenOutDto> GetThirdTokenAsync([FromBody] ThirdTokenInDto dto)
+        => await _identityAppService.GetThredTokenAsync(dto);
 
     [AllowAnonymous]
     [ApiExplorerSettings(IgnoreApi = true)]

+ 2 - 2
src/Hotline.Api/config/appsettings.Development.json

@@ -62,13 +62,13 @@
     }
   },
   "ConnectionStrings": {
-    "Hotline": "PORT=5432;DATABASE=hotline;HOST=110.188.24.182;PASSWORD=fengwo11!!;USER ID=dev;"
+    "Hotline": "PORT=5432;DATABASE=hotline_dev;HOST=110.188.24.182;PASSWORD=fengwo11!!;USER ID=dev;"
   },
   "Cache": {
     "Host": "110.188.24.182",
     "Port": 50179,
     "Password": "fengwo123!$!$",
-    "Database": 3 //test:3, dev:5
+    "Database": 5 //test:3, dev:5
   },
   "Swagger": true,
   "AccLog":  false,

+ 1 - 1
src/Hotline.Application/Identity/IIdentityAppService.cs

@@ -27,7 +27,7 @@ namespace Hotline.Application.Identity
         /// <param name="dto"></param>
         /// <returns></returns>
         /// <exception cref="UserFriendlyException"></exception>
-        Task<ApiResponse<TokenOutDto>> GetThredTokenAsync(ThirdTokenInDto dto);
+        Task<TokenOutDto> GetThredTokenAsync(ThirdTokenInDto dto);
 
         Task<string> LoginAsync(LoginDto dto, CancellationToken cancellationToken);
 

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

@@ -307,7 +307,7 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
     /// <param name="dto"></param>
     /// <returns></returns>
     /// <exception cref="UserFriendlyException"></exception>
-    public async Task<ApiResponse<TokenOutDto>> GetThredTokenAsync(ThirdTokenInDto dto)
+    public async Task<TokenOutDto> GetThredTokenAsync(ThirdTokenInDto dto)
     {
         var thirdDto = dto.Adapt<ThirdTokenDto>();
         if (dto.ThirdType == EThirdType.WeChat)
@@ -316,12 +316,14 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
             thirdDto.Secret = _systemSettingCacheManager.WxOpenAppSecret;
         }
         var thirdToken = await _thirdIdentiyService.GetTokenAsync(thirdDto);
+        var phone = await _thirdIdentiyService.GetPhoneNumberAsync(thirdDto);
         var thirdAccount = await _thirdAccountRepository.QueryByOpenIdAsync(thirdToken.OpenId);
 
         // 新用户注册
         if (thirdAccount is null)
         {
             thirdAccount = thirdToken.Adapt<ThirdAccount>();
+            thirdAccount.PhoneNumber = phone.PhoneNumber;
             thirdAccount.Id = await _thirdAccountRepository.AddAsync(thirdAccount);
         }
 
@@ -337,69 +339,6 @@ public class IdentityAppService : IIdentityAppService, IScopeDependency
         var expiredSeconds = jwtOptions.Expired <= 0 ? 3600 : jwtOptions.Expired;
         await _cacheAudience.SetAsync(audience.Id, audience, TimeSpan.FromSeconds(expiredSeconds));
         var token = _jwtSecurity.EncodeJwtToken(claims, audience.Ticket);
-        if (thirdAccount.PhoneNumber.IsNullOrEmpty())
-            return new ApiResponse<TokenOutDto> 
-            {
-                Code = 201,
-                Message = "请绑定手机号码 '/Identity/third/phone'",
-                Result = new TokenOutDto(thirdAccount.CitizenType, token),
-            };
-        return new ApiResponse<TokenOutDto> { 
-            Code = 0,
-            Result = new TokenOutDto(thirdAccount.CitizenType, token),
-        };
-    }
-
-    /// <summary>
-    /// 获取微信用户手机号码
-    /// </summary>
-    /// <param name="dto"></param>
-    /// <returns></returns>
-    /// <exception cref="UserFriendlyException"></exception>
-    public async Task GetThirdPhoneAsync(ThirdPhoneInDto dto)
-    {
-        var thirdAccount = await _thirdAccountRepository.QueryByOpenIdAsync(_sessionContext.OpenId)
-             ?? throw new UserFriendlyException(401, "请重新登录");
-
-        var thirdDto = dto.Adapt<ThirdTokenDto>();
-        if (dto.ThirdType == EThirdType.WeChat)
-        {
-            thirdDto.AppId = _systemSettingCacheManager.WxOpenAppId;
-        }
-        var thirdPhone = await _thirdIdentiyService.GetPhoneNumberAsync(thirdDto);
-        if (thirdPhone.IsError)
-            throw new UserFriendlyException(thirdPhone.ErrorCode + ":" + thirdPhone.ErrorMessage);
-
-        thirdAccount.PhoneNumber = thirdPhone.PhoneNumber;
-        var guider = await _guiderInfoRepository.Queryable()
-            .Where(m => m.PhoneNumber == thirdAccount.PhoneNumber)
-            .FirstAsync();
-        if (guider is not null)
-        { // 回填网格员信息
-            thirdAccount.UserId = guider.Id;
-            thirdAccount.CitizenType = EReadPackUserType.Gukder;
-            await _thirdAccountRepository.UpdateAsync(thirdAccount);
-            return;
-        }
-
-        var citizen = await _citizenRepository.Queryable()
-            .Where(m => m.PhoneNumber == thirdAccount.PhoneNumber)
-            .FirstAsync();
-        if (citizen is not null)
-        {
-            thirdAccount.CitizenType = EReadPackUserType.Citizen;
-            thirdAccount.UserId = citizen.Id;
-            await _thirdAccountRepository.UpdateAsync(thirdAccount);
-        }
-        else
-        {
-            citizen = new Citizen
-            {
-                PhoneNumber = thirdAccount.PhoneNumber
-            };
-            thirdAccount.UserId = await _citizenRepository.AddAsync(citizen);
-            thirdAccount.CitizenType = EReadPackUserType.Citizen;
-            await _thirdAccountRepository.UpdateAsync(thirdAccount);
-        }
+        return new TokenOutDto(thirdAccount.CitizenType, token);
     }
-}
+  }

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

@@ -25,9 +25,14 @@ public class TokenOutDto
 public class ThirdTokenInDto
 {
     /// <summary>
-    /// 微信小程序中获取的Code
+    /// 微信小程序登录Code
     /// </summary>
-    public string Code { get; set; }
+    public string LoginCode { get; set; }
+
+    /// <summary>
+    /// 微信小程序手机号Code
+    /// </summary>
+    public string TelCode { get; set; }
 
     /// <summary>
     /// 第三方平台类型(不传默认微信)

+ 4 - 4
src/Hotline.WeChat/WeChatService.cs

@@ -25,15 +25,15 @@ public class WeChatService : IThirdIdentiyService
     {
         try
         {
-            var result = await SnsApi.JsCode2JsonAsync(dto.AppId, dto.Secret, dto.Code);
+            var result = await SnsApi.JsCode2JsonAsync(dto.AppId, dto.Secret, dto.LoginCode);
             if (result.errcode != ReturnCode.请求成功) throw UserFriendlyException.SameMessage("获取微信用户信息失败");
             return new ThirdTokenOutDto() { SessionKey = result.session_key, OpenId = result.openid };
 
         }
         catch (Exception e)
         {
-            var msg = e.Message;
-            throw;
+            _logger.LogError("微信登录失败:" + e.Message + "\r\n" + e.StackTrace);
+            throw UserFriendlyException.SameMessage("微信登录失败:" + e.Message);
         }
     }
 
@@ -45,7 +45,7 @@ public class WeChatService : IThirdIdentiyService
     public async Task<ThirdPhoneOutDto> GetPhoneNumberAsync(ThirdTokenDto dto)
     {
         _logger.LogInformation($"GetPhoneNumberAsync: {dto.ToJson()}");
-        var result = await BusinessApi.GetUserPhoneNumberAsync(dto.AppId, dto.Code);
+        var result = await BusinessApi.GetUserPhoneNumberAsync(dto.AppId, dto.TelCode);
         if (result.errcode != ReturnCode.请求成功)
             _logger.LogError($"GetPhoneNumberAsync: {result.ToJson()}");
         return new ThirdPhoneOutDto()