EstimateRevenue.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using dodohold.core;
  2. using Sayaka.Common;
  3. using System.Drawing.Printing;
  4. using System.Text.Json;
  5. namespace molilian.core
  6. {
  7. public partial class PddUnionPlus
  8. {
  9. public async Task queryEstimateRevenueList(DateTime startTime, DateTime endTime, CancellationToken cancellationToken = default)
  10. {
  11. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  12. string cookies = _account.cookies;
  13. bool hasNext = true;
  14. int pageNo = 1;
  15. int pageSize = 100;
  16. while (hasNext)
  17. {
  18. var args = new
  19. {
  20. startTime = startTime.ToString("yyyy-MM-dd"),
  21. endTime = endTime.ToString("yyyy-MM-dd"),
  22. orderPromotionType = 0,
  23. pageNumber = pageNo,
  24. pageSize
  25. };
  26. DateTime stime = DateTime.Now;
  27. string url = "https://jinbao.pinduoduo.com/network/api/order/stat/overall/list";
  28. var root = await this.RequestAsync(url, args.Convert2Json());
  29. TimeSpan ts2 = DateTime.Now - stime;
  30. string xx = ts2.TotalSeconds.ToString();
  31. var success = root.Read<bool>("success");
  32. if (!success) break;
  33. var list = root.ElementRead("result").ElementRead("list");
  34. string filter = "accountId=@accountId AND report_date=@report_date AND pid=@pid";
  35. var len = list.GetArrayLength();
  36. if (len == 0) break;
  37. foreach (var item in list.EnumerateArray())
  38. {
  39. var report = item.GetRawText().Convert2Object<PddEstimateRevenueDTO>();
  40. var exist = new DBContext.Table("pdd_estimate_revenue")
  41. .Fields("id")
  42. .Get<dynamic>(filter, new { report_date = report.calcDate, accountId = _account.id, report.pid });
  43. var update = new DBContext.Table("pdd_estimate_revenue")
  44. .Add("accountId", _account.id)
  45. .Add("accountName", _account.name)
  46. .Add("report_date", report.calcDate)
  47. .Add("pid", report.pid)
  48. .Add("pidName", report.pidName)
  49. .Add("orderNum", report.orderNum)
  50. .Add("orderAmount", report.orderAmount)
  51. .Add("feeAmount", report.feeAmount)
  52. .Add("avgPrice", report.avgPrice)
  53. .Add("mallRelFeeAmount", report.mallRelFeeAmount)
  54. .Add("lockFeeAmount", report.lockFeeAmount)
  55. .Add("last_time", DateTime.Now);
  56. if (exist == null)
  57. {
  58. update.Add("report_date", report.calcDate)
  59. .Add("create_time", DateTime.Now)
  60. .Create(DBContext.InsertType.REPLACE);
  61. }
  62. else
  63. {
  64. update.Where("id=@id", new { exist.id }).Update();
  65. }
  66. }
  67. pageNo++;
  68. }
  69. }
  70. public async Task<int> queryMallBalance(CancellationToken cancellationToken = default)
  71. {
  72. try
  73. {
  74. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  75. string cookies = _account.cookies;
  76. DateTime stime = DateTime.Now;
  77. string url = "https://jinbao.pinduoduo.com/network/api/finance/mallBalance";
  78. var root = await this.RequestAsync(url);
  79. TimeSpan ts2 = DateTime.Now - stime;
  80. string xx = ts2.TotalSeconds.ToString();
  81. var success = root.Read<bool>("success");
  82. if (!success) throw new Exception(root.Convert2Json());
  83. var balanceAmount = root.PathRead<int>("result.balanceAmount");
  84. var withdrawalAmount = root.PathRead<int>("result.withdrawalAmount");
  85. return new DBContext.Table("pdd_pool")
  86. .Add("draw_balance", withdrawalAmount * 0.001)
  87. .Where("id=@id", new { _account.id })
  88. .Update();
  89. }
  90. catch (Exception ex)
  91. {
  92. if (ex.Message.Contains("43001:会话已过期"))
  93. {
  94. PddPoolCore.CookieDisabled(_account.id);
  95. }
  96. string message = $"【PDD报表接口异常】{_account.id}:{_account.name}\tmallBalance\n{ex.Message}\n{ex.StackTrace}";
  97. NotifyCore.Notify(new NifyMessage
  98. {
  99. message = message,
  100. priority = NifyMessagePriority.high,
  101. tags = ["red_circle"]
  102. });
  103. }
  104. return 0;
  105. }
  106. public async Task<JsonElement> RequestAsync(string url, string data = null, CancellationToken cancellationToken = default)
  107. {
  108. string cookies = _account.cookies.Trim();
  109. string useragent = ProviderFakeUserAgent.RandomComputer;
  110. WebClientUtility client = new WebClientUtility();
  111. client.Proxy = _proxy;
  112. #if DEBUG
  113. client.Proxy = null;
  114. #endif
  115. client.SetContentType("application/json");
  116. client.AddHeaders("Referer", "https://jinbao.pinduoduo.com/data/data-analysis");
  117. client.AddHeaders("Origin", "https://jinbao.pinduoduo.com");
  118. client.UserAgent = useragent;
  119. client.AddHeaders("Cookie", cookies);
  120. if (!string.IsNullOrEmpty(data)) client.Post(data);
  121. var response = await client.RequestAsync(url, string.IsNullOrEmpty(data) ? "GET" : "POST", cancellationToken);
  122. string body = response.Body();
  123. var root = body.Convert2JsonElement();
  124. //string message = string.Empty;
  125. // todo 这里我会做一些 日志 处理
  126. var success = root.Read<bool>("success");
  127. if (!success)
  128. {
  129. var errorCode = root.Read<int>("errorCode");
  130. var errorMsg = root.Read<string>("errorMsg");
  131. _ = new LoggerLibrary("PddUnion", "unsuccessful").Info(url, data).Info(body).SaveAsync();
  132. throw new APIException($"{errorCode}:{errorMsg}");
  133. }
  134. return root;
  135. }
  136. }
  137. }