diff --git a/src/Shentun.ColumnReferencePlugIns/WebApiOutDtoBase.cs b/src/Shentun.Peis.Application.Contracts/WebApiOutDtoBase.cs similarity index 89% rename from src/Shentun.ColumnReferencePlugIns/WebApiOutDtoBase.cs rename to src/Shentun.Peis.Application.Contracts/WebApiOutDtoBase.cs index abea510..be75744 100644 --- a/src/Shentun.ColumnReferencePlugIns/WebApiOutDtoBase.cs +++ b/src/Shentun.Peis.Application.Contracts/WebApiOutDtoBase.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Shentun.Peis.PlugIns +namespace Shentun.Peis { public class WebApiOutDtoBase { diff --git a/src/Shentun.Peis.HttpApi.Host/AppServiceHelper.cs b/src/Shentun.Peis.HttpApi.Host/AppServiceHelper.cs index 6a0d890..53e05b9 100644 --- a/src/Shentun.Peis.HttpApi.Host/AppServiceHelper.cs +++ b/src/Shentun.Peis.HttpApi.Host/AppServiceHelper.cs @@ -25,10 +25,10 @@ namespace Shentun.Peis .Build(); _appBaseAddress = AppConfig.GetSection("App") .GetSection("SelfUrl").Value; - _appBaseAddress = AppConfig.GetSection("App") - .GetSection("SelfUrl").Value; - _appBaseAddress = AppConfig.GetSection("App") - .GetSection("SelfUrl").Value; + _selfUser = AppConfig.GetSection("App") + .GetSection("SelfUser").Value; + _selfPassword = AppConfig.GetSection("App") + .GetSection("SelfPassword").Value; } public async Task CallAppServiceAsync(string url, TInput data) @@ -48,7 +48,7 @@ namespace Shentun.Peis { sendData = JsonConvert.SerializeObject(data); } - + using (HttpContent httpContent = new StringContent(sendData)) { httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); @@ -60,7 +60,62 @@ namespace Shentun.Peis throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); } result = await response.Content.ReadAsStringAsync(); + + var resultDto = JsonConvert.DeserializeObject(result); + if (resultDto is WebApiOutDtoBase) + { + var webApiOutDtoBase = resultDto as WebApiOutDtoBase; + if (webApiOutDtoBase.Code == "-1") + { + throw new Exception($"调用服务失败{webApiOutDtoBase.Message}"); + } + } + return resultDto; + } + + } + } + } + + public TOut CallAppService(string url, TInput data) + { + string baseAddress = _appBaseAddress; + using (var httpClientHandler = new HttpClientHandler()) + { + using (var httpClient = new HttpClient(httpClientHandler)) + { + httpClient.BaseAddress = new Uri(baseAddress); + + httpClient.DefaultRequestHeaders.Accept.Add( + new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型 + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accesToken); + string sendData = ""; + if (data != null) + { + sendData = JsonConvert.SerializeObject(data); + } + + using (HttpContent httpContent = new StringContent(sendData)) + { + httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; + string result; + if (!response.IsSuccessStatusCode) + { + result = response.Content.ReadAsStringAsync().Result; + throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); + } + result = response.Content.ReadAsStringAsync().Result; + var resultDto = JsonConvert.DeserializeObject(result); + if (resultDto is WebApiOutDtoBase) + { + var webApiOutDtoBase = resultDto as WebApiOutDtoBase; + if (webApiOutDtoBase.Code == "-1") + { + throw new Exception($"调用服务失败{webApiOutDtoBase.Message}"); + } + } return resultDto; } @@ -68,10 +123,15 @@ namespace Shentun.Peis } } - public async Task LoginAsync() + public async Task LoginAsync() { await LoginAsync(_selfUser, _selfPassword); } + public void Login() + { + Login(_selfUser, _selfPassword); + return; + } public async Task LoginAsync(string userId, string password) { @@ -111,7 +171,54 @@ namespace Shentun.Peis throw new Exception($"登录失败{restultDto.Message}"); } _accesToken = restultDto.Data.access_token; - + + } + + } + } + } + + public void Login(string userId, string password) + { + + using (var httpClientHandler = new HttpClientHandler()) + { + using (var httpClient = new HttpClient(httpClientHandler)) + { + httpClient.BaseAddress = new Uri(_appBaseAddress); + + httpClient.DefaultRequestHeaders.Accept.Add( + new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型 + var url = "api/identity/users/login"; + var loginUser = new LoginInputDto() + { + UserName = userId, + Password = password + }; + var sendData = JsonConvert.SerializeObject(loginUser); + using (HttpContent httpContent = new StringContent(sendData)) + { + httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); + HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result; + string result; + if (!response.IsSuccessStatusCode) + { + result = response.Content.ReadAsStringAsync().Result; + throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result); + } + result = response.Content.ReadAsStringAsync().Result; + var restultDto = JsonConvert.DeserializeObject(result); + if (restultDto == null) + { + throw new Exception("返回结果是空"); + } + if (restultDto.Code != "1") + { + throw new Exception($"登录失败{restultDto.Message}"); + } + _accesToken = restultDto.Data.access_token; + return; + } } diff --git a/src/Shentun.Peis.HttpApi.Host/PeisHttpApiHostModule.cs b/src/Shentun.Peis.HttpApi.Host/PeisHttpApiHostModule.cs index eb2d145..8825a4a 100644 --- a/src/Shentun.Peis.HttpApi.Host/PeisHttpApiHostModule.cs +++ b/src/Shentun.Peis.HttpApi.Host/PeisHttpApiHostModule.cs @@ -559,7 +559,7 @@ public class PeisHttpApiHostModule : AbpModule app.UseConfiguredEndpoints(); //任务计划 - // await StartScheduler(context); + await StartScheduler(context); } private async Task StartScheduler(ApplicationInitializationContext context) @@ -570,17 +570,25 @@ public class PeisHttpApiHostModule : AbpModule //BackgroundJob.Enqueue(x => x.DoWorkWithArgAsync(new Guid(""))); var appServiceHelper = new AppServiceHelper(); await appServiceHelper.LoginAsync(); - var thirdInterfaceDtos = appServiceHelper.CallAppServiceAsync>("api/app/ThirdInterface/GetList", null).Result; - if (thirdInterfaceDtos != null) + var thirdInterFaceForHostOutDto = await appServiceHelper.CallAppServiceAsync("api/app/ThirdInterface/GetList", null); + if (thirdInterFaceForHostOutDto != null) { - foreach (var thirdInterfaceDto in thirdInterfaceDtos) + foreach (var thirdInterfaceDto in thirdInterFaceForHostOutDto.Data) { var parmValue = thirdInterfaceDto.ParmValue; if (!string.IsNullOrWhiteSpace(parmValue)) { var configurationBuilder = new ConfigurationBuilder() .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue))); - var interfaceConfig = configurationBuilder.Build(); + IConfigurationRoot interfaceConfig; + try + { + interfaceConfig = configurationBuilder.Build(); + } + catch (Exception ex) + { + continue; + } var isActive = interfaceConfig.GetSection("Interface").GetSection("Scheduler") .GetSection("IsActive").Value; diff --git a/src/Shentun.Peis.HttpApi.Host/Schedulers/ChargeRequestInterfaceQueryWorker.cs b/src/Shentun.Peis.HttpApi.Host/Schedulers/ChargeRequestInterfaceQueryWorker.cs index 94d7bdc..1d1f6d0 100644 --- a/src/Shentun.Peis.HttpApi.Host/Schedulers/ChargeRequestInterfaceQueryWorker.cs +++ b/src/Shentun.Peis.HttpApi.Host/Schedulers/ChargeRequestInterfaceQueryWorker.cs @@ -17,17 +17,21 @@ namespace Shentun.Peis.Schedulers { public interface IChargeRequestInterfaceQueryWorker { - public Task DoWorkAsync(Guid interfaceId); + public void DoWorkAsync(Guid interfaceId); } public class ChargeRequestInterfaceQueryWorker : HangfireBackgroundWorkerBase, IChargeRequestInterfaceQueryWorker { private string isActive = "N"; + private static long i; + private static bool _isRunning = false; + private static readonly object lockObject = new object(); + public ChargeRequestInterfaceQueryWorker() { //RecurringJobId = nameof(ChargeRequestInterfaceQueryWorker); //CronExpression = Cron.Daily(); //获取所有第三方接口 - + } public override Task DoWorkAsync(CancellationToken cancellationToken = default) @@ -35,47 +39,52 @@ namespace Shentun.Peis.Schedulers Logger.LogInformation("Executed ChargeRequestInterfaceQueryWorker..!"); return Task.CompletedTask; } - public Task DoWorkAsync(Guid interfaceId) + public void DoWorkAsync(Guid interfaceId) { + if (_isRunning) return; + //lock (lockObject) + //{ + _isRunning = true; try { - var appServiceHelper = new AppServiceHelper(); - appServiceHelper.LoginAsync(); - var thirdInterfaceDtos = appServiceHelper.CallAppServiceAsync>("api/app/ThirdInterface/GetList", null).Result; - var thirdInterfaceDto = thirdInterfaceDtos.Where(o => o.Id == interfaceId).FirstOrDefault(); - if (thirdInterfaceDto == null) - { - return Task.CompletedTask; - } - - var parmValue = thirdInterfaceDto.ParmValue; - if (!string.IsNullOrWhiteSpace(parmValue)) - { - var configurationBuilder = new ConfigurationBuilder() - .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue))); - var interfaceConfig = configurationBuilder.Build(); + var appServiceHelper = new AppServiceHelper(); + appServiceHelper.Login(); + var thirdInterFaceForHostOutDto = appServiceHelper.CallAppService("api/app/ThirdInterface/GetList", null); + var thirdInterfaceDto = thirdInterFaceForHostOutDto.Data.Where(o => o.Id == interfaceId).FirstOrDefault(); + if (thirdInterfaceDto == null) + { + return ; + } - isActive = interfaceConfig.GetSection("Interface").GetSection("Scheduler") - .GetSection("IsActive").Value; - if (isActive != "Y") + var parmValue = thirdInterfaceDto.ParmValue; + if (!string.IsNullOrWhiteSpace(parmValue)) { - return Task.CompletedTask; + var configurationBuilder = new ConfigurationBuilder() + .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue))); + var interfaceConfig = configurationBuilder.Build(); + + isActive = interfaceConfig.GetSection("Interface").GetSection("Scheduler") + .GetSection("IsActive").Value; + if (isActive != "Y") + { + return ; + } + var assemblyName = interfaceConfig.GetSection("Interface").GetSection("AssemblyName").Value; + var className = interfaceConfig.GetSection("Interface").GetSection("ClassName").Value; + var funName = "DoWorkAsync"; + //object[] objects = new object[] { chargeRequestPlugInsInput }; + ReflectionHelper.InvokeAsync(assemblyName, className, parmValue, funName); } - var assemblyName = interfaceConfig.GetSection("Interface").GetSection("AssemblyName").Value; - var className = interfaceConfig.GetSection("Interface").GetSection("ClassName").Value; - var funName = "DoWorkAsync"; - //object[] objects = new object[] { chargeRequestPlugInsInput }; - ReflectionHelper.InvokeAsync(assemblyName, className, parmValue, funName); - } - Logger.LogInformation("Executed ChargeRequestInterfaceQueryWorker..!"); + Logger.LogInformation("Executed ChargeRequestInterfaceQueryWorker..!"); - } - catch (Exception ex) - { - Logger.LogError("Executed ChargeRequestInterfaceQueryWorker Error" + ex.Message); - } - - return Task.CompletedTask; + } + catch (Exception ex) + { + Logger.LogError("Executed ChargeRequestInterfaceQueryWorker Error" + ex.Message); + } + _isRunning = false; + return ; + //} } } } diff --git a/src/Shentun.Peis.HttpApi.Host/Schedulers/ThirdInterFaceForHostOutDto.cs b/src/Shentun.Peis.HttpApi.Host/Schedulers/ThirdInterFaceForHostOutDto.cs new file mode 100644 index 0000000..59e2902 --- /dev/null +++ b/src/Shentun.Peis.HttpApi.Host/Schedulers/ThirdInterFaceForHostOutDto.cs @@ -0,0 +1,10 @@ +using Shentun.Peis.ThirdInterfaces; +using System.Collections.Generic; + +namespace Shentun.Peis.Schedulers +{ + public class ThirdInterFaceForHostOutDto:WebApiOutDtoBase + { + public List Data { get; set; } + } +} diff --git a/src/Shentun.Peis.HttpApi.Host/Shentun.Peis.HttpApi.Host.csproj b/src/Shentun.Peis.HttpApi.Host/Shentun.Peis.HttpApi.Host.csproj index 7bf9191..3aaa500 100644 --- a/src/Shentun.Peis.HttpApi.Host/Shentun.Peis.HttpApi.Host.csproj +++ b/src/Shentun.Peis.HttpApi.Host/Shentun.Peis.HttpApi.Host.csproj @@ -41,6 +41,7 @@ + diff --git a/src/Shentun.Peis.HttpApi.Host/appsettings.json b/src/Shentun.Peis.HttpApi.Host/appsettings.json index e185dbf..e209394 100644 --- a/src/Shentun.Peis.HttpApi.Host/appsettings.json +++ b/src/Shentun.Peis.HttpApi.Host/appsettings.json @@ -1,7 +1,7 @@ { "App": { - "SelfUrl": "http://10.1.12.140:9529", - //"SelfUrl": "http://localhost:9530", + //"SelfUrl": "http://10.1.12.140:9529", + "SelfUrl": "http://localhost:9530", "ClientUrl": "http://localhost:9530", "CorsOrigins": "https://*.Peis.com,http://localhost:4200,http://localhost:9530,http://192.168.1.108:9530,http://localhost:8080,http://localhost:8081", "RedirectAllowedUrls": "http://localhost:9530",