Browse Source

收费申请

bjmzak
DESKTOP-G961P6V\Zhh 2 years ago
parent
commit
af39310e46
  1. 2
      src/Shentun.Peis.Application.Contracts/WebApiOutDtoBase.cs
  2. 121
      src/Shentun.Peis.HttpApi.Host/AppServiceHelper.cs
  3. 18
      src/Shentun.Peis.HttpApi.Host/PeisHttpApiHostModule.cs
  4. 81
      src/Shentun.Peis.HttpApi.Host/Schedulers/ChargeRequestInterfaceQueryWorker.cs
  5. 10
      src/Shentun.Peis.HttpApi.Host/Schedulers/ThirdInterFaceForHostOutDto.cs
  6. 1
      src/Shentun.Peis.HttpApi.Host/Shentun.Peis.HttpApi.Host.csproj
  7. 4
      src/Shentun.Peis.HttpApi.Host/appsettings.json

2
src/Shentun.ColumnReferencePlugIns/WebApiOutDtoBase.cs → 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
{

121
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<TOut> CallAppServiceAsync<TInput, TOut>(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<TOut>(result);
if (resultDto is WebApiOutDtoBase)
{
var webApiOutDtoBase = resultDto as WebApiOutDtoBase;
if (webApiOutDtoBase.Code == "-1")
{
throw new Exception($"调用服务失败{webApiOutDtoBase.Message}");
}
}
return resultDto;
}
}
}
}
public TOut CallAppService<TInput, TOut>(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<TOut>(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<LoginOutDto>(result);
if (restultDto == null)
{
throw new Exception("返回结果是空");
}
if (restultDto.Code != "1")
{
throw new Exception($"登录失败{restultDto.Message}");
}
_accesToken = restultDto.Data.access_token;
return;
}
}

18
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<ChargeRequestInterfaceQueryWorker>(x => x.DoWorkWithArgAsync(new Guid("")));
var appServiceHelper = new AppServiceHelper();
await appServiceHelper.LoginAsync();
var thirdInterfaceDtos = appServiceHelper.CallAppServiceAsync<object, List<ThirdInterfaceDto>>("api/app/ThirdInterface/GetList", null).Result;
if (thirdInterfaceDtos != null)
var thirdInterFaceForHostOutDto = await appServiceHelper.CallAppServiceAsync<object, ThirdInterFaceForHostOutDto>("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;

81
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<object, List<ThirdInterfaceDto>>("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<object, ThirdInterFaceForHostOutDto>("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 ;
//}
}
}
}

10
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<ThirdInterfaceDto> Data { get; set; }
}
}

1
src/Shentun.Peis.HttpApi.Host/Shentun.Peis.HttpApi.Host.csproj

@ -41,6 +41,7 @@
<ItemGroup>
<Folder Include="UpLoad\" />
<Folder Include="photo\" />
</ItemGroup>
<ItemGroup>

4
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",

Loading…
Cancel
Save