308 lines
12 KiB
C#
308 lines
12 KiB
C#
using AllBase;
|
||
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<string, object> Data { get; set; } = new Dictionary<string, object>();
|
||
|
||
public PddApi(ShopLoginInfo shop)
|
||
{
|
||
if (shop.CurrentAuthInfo == null)
|
||
{
|
||
PddApiHelper.RefreshApi(shop);
|
||
}
|
||
|
||
Data["access_token"] = shop.AccessToken;
|
||
}
|
||
|
||
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 PddPopAuthTokenCreate()
|
||
{
|
||
string type = "pdd.pop.auth.token.create";
|
||
if (!Data.Keys.Contains("code"))
|
||
Data["code"] = "83be994d5ed04e6cb9643a423094a3296bf8827a";
|
||
string html = PddApiRequest(type, Data);
|
||
return html;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 关联
|
||
/// </summary>
|
||
/// <param name="pid"></param>
|
||
/// <param name="outGoodsId"></param>
|
||
/// <param name="minPrice">最低价 分</param>
|
||
/// <param name="maxPrice">最高价 分</param>
|
||
/// <param name="postAge">邮费 元</param>
|
||
/// <param name="sourceType">
|
||
/// 外部平台枚举值, 0-淘宝/天猫 ,1-京东,2-1688,3-唯品会,4-苏宁,5-亚马逊,6-网易,
|
||
/// 8-微店,9-网商园,10-蘑菇街,11-vvic搜款网,12-搜鞋网,13-一起做网店17zwd,14-杭州女装网,
|
||
/// 15-折800,16-当当,17-青创网,18-开山网,19-新款网
|
||
/// </param>
|
||
/// <returns></returns>
|
||
public string Relation2603(long pid, long outGoodsId, int minPrice, int maxPrice, int postAge, int sourceType)
|
||
{
|
||
string type = "pdd.goods.relation.set";
|
||
Data = new Dictionary<string, object>();
|
||
Data.Add("goods_id", pid.ToString());
|
||
Data.Add("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.Add("out_detail_url", detailUrl);
|
||
Data.Add("out_low_goods_price", minPrice.ToString());
|
||
Data.Add("out_high_goods_price", maxPrice.ToString());
|
||
Data.Add("source_type", sourceType.ToString());
|
||
Data.Add("postage", postAge.ToString());
|
||
string html = PddApiRequest(type, Data);
|
||
return html;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新商品,然后设置关联映射(更新成功必须设置映射关系)。
|
||
/// 拼多多要求所有的商品必须与淘宝商品ID关联,且拼多多每个SKU价格小于等于淘宝价格*1.08。
|
||
/// 如果有以下情况:1.上家为拼多多商品、2.加价大于8%、3.单独有增加邮费,则去淘宝查找同款,获取单价大于当前加价后价格93%的
|
||
/// 淘宝ID,然后把价格和淘宝ID通过接口影射修改商品接口。其他情况,使用拼多多查询之前的映射关系,再次映射。如果无法通过以上
|
||
/// 办法获取映射,则想尽所有办法做一个正确的,甚至虚假的映射。
|
||
/// </summary>
|
||
/// <param name="pddRelation">关联参数类</param>
|
||
/// <returns></returns>
|
||
public string UpdataThenRelation(PinduoduoRelation pddRelation)
|
||
{
|
||
Dictionary<string, object> dataTemp = new Dictionary<string, object>();
|
||
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, long.Parse(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;
|
||
}
|
||
|
||
|
||
public static string clientId { get; set; } = "ad502ec19e0c49ab9df14f83494e153c";
|
||
public static string clientSecret { get; set; } = "bd52aba51737d03fc073711d386b3b0779d010ad";
|
||
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
/// <param name="type">拼多多调用接口 pdd.*</param>
|
||
/// <param name="dict">拼多多接口入参</param>
|
||
/// <returns></returns>
|
||
static string PddApiRequest(string type, Dictionary<string,object> dict)
|
||
{
|
||
|
||
if (string.IsNullOrEmpty(type))
|
||
return "{\"success\":false, \"error_msg\":\"调用接口不能为空\"}";
|
||
if (dict == null)
|
||
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["client_id"] = clientId;
|
||
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;
|
||
}
|
||
}
|
||
}
|