fix 多钱包调整

This commit is contained in:
2024-11-08 16:36:10 +08:00
parent 36bfd2e8f6
commit 1a0ba28249
36 changed files with 418 additions and 434 deletions

View File

@@ -1,54 +1,37 @@
using System.Net.Http.Headers;
using System.Text;
using Dlhis.Middle.Entities;
using Dlhis.Middle.Request;
using Dlhis.Middle.Response;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.UnifyResultAndValidation;
using Kurisu.AspNetCore.Utils.Extensions;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
namespace Dlhis.Middle.Services.Implements;
public class RemoteOrderService : IRemoteOrderService
{
private readonly string _hostName;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IMemoryCache _memoryCache;
private readonly IHealthCheckService _healthCheckService;
private readonly IDbContext _dbContext;
public RemoteOrderService(IHttpClientFactory httpClientFactory,
IMemoryCache memoryCache,
IConfiguration configuration,
public RemoteOrderService(
IHealthCheckService healthCheckService,
IDbContext dbContext)
{
_httpClientFactory = httpClientFactory;
_memoryCache = memoryCache;
_hostName = configuration.GetSection("hostName").Value;
_healthCheckService = healthCheckService;
_dbContext = dbContext;
}
/// <inheritdoc />
public async Task<DefaultApiResult<OrderResultResponse>> AddAsync(OrderAddRequest request)
{
var token = await AuthAsync();
var httpClient = _httpClientFactory.CreateClient("face");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var content = new StringContent(request.ToJson(), Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync($"https://{_hostName}/api/canteen/orders", content);
var responseJson = await response.Content.ReadAsStringAsync();
return responseJson.ToObject<DefaultApiResult<OrderResultResponse>>();
}
///// <inheritdoc />
// public async Task<DefaultApiResult<OrderResultResponse>> AddAsync(OrderAddRequest request)
// {
// var token = await _tokenService.AuthAsync();
//
// var httpClient = _httpClientFactory.CreateClient("face");
// httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
//
// var content = new StringContent(request.ToJson(), Encoding.UTF8, "application/json");
//
// var response = await httpClient.PostAsync($"{_settingService.GetBaseUrl()}/api/canteen/orders", content);
//
// var responseJson = await response.Content.ReadAsStringAsync();
// return responseJson.ToObject<DefaultApiResult<OrderResultResponse>>();
// }
/// <inheritdoc />
public async Task<bool> AbleRemoteCallAsync()
@@ -60,28 +43,4 @@ public class RemoteOrderService : IRemoteOrderService
return !_healthCheckService.GetIsOffline();
}
private async Task<string> AuthAsync()
{
var httpClient = _httpClientFactory.CreateClient("face");
if (_memoryCache.TryGetValue("token", out string token)) return token;
var tokenResponse = await httpClient.PostAsync($"https://{_hostName}/api/User/OAuth2/clientgrant", new StringContent(JsonConvert.SerializeObject(new
{
ClientId = "e509c2aaa98e300c",
ClientSecret = "6184d8979fc48105"
}), Encoding.UTF8, "application/json"));
var tokenJson = await tokenResponse.Content.ReadAsStringAsync();
var tokenInfo = JsonConvert.DeserializeObject<DefaultApiResult<TokenResponse>>(tokenJson);
token = tokenInfo.Data.AccessToken;
_memoryCache.Set("token", token, TimeSpan.FromSeconds(tokenInfo.Data.Expires));
return token;
}
}