From dedf6d740bbef319951cb1c7983f6d52e2622594 Mon Sep 17 00:00:00 2001 From: AnonymousDotNet <18776095145@163.com> Date: Fri, 13 Dec 2024 19:37:47 +0800 Subject: [PATCH] wip: update CreateWeChatUser API --- .../Users/Services/WeChatUserService.cs | 8 ++++---- .../Controllers/UserController.cs | 19 +++++++------------ 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/WeChatUserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/WeChatUserService.cs index d79c1648..1da9b47e 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/WeChatUserService.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/WeChatUserService.cs @@ -40,7 +40,7 @@ public class WeChatUserService : IWeChatUserService public async Task WeChatUserLogin(string code) { - // 实现获取 access_token 和 openid,再获取用户信息 + // Implement obtaining access_token and openid, and then obtaining user information if (string.IsNullOrEmpty(code)) { _logger.LogError("Create WeChatUser Error: code is empty, Please check if the code has been obtained correctly!"); @@ -52,7 +52,7 @@ public class WeChatUserService : IWeChatUserService try { - // Step 1: 使用 code 获取 access_token 和 openid + // Step 1: Use the code to obtain the access_token and openid var tokenUrl = $"https://api.weixin.qq.com/sns/oauth2/access_token?appid={appId}&secret={appSecret}&code={code}&grant_type=authorization_code"; using var httpClient = new HttpClient(); @@ -74,7 +74,7 @@ public class WeChatUserService : IWeChatUserService var userInfoResponse = await httpClient.GetStringAsync(userInfoUrl); var userInfo = JObject.Parse(userInfoResponse); - // 提取用户信息 + // Retrieve user information var nickname = userInfo["nickname"]?.ToString() ?? string.Empty; var avatar = userInfo["headimgurl"]?.ToString() ?? string.Empty; var country = userInfo["country"]?.ToString() ?? string.Empty; @@ -84,7 +84,7 @@ public class WeChatUserService : IWeChatUserService var privilege = userInfo["privilege"]?.ToString() ?? string.Empty; var unionId = userInfo["unionId"]?.ToString() ?? string.Empty; - // 创建或更新 WeChatUser + // Create or update WeChatUser var weChatUser = new WeChatUser { OpenId = openId, diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs index a39200e2..964e8aee 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs @@ -74,25 +74,20 @@ public class UserController : ControllerBase return UserViewModel.FromUser(createdUser); } + [AllowAnonymous] [HttpPost("/user/wechat")] - public async Task CreateWeChatUser(string code) + public async Task CreateWeChatUser(string code) { // Get WeChat User Info - var wechatUser = await _weChatUserService.WeChatUserLogin(code); - if (wechatUser == null) - { - return; - } + var wechatUserInfo = await _weChatUserService.WeChatUserLogin(code); - var weChatUser = await _weChatUserService.CreateWeChatUser(wechatUser); - - if (wechatUser == null) - { - return; - } + // Create WeChatUser + var weChatUser = await _weChatUserService.CreateWeChatUser(wechatUserInfo); // Create User var createdUser = await _userService.CreateUser(weChatUser.ToUser()); + + return UserViewModel.FromUser(createdUser); } [AllowAnonymous]