Browse Source

修复价格问题

qianyi
shanji 3 years ago
parent
commit
30fa9ff98f
  1. 1
      BBWY.Client/APIServices/OneBoundAPIService.cs
  2. 32
      BBWY.Client/APIServices/PurchaseProductAPIService.cs
  3. 2
      BBWY.Client/App.xaml.cs
  4. 1
      BBWY.Client/BBWY.Client.csproj

1
BBWY.Client/APIServices/OneBoundAPIService.cs

@ -29,6 +29,7 @@ namespace BBWY.Client.APIServices
{
try
{
https://api-gw.onebound.cn/1688/item_get/key=t5060712539&secret=20211103&num_iid=649560646832&lang=zh-CN&cache=no
var result = restApiService.SendRequest("https://api-gw.onebound.cn/", $"{platform}/item_get", $"key={key}&secret={secret}&num_iid={productId}&lang=zh-CN&cache=no", null, HttpMethod.Get, paramPosition: ParamPosition.Query, enableRandomTimeStamp: true);
if (result.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception($"{result.StatusCode} {result.Content}");

32
BBWY.Client/APIServices/PurchaseProductAPIService.cs

@ -2,6 +2,7 @@
using BBWY.Client.Models;
using BBWY.Common.Http;
using BBWY.Common.Models;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
@ -16,15 +17,21 @@ namespace BBWY.Client.APIServices
public class PurchaseProductAPIService : IDenpendency
{
private RestApiService restApiService;
private IMemoryCache memoryCache;
private string oneBoundKey = "t5060712539";
private string oneBoundSecret = "20211103";
private ConcurrentDictionary<string, (Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus)> productChaches;
private TimeSpan purchaseProductCacheTimeSpan;
//private TimeSpan _1688SessionIdTimeSpan;
//private ConcurrentDictionary<string, (Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus)> productChaches;
private IDictionary<string, string> _1688ProductDetailRequestHeader;
public PurchaseProductAPIService(RestApiService restApiService)
public PurchaseProductAPIService(RestApiService restApiService, IMemoryCache memoryCache)
{
this.restApiService = restApiService;
this.memoryCache = memoryCache;
_1688ProductDetailRequestHeader = new Dictionary<string, string>()
{
{ "Host","detail.1688.com"},
@ -33,12 +40,14 @@ namespace BBWY.Client.APIServices
{ "Accept-Encoding","gzip, deflate, br"},
{ "Accept-Language","zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"}
};
productChaches = new ConcurrentDictionary<string, (Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus)>();
purchaseProductCacheTimeSpan = TimeSpan.FromDays(1);
//_1688SessionIdTimeSpan = TimeSpan.FromMinutes(10);
//productChaches = new ConcurrentDictionary<string, (Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus)>();
}
public (Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus)? GetProductInfo(Platform platform, string productId, string skuId, string purchaseProductId, PurchaseOrderMode priceMode, PurchaseProductAPIMode apiMode)
{
if (productChaches.TryGetValue($"{purchaseProductId}_{priceMode}", out (Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus) tuple))
if (memoryCache.TryGetValue<(Purchaser, IList<PurchaseSchemeProductSku>)>($"{purchaseProductId}_{priceMode}", out var tuple))
return tuple.Copy();
(Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus)? data = null;
@ -57,7 +66,14 @@ namespace BBWY.Client.APIServices
}
if (data != null)
productChaches.TryAdd($"{purchaseProductId}_{priceMode}", data.Value);
{
try
{
memoryCache.Set<(Purchaser, IList<PurchaseSchemeProductSku>)>($"{purchaseProductId}_{priceMode}", data.Value, purchaseProductCacheTimeSpan);
}
catch { }
}
return data?.Copy();
}
@ -189,7 +205,7 @@ namespace BBWY.Client.APIServices
imageUrl = j.Value<string>("imageUrl")
}).ToList();
var price = jobject["data"]["1081181309582"]["data"]["priceModel"]["currentPrices"][0].Value<decimal>("price");
var firstPrice = jobject["data"]["1081181309582"]["data"]["priceModel"]["currentPrices"][0].Value<decimal>("price");
var purchaseSchemeProductSkus = new List<PurchaseSchemeProductSku>();
@ -200,12 +216,14 @@ namespace BBWY.Client.APIServices
var matchName = name.Contains("&gt;") ? name.Substring(0, name.IndexOf("&gt;")) : name;
var value = jskuProperty.Value;
var skuPrice = value.Value<decimal>("price");
purchaseSchemeProductSkus.Add(new PurchaseSchemeProductSku()
{
ProductId = productId,
SkuId = skuId,
PurchaseProductId = purchaseProductId,
Price = price,
Price = skuPrice == 0M ? firstPrice : skuPrice,
Title = name,
PurchaseSkuId = value.Value<string>("skuId"),
PurchaseSkuSpecId = value.Value<string>("specId"),

2
BBWY.Client/App.xaml.cs

@ -75,6 +75,8 @@ namespace BBWY.Client
{
AutomaticDecompression = System.Net.DecompressionMethods.GZip
});
serviceCollection.AddMemoryCache();
serviceCollection.AddSingleton<RestApiService>();
serviceCollection.AddSingleton(gl);
serviceCollection.BatchRegisterServices(new Assembly[] { Assembly.Load("BBWY.Client") }, typeof(IDenpendency));

1
BBWY.Client/BBWY.Client.csproj

@ -29,6 +29,7 @@
<ItemGroup>
<PackageReference Include="HandyControl" Version="3.3.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.28" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />

Loading…
Cancel
Save