diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index e50426d7..cdbdf95b 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -18,7 +18,7 @@ public partial class AgentService { var db = _services.GetRequiredService(); var query = from agent in db.Agent - where agent.OwnerId == _user.Id && agent.Id == id + where agent.Id == id select agent.ToAgent(); var profile = query.FirstOrDefault(); diff --git a/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs b/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs deleted file mode 100644 index dc22732a..00000000 --- a/src/Plugins/BotSharp.Plugin.WeChat/Repository/DbTables/WeChatAccount.cs +++ /dev/null @@ -1,26 +0,0 @@ -using BotSharp.Core.Repository.Abstraction; -using EntityFrameworkCore.BootKit; -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; -using System.Text; - -namespace BotSharp.Plugin.WeChat.Repository.DbTables -{ - [Table("WeChatAccount")] - public class WeChatAccount : DbRecord, IAgentTable - { - [Required] - [MaxLength(18)] - public string WeChatAppId { get; set; } - - [Required] - [MaxLength(36)] - public string WeChatOpenId { get; set; } - - [Required] - [MaxLength(36)] - public string UserId { get; set; } - } -} diff --git a/src/Plugins/BotSharp.Plugin.WeChat/Users/IWeChatAccountUserService.cs b/src/Plugins/BotSharp.Plugin.WeChat/Users/IWeChatAccountUserService.cs new file mode 100644 index 00000000..68f21019 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.WeChat/Users/IWeChatAccountUserService.cs @@ -0,0 +1,11 @@ +using BotSharp.Abstraction.Users; +using BotSharp.Abstraction.Users.Models; +using System.Threading.Tasks; + +namespace BotSharp.Plugin.WeChat.Users +{ + public interface IWeChatAccountUserService + { + Task GetOrCreateWeChatAccountUserAsync(string appId, string openId); + } +} diff --git a/src/Plugins/BotSharp.Plugin.WeChat/Users/WeChatAccountUserService.cs b/src/Plugins/BotSharp.Plugin.WeChat/Users/WeChatAccountUserService.cs new file mode 100644 index 00000000..d9fce4e9 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.WeChat/Users/WeChatAccountUserService.cs @@ -0,0 +1,31 @@ +using BotSharp.Abstraction.Users; +using BotSharp.Abstraction.Users.Models; +using BotSharp.Core.Repository; +using BotSharp.Plugin.WeChat.Repository.DbTables; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace BotSharp.Plugin.WeChat.Users +{ + public class WeChatAccountUserService : IWeChatAccountUserService + { + private readonly IUserService userService; + + public WeChatAccountUserService(IUserService userService) + { + this.userService = userService; + } + public async Task GetOrCreateWeChatAccountUserAsync(string appId, string openId) + { + var user = await userService.CreateUser(new User() + { + Email = openId + "@" + appId + ".wechat", + Password = openId + }); + return user; + } + } +} diff --git a/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs b/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs index 65a7f600..d8946603 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/WeChatBackgroundService.cs @@ -2,6 +2,9 @@ using BotSharp.Abstraction.Agents; using BotSharp.Abstraction.Conversations; using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Models; +using BotSharp.Abstraction.Users; +using BotSharp.Abstraction.Users.Models; +using BotSharp.Plugin.WeChat.Users; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; using Microsoft.Extensions.DependencyInjection; @@ -39,20 +42,18 @@ namespace BotSharp.Plugin.WeChat private async Task HandleTextMessageAsync(string openid, string message) { var scoped = _service.CreateScope().ServiceProvider; - var context = scoped.GetService(); - context.HttpContext = new DefaultHttpContext(); - context.HttpContext.User = new ClaimsPrincipal( - new ClaimsIdentity(new List() { new Claim(ClaimTypes.NameIdentifier, openid) })); + var user = await GetWeChatAccountUserAsync(openid, scoped); + BindWeChatAccountUser(user, scoped); var conversationService = scoped.GetRequiredService(); var latestConversationId = (await conversationService.GetConversations()).OrderByDescending(_ => _.CreatedTime).FirstOrDefault()?.Id; latestConversationId ??= (await conversationService.NewConversation(new Conversation() - { - UserId = openid, - AgentId = AgentId + { + UserId = openid, + AgentId = AgentId }))?.Id; var result = await conversationService.SendMessage(AgentId, latestConversationId, new RoleDialogModel @@ -64,6 +65,31 @@ namespace BotSharp.Plugin.WeChat await ReplyTextMessageAsync(openid, result); } + private async Task GetWeChatAccountUserAsync(string openId, IServiceProvider service) + { + var userService = service.GetRequiredService(); + + return await userService.GetOrCreateWeChatAccountUserAsync(WeChatAppId, openId); + } + private void BindWeChatAccountUser(User user,IServiceProvider service) + { + var context = service.GetService(); + var claims = new List + { + new Claim(ClaimTypes.NameIdentifier, user.Id) + }; + + if (!string.IsNullOrWhiteSpace(user.Email)) + { + claims.Add(new Claim(ClaimTypes.Email, user.Email)); + } + + context.HttpContext = new DefaultHttpContext + { + User = new ClaimsPrincipal(new ClaimsIdentity(claims)) + }; + } + private async Task ReplyTextMessageAsync(string openid, string content) { await Senparc.Weixin.MP.AdvancedAPIs.CustomApi.SendTextAsync(WeChatAppId, openid, content); diff --git a/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs b/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs index 0739ebd5..b81d8ea4 100644 --- a/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.WeChat/WeChatPlugin.cs @@ -18,6 +18,7 @@ using Senparc.CO2NET.RegisterServices; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Http; using BotSharp.Abstraction.Users; +using BotSharp.Plugin.WeChat.Users; namespace BotSharp.Plugin.WeChat { @@ -26,6 +27,8 @@ namespace BotSharp.Plugin.WeChat { public void RegisterDI(IServiceCollection services, IConfiguration config) { + services.AddScoped (); + services.AddMemoryCache(); services.Configure(config.GetSection("WeChat")); diff --git a/src/Plugins/BotSharp.Plugin.WeChat/WeChatUserIdentity.cs b/src/Plugins/BotSharp.Plugin.WeChat/WeChatUserIdentity.cs deleted file mode 100644 index a6edbf81..00000000 --- a/src/Plugins/BotSharp.Plugin.WeChat/WeChatUserIdentity.cs +++ /dev/null @@ -1,19 +0,0 @@ -using BotSharp.Abstraction.Users; -using System; -using System.Collections.Generic; -using System.Text; - -namespace BotSharp.Plugin.WeChat -{ - public class WeChatUserIdentity : IUserIdentity - { - - public string Id => throw new NotImplementedException(); - - public string Email => throw new NotImplementedException(); - - public string FirstName => throw new NotImplementedException(); - - public string LastName => throw new NotImplementedException(); - } -}