using AllBase; using AllBase.Helper; using AllBase.Model.Result; using NaiBa.Util.Extension; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using Work.CollectClass; namespace TemuKJD.BLL.PddApi { public class PddApi { public Dictionary Data { get; set; } = new Dictionary(); public PddApi(ShopLoginInfo shop) { if (shop.CurrentAuthInfo == null) { PddApiHelper.RefreshApi(shop); } Data["access_token"] = shop.AccessToken; Data["client_id"] = shop.AppKey; } /// /// 貔貅管家 /// public static string clientId_pi_xiu_guan_jia { get; set; } = "ad502ec19e0c49ab9df14f83494e153c"; ///// ///// 小红椒商品管理 ///// //public static string clientId_xiao_hong_jiao_ { get; set; } = "48e76c60853d47d8a4e2e631b6db26b5"; public string PddGoodsAdd() { string type = "pdd.goods.add"; string html = PddApiRequest(type, Data); return html; } public string PddGoodsDetailGet() { string type = "pdd.goods.detail.get"; string html = PddApiRequest(type, Data); return html; } public string PddGoodsInformationUpdate() { string type = "pdd.goods.information.update"; string html = PddApiRequest(type, Data); return html; } public string PddGoodsLogisticsTemplateGet() { string type = "pdd.goods.logistics.template.get"; string html = PddApiRequest(type, Data); return html; } public string PddGoodsImageUpload() { string type = "pdd.goods.image.upload"; string html = PddApiRequest(type, Data); return html; } public string PddGoodsSpecGet() { string type = "pdd.goods.spec.get"; string html = PddApiRequest(type, Data); return html; } public string PddGoodsSpecIdGet() { string type = "pdd.goods.spec.id.get"; string html = PddApiRequest(type, Data); return html; } public string PddGoodsCatRuleGet() { string type = "pdd.goods.cat.rule.get"; string html = PddApiRequest(type, Data); return html; } public string PddGoodsGetRelation(string pid = "") { string type = "pdd.goods.get.relation"; if (!string.IsNullOrEmpty(pid)) Data["pdd_goods_id"] = "[" + pid.ToString() + "]"; string html = PddApiRequest(type, Data); return html; } public string PddGoodsCatsGet() { string type = "pdd.goods.cats.get"; string html = PddApiRequest(type, Data); return html; } public string PddPopAuthTokenCreate(string code) { string type = "pdd.pop.auth.token.create"; if (!Data.Keys.Contains("code")) Data["code"] = code; //"83be994d5ed04e6cb9643a423094a3296bf8827a"; string html = PddApiRequest(type, Data); return html; } /// /// 关联 /// /// /// /// 最低价 分 /// 最高价 分 /// 邮费 元 /// /// 外部平台枚举值, 0-淘宝/天猫 ,1-京东,2-1688,3-唯品会,4-苏宁,5-亚马逊,6-网易, /// 8-微店,9-网商园,10-蘑菇街,11-vvic搜款网,12-搜鞋网,13-一起做网店17zwd,14-杭州女装网, /// 15-折800,16-当当,17-青创网,18-开山网,19-新款网 /// /// public string Relation2603(long goodsId, string outGoodsId, int minPrice, int maxPrice, int postAge, int sourceType) { string type = "pdd.goods.relation.set"; Data["goods_id"] = goodsId.ToString(); Data["out_goods_id"] = outGoodsId.ToString(); string detailUrl = ""; if (sourceType == 0) detailUrl = "https://item.taobao.com/item.htm?id=" + outGoodsId; if (sourceType == 2) detailUrl = "https://detail.1688.com/offer/" + outGoodsId + ".html"; else if (sourceType == 8) detailUrl = "https://weidian.com/item.html?itemID=" + outGoodsId; else if (sourceType == 1) detailUrl = "https://item.jd.com/" + outGoodsId + ".html"; Data["out_detail_url"] = detailUrl; Data["out_low_goods_price"] = minPrice.ToString(); Data["out_high_goods_price"] = maxPrice.ToString(); //Data["source_type"] = sourceType.ToString(); Data["postage"] = postAge.ToString(); string html = PddApiRequest(type, Data); return html; } /// /// 更新商品,然后设置关联映射(更新成功必须设置映射关系)。 /// 拼多多要求所有的商品必须与淘宝商品ID关联,且拼多多每个SKU价格小于等于淘宝价格*1.08。 /// 如果有以下情况:1.上家为拼多多商品、2.加价大于8%、3.单独有增加邮费,则去淘宝查找同款,获取单价大于当前加价后价格93%的 /// 淘宝ID,然后把价格和淘宝ID通过接口影射修改商品接口。其他情况,使用拼多多查询之前的映射关系,再次映射。如果无法通过以上 /// 办法获取映射,则想尽所有办法做一个正确的,甚至虚假的映射。 /// /// 关联参数类 /// public string UpdataThenRelation(PinduoduoRelation pddRelation) { Dictionary dataTemp = new Dictionary(); foreach (var keyValue in this.Data) { dataTemp.Add(keyValue.Key, keyValue.Value); } string upResult = string.Empty; #region 先判断是否有基本数据足以关联,不足直接退出 if (pddRelation == null) pddRelation = new PinduoduoRelation(); if (pddRelation.goods_id == null) { if (dataTemp.ContainsKey("goods_id")) { try { pddRelation.goods_id = Convert.ToInt64(dataTemp["goods_id"]); } catch { //LogSave.Add(Name, "拼多多id获取失败,跳过"); return "拼多多id获取失败"; } } else { //LogSave.Add(Name, "拼多多id获取失败,跳过"); return "拼多多id获取失败"; } } if (pddRelation.out_low_goods_price == null || pddRelation.out_high_goods_price == null) { pddRelation.out_low_goods_price = int.MaxValue; pddRelation.out_high_goods_price = 0; if (dataTemp.ContainsKey("sku_list")) { var skuJson = dataTemp["sku_list"]; var mPriceMatches = BaseRegex.GetMatchRegex(skuJson.ParseToString(), "\"multi_price\":\"([\\s\\S]*?)\""); if (mPriceMatches.Count > 0) { foreach (Match mPriceMatch in mPriceMatches) { try { string strPrice = BaseRegex.GetMatchRegexString(mPriceMatch.ToString(), "\"multi_price\":\"([\\s\\S]*?)\""); long price = Convert.ToInt64(strPrice); if (price < pddRelation.out_low_goods_price) pddRelation.out_low_goods_price = price; if (price > pddRelation.out_high_goods_price) pddRelation.out_high_goods_price = price; } catch { } } pddRelation.out_low_goods_price = (long)(pddRelation.out_low_goods_price * 0.93); pddRelation.out_high_goods_price = (long)(pddRelation.out_high_goods_price * 0.93); } } if (pddRelation.out_low_goods_price == int.MaxValue) return "关联价格获取失败"; } #endregion #region 获取其他关联数据 #region 通过服务器缓存获取关联数据 //// 内部获取上家id //string idx = ""; //var re = new JumpUpProductNew(Shop).GetServerId(pddRelation.goods_id.Value, out idx); #endregion #region 通过接口获取关联数据 if (pddRelation.out_goods_id == null || pddRelation.source_type == null) { string html = PddGoodsGetRelation(pddRelation.goods_id.ToString()); // 有获取到对应的上家id if (html.Contains("out_goods_id") && html.Contains("out_goods_platform")) { if (pddRelation.out_goods_id == null) pddRelation.out_goods_id = BaseRegex.GetLongJson(ref html, "out_goods_id").ToString(); if (pddRelation.source_type == null) pddRelation.source_type = BaseRegex.GetIntJson(ref html, "out_goods_platform"); } else { // 未获取到上家id的情况,随机淘宝id,临时使用 pddRelation.out_goods_id = "5" + Randomx.GetNumber(11); pddRelation.source_type = 0; } } #endregion if (pddRelation.postage == null) pddRelation.postage = 0; #endregion #region 更新商品 this.Data = dataTemp; upResult = this.PddGoodsInformationUpdate(); // 调用失败直接返回结果,不设置关联 if (string.IsNullOrEmpty(upResult) || !upResult.Contains("\"is_success\":true")) return upResult; #endregion #region 设置关联 try { string html = this.Relation2603(pddRelation.goods_id.Value, pddRelation.out_goods_id, (int)pddRelation.out_low_goods_price.Value, (int)pddRelation.out_high_goods_price, (int)pddRelation.postage.Value, pddRelation.source_type.Value); if (string.IsNullOrEmpty(html) || !html.Contains("\"is_success\":true")) { //LogSave.Add(Name, "关联设置失败,商品id是" + pddRelation.goods_id); return "关联设置失败,商品id是" + pddRelation.goods_id; } } catch { } #endregion return upResult; } /// /// /// /// 拼多多调用接口 pdd.* /// 拼多多接口入参 /// static string PddApiRequest(string type, Dictionary dict) { if (string.IsNullOrEmpty(type)) return "{\"success\":false, \"error_msg\":\"调用接口不能为空\"}"; if (dict == null) return "{\"success\":false, \"error_msg\":\"参数数组不能为空\"}"; if (!dict.Keys.Contains("client_id") || string.IsNullOrEmpty(dict["client_id"].ParseToString())) return "{\"success\":false, \"error_msg\":\"调用Api编号不能为空\"}"; if (!dict.Keys.Contains("access_token") || string.IsNullOrWhiteSpace(dict["access_token"].ParseToString())) return "{\"success\":false, \"error_msg\":\"调用令牌不能为空\"}"; var clientSecret = GlobalConfig.PddClientInfoList.FirstOrDefault(c => c.client_id == dict["client_id"].ParseToString())?.client_secret; if (clientSecret.IsNullOrEmpty()) return "{\"success\":false, \"error_msg\":\"调用密钥不能为空\"}"; DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); var timeStamp = (long)(DateTime.Now - startTime).TotalMilliseconds; dict["type"] = type; dict["timestamp"] = timeStamp.ToString(); dict["data_type"] = "JSON"; var paramsDic = SignHelper.BuildSign(dict, clientSecret); var jsonBody = JsonConvert.SerializeObject(paramsDic); string result = GetHtml.PostUtf8("http://gw-api.pinduoduo.com/api/router", jsonBody, "application/json"); return result; } } }