|
|
@ -4,6 +4,7 @@ using BBWY.Common.Http; |
|
|
|
using BBWY.Common.Models; |
|
|
|
using Newtonsoft.Json.Linq; |
|
|
|
using System; |
|
|
|
using System.Collections.Concurrent; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Net.Http; |
|
|
@ -17,6 +18,7 @@ namespace BBWY.Client.APIServices |
|
|
|
private RestApiService restApiService; |
|
|
|
private string oneBoundKey = "t5060712539"; |
|
|
|
private string oneBoundSecret = "20211103"; |
|
|
|
private ConcurrentDictionary<string, (Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus)> productChaches; |
|
|
|
|
|
|
|
private IDictionary<string, string> _1688ProductDetailRequestHeader; |
|
|
|
|
|
|
@ -31,6 +33,7 @@ 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)>(); |
|
|
|
} |
|
|
|
|
|
|
|
public (Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus)? GetProductInfo(Platform platform, string productId, string skuId, string purchaseProductId, PurchaseOrderMode priceMode, PurchaseProductAPIMode apiMode) |
|
|
@ -39,13 +42,17 @@ namespace BBWY.Client.APIServices |
|
|
|
if (apiMode == PurchaseProductAPIMode.Spider) |
|
|
|
{ |
|
|
|
data = LoadFromSpider(platform, productId, skuId, purchaseProductId, priceMode); |
|
|
|
if (data == null) |
|
|
|
data = LoadFromOneBound(platform, productId, skuId, purchaseProductId, priceMode); |
|
|
|
} |
|
|
|
else if (apiMode == PurchaseProductAPIMode.OneBound) |
|
|
|
{ |
|
|
|
data = LoadFromOneBound(platform, productId, skuId, purchaseProductId, priceMode); |
|
|
|
if (data == null) |
|
|
|
data = LoadFromSpider(platform, productId, skuId, purchaseProductId, priceMode); |
|
|
|
} |
|
|
|
|
|
|
|
return data; |
|
|
|
return data?.Copy(); |
|
|
|
} |
|
|
|
|
|
|
|
private (Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus)? LoadFromOneBound(Platform platform, string productId, string skuId, string purchaseProductId, PurchaseOrderMode priceMode) |
|
|
@ -59,6 +66,9 @@ namespace BBWY.Client.APIServices |
|
|
|
if (string.IsNullOrEmpty(platformStr)) |
|
|
|
return null; |
|
|
|
|
|
|
|
if (productChaches.TryGetValue($"{purchaseProductId}_{priceMode}", out (Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus) tuple)) |
|
|
|
return tuple; |
|
|
|
|
|
|
|
var result = restApiService.SendRequest("https://api-gw.onebound.cn/", $"{platformStr}/item_get", $"key={oneBoundKey}&secret={oneBoundSecret}&num_iid={purchaseProductId}&lang=zh-CN&cache=no&agent={(priceMode == PurchaseOrderMode.批发 ? 0 : 1)}", null, HttpMethod.Get, paramPosition: ParamPosition.Query, enableRandomTimeStamp: true); |
|
|
|
if (result.StatusCode != System.Net.HttpStatusCode.OK) |
|
|
|
throw new Exception($"{result.StatusCode} {result.Content}"); |
|
|
@ -83,7 +93,7 @@ namespace BBWY.Client.APIServices |
|
|
|
PurchaseSkuId = j.Value<string>("sku_id"), |
|
|
|
PurchaseSkuSpecId = j.Value<string>("spec_id"), |
|
|
|
Title = j.Value<string>("properties_name"), |
|
|
|
Logo = GetSkuLogo(j, (JArray)j["item"]["prop_imgs"]["prop_img"]) |
|
|
|
Logo = GetOneBoundSkuLogo(j, (JArray)j["item"]["prop_imgs"]["prop_img"]) |
|
|
|
}).ToList(); |
|
|
|
|
|
|
|
var purchaserId = j["item"]["seller_info"].Value<string>("user_num_id"); |
|
|
@ -92,14 +102,16 @@ namespace BBWY.Client.APIServices |
|
|
|
purchaserName = j["item"]["seller_info"].Value<string>("shop_name"); |
|
|
|
var purchaserLocation = j["item"].Value<string>("location"); |
|
|
|
|
|
|
|
return ( |
|
|
|
new Purchaser() |
|
|
|
tuple = (new Purchaser() |
|
|
|
{ |
|
|
|
Id = purchaserId, |
|
|
|
Name = purchaserName, |
|
|
|
Location = purchaserLocation |
|
|
|
}, |
|
|
|
list); |
|
|
|
}, list); |
|
|
|
|
|
|
|
productChaches.TryAdd($"{purchaseProductId}_{priceMode}", tuple); |
|
|
|
|
|
|
|
return tuple; |
|
|
|
} |
|
|
|
} |
|
|
|
catch { } |
|
|
@ -108,7 +120,7 @@ namespace BBWY.Client.APIServices |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private string GetSkuLogo(JToken skuJToken, JArray prop_img) |
|
|
|
private string GetOneBoundSkuLogo(JToken skuJToken, JArray prop_img) |
|
|
|
{ |
|
|
|
if (!prop_img.HasValues) |
|
|
|
return "pack://application:,,,/Resources/Images/defaultItem.png"; |
|
|
@ -124,11 +136,13 @@ namespace BBWY.Client.APIServices |
|
|
|
|
|
|
|
private (Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus)? LoadFromSpider(Platform platform, string productId, string skuId, string purchaseProductId, PurchaseOrderMode priceMode) |
|
|
|
{ |
|
|
|
if (productChaches.TryGetValue($"{purchaseProductId}_{priceMode}", out (Purchaser purchaser, IList<PurchaseSchemeProductSku> purchaseSchemeProductSkus) tuple)) |
|
|
|
return tuple; |
|
|
|
|
|
|
|
switch (platform) |
|
|
|
{ |
|
|
|
case Platform.阿里巴巴: |
|
|
|
LoadFrom1688Spider(platform, productId, skuId, purchaseProductId, priceMode); |
|
|
|
break; |
|
|
|
return LoadFrom1688Spider(platform, productId, skuId, purchaseProductId, priceMode); |
|
|
|
case Platform.拼多多: |
|
|
|
|
|
|
|
break; |
|
|
@ -146,23 +160,73 @@ namespace BBWY.Client.APIServices |
|
|
|
|
|
|
|
|
|
|
|
//data/1081181309582/data/pirceModel/[currentPrices]/[0]price
|
|
|
|
var _1688pageResult = restApiService.SendRequest("https://detail.1688.com", |
|
|
|
$"offer/{purchaseProductId}.html", |
|
|
|
$"clickid={Guid.NewGuid().ToString().Md5Encrypt()}&sessionid={Guid.NewGuid().ToString().Md5Encrypt()}&sk={(priceMode == PurchaseOrderMode.批发 ? "order" : "consign")}", |
|
|
|
_1688ProductDetailRequestHeader, |
|
|
|
HttpMethod.Get); |
|
|
|
|
|
|
|
if (_1688pageResult.StatusCode != System.Net.HttpStatusCode.OK) |
|
|
|
return null; |
|
|
|
try |
|
|
|
{ |
|
|
|
var _1688pageResult = restApiService.SendRequest("https://detail.1688.com", |
|
|
|
$"offer/{purchaseProductId}.html", |
|
|
|
$"clickid={Guid.NewGuid().ToString().Md5Encrypt()}&sessionid={Guid.NewGuid().ToString().Md5Encrypt()}&sk={(priceMode == PurchaseOrderMode.批发 ? "order" : "consign")}", |
|
|
|
_1688ProductDetailRequestHeader, |
|
|
|
HttpMethod.Get, |
|
|
|
httpClientName: "gzip"); |
|
|
|
|
|
|
|
if (_1688pageResult.StatusCode != System.Net.HttpStatusCode.OK) |
|
|
|
return null; |
|
|
|
|
|
|
|
var match = Regex.Match(_1688pageResult.Content, @"(window\.__INIT_DATA=)(.*)(\s?</script>)"); |
|
|
|
if (!match.Success) |
|
|
|
return null; |
|
|
|
var match = Regex.Match(_1688pageResult.Content, @"(window\.__INIT_DATA=)(.*)(\r*\n*\s*</script>)"); |
|
|
|
if (!match.Success) |
|
|
|
return null; |
|
|
|
|
|
|
|
var jsonStr = match.Groups[2].Value; |
|
|
|
var j = JObject.Parse(jsonStr); |
|
|
|
var jsonStr = match.Groups[2].Value; |
|
|
|
var jobject = JObject.Parse(jsonStr); |
|
|
|
|
|
|
|
return null; |
|
|
|
var purchaser = new Purchaser() |
|
|
|
{ |
|
|
|
Id = jobject["globalData"]["tempModel"]["sellerUserId"].ToString(), |
|
|
|
Name = jobject["globalData"]["tempModel"]["companyName"].ToString(), |
|
|
|
Location = jobject["data"]["1081181309101"]["data"]["location"].ToString() |
|
|
|
}; |
|
|
|
|
|
|
|
var colors3216 = jobject["globalData"]["skuModel"]["skuProps"].FirstOrDefault(j => j.Value<int>("fid") == 3216)["value"].Children().Select(j => new |
|
|
|
{ |
|
|
|
name = j.Value<string>("name"), |
|
|
|
imageUrl = j.Value<string>("imageUrl") |
|
|
|
}).ToList(); |
|
|
|
|
|
|
|
var price = jobject["data"]["1081181309582"]["data"]["priceModel"]["currentPrices"][0].Value<decimal>("price"); |
|
|
|
|
|
|
|
var purchaseSchemeProductSkus = new List<PurchaseSchemeProductSku>(); |
|
|
|
|
|
|
|
foreach (var jsku in jobject["globalData"]["skuModel"]["skuInfoMap"].Children()) |
|
|
|
{ |
|
|
|
var jskuProperty = jsku as JProperty; |
|
|
|
var name = jskuProperty.Name; |
|
|
|
var matchName = name.Contains(">") ? name.Substring(0, name.IndexOf(">")) : name; |
|
|
|
var value = jskuProperty.Value; |
|
|
|
|
|
|
|
purchaseSchemeProductSkus.Add(new PurchaseSchemeProductSku() |
|
|
|
{ |
|
|
|
ProductId = productId, |
|
|
|
SkuId = skuId, |
|
|
|
PurchaseProductId = purchaseProductId, |
|
|
|
Price = price, |
|
|
|
Title = name, |
|
|
|
PurchaseSkuId = value.Value<string>("skuId"), |
|
|
|
PurchaseSkuSpecId = value.Value<string>("specId"), |
|
|
|
Logo = colors3216.FirstOrDefault(c => c.name == matchName)?.imageUrl ?? "pack://application:,,,/Resources/Images/defaultItem.png" |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
var tuple = (purchaser, purchaseSchemeProductSkus); |
|
|
|
productChaches.TryAdd($"{purchaseProductId}_{priceMode}", tuple); |
|
|
|
|
|
|
|
return tuple; |
|
|
|
} |
|
|
|
catch |
|
|
|
{ |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|