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(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(); product.GoodsUrl = ""; product.BrandId = goodsToken["brandId"].ParseToLong(); product.Title = goodsToken["goodsName"]?.Value(); product.Description = goodsToken["short_name"]?.Value() ?? ""; product.ThumbUrl = goodsToken["thumbUrl"]?.Value() ?? ""; product.CategoryId = goodsToken["catID"]?.Value() ?? 0; product.CategoryPath = GetCategoryPath(goodsToken); product.MallId = goodsToken["mallID"]?.Value() ?? 0; product.Price = goodsToken["linePrice"]?.Value() ?? 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; } /// /// 获取类目路径 /// private List GetCategoryPath(JToken goods) { var result = new List(); // 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(); if (value > 0) result.Add(value); } } // 如果没有,则至少加入当前类目 if (result.Count == 0) { var categoryId = goods["catID"]?.Value() ?? 0; if (categoryId > 0) result.Add(categoryId); } return result; } /// /// 商品属性 /// private List GetProperties(JToken goods) { var result = new List(); var propertyArray = goods["goodsProperty"] as JArray; if (propertyArray == null) return result; foreach (var item in propertyArray) { var key = item["key"]?.Value(); var values = item["values"] as JArray; result.Add(new Property { Key = key ?? "", Value = string.Join(",", values?.Select(a => a)) }); } return result; } /// /// 主图 /// private List GetMainImages(JToken goods) { var result = new List(); var images = goods["topGallery"] as JArray; if (images != null) { foreach (var item in images) { var url = item["url"]?.Value(); 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; } /// /// 详情图 /// private List GetDetailImages(JToken goods) { var result = new List(); var images = goods["detailGallery"] as JArray; if (images == null) return result; foreach (var item in images) { var url = item["url"]?.Value(); if (!string.IsNullOrEmpty(url)) result.Add(url); } return result; } /// /// 商品视频 /// private List GetVideos(JToken goods) { var result = new List(); var videos = goods["videoGallery"] as JArray; if (videos == null) return result; foreach (var item in videos) { var url = item["videoUrl"]?.Value() ?? item["url"]?.Value(); if (!string.IsNullOrEmpty(url)) result.Add(url); } return result; } /// /// 获取规格 /// private List GetSpecs(JToken goods) { var result = new List(); 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() ?? ""; var specValue = spec["spec_value"]?.Value() ?? ""; var specKeyId = spec["spec_key_id"]?.Value() ?? 0; var specValueId = spec["spec_value_id"]?.Value() ?? 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; } /// /// SKU /// private List GetSkus(JToken goods) { var result = new List(); 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(); if (specs != null) { foreach (var spec in specs) { var key = spec["spec_key"]?.Value(); var value = spec["spec_value"]?.Value(); if (!string.IsNullOrEmpty(value)) specValues.Add($"{key}:{value}"); } } // 优先使用 normalPrice var price = sku["normalPrice"].Value(); result.Add(new SkuItem { SkuId = sku["skuId"]?.Value() ?? "", Spec = string.Join(";", specValues), Price = price, // oldGroupPrice 在这个 JSON 中是整数分价格, // 这里暂时转成字符串 OriginalPrice = sku["oldGroupPrice"]?.Value() ?? "", Stock = sku["quantity"]?.Value() ?? 0, SpecImage = sku["thumbUrl"]?.Value() ?? "", }); } return result; } } }