SpreadEffect.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using dodohold.core;
  2. using Sayaka.Common;
  3. namespace molilian.core
  4. {
  5. public partial class JdUnionPlus
  6. {
  7. public async Task<List<DateTime>> querySpreadEffectData(DateTime startDate, DateTime endDate, CancellationToken cancellationToken = default)
  8. {
  9. List<DateTime> changed_dates = new List<DateTime>();
  10. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  11. string cookies = _account.cookies;
  12. string useragent = _account.user_agent;
  13. if (string.IsNullOrEmpty(useragent)) useragent = ProviderFakeUserAgent.RandomComputer;
  14. bool hasNext = true;
  15. int pageNo = 1;
  16. while (hasNext)
  17. {
  18. var args = new
  19. {
  20. funName = "querySpreadEffectData",
  21. param = new
  22. {
  23. startDate = startDate.ToString("yyyy-MM-dd"),
  24. endDate = endDate.ToString("yyyy-MM-dd"),
  25. mediaId = "",
  26. proCont = "",
  27. promotionId = "",
  28. sourceEmt = "",
  29. pageNo,
  30. pageSize = 20
  31. },
  32. };
  33. string data = args.Convert2Json().UrlEncode();
  34. string url = $"https://api.m.jd.com/api?functionId=union_report&appid=unionpc&loginType=3&body={data}";
  35. WebClientUtility client = new WebClientUtility();
  36. client.Proxy = _proxy;
  37. #if DEBUG
  38. client.Proxy = null;
  39. #endif
  40. client.SetContentType("application/x-www-form-urlencoded");
  41. client.AddHeaders("X-Referer-Page", "https://union.jd.com/proManager/index");
  42. client.AddHeaders("X-Rp-Client", "h5_1.0.0");
  43. client.AddHeaders("Referer", "https://union.jd.com/");
  44. client.AddHeaders("Origin", "https://union.jd.com");
  45. client.UserAgent = useragent;
  46. client.AddHeaders("Cookie", cookies);
  47. client.Post($"body={data}");
  48. DateTime stime = DateTime.Now;
  49. var response = await client.RequestAsync(url, "POST", cancellationToken);
  50. TimeSpan ts2 = stime - DateTime.Now;
  51. string xx = ts2.TotalSeconds.ToString();
  52. string body = response.Body();
  53. var root = body.Convert2JsonElement();
  54. int code = root.Read<int>("code");
  55. string message = root.Read<string>("message");
  56. hasNext = root.Read<bool>("hasNext", false);
  57. pageNo++;
  58. if (code == 200 && "success".Equals(message))
  59. {
  60. var list = root.ElementRead("result").ElementRead("spreadReportInfoChatList");
  61. foreach (var item in list.EnumerateArray())
  62. {
  63. var report = item.GetRawText().Convert2Object<SpreadReportInfoDTO>();
  64. if (report.clickNum == 0 && report.orderNum == 0 &&
  65. report.cosFee == 0 && report.finishOrderNum == 0) continue;
  66. if (DateTime.TryParse(report.accountDate, out DateTime report_date))
  67. {
  68. changed_dates.Add(report_date.Date);
  69. var exist = new DBContext.Table("jd_spread_report")
  70. .Fields("id")
  71. .Get<dynamic>("report_date=@report_date AND accountId=@accountId",
  72. new { report_date = report_date.Date, accountId = _account.id });
  73. var update = new DBContext.Table("jd_spread_report")
  74. .Add("accountId", _account.id)
  75. .Add("accountName", _account.name)
  76. .Add("clickNum", report.clickNum)
  77. .Add("cosFee", report.cosFee)
  78. .Add("cosPrice", report.cosPrice)
  79. .Add("finishCosFee", report.finishCosFee)
  80. .Add("finishCosPrice", report.finishCosPrice)
  81. .Add("finishOrderNum", report.finishOrderNum)
  82. .Add("orderNum", report.orderNum)
  83. .Add("last_time", DateTime.Now);
  84. if (exist == null)
  85. {
  86. update.Add("report_date", report_date.Date)
  87. .Add("create_time", DateTime.Now)
  88. .Create(DBContext.InsertType.REPLACE);
  89. }
  90. else
  91. {
  92. update.Where("id=@id", new { exist.id }).Update();
  93. }
  94. }
  95. }
  96. }
  97. else
  98. {
  99. throw new APIException(body);
  100. }
  101. }
  102. return changed_dates;
  103. }
  104. }
  105. }