wip: update CreateWeChatUser API

This commit is contained in:
AnonymousDotNet 2024-12-13 19:37:47 +08:00
parent d41d6eddd8
commit dedf6d740b
2 changed files with 11 additions and 16 deletions

View file

@ -40,7 +40,7 @@ public class WeChatUserService : IWeChatUserService
public async Task<WeChatUser> 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,

View file

@ -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<UserViewModel> 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]