316 lines
6.5 KiB
C#
316 lines
6.5 KiB
C#
using AllBase.Model.Result.PDDResult;
|
|
using CefSharp.DevTools.CSS;
|
|
using NaiBa.Util.Extension;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using TemuKJD.BLL.ImportConverters;
|
|
using Work.GoodDetail;
|
|
|
|
namespace TemuKJD.BLL.ImportConverts
|
|
{
|
|
internal class ConverterToPddV5 : GoodsConverterBase
|
|
{
|
|
private JObject jsonRoot;
|
|
|
|
|
|
public override string ConverterName => "To PDD V5";
|
|
|
|
public override PlatformType Platform => PlatformType.PDD;
|
|
|
|
protected override bool IsMatchProvider()
|
|
{
|
|
try
|
|
{
|
|
jsonRoot = JsonConvert.DeserializeObject<JObject>(RawData);
|
|
return jsonRoot.SelectToken("$.store.initDataObj.goods") != null;
|
|
}
|
|
catch { return false; }
|
|
}
|
|
|
|
protected override PDDFrontProductInfo5 ConverterProvider()
|
|
{
|
|
var goodsToken = jsonRoot.SelectToken("$.store.initDataObj.goods");
|
|
var product = new PDDFrontProductInfo5();
|
|
|
|
// 逐个属性赋值,方便打断点调试
|
|
product.GoodsId = goodsToken["goodsID"].Value<long>();
|
|
|
|
product.GoodsUrl = "";
|
|
|
|
product.BrandId = goodsToken["brandId"].ParseToLong();
|
|
|
|
product.Title = goodsToken["goodsName"]?.Value<string>();
|
|
|
|
product.Description = goodsToken["short_name"]?.Value<string>() ?? "";
|
|
|
|
product.ThumbUrl = goodsToken["thumbUrl"]?.Value<string>() ?? "";
|
|
|
|
product.CategoryId = goodsToken["catID"]?.Value<int>() ?? 0;
|
|
|
|
product.CategoryPath = GetCategoryPath(goodsToken);
|
|
|
|
product.MallId = goodsToken["mallID"]?.Value<int>() ?? 0;
|
|
|
|
product.Price = goodsToken["linePrice"]?.Value<decimal>() ?? 0;
|
|
|
|
product.Propertys = GetProperties(goodsToken);
|
|
|
|
product.MainImages = GetMainImages(goodsToken);
|
|
|
|
product.DetailImages = GetDetailImages(goodsToken);
|
|
|
|
product.Videos = GetVideos(goodsToken);
|
|
|
|
product.Specs = GetSpecs(goodsToken);
|
|
|
|
product.Skus = GetSkus(goodsToken);
|
|
|
|
return product;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取类目路径
|
|
/// </summary>
|
|
private List<int> GetCategoryPath(JToken goods)
|
|
{
|
|
var result = new List<int>();
|
|
|
|
// JSON 中有 catID1 ~ catID4
|
|
for (int i = 1; i <= 4; i++)
|
|
{
|
|
var token = goods[$"catID{i}"];
|
|
|
|
if (token != null && token.Type != JTokenType.Null)
|
|
{
|
|
var value = token.Value<int>();
|
|
|
|
if (value > 0)
|
|
result.Add(value);
|
|
}
|
|
}
|
|
|
|
// 如果没有,则至少加入当前类目
|
|
if (result.Count == 0)
|
|
{
|
|
var categoryId = goods["catID"]?.Value<int>() ?? 0;
|
|
|
|
if (categoryId > 0)
|
|
result.Add(categoryId);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 商品属性
|
|
/// </summary>
|
|
private List<Property> GetProperties(JToken goods)
|
|
{
|
|
var result = new List<Property>();
|
|
|
|
var propertyArray = goods["goodsProperty"] as JArray;
|
|
|
|
if (propertyArray == null)
|
|
return result;
|
|
|
|
foreach (var item in propertyArray)
|
|
{
|
|
var key = item["key"]?.Value<string>();
|
|
|
|
var values = item["values"] as JArray;
|
|
|
|
result.Add(new Property
|
|
{
|
|
Key = key ?? "",
|
|
Value = string.Join(",", values?.Select(a => a))
|
|
});
|
|
}
|
|
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 主图
|
|
/// </summary>
|
|
private List<string> GetMainImages(JToken goods)
|
|
{
|
|
var result = new List<string>();
|
|
|
|
var images = goods["topGallery"] as JArray;
|
|
|
|
if (images != null)
|
|
{
|
|
foreach (var item in images)
|
|
{
|
|
var url = item["url"]?.Value<string>();
|
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
result.Add(url);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
images = goods["viewImageData"] as JArray;
|
|
if (images != null)
|
|
{
|
|
foreach (var item in images)
|
|
{
|
|
var url = item.ToString();
|
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
result.Add(url);
|
|
}
|
|
return result;
|
|
}
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 详情图
|
|
/// </summary>
|
|
private List<string> GetDetailImages(JToken goods)
|
|
{
|
|
var result = new List<string>();
|
|
|
|
var images = goods["detailGallery"] as JArray;
|
|
|
|
if (images == null)
|
|
return result;
|
|
|
|
foreach (var item in images)
|
|
{
|
|
var url = item["url"]?.Value<string>();
|
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
result.Add(url);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 商品视频
|
|
/// </summary>
|
|
private List<string> GetVideos(JToken goods)
|
|
{
|
|
var result = new List<string>();
|
|
|
|
var videos = goods["videoGallery"] as JArray;
|
|
|
|
if (videos == null)
|
|
return result;
|
|
|
|
foreach (var item in videos)
|
|
{
|
|
var url =
|
|
item["videoUrl"]?.Value<string>() ??
|
|
item["url"]?.Value<string>();
|
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
result.Add(url);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取规格
|
|
/// </summary>
|
|
private List<SpecItem> GetSpecs(JToken goods)
|
|
{
|
|
var result = new List<SpecItem>();
|
|
|
|
var skus = goods["skus"] as JArray;
|
|
|
|
if (skus == null)
|
|
return result;
|
|
|
|
foreach (var sku in skus)
|
|
{
|
|
var specs = sku["specs"] as JArray;
|
|
|
|
if (specs == null)
|
|
continue;
|
|
|
|
foreach (var spec in specs)
|
|
{
|
|
var specKey = spec["spec_key"]?.Value<string>() ?? "";
|
|
var specValue = spec["spec_value"]?.Value<string>() ?? "";
|
|
var specKeyId = spec["spec_key_id"]?.Value<long>() ?? 0;
|
|
var specValueId = spec["spec_value_id"]?.Value<long>() ?? 0;
|
|
|
|
// 去重
|
|
if (result.Any(x => x.SpecKey == specKey && x.SpecValue == specValue))
|
|
continue;
|
|
|
|
result.Add(new SpecItem
|
|
{
|
|
SpecKey = specKey,
|
|
SpecValue = specValue,
|
|
SpecKeyId = specKeyId,
|
|
SpecValueId = specValueId,
|
|
});
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// SKU
|
|
/// </summary>
|
|
private List<SkuItem> GetSkus(JToken goods)
|
|
{
|
|
var result = new List<SkuItem>();
|
|
|
|
var skus = goods["skus"] as JArray;
|
|
|
|
if (skus == null)
|
|
return result;
|
|
|
|
foreach (var sku in skus)
|
|
{
|
|
var specs = sku["specs"] as JArray;
|
|
|
|
var specValues = new List<string>();
|
|
|
|
if (specs != null)
|
|
{
|
|
foreach (var spec in specs)
|
|
{
|
|
var key = spec["spec_key"]?.Value<string>();
|
|
var value = spec["spec_value"]?.Value<string>();
|
|
|
|
if (!string.IsNullOrEmpty(value))
|
|
specValues.Add($"{key}:{value}");
|
|
}
|
|
}
|
|
|
|
// 优先使用 normalPrice
|
|
var price = sku["normalPrice"].Value<decimal>();
|
|
|
|
result.Add(new SkuItem
|
|
{
|
|
SkuId = sku["skuId"]?.Value<string>() ?? "",
|
|
|
|
Spec = string.Join(";", specValues),
|
|
|
|
Price = price,
|
|
|
|
// oldGroupPrice 在这个 JSON 中是整数分价格,
|
|
// 这里暂时转成字符串
|
|
OriginalPrice = sku["oldGroupPrice"]?.Value<string>() ?? "",
|
|
|
|
Stock = sku["quantity"]?.Value<int>() ?? 0,
|
|
|
|
SpecImage = sku["thumbUrl"]?.Value<string>() ?? "",
|
|
});
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
}
|
|
}
|