TaobaoOpenPlus.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using dodohold.core;
  2. using System.Text.Json;
  3. using System.Xml;
  4. using TencentCloud.Lighthouse.V20200324.Models;
  5. using TencentCloud.Weilingwith.V20230427.Models;
  6. using Top.Api;
  7. using Top.Api.Request;
  8. using Top.Api.Response;
  9. namespace molilian.core
  10. {
  11. public partial class TaobaoOpenPlus
  12. {
  13. public enum RecommendRequestType
  14. {
  15. first,
  16. second,
  17. ori,
  18. }
  19. string _app_key = string.Empty;
  20. string _app_secret = string.Empty;
  21. string _pid = string.Empty;
  22. string _server_url = "https://eco.taobao.com/router/rest";
  23. public TaobaoOpenPlus(string app_key, string app_secret, string pid)
  24. {
  25. _app_key = app_key;
  26. _app_secret = app_secret;
  27. _pid = pid;
  28. }
  29. public static PromotionQueryItemDTO ConverPromotionQueryItem(JsonElement item)
  30. {
  31. PromotionQueryItemDTO result = JsonSerializer.Deserialize<PromotionQueryItemDTO>(item.GetRawText());
  32. string h5_url = item.Read("couponShareUrl", string.Empty);
  33. if (string.IsNullOrEmpty(h5_url))
  34. {
  35. h5_url = item.Read("url", string.Empty);
  36. }
  37. if (h5_url.StartsWith("//")) h5_url = "https:" + h5_url;
  38. result.h5_url = h5_url;
  39. result.deeplink_url = AlimamaPlus.GetDeeplink(h5_url);
  40. if (result.pic.StartsWith("//")) result.pic = "https:" + result.pic;
  41. return result;
  42. }
  43. public TbkThorMfrsPromotionQueryResponse PromotionQueryRequest(string base64, string oaid)
  44. {
  45. ITopClient client = new DefaultTopClient(_server_url, _app_key, _app_secret, "json");
  46. TbkThorMfrsPromotionQueryRequest req = new();
  47. TbkThorMfrsPromotionQueryRequest.ManufacturerLaunchOptionDomain obj1 = new()
  48. {
  49. Img = base64,
  50. Pid = _pid,
  51. UniqueId = oaid,
  52. UniqueIdType = "oaid",
  53. SceneCode = "h5_img",
  54. SceneSubCode = "Hitoch"
  55. };
  56. req.Option_ = obj1;
  57. TbkThorMfrsPromotionQueryResponse rsp = client.Execute(req);
  58. return rsp;
  59. }
  60. public async Task<(List<PromotionQueryItemDTO>, string)> GetRecommendData(RecommendRequestType type, string oaid,
  61. string cookies)
  62. {
  63. string token = "undefined";
  64. if (!string.IsNullOrEmpty(cookies)) token = cookies.GetContentPart("_m_h5_tk=", "_");
  65. string url = GetRecommendUrl(_pid, oaid, token, type);
  66. string body = string.Empty;
  67. if (string.IsNullOrEmpty(cookies))
  68. {
  69. var res = await new WebClientUtility().RequestAsync(url);
  70. body = res.Body();
  71. cookies = res.ResponseMessage.GetCookiesString();
  72. token = cookies.GetContentPart("_m_h5_tk=", "_");
  73. }
  74. url = GetRecommendUrl(_pid, oaid, token, type);
  75. WebClientUtility client = new WebClientUtility();
  76. if (!string.IsNullOrEmpty(cookies)) client.SetCookies(cookies);
  77. var response = await client.RequestAsync(url);
  78. body = response.Body();
  79. body = body.GetContentPart("mtopjsonp1(", ")");
  80. var root = body.Convert2JsonElement();
  81. var ret = root.PathRead("ret[0]", string.Empty);
  82. if (!"SUCCESS::调用成功".Equals(ret))
  83. {
  84. throw new Exception(ret);
  85. }
  86. var resultList = new List<PromotionQueryItemDTO>();
  87. foreach (var data in root.ElementRead("data").ElementRead("resultList").EnumerateArray())
  88. {
  89. PromotionQueryItemDTO item = ConverPromotionQueryItem(data);
  90. resultList.Add(item);
  91. }
  92. return (resultList, cookies);
  93. }
  94. public string GetRecommendUrl(string pid, string oaid, string token, RecommendRequestType type)
  95. {
  96. var first_data = $"{{\"scene\":\"pltSimilarPromotion\",\"variableMap\":\"{{\\\"pid\\\":\\\"{pid}\\\",\\\"oaid\\\":\\\"{oaid}\\\",\\\"type\\\":\\\"first\\\"}}\"}}";
  97. var second_data = $"{{\"scene\":\"pltSimilarPromotion\",\"variableMap\":\"{{\\\"pid\\\":\\\"{pid}\\\",\\\"oaid\\\":\\\"{oaid}\\\",\\\"type\\\":\\\"second\\\"}}\"}}";
  98. var ori_data = $"{{\"scene\":\"pltPromotionImg\",\"variableMap\":\"{{\\\"pid\\\":\\\"{pid}\\\",\\\"oaid\\\":\\\"{oaid}\\\",\\\"hasCoupon\\\":false,\\\"filterType\\\":\\\"ori\\\",\\\"seq\\\":\\\"\\\"}}\"}}";
  99. string ts = DateTime.Now.Convert2UnixTimestamp(true).ToString();
  100. string data = type switch
  101. {
  102. RecommendRequestType.first => first_data,
  103. RecommendRequestType.second => second_data,
  104. _ => ori_data,
  105. };
  106. string sign = GetSign(token, ts, data);
  107. string url = $"https://h5api.m.taobao.com/h5/mtop.union.thor.landing.page.recommend/1.0/" +
  108. $"?jsv=2.4.16&appKey=12574478&t={ts}&sign={sign}&v=1.0&api=mtop.union.thor.landing.page.recommend" +
  109. $"&timeout=20000&AntiCreep=true&AntiFlood=true&type=jsonp&dataType=jsonp&callback=mtopjsonp1" +
  110. $"&data={data.UrlEncode()}";
  111. return url;
  112. }
  113. private string GetSign(string token, string ts, string data)
  114. {
  115. string g = "12574478";
  116. //%7B%22scene%22%3A%22pltPromotionImg%22%2C%22variableMap%22%3A%22%7B%5C%22pid%5C%22%3A%5C%22mm_5993059531_3080100059_115689750259%5C%22%2C%5C%22oaid%5C%22%3A%5C%22-7775661578900138800%5C%22%2C%5C%22hasCoupon%5C%22%3Afalse%2C%5C%22filterType%5C%22%3A%5C%22ori%5C%22%2C%5C%22seq%5C%22%3A%5C%22%5C%22%7D%22%7D
  117. //%7B%22scene%22%3A%22pltPromotionImg%22%2C%22variableMap%22%3A%22%7B%5C%22pid%5C%22%3A%5C%22mm_5993059531_3080100059_115689750259%5C%22%2C%5C%22oaid%5C%22%3A%5C%22-7775661578900138800%5C%22%2C%5C%22hasCoupon%5C%22%3Afalse%2C%5C%22filterType%5C%22%3A%5C%22ori%5C%22%2C%5C%22seq%5C%22%3A%5C%22%5C%22%7D%22%7D
  118. var signString = $"{token}&{ts}&{g}&{data}";
  119. return signString.MD5();
  120. }
  121. }
  122. }