update wechat account user bind
This commit is contained in:
parent
2d7501e24b
commit
21a1e69447
|
|
@ -18,7 +18,7 @@ public partial class AgentService
|
|||
{
|
||||
var db = _services.GetRequiredService<AgentDbContext>();
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
|
@ -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<User> GetOrCreateWeChatAccountUserAsync(string appId, string openId);
|
||||
}
|
||||
}
|
||||
|
|
@ -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<User> GetOrCreateWeChatAccountUserAsync(string appId, string openId)
|
||||
{
|
||||
var user = await userService.CreateUser(new User()
|
||||
{
|
||||
Email = openId + "@" + appId + ".wechat",
|
||||
Password = openId
|
||||
});
|
||||
return user;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<IHttpContextAccessor>();
|
||||
context.HttpContext = new DefaultHttpContext();
|
||||
context.HttpContext.User = new ClaimsPrincipal(
|
||||
new ClaimsIdentity(new List<Claim>() { new Claim(ClaimTypes.NameIdentifier, openid) }));
|
||||
|
||||
var user = await GetWeChatAccountUserAsync(openid, scoped);
|
||||
BindWeChatAccountUser(user, scoped);
|
||||
|
||||
var conversationService = scoped.GetRequiredService<IConversationService>();
|
||||
|
||||
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<User> GetWeChatAccountUserAsync(string openId, IServiceProvider service)
|
||||
{
|
||||
var userService = service.GetRequiredService<IWeChatAccountUserService>();
|
||||
|
||||
return await userService.GetOrCreateWeChatAccountUserAsync(WeChatAppId, openId);
|
||||
}
|
||||
private void BindWeChatAccountUser(User user,IServiceProvider service)
|
||||
{
|
||||
var context = service.GetService<IHttpContextAccessor>();
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -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<IWeChatAccountUserService,WeChatAccountUserService> ();
|
||||
|
||||
services.AddMemoryCache();
|
||||
|
||||
services.Configure<SenparcWeixinSetting>(config.GetSection("WeChat"));
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue