对接老ops
This commit is contained in:
@@ -34,12 +34,16 @@ namespace WMS.Web.Domain.Entitys
|
||||
/// <summary>
|
||||
/// 供应商Id
|
||||
/// </summary>
|
||||
public int SupplierId { get; set; }
|
||||
public int? SupplierId { get; set; }
|
||||
/// <summary>
|
||||
/// 组织Id
|
||||
/// </summary>
|
||||
public int? OrgId { get; set; }
|
||||
/// <summary>
|
||||
/// 创建时间(对应老OPS的创建时间)
|
||||
/// </summary>
|
||||
public DateTime CreateTime { get; set; } = DateTime.Now;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 明细
|
||||
/// </summary>
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace WMS.Web.Domain.IService.Public
|
||||
{
|
||||
public interface IHttpClientService
|
||||
{
|
||||
HttpClient BuildHttpClient(Dictionary<string, string> dicDefaultHeaders, int? timeoutSecond = 180);
|
||||
Task<T> GetAsync<T>(string url, int timeoutSecond = 180);
|
||||
Task<T> GetAsync<T>(string url, Dictionary<string, string> dicHeaders, int timeoutSecond = 180);
|
||||
Task<T> PostAsync<T>(string url, string requestBody, int timeoutSecond = 180);
|
||||
|
||||
13
src/WMS.Web.Domain/IService/Public/IOpsService.cs
Normal file
13
src/WMS.Web.Domain/IService/Public/IOpsService.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WMS.Web.Core.Dto;
|
||||
|
||||
namespace WMS.Web.Domain.IService.Public
|
||||
{
|
||||
public interface IOpsService
|
||||
{
|
||||
Task GetBox(OpsBoxRequest request);
|
||||
}
|
||||
}
|
||||
16
src/WMS.Web.Domain/Options/OpsOptions.cs
Normal file
16
src/WMS.Web.Domain/Options/OpsOptions.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace WMS.Web.Domain.Options
|
||||
{
|
||||
/// <summary>
|
||||
/// 老ops对接
|
||||
/// </summary>
|
||||
public class OpsOptions
|
||||
{
|
||||
public string Url { get; set; }
|
||||
public string UserName { get; set; }
|
||||
public string PassWord { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -105,8 +105,8 @@ namespace WMS.Web.Domain.Services.Public
|
||||
/// <returns></returns>
|
||||
public async Task<T> PostAsync<T>(string url, string requestBody, Dictionary<string, string> dicHeaders, int timeoutSecond = 180)
|
||||
{
|
||||
var client = BuildHttpClient(null, timeoutSecond);
|
||||
var requestContent = GenerateStringContent(requestBody, dicHeaders);
|
||||
var client = BuildHttpClient(dicHeaders, timeoutSecond);
|
||||
var requestContent = GenerateStringContent(requestBody, contentHeaders);
|
||||
var response = await client.PostAsync(url, requestContent);
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
if (response.IsSuccessStatusCode)
|
||||
@@ -213,7 +213,7 @@ namespace WMS.Web.Domain.Services.Public
|
||||
/// <param name="dicDefaultHeaders"></param>
|
||||
/// <param name="timeoutSecond"></param>
|
||||
/// <returns></returns>
|
||||
private HttpClient BuildHttpClient(Dictionary<string, string> dicDefaultHeaders, int? timeoutSecond)
|
||||
public HttpClient BuildHttpClient(Dictionary<string, string> dicDefaultHeaders, int? timeoutSecond=180)
|
||||
{
|
||||
var httpClient = _clientFactory.CreateClient("ops_client");
|
||||
httpClient.DefaultRequestHeaders.Clear(); //为了使客户端不受最后一个请求的影响,它需要清除DefaultRequestHeaders
|
||||
|
||||
81
src/WMS.Web.Domain/Services/Public/OpsService.cs
Normal file
81
src/WMS.Web.Domain/Services/Public/OpsService.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WMS.Web.Core.Dto;
|
||||
using WMS.Web.Domain.IService.Public;
|
||||
using WMS.Web.Domain.Options;
|
||||
|
||||
namespace WMS.Web.Domain.Services.Public
|
||||
{
|
||||
/// <summary>
|
||||
/// ops服务
|
||||
/// </summary>
|
||||
public class OpsService : IOpsService
|
||||
{
|
||||
private readonly IHttpClientService _httpClientService;
|
||||
private readonly ILogger<OpsService> _logger;
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
private int hours = 10;//过期时间 默认10小时
|
||||
private readonly OpsOptions _option;
|
||||
|
||||
public OpsService(IHttpClientService httpClientService, ILogger<OpsService> logger,
|
||||
IMemoryCache memoryCache, IOptions<OpsOptions> option)
|
||||
{
|
||||
this._memoryCache = memoryCache;
|
||||
this._httpClientService = httpClientService;
|
||||
this._logger = logger;
|
||||
_option = option?.Value;
|
||||
}
|
||||
|
||||
public async Task GetBox(OpsBoxRequest request)
|
||||
{
|
||||
var token = await GetToken();
|
||||
Dictionary<string, string> dicHeaders = new Dictionary<string, string>();
|
||||
dicHeaders.Add("Authorization", "Bearer " + token);
|
||||
//_httpClientService.BuildHttpClient(contentHeaders);
|
||||
var res= await _httpClientService.PostAsync<OpsReponse>(_option.Url + "BarCode/carton-box_api/carton-box", JsonConvert.SerializeObject(request), dicHeaders);
|
||||
}
|
||||
|
||||
private async Task<string> GetToken()
|
||||
{
|
||||
OpsLogin login = new OpsLogin()
|
||||
{
|
||||
hAccount = _option.UserName,
|
||||
hPassword = _option.PassWord
|
||||
};
|
||||
var res = await _httpClientService.PostAsync<OpsReponse>(_option.Url + "api/login", JsonConvert.SerializeObject(login));
|
||||
var token = res.data?.token ?? "";
|
||||
if (string.IsNullOrEmpty(token))
|
||||
_logger.LogInformation("老Ops登录失败:" + res?.errors);
|
||||
return token;
|
||||
}
|
||||
|
||||
}
|
||||
public class OpsLogin
|
||||
{
|
||||
public OpsLogin() { }
|
||||
public string hAccount { get; set; } = "admin";
|
||||
public string hPassword { get; set; } = "888888";
|
||||
}
|
||||
public class OpsInfo
|
||||
{
|
||||
public int code { get; set; }
|
||||
public string message { get; set; }
|
||||
public string date { get; set; }
|
||||
public string token { get; set; }
|
||||
}
|
||||
public class OpsReponse
|
||||
{
|
||||
public int statusCode { get; set; }
|
||||
public bool succeeded { get; set; }
|
||||
public string errors { get; set; }
|
||||
public string extras { get; set; }
|
||||
|
||||
public OpsInfo data { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user