144 lines
6.8 KiB
C#
144 lines
6.8 KiB
C#
using AllBase.Model;
|
|
using AllBase.Model.Result;
|
|
using AllBase.Model.Result.PDDResult;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using TemuKJD.BLL.ImportConverts;
|
|
using AllBase;
|
|
using System.Collections.Generic;
|
|
using TemuKJD.BLL;
|
|
|
|
namespace Work.Services;
|
|
|
|
public static class ComplianceProductReader
|
|
{
|
|
public static Product Read(ComplianceGoods item)
|
|
{
|
|
KJDDBProduct db;
|
|
if (item.type == ComplianceGoods.Cloud)
|
|
db = new KJDDBProduct { product_json = CloudGoodsService.GetGoodsDetail(long.Parse(item.id)) };
|
|
else if (item.type == ComplianceGoods.Library)
|
|
{
|
|
db = GetProductDetail(item.id, out var message);
|
|
if (!string.IsNullOrEmpty(message)) throw new InvalidDataException(message);
|
|
}
|
|
else throw new InvalidDataException("未知商品来源:" + item.type);
|
|
|
|
if (string.IsNullOrWhiteSpace(db?.product_json)) throw new InvalidDataException("未取得商品详情。");
|
|
var raw = db.product_json;
|
|
var root = JObject.Parse(raw);
|
|
PDDProductInfo info;
|
|
string error = null;
|
|
var converter = new PDDProdFormConverter();
|
|
if (root.SelectToken("detail.galleries") is JArray)
|
|
info = root.ToObject<PDDProductInfo>();
|
|
else if (db.product_get_type_text == "厂商直采") info = converter.JstPddProdToPddConvert(out error, db);
|
|
else if (db.product_get_type_text == "微信店采") info = converter.WXProdToPddConvert(out error, db);
|
|
else if (db.product_get_type_text == "微信小店") info = converter.WXClientToPddConvert(out error, db);
|
|
else if (db.product_get_type_text == "1688厂家") info = converter._1688ClientToPddConvert(out error, db);
|
|
else if (db.product_get_type_text == "1688外部数据") info = converter._1688OutToPddConvert(out error, db);
|
|
else
|
|
{
|
|
var front = ConverterToPddFactory.SetContentConvert(raw);
|
|
if (front != null)
|
|
{
|
|
var originalId = front.GoodsId > 0 ? front.GoodsId.ToString() : db.spu_id;
|
|
return new Product
|
|
{
|
|
ProductId = item.type == ComplianceGoods.Cloud ? item.id : originalId,
|
|
Platform = item.platform.HasValue ? (PlatformType)item.platform.Value : front.Platform,
|
|
Title = front.Title ?? item.name, Description = front.Description,
|
|
MainImages = front.MainImages, DetailImages = front.DetailImages,
|
|
Skus = front.Skus?.Select(x => new ProductSku { SkuId = x.SkuId, ImageUrl = x.SpecImage }).ToList()
|
|
};
|
|
}
|
|
string converted;
|
|
if (raw.Contains("\"spec_keys\"") && raw.Contains("\"spec_values\""))
|
|
converted = converter.PddFrontToPddBackConvert(raw, isSaveDb: false);
|
|
else if (raw.Contains("\"goods_id\"") && raw.Contains("\"gallery\""))
|
|
converted = converter.PddFrontToPddBackConvert2(raw, isSaveDb: false);
|
|
else if (raw.Contains("\"topGallery\"") && raw.Contains("\"detailGallery\""))
|
|
converted = converter.PddFrontToPddBackConvert3(raw, isSaveDb: false);
|
|
else if (raw.Contains("\"商品编号\"") && raw.Contains("\"商品链接\""))
|
|
converted = converter.PddFrontToPddBackConvertV4(raw, isSaveDb: false);
|
|
else throw new InvalidDataException("不支持的商品详情格式。");
|
|
if (converted == null || !converted.StartsWith("success", StringComparison.Ordinal))
|
|
throw new InvalidDataException(converted ?? "商品详情转换失败。");
|
|
info = JObject.Parse(converted.Substring(7)).ToObject<PDDProductInfo>();
|
|
}
|
|
if (!string.IsNullOrEmpty(error) || info?.detail == null)
|
|
throw new InvalidDataException(error ?? "商品详情转换失败。");
|
|
var detail = info.detail;
|
|
var platform = db.product_get_type_text switch
|
|
{
|
|
"微信店采" or "微信小店" or "微信外部数据" or "微信" => PlatformType.WX,
|
|
"厂商直采" or "1688厂家" or "1688外部数据" => PlatformType.None,
|
|
_ => PlatformType.PDD
|
|
};
|
|
var productId = db.product_get_type_text == "厂商直采"
|
|
? (string)root.SelectToken("pddData.itemSpuId") ?? db.spu_id
|
|
: detail.goods_id > 0 ? detail.goods_id.ToString() : db.spu_id;
|
|
return new Product
|
|
{
|
|
Platform = item.platform.HasValue ? (PlatformType)item.platform.Value : platform,
|
|
ProductId = item.type == ComplianceGoods.Cloud ? item.id : productId,
|
|
Title = detail.goods_name ?? item.name, Description = detail.goods_desc,
|
|
MainImages = detail.galleries?.Where(x => x.type == 1).Select(x => x.url).ToList(),
|
|
DetailImages = detail.galleries?.Where(x => x.type == 2).Select(x => x.url).ToList(),
|
|
Skus = detail.sku?.Select(x => new ProductSku { SkuId = x.sku_id.ToString(), ImageUrl = x.thumb_url }).ToList()
|
|
};
|
|
}
|
|
|
|
public static KJDDBProduct GetProductDetail(string prodDbId, out string errMsg, bool noViolationProduct = false, bool noBanWx = false)
|
|
{
|
|
errMsg = "";
|
|
|
|
if (string.IsNullOrEmpty(prodDbId))
|
|
{
|
|
errMsg = "产品编号为空";
|
|
return new KJDDBProduct();
|
|
}
|
|
|
|
List<KJDDBProduct> list = new List<KJDDBProduct>();
|
|
|
|
var url = BaseClass.BuildObjString("product/get_pdd_product_detail_list");
|
|
var data = UserLogin.GetSignaturex() + "&search_db_ids=" + prodDbId;
|
|
if (noViolationProduct)
|
|
data += "&wx_safe_status=安全商品,未校验";
|
|
if (noBanWx)
|
|
data += "&ban_wx=0";
|
|
if (BaseClass.SaLevel > 0 || BaseClass.UserAuthority.Contains("爆款数据") || BaseClass.UserAuthority.Contains("学员数据") || BaseClass.ParentAuthority.Contains("学员数据"))
|
|
data += "&special_prod_type=普通商品,爆款数据";
|
|
|
|
var html = GetHtml.PostUtf8(url, data);
|
|
if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200"))
|
|
html = GetHtml.PostUtf8(url, data);
|
|
if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200"))
|
|
html = GetHtml.PostUtf8(url, data);
|
|
if (string.IsNullOrEmpty(html) || !html.Contains("\"code\":200"))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var jArr = JObject.Parse(html)["data"];
|
|
if (jArr.Count() == 0)
|
|
return null;
|
|
var sProd = JsonHelper.SerializeObject(jArr);
|
|
|
|
try
|
|
{
|
|
list = JsonHelper.DeserializeJsonToList<KJDDBProduct>(sProd);
|
|
var item = list.FirstOrDefault();
|
|
if (item == null)
|
|
item = new KJDDBProduct();
|
|
return item;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|