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
{
///
/// ops服务
///
public class OpsService : IOpsService
{
private readonly IHttpClientService _httpClientService;
private readonly ILogger _logger;
private readonly IMemoryCache _memoryCache;
private int hours = 10;//过期时间 默认10小时
private readonly OpsOptions _option;
public OpsService(IHttpClientService httpClientService, ILogger logger,
IMemoryCache memoryCache, IOptions option)
{
this._memoryCache = memoryCache;
this._httpClientService = httpClientService;
this._logger = logger;
_option = option?.Value;
}
public async Task> GetBox(OpsBoxRequest request)
{
OpsBoxResponse respnose = new OpsBoxResponse();
List details = new List();
List ss = new List();
ss.Add(new SerialNumbersResponse() {
SerialNumber="sss",
BarCereateUser="sdf",
BarCreateTime="20-20-20"
});
ss.Add(new SerialNumbersResponse()
{
SerialNumber = "sss2",
BarCereateUser = "sdf2",
BarCreateTime = "20-20-20"
});
details.Add(new OpsBoxDetailsResponse()
{
MaterialId = 1,
Qty = 1,
SerialNumbers = ss
});
respnose.Details = details;
var str = JsonConvert.SerializeObject(respnose);
var token = await GetToken();
Dictionary dicHeaders = new Dictionary();
dicHeaders.Add("Authorization", "Bearer " + token);
//_httpClientService.BuildHttpClient(contentHeaders);
var res = await _httpClientService.PostAsync(_option.Url + "BarCode/carton-box_api/carton-box", JsonConvert.SerializeObject(request), dicHeaders);
if (!res.succeeded) return new List();
return JsonConvert.DeserializeObject>(res.data.date);
}
private async Task GetToken()
{
OpsLogin login = new OpsLogin()
{
hAccount = _option.UserName,
hPassword = _option.PassWord
};
var res = await _httpClientService.PostAsync(_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; }
}
}