open.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using dodohold.core;
  2. using Sayaka.Common;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Web;
  6. namespace molilian.core
  7. {
  8. public partial class PddUnionPlus
  9. {
  10. static string _baseUrl = "https://gw-api.pinduoduo.com/api/router";
  11. public class ErrorResponse
  12. {
  13. public string error_msg { get; set; }
  14. public string sub_msg { get; set; }
  15. public string sub_code { get; set; }
  16. public int error_code { get; set; }
  17. public string request_id { get; set; }
  18. }
  19. public class GoodsZsUnitGenerateResponse
  20. {
  21. public string multi_group_mobile_short_url { get; set; }
  22. public string multi_group_url { get; set; }
  23. public string mobile_url { get; set; }
  24. public string multi_group_short_url { get; set; }
  25. public string mobile_short_url { get; set; }
  26. public string multi_group_mobile_url { get; set; }
  27. public string request_id { get; set; }
  28. public string url { get; set; }
  29. public string short_url { get; set; }
  30. }
  31. public class UnionGeneratelinkDTO
  32. {
  33. public GoodsZsUnitGenerateResponse goods_zs_unit_generate_response { get; set; }
  34. public ErrorResponse error_response { get; set; }
  35. }
  36. public static Dictionary<string, string> GenerateSignature(PddPoolDTO account, Dictionary<string, string> args)
  37. {
  38. if (args.ContainsKey("sign")) args.Remove("sign");
  39. // 对参数进行ASCII升序排序
  40. var sortedArgs = args.OrderBy(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value);
  41. // 拼接排序后的参数
  42. StringBuilder stringBuilder = new StringBuilder();
  43. foreach (var kv in sortedArgs)
  44. {
  45. stringBuilder.Append(kv.Key).Append(kv.Value);
  46. }
  47. // 在头部和尾部分别拼接client_secret
  48. string signString = account.app_secret + stringBuilder.ToString() + account.app_secret;
  49. signString = signString.MD5(false, false);
  50. if (!args.TryAdd("sign", signString))
  51. {
  52. args["sign"] = signString;
  53. }
  54. return args;
  55. }
  56. /// <summary>
  57. /// 多多进宝转链接口
  58. /// </summary>
  59. /// <param name="result"></param>
  60. /// <param name="sourceUrl"></param>
  61. /// <param name="cancellationToken"></param>
  62. /// <returns></returns>
  63. public async Task<PddDataDTO> UnionGeneratelink(PddDataDTO result, string sourceUrl, CancellationToken cancellationToken = default)
  64. {
  65. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  66. var args = new Dictionary<string, string>
  67. {
  68. { "type", "pdd.ddk.goods.zs.unit.url.gen" },
  69. { "data_type", "JSON" },
  70. { "client_id", _account.app_key },
  71. { "pid", _account.pid },
  72. { "source_url", sourceUrl },
  73. { "timestamp", ts.ToString() }
  74. };
  75. args = GenerateSignature(_account, args);
  76. var query = HttpUtility.ParseQueryString(string.Empty);
  77. foreach (var kvp in args) query[kvp.Key] = kvp.Value;
  78. string urlWithQuery = $"{_baseUrl}?{query}";
  79. WebClientUtility client = new();
  80. client.Proxy = _proxy;
  81. #if DEBUG
  82. client.Proxy = null;
  83. #endif
  84. client.SetContentType("application/json");
  85. var response = await client.RequestAsync(urlWithQuery, "GET", cancellationToken);
  86. string body = response.Body();
  87. var root = body.Convert2Object<UnionGeneratelinkDTO>();
  88. string message = string.Empty;
  89. _ = new LoggerLibrary("PddUnion", "debug ").Info(urlWithQuery, body).SaveAsync();
  90. if (root.error_response != null || root.goods_zs_unit_generate_response == null)
  91. {
  92. message = $"{root.error_response.sub_code}:{root.error_response.sub_msg}";
  93. result.success = false;
  94. result.message = "转链失败";
  95. result.reason = message;
  96. _ = new LoggerLibrary("PddUnion", "UnionGeneratelink").Info(urlWithQuery, body).SaveAsync();
  97. return result;
  98. }
  99. if (string.IsNullOrEmpty(root.goods_zs_unit_generate_response.url))
  100. {
  101. result.success = false;
  102. result.message = "转链失败";
  103. result.reason = "无法转链";
  104. _ = new LoggerLibrary("PddUnion", "UnionGeneratelink").Info(urlWithQuery, body).SaveAsync();
  105. return result;
  106. }
  107. result.itemId = root.goods_zs_unit_generate_response.url.GetContentPart("goods_id=", "&");
  108. result.success = true;
  109. result.message = "OK";
  110. result.link_type = LinkTypeEnum.goods;
  111. result.shortLinkurl = root.goods_zs_unit_generate_response.short_url;
  112. result.content = result.shortLinkurl;
  113. result.deeplink_url = GetDeeplink(result.shortLinkurl);
  114. return result;
  115. }
  116. }
  117. }