From d87afc7151e69b697f8f761754119469f4cd23a7 Mon Sep 17 00:00:00 2001 From: AnonymousDotNet <18776095145@163.com> Date: Sat, 14 Dec 2024 15:55:16 +0800 Subject: [PATCH] revert: #57 --- .../Repositories/IBotSharpRepository.cs | 4 - .../Users/IWeChatUserService.cs | 14 --- .../Users/Models/WeChatUser.cs | 73 ----------- .../Users/Services/WeChatUserService.cs | 113 ------------------ .../Controllers/UserController.cs | 21 +--- .../Collections/WeChatUserDocument.cs | 75 ------------ .../MongoDbContext.cs | 2 - .../Repository/MongoRepository.WeChatUser.cs | 58 --------- src/WebStarter/appsettings.json | 10 -- 9 files changed, 1 insertion(+), 369 deletions(-) delete mode 100644 src/Infrastructure/BotSharp.Abstraction/Users/IWeChatUserService.cs delete mode 100644 src/Infrastructure/BotSharp.Abstraction/Users/Models/WeChatUser.cs delete mode 100644 src/Infrastructure/BotSharp.Core/Users/Services/WeChatUserService.cs delete mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Collections/WeChatUserDocument.cs delete mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.WeChatUser.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index aafb5b5f..3fca7372 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -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 diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/IWeChatUserService.cs b/src/Infrastructure/BotSharp.Abstraction/Users/IWeChatUserService.cs deleted file mode 100644 index a6b69865..00000000 --- a/src/Infrastructure/BotSharp.Abstraction/Users/IWeChatUserService.cs +++ /dev/null @@ -1,14 +0,0 @@ -using BotSharp.Abstraction.Users.Models; - -namespace BotSharp.Abstraction.Users; - -public interface IWeChatUserService -{ - Task GetWeChatUser(string openId); - - Task CreateWeChatUser(WeChatUser weChatUser); - - Task UpdateWeChatUser(WeChatUser weChatUser); - - Task WeChatUserLogin(string code); -} diff --git a/src/Infrastructure/BotSharp.Abstraction/Users/Models/WeChatUser.cs b/src/Infrastructure/BotSharp.Abstraction/Users/Models/WeChatUser.cs deleted file mode 100644 index 165787a0..00000000 --- a/src/Infrastructure/BotSharp.Abstraction/Users/Models/WeChatUser.cs +++ /dev/null @@ -1,73 +0,0 @@ -using BotSharp.Abstraction.Users.Enums; - -namespace BotSharp.Abstraction.Users.Models; - -public class WeChatUser -{ - public string Id { get; set; } = string.Empty; - - /// - /// User unique identifier (unique under the current application) - /// - public string OpenId { get; set; } = string.Empty; - - /// - /// User unique identifier (cross application unique, requiring open platform binding) - /// - public string UnionId { get; set; } = string.Empty; - - /// - /// User's gender: 1- Male, 2- Female, 0- Unknown - /// - public int Sex { get; set; } - - /// - /// The province where the user's personal information is filled in - /// - public string Province { get; set; } = string.Empty; - - public string City { get; set; } = string.Empty; - - public string NickName { get; set; } = string.Empty; - - /// - /// User avatar URL (46/64/96/132/0 pixels) - /// - public string Headimgurl { get; set; } = string.Empty; - - public string PhoneNumber { get; set; } = string.Empty; - - /// - /// The country where the user is located, such as China CN - /// - public string Country { get; set; } = "CN"; - - /// - /// User privilege information (such as WeChat membership, etc.) - /// - public string[] Privilege { get; set; } = Array.Empty(); - - //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, - }; - } -} diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/WeChatUserService.cs b/src/Infrastructure/BotSharp.Core/Users/Services/WeChatUserService.cs deleted file mode 100644 index 55b8e0e9..00000000 --- a/src/Infrastructure/BotSharp.Core/Users/Services/WeChatUserService.cs +++ /dev/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 logger, IConfiguration configuration) - { - _services = services; - _logger = logger; - _configuration = configuration; - } - - public async Task GetWeChatUser(string openId) - { - var db = _services.GetRequiredService(); - var weChatUser = db.GetWeChatUser(openId); - return weChatUser; - } - - public async Task CreateWeChatUser(WeChatUser weChatUser) - { - var db = _services.GetRequiredService(); - return db.CreateWeChatUser(weChatUser); - } - - public async Task UpdateWeChatUser(WeChatUser weChatUser) - { - var db = _services.GetRequiredService(); - return db.UpdateWeChatUser(weChatUser); - } - - #region 微信扫码登录(OAuth 2.0 网页授权) - - public async Task 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 网页授权) -} diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs index 964e8aee..c893bcab 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/UserController.cs @@ -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 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> ActivateUser(UserActivationModel model) diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/WeChatUserDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/WeChatUserDocument.cs deleted file mode 100644 index c85c7f4e..00000000 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/WeChatUserDocument.cs +++ /dev/null @@ -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; - - /// - /// User unique identifier (unique under the current application) - /// - public string OpenId { get; set; } = string.Empty; - - /// - /// User unique identifier (cross application unique, requiring open platform binding) - /// - public string UnionId { get; set; } = string.Empty; - - /// - /// User's gender: 1- Male, 2- Female, 0- Unknown - /// - public int Sex { get; set; } - - /// - /// The province where the user's personal information is filled in - /// - public string Province { get; set; } = string.Empty; - - public string City { get; set; } = string.Empty; - - public string NickName { get; set; } = string.Empty; - - /// - /// User avatar URL (46/64/96/132/0 pixels) - /// - public string Headimgurl { get; set; } = string.Empty; - - public string PhoneNumber { get; set; } = string.Empty; - - /// - /// The country where the user is located, such as China CN - /// - public string Country { get; set; } = "CN"; - - /// - /// User privilege information (such as WeChat membership, etc.) - /// - public string[] Privilege { get; set; } = Array.Empty(); - - //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 - }; - } -} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index 4c633dd9..82e9f0d4 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -169,6 +169,4 @@ public class MongoDbContext public IMongoCollection CrontabItems => Database.GetCollection($"{_collectionPrefix}_CronTabItems"); - public IMongoCollection WeChatUsers - => Database.GetCollection($"{_collectionPrefix}_WeChatUsers"); } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.WeChatUser.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.WeChatUser.cs deleted file mode 100644 index 91cca0bf..00000000 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.WeChatUser.cs +++ /dev/null @@ -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.Filter.Eq(x => x.Id, weChatUser.Id); - var update = Builders.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; - } -} diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index 3efc0b4d..c3d4bdd5 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -273,16 +273,6 @@ "WeixinAppSecret": "#{WeixinAppSecret}#" }, - "WeChatQtoss": { - "AppId": "", - "AppSecret": "" - }, - - "WeChatQtossAI": { - "AppId": "", - "AppSecret": "" - }, - "KnowledgeBase": { "VectorDb": { "Provider": "Qdrant"