85 lines
3.0 KiB
C#
85 lines
3.0 KiB
C#
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<List<OpsBoxResponse>> 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);
|
|
if (!res.succeeded) return new List<OpsBoxResponse>();
|
|
var list = JsonConvert.DeserializeObject<List<OpsBoxResponse>>(res.data.date);
|
|
return list;
|
|
}
|
|
|
|
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; }
|
|
}
|
|
}
|