|
@ -1,27 +1,43 @@ |
|
|
using System; |
|
|
using System; |
|
|
|
|
|
using System.Net.Http; |
|
|
|
|
|
using System.Net.Http.Headers; |
|
|
|
|
|
using System.Text; |
|
|
|
|
|
using System.Text.Json; |
|
|
|
|
|
using System.Threading; |
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
|
|
|
|
using IdentityModel.Client; |
|
|
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
using Microsoft.Extensions.Logging; |
|
|
using Microsoft.Extensions.Logging; |
|
|
using Microsoft.Extensions.Options; |
|
|
using Microsoft.Extensions.Options; |
|
|
using Volo.Abp.BackgroundWorkers; |
|
|
using Volo.Abp.BackgroundWorkers; |
|
|
|
|
|
using Volo.Abp.IdentityServer.Clients; |
|
|
using Volo.Abp.Threading; |
|
|
using Volo.Abp.Threading; |
|
|
using Volo.Abp.Uow; |
|
|
using Volo.Abp.Uow; |
|
|
using Win_in.Sfs.Wms.DataExchange.Domain; |
|
|
using Win_in.Sfs.Wms.DataExchange.Domain; |
|
|
using Win_in.Sfs.Wms.DataExchange.Domain.Shared; |
|
|
using Win_in.Sfs.Wms.DataExchange.Domain.Shared; |
|
|
|
|
|
using Win_in.Sfs.Wms.Store.Application.Contracts; |
|
|
|
|
|
using static IdentityServer4.Models.IdentityResources; |
|
|
|
|
|
|
|
|
namespace Win_in.Sfs.Wms.DataExchange.Agent; |
|
|
namespace Win_in.Sfs.Wms.DataExchange.Agent; |
|
|
|
|
|
|
|
|
public class IncomingToWmsWorker : AsyncPeriodicBackgroundWorkerBase |
|
|
public class IncomingToWmsWorker : AsyncPeriodicBackgroundWorkerBase |
|
|
{ |
|
|
{ |
|
|
private readonly IOptions<DataExchangeOptions> _options; |
|
|
private readonly IOptions<DataExchangeOptions> _options; |
|
|
|
|
|
private readonly IUnitOfWorkManager _unitOfWorkManager; |
|
|
|
|
|
private readonly HttpClient _httpClient; |
|
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory; |
|
|
|
|
|
|
|
|
public IncomingToWmsWorker( |
|
|
public IncomingToWmsWorker( |
|
|
AbpAsyncTimer timer, |
|
|
AbpAsyncTimer timer, |
|
|
IOptions<DataExchangeOptions> options, |
|
|
IOptions<DataExchangeOptions> options, |
|
|
IServiceScopeFactory serviceScopeFactory |
|
|
IServiceScopeFactory serviceScopeFactory, IUnitOfWorkManager unitOfWorkManager, HttpClient httpClient, IHttpClientFactory httpClientFactory) : base(timer, serviceScopeFactory) |
|
|
) : base(timer, serviceScopeFactory) |
|
|
|
|
|
{ |
|
|
{ |
|
|
_options = options; |
|
|
_options = options; |
|
|
|
|
|
_unitOfWorkManager = unitOfWorkManager; |
|
|
|
|
|
_httpClient = httpClient; |
|
|
|
|
|
_httpClientFactory = httpClientFactory; |
|
|
Timer.Period = options.Value.IncomingOptions.PeriodSeconds * 1000; //default 5 minutes
|
|
|
Timer.Period = options.Value.IncomingOptions.PeriodSeconds * 1000; //default 5 minutes
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -124,7 +140,7 @@ public class IncomingToWmsWorker : AsyncPeriodicBackgroundWorkerBase |
|
|
await incomingToWms.HandleAsnsAsync(workerContext).ConfigureAwait(false); |
|
|
await incomingToWms.HandleAsnsAsync(workerContext).ConfigureAwait(false); |
|
|
break; |
|
|
break; |
|
|
case EnumIncomingDataType.ProductReceipt: |
|
|
case EnumIncomingDataType.ProductReceipt: |
|
|
await incomingToWms.HandleProductReceiptsAsync(workerContext).ConfigureAwait(false); |
|
|
await SendProductReceipt(workerContext, incomingToWms); |
|
|
break; |
|
|
break; |
|
|
case EnumIncomingDataType.MaterialRequest: |
|
|
case EnumIncomingDataType.MaterialRequest: |
|
|
await incomingToWms.HandleMaterialRequestsAsync(workerContext).ConfigureAwait(false); |
|
|
await incomingToWms.HandleMaterialRequestsAsync(workerContext).ConfigureAwait(false); |
|
@ -150,4 +166,48 @@ public class IncomingToWmsWorker : AsyncPeriodicBackgroundWorkerBase |
|
|
incomingToWms.SetSuccess(); |
|
|
incomingToWms.SetSuccess(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async Task SendProductReceipt(PeriodicBackgroundWorkerContext workerContext, IncomingToWms incomingToWms) |
|
|
|
|
|
{ // 定义请求的 URL
|
|
|
|
|
|
string apiUrl = "http://localhost:59095/api/wms/store/product-receipt-note"; |
|
|
|
|
|
string authUrl = "http://dev.ccwin-in.com:60083"; |
|
|
|
|
|
var productReceiptJson = JsonSerializer.Deserialize<ProductReceiptNoteEditInput>(incomingToWms.DataContent); |
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
|
|
|
Thread.Sleep(60000); |
|
|
|
|
|
// 将参数转换为 JSON 格式
|
|
|
|
|
|
string jsonContent = JsonSerializer.Serialize(productReceiptJson); |
|
|
|
|
|
|
|
|
|
|
|
// 创建请求的内容
|
|
|
|
|
|
StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); |
|
|
|
|
|
|
|
|
|
|
|
var result = await _httpClientFactory.CreateClient().RequestPasswordTokenAsync(new PasswordTokenRequest |
|
|
|
|
|
{ |
|
|
|
|
|
Address = $"{authUrl.TrimEnd('/')}/connect/token", |
|
|
|
|
|
GrantType = "password", |
|
|
|
|
|
ClientId = "Auth_App", |
|
|
|
|
|
ClientSecret = "1q2w3E*", |
|
|
|
|
|
UserName = "jiekou1", |
|
|
|
|
|
Password = "1q2w3E*" |
|
|
|
|
|
}).ConfigureAwait(false); |
|
|
|
|
|
|
|
|
|
|
|
// 添加 Token 到请求头部
|
|
|
|
|
|
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); |
|
|
|
|
|
// 发送 POST 请求
|
|
|
|
|
|
HttpResponseMessage response = await _httpClient.PostAsync(apiUrl, content); |
|
|
|
|
|
|
|
|
|
|
|
// 确保响应成功
|
|
|
|
|
|
response.EnsureSuccessStatusCode(); |
|
|
|
|
|
|
|
|
|
|
|
// 读取响应内容并返回
|
|
|
|
|
|
string responseBody = await response.Content.ReadAsStringAsync(); |
|
|
|
|
|
} |
|
|
|
|
|
catch (HttpRequestException ex) |
|
|
|
|
|
{ |
|
|
|
|
|
// 处理请求异常
|
|
|
|
|
|
// 可以记录日志或者抛出异常
|
|
|
|
|
|
Console.WriteLine(ex.Message); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await incomingToWms.HandleProductReceiptsAsync(workerContext).ConfigureAwait(false); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|