revert: #57
This commit is contained in:
parent
802401690b
commit
d87afc7151
|
|
@ -50,10 +50,6 @@ public interface IBotSharpRepository : IHaveServiceProvider
|
|||
User? GetUserDetails(string userId, bool includeAgent = false) => throw new NotImplementedException();
|
||||
bool UpdateUser(User user, bool updateUserAgents = false) => throw new NotImplementedException();
|
||||
|
||||
WeChatUser? GetWeChatUser(string openId) => throw new NotImplementedException();
|
||||
WeChatUser CreateWeChatUser(WeChatUser weChatUser) => throw new NotImplementedException();
|
||||
WeChatUser? UpdateWeChatUser(WeChatUser weChatUser) => throw new NotImplementedException();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Agent
|
||||
|
|
|
|||
|
|
@ -1,14 +0,0 @@
|
|||
using BotSharp.Abstraction.Users.Models;
|
||||
|
||||
namespace BotSharp.Abstraction.Users;
|
||||
|
||||
public interface IWeChatUserService
|
||||
{
|
||||
Task<WeChatUser?> GetWeChatUser(string openId);
|
||||
|
||||
Task<WeChatUser?> CreateWeChatUser(WeChatUser weChatUser);
|
||||
|
||||
Task<WeChatUser?> UpdateWeChatUser(WeChatUser weChatUser);
|
||||
|
||||
Task<WeChatUser> WeChatUserLogin(string code);
|
||||
}
|
||||
|
|
@ -1,73 +0,0 @@
|
|||
using BotSharp.Abstraction.Users.Enums;
|
||||
|
||||
namespace BotSharp.Abstraction.Users.Models;
|
||||
|
||||
public class WeChatUser
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// User unique identifier (unique under the current application)
|
||||
/// </summary>
|
||||
public string OpenId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// User unique identifier (cross application unique, requiring open platform binding)
|
||||
/// </summary>
|
||||
public string UnionId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// User's gender: 1- Male, 2- Female, 0- Unknown
|
||||
/// </summary>
|
||||
public int Sex { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The province where the user's personal information is filled in
|
||||
/// </summary>
|
||||
public string Province { get; set; } = string.Empty;
|
||||
|
||||
public string City { get; set; } = string.Empty;
|
||||
|
||||
public string NickName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// User avatar URL (46/64/96/132/0 pixels)
|
||||
/// </summary>
|
||||
public string Headimgurl { get; set; } = string.Empty;
|
||||
|
||||
public string PhoneNumber { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// The country where the user is located, such as China CN
|
||||
/// </summary>
|
||||
public string Country { get; set; } = "CN";
|
||||
|
||||
/// <summary>
|
||||
/// User privilege information (such as WeChat membership, etc.)
|
||||
/// </summary>
|
||||
public string[] Privilege { get; set; } = Array.Empty<string>();
|
||||
|
||||
//public string AppId { get; set; } = string.Empty;
|
||||
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public User ToUser()
|
||||
{
|
||||
return new User
|
||||
{
|
||||
Id = Id,
|
||||
Phone = PhoneNumber,
|
||||
UserName = PhoneNumber,
|
||||
FirstName = PhoneNumber,
|
||||
LastName = PhoneNumber,
|
||||
Email = null,
|
||||
Password = string.Empty,
|
||||
Role = UserRole.User,
|
||||
Type = UserType.Client,
|
||||
RegionCode = "CN",
|
||||
ReferralCode = null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,113 +0,0 @@
|
|||
using BotSharp.Abstraction.Users.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace BotSharp.Core.Users.Services;
|
||||
|
||||
public class WeChatUserService : IWeChatUserService
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public WeChatUserService(IServiceProvider services, ILogger<WeChatUserService> logger, IConfiguration configuration)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public async Task<WeChatUser?> GetWeChatUser(string openId)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var weChatUser = db.GetWeChatUser(openId);
|
||||
return weChatUser;
|
||||
}
|
||||
|
||||
public async Task<WeChatUser> CreateWeChatUser(WeChatUser weChatUser)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
return db.CreateWeChatUser(weChatUser);
|
||||
}
|
||||
|
||||
public async Task<WeChatUser?> UpdateWeChatUser(WeChatUser weChatUser)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
return db.UpdateWeChatUser(weChatUser);
|
||||
}
|
||||
|
||||
#region 微信扫码登录(OAuth 2.0 网页授权)
|
||||
|
||||
public async Task<WeChatUser> WeChatUserLogin(string code)
|
||||
{
|
||||
// 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!");
|
||||
throw new Exception("code is empty");
|
||||
}
|
||||
|
||||
var appId = _configuration["WeChatQtoss:AppId"] ?? throw new Exception("AppId is not configured.");
|
||||
var appSecret = _configuration["WeChatQtoss:AppSecret"] ?? throw new Exception("AppSecret is not configured.");
|
||||
|
||||
try
|
||||
{
|
||||
// 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();
|
||||
var tokenResponse = await httpClient.GetStringAsync(tokenUrl);
|
||||
|
||||
var tokenData = JObject.Parse(tokenResponse);
|
||||
var accessToken = tokenData["access_token"]?.ToString();
|
||||
var openId = tokenData["openid"]?.ToString();
|
||||
|
||||
if (string.IsNullOrEmpty(accessToken) || string.IsNullOrEmpty(openId))
|
||||
{
|
||||
_logger.LogError("Create WeChatUser Error: Failed to get access_token or openid");
|
||||
throw new Exception("Failed to get access_token or openid");
|
||||
}
|
||||
|
||||
// Step 2: Retrieve user information using access_token and openid
|
||||
var userInfoUrl = $"https://api.weixin.qq.com/sns/userinfo?access_token={accessToken}&openid={openId}&lang=zh_CN";
|
||||
|
||||
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() ?? "CN";
|
||||
var province = userInfo["province"]?.ToString() ?? string.Empty;
|
||||
var city = userInfo["city"]?.ToString() ?? string.Empty;
|
||||
var sex = userInfo["sex"]?.ToString() ?? "0";
|
||||
var privilege = userInfo["privilege"]?.ToString() ?? string.Empty;
|
||||
var unionId = userInfo["unionId"]?.ToString() ?? string.Empty;
|
||||
|
||||
// Create or update WeChatUser
|
||||
var weChatUser = new WeChatUser
|
||||
{
|
||||
OpenId = openId,
|
||||
NickName = nickname,
|
||||
Sex = int.Parse(sex),
|
||||
Province = province,
|
||||
City = city,
|
||||
Country = ((country != "CN" && country.Contains("CN")) ? "CN" : country),
|
||||
Headimgurl = avatar,
|
||||
Privilege = privilege.Split(','),
|
||||
UnionId = unionId
|
||||
};
|
||||
|
||||
return await CreateWeChatUser(weChatUser);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Create WeChatUser Error: {ex.Message}");
|
||||
throw new Exception($"Failed to create WeChatUser: {ex.Message}", ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endregion 微信扫码登录(OAuth 2.0 网页授权)
|
||||
}
|
||||
|
|
@ -13,20 +13,17 @@ public class UserController : ControllerBase
|
|||
private readonly IUserService _userService;
|
||||
private readonly IUserIdentity _user;
|
||||
private readonly AccountSetting _setting;
|
||||
private readonly IWeChatUserService _weChatUserService;
|
||||
|
||||
public UserController(
|
||||
IUserService userService,
|
||||
IServiceProvider services,
|
||||
IUserIdentity user,
|
||||
AccountSetting setting,
|
||||
IWeChatUserService weChatUserService)
|
||||
AccountSetting setting)
|
||||
{
|
||||
_services = services;
|
||||
_userService = userService;
|
||||
_user = user;
|
||||
_setting = setting;
|
||||
_weChatUserService = weChatUserService;
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
|
|
@ -74,22 +71,6 @@ public class UserController : ControllerBase
|
|||
return UserViewModel.FromUser(createdUser);
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpPost("/user/wechat")]
|
||||
public async Task<UserViewModel> CreateWeChatUser(string code)
|
||||
{
|
||||
// Get WeChat User Info
|
||||
var wechatUserInfo = await _weChatUserService.WeChatUserLogin(code);
|
||||
|
||||
// Create WeChatUser
|
||||
var weChatUser = await _weChatUserService.CreateWeChatUser(wechatUserInfo);
|
||||
|
||||
// Create User
|
||||
var createdUser = await _userService.CreateUser(weChatUser.ToUser());
|
||||
|
||||
return UserViewModel.FromUser(createdUser);
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpPost("/user/activate")]
|
||||
public async Task<ActionResult<Token>> ActivateUser(UserActivationModel model)
|
||||
|
|
|
|||
|
|
@ -1,75 +0,0 @@
|
|||
using BotSharp.Abstraction.Users.Models;
|
||||
|
||||
namespace BotSharp.Plugin.MongoStorage.Collections;
|
||||
|
||||
public class WeChatUserDocument : MongoBase
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// User unique identifier (unique under the current application)
|
||||
/// </summary>
|
||||
public string OpenId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// User unique identifier (cross application unique, requiring open platform binding)
|
||||
/// </summary>
|
||||
public string UnionId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// User's gender: 1- Male, 2- Female, 0- Unknown
|
||||
/// </summary>
|
||||
public int Sex { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The province where the user's personal information is filled in
|
||||
/// </summary>
|
||||
public string Province { get; set; } = string.Empty;
|
||||
|
||||
public string City { get; set; } = string.Empty;
|
||||
|
||||
public string NickName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// User avatar URL (46/64/96/132/0 pixels)
|
||||
/// </summary>
|
||||
public string Headimgurl { get; set; } = string.Empty;
|
||||
|
||||
public string PhoneNumber { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// The country where the user is located, such as China CN
|
||||
/// </summary>
|
||||
public string Country { get; set; } = "CN";
|
||||
|
||||
/// <summary>
|
||||
/// User privilege information (such as WeChat membership, etc.)
|
||||
/// </summary>
|
||||
public string[] Privilege { get; set; } = Array.Empty<string>();
|
||||
|
||||
//public string AppId { get; set; } = string.Empty;
|
||||
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public WeChatUser ToWeChatUser()
|
||||
{
|
||||
return new WeChatUser
|
||||
{
|
||||
Id = Id,
|
||||
OpenId = OpenId,
|
||||
UnionId = UnionId,
|
||||
Sex = Sex,
|
||||
Province = Province,
|
||||
City = City,
|
||||
NickName = NickName,
|
||||
Headimgurl = Headimgurl,
|
||||
PhoneNumber = PhoneNumber,
|
||||
Country = Country,
|
||||
Privilege = Privilege,
|
||||
CreatedAt = CreatedAt,
|
||||
UpdatedAt = UpdatedAt
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -169,6 +169,4 @@ public class MongoDbContext
|
|||
public IMongoCollection<CrontabItemDocument> CrontabItems
|
||||
=> Database.GetCollection<CrontabItemDocument>($"{_collectionPrefix}_CronTabItems");
|
||||
|
||||
public IMongoCollection<WeChatUserDocument> WeChatUsers
|
||||
=> Database.GetCollection<WeChatUserDocument>($"{_collectionPrefix}_WeChatUsers");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,58 +0,0 @@
|
|||
using BotSharp.Abstraction.Users.Models;
|
||||
|
||||
namespace BotSharp.Plugin.MongoStorage.Repository;
|
||||
|
||||
public partial class MongoRepository
|
||||
{
|
||||
public WeChatUser? GetWeChatUser(string openId)
|
||||
{
|
||||
var weChatUser = _dc.WeChatUsers.AsQueryable().FirstOrDefault(x => x.OpenId == openId);
|
||||
return weChatUser != null ? weChatUser.ToWeChatUser() : null;
|
||||
}
|
||||
|
||||
public WeChatUser CreateWeChatUser(WeChatUser weChatUser)
|
||||
{
|
||||
var weChatUserInfo = new WeChatUserDocument
|
||||
{
|
||||
Id = weChatUser.Id ?? Guid.NewGuid().ToString(),
|
||||
OpenId = weChatUser.OpenId,
|
||||
UnionId = weChatUser.UnionId,
|
||||
Sex = weChatUser.Sex,
|
||||
Province = weChatUser.Province,
|
||||
City = weChatUser.City,
|
||||
NickName = weChatUser.NickName,
|
||||
Headimgurl = weChatUser.Headimgurl,
|
||||
PhoneNumber = weChatUser.PhoneNumber,
|
||||
Country = weChatUser.Country,
|
||||
Privilege = weChatUser.Privilege,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
};
|
||||
|
||||
_dc.WeChatUsers.InsertOne(weChatUserInfo);
|
||||
|
||||
return weChatUserInfo.ToWeChatUser();
|
||||
}
|
||||
|
||||
public WeChatUser? UpdateWeChatUser(WeChatUser weChatUser)
|
||||
{
|
||||
if (weChatUser == null) return null;
|
||||
|
||||
var filter = Builders<WeChatUserDocument>.Filter.Eq(x => x.Id, weChatUser.Id);
|
||||
var update = Builders<WeChatUserDocument>.Update
|
||||
.Set(x => x.OpenId, weChatUser.OpenId)
|
||||
.Set(x => x.UnionId, weChatUser.UnionId)
|
||||
.Set(x => x.Sex, weChatUser.Sex)
|
||||
.Set(x => x.Province, weChatUser.Province)
|
||||
.Set(x => x.City, weChatUser.City)
|
||||
.Set(x => x.NickName, weChatUser.NickName)
|
||||
.Set(x => x.Headimgurl, weChatUser.Headimgurl)
|
||||
.Set(x => x.PhoneNumber, weChatUser.PhoneNumber)
|
||||
.Set(x => x.Country, weChatUser.Country)
|
||||
.Set(x => x.Privilege, weChatUser.Privilege)
|
||||
.Set(x => x.UpdatedAt, DateTime.UtcNow);
|
||||
|
||||
_dc.WeChatUsers.UpdateOne(filter, update);
|
||||
|
||||
return weChatUser;
|
||||
}
|
||||
}
|
||||
|
|
@ -273,16 +273,6 @@
|
|||
"WeixinAppSecret": "#{WeixinAppSecret}#"
|
||||
},
|
||||
|
||||
"WeChatQtoss": {
|
||||
"AppId": "",
|
||||
"AppSecret": ""
|
||||
},
|
||||
|
||||
"WeChatQtossAI": {
|
||||
"AppId": "",
|
||||
"AppSecret": ""
|
||||
},
|
||||
|
||||
"KnowledgeBase": {
|
||||
"VectorDb": {
|
||||
"Provider": "Qdrant"
|
||||
|
|
|
|||
Loading…
Reference in a new issue