From 6b7c58f3f34f51942950009e60c73d45b3affb99 Mon Sep 17 00:00:00 2001 From: wanghaomei Date: Fri, 19 May 2023 11:25:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E6=94=B6=E5=85=AC=E5=8F=B8=E4=BA=BA?= =?UTF-8?q?=E5=91=98=E5=8F=98=E5=8A=A8=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Myshipping.Core/Entity/SysTenant.cs | 6 + .../MQ/RecCompanyUserChangeService.cs | 140 ++++++++++++++++++ Myshipping.Core/Myshipping.Core.csproj | 1 + Myshipping.Core/Myshipping.Core.xml | 32 ++++ Myshipping.Core/Service/Auth/AuthService.cs | 17 ++- Myshipping.Web.Core/Startup.cs | 13 +- 6 files changed, 197 insertions(+), 12 deletions(-) create mode 100644 Myshipping.Core/MQ/RecCompanyUserChangeService.cs diff --git a/Myshipping.Core/Entity/SysTenant.cs b/Myshipping.Core/Entity/SysTenant.cs index d8cd157..a30e266 100644 --- a/Myshipping.Core/Entity/SysTenant.cs +++ b/Myshipping.Core/Entity/SysTenant.cs @@ -62,4 +62,10 @@ public class SysTenant : DEntityBase /// 租户类型 /// public TenantTypeEnum TenantType { get; set; } + + /// + /// 大简云公司ID + /// + [MaxLength(50)] + public string CompId { get; set; } } diff --git a/Myshipping.Core/MQ/RecCompanyUserChangeService.cs b/Myshipping.Core/MQ/RecCompanyUserChangeService.cs new file mode 100644 index 0000000..a14462f --- /dev/null +++ b/Myshipping.Core/MQ/RecCompanyUserChangeService.cs @@ -0,0 +1,140 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Myshipping.Core.Entity; +using Myshipping.Core.Service; +using Newtonsoft.Json.Linq; +using RabbitMQ.Client; +using RabbitMQ.Client.Events; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Yitter.IdGenerator; + +namespace Myshipping.Core.MQ +{ + /// + /// 接收公司、员工变动消息推送 + /// + public class RecCompanyUserChangeService : BackgroundService + { + private readonly IServiceScopeFactory _scopeFactory; + private readonly IServiceScope _serviceScope; + + private readonly ILogger _logger; + private readonly ISysTenantService _tenantService; + private readonly ISysUserService _userService; + private readonly SqlSugarRepository _sysUserRep; + private readonly SqlSugarRepository _sysTenantRep; + + private IConnection mqConn; + private IModel model; + + public RecCompanyUserChangeService(IServiceScopeFactory scopeFactory) + { + _scopeFactory = scopeFactory; + //通过这个注入DBContext + _serviceScope = _scopeFactory.CreateScope(); + + _logger = _serviceScope.ServiceProvider.GetService>(); + _tenantService = _serviceScope.ServiceProvider.GetService(); + _userService = _serviceScope.ServiceProvider.GetService(); + _sysUserRep = _serviceScope.ServiceProvider.GetService>(); + _sysTenantRep = _serviceScope.ServiceProvider.GetService>(); + } + + public override void Dispose() + { + base.Dispose(); + _serviceScope.Dispose(); + mqConn.Close(); + + _logger.LogInformation("RecCompanyUserChangeService Dispose"); + } + + protected override Task ExecuteAsync(CancellationToken stoppingToken) + { + _logger.LogInformation("RecCompanyUserChangeService ExecuteAsync"); + + return Task.Run(async () => + { + _logger.LogInformation("RecCompanyUserChangeService ExecuteAsync RunTask"); + + string ExchangeName = "djy.comp&user.change"; + string QueueName = $"djy.comp&user.change.{DateTime.Now.Ticks}"; + + ConnectionFactory factory = new ConnectionFactory(); + var cache = _serviceScope.ServiceProvider.GetService(); + var sysConfig = await cache.GetAllSysConfig(); + var mqUrl = sysConfig.First(x => x.Code == "CompanyUserChangeMqUrl").Value; + factory.Uri = new Uri(mqUrl); + mqConn = factory.CreateConnection("客户订舱平台"); + + model = mqConn.CreateModel(); + model.ExchangeDeclare(ExchangeName, ExchangeType.Fanout); + model.QueueDeclare(QueueName, false, false, true, null); + model.QueueBind(QueueName, ExchangeName, "", null); + + var consumer = new EventingBasicConsumer(model); + consumer.Received += Consumer_Received; + model.BasicConsume(QueueName, true, consumer); + + + }); + + } + + private void Consumer_Received(object ch, BasicDeliverEventArgs ea) + { + var body = ea.Body; + var strBody = Encoding.UTF8.GetString(body.ToArray()); + + var jobj = JObject.Parse(strBody); + var type = jobj.GetStringValue("type"); + if (type == "CompanyAudit") //新公司审核通过 + { + var comp = jobj.GetJObjectValue("company"); + var compid = comp.GetStringValue("CompId"); + var compname = comp.GetStringValue("CompName"); + var adminShowName = comp.GetStringValue("AdminShowName"); + + var findTenant = _sysTenantRep.FirstOrDefault(x => x.CompId == compid); //先根据关联ID查找 + if (findTenant == null) + { + findTenant = _sysTenantRep.FirstOrDefault(x => x.Name == compname); //再根据公司全称查找 + } + + if (findTenant == null) //找不到,新建 + { + var tenant = new SysTenant(); + tenant.Id = YitIdHelper.NextId(); + tenant.Name = compname; + tenant.AdminName = adminShowName; + tenant.Email = $"{tenant.Id}"; + tenant.TenantType = TenantTypeEnum.COMMON; + var newTenant = _sysTenantRep.InsertReturnEntity(tenant); + + _tenantService.InitNewTenant(tenant); + } + + } + else if (type == "UserJoin") //用户加入公司 + { + + } + else if (type == "UserLeave") //用户离职 + { + + } + else if (type == "CompanyUserSync") //公司及员工数据同步 + { + + } + + _logger.LogInformation($"收到消息:{strBody}"); + } + } +} diff --git a/Myshipping.Core/Myshipping.Core.csproj b/Myshipping.Core/Myshipping.Core.csproj index b02a9de..463bf24 100644 --- a/Myshipping.Core/Myshipping.Core.csproj +++ b/Myshipping.Core/Myshipping.Core.csproj @@ -54,6 +54,7 @@ + diff --git a/Myshipping.Core/Myshipping.Core.xml b/Myshipping.Core/Myshipping.Core.xml index cc3c7a7..6e66eeb 100644 --- a/Myshipping.Core/Myshipping.Core.xml +++ b/Myshipping.Core/Myshipping.Core.xml @@ -2424,6 +2424,26 @@ 是否可见 + + + 海关登记号 + + + + + 英文名 + + + + + 舱单传输人备案号 + + + + + 宣传标语 + + @@ -5816,6 +5836,11 @@ 邮箱 + + + 接收公司、员工变动消息推送 + + AccessToken参数 @@ -6111,6 +6136,13 @@ + + + 使用跳转code登录 + + + + 登录输入参数 diff --git a/Myshipping.Core/Service/Auth/AuthService.cs b/Myshipping.Core/Service/Auth/AuthService.cs index 6df3b57..f666a8d 100644 --- a/Myshipping.Core/Service/Auth/AuthService.cs +++ b/Myshipping.Core/Service/Auth/AuthService.cs @@ -343,10 +343,21 @@ public class AuthService : IAuthService, IDynamicApiController, ITransient { jData = jRtn.GetJObjectValue("data"); - var dic = new Dictionary(); - foreach (var prop in jData.Properties()) + var compId = jData.GetStringValue("compId"); + var comname = jData.GetStringValue("comname"); + var userId = jData.GetStringValue("gid"); + var showname = jData.GetStringValue("showname"); + + var tenant = _sysTenantRep.FirstOrDefault(x => x.CompId == compId); + if (tenant == null) + { + throw Oops.Bah($"{comname}不存在,请先完成公司认证!"); + } + + var user = _sysUserRep.FirstOrDefault(u => u.DjyUserId == userId); + if (tenant == null) { - dic.Add(prop.Name, jData[prop.Name]); + throw Oops.Bah($"{showname}不存在,请先加入公司{comname}!"); } var accessToken = JWTEncryption.Encrypt(dic); diff --git a/Myshipping.Web.Core/Startup.cs b/Myshipping.Web.Core/Startup.cs index 9cc2134..2c5077c 100644 --- a/Myshipping.Web.Core/Startup.cs +++ b/Myshipping.Web.Core/Startup.cs @@ -22,6 +22,7 @@ using System.Collections.Generic; using Newtonsoft.Json.Serialization; using Newtonsoft.Json; using Myshipping.Application.ConfigOption; +using Myshipping.Core.MQ; namespace Myshipping.Web.Core; @@ -69,15 +70,7 @@ public class Startup : AppStartup services.AddCorsAccessor(); // 配置远程请求 - services.AddRemoteRequest(option => - { - // 配置天气预报GZIP - option.AddHttpClient("wthrcdn", c => - { - c.BaseAddress = new Uri("http://wthrcdn.etouch.cn/"); - }).ConfigurePrimaryHttpMessageHandler(_ => - new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip }); - }); + services.AddRemoteRequest(); services.AddControllersWithViews() .AddMvcFilter() .AddMvcFilter() @@ -113,6 +106,8 @@ public class Startup : AppStartup //钉钉消息 builder.AddSubscriber(); }); + + services.AddHostedService(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env)