|
|
@ -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(">") ? name.Substring(0, name.IndexOf(">")) : 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"), |
|
|
|