SpreadEffect.cs 7.3 KB

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