SpreadEffect.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 query_date, 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 = query_date.AddMonths(-1).ToString("yyyy-MM-dd"),
  24. endDate = query_date.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. var response = await client.RequestAsync(url, "POST", cancellationToken);
  49. string body = response.Body();
  50. var root = body.Convert2JsonElement();
  51. int code = root.Read<int>("code");
  52. string message = root.Read<string>("message");
  53. hasNext = root.Read<bool>("hasNext", false);
  54. if (code == 200 && "success".Equals(message))
  55. {
  56. var list = root.ElementRead("result").ElementRead("spreadReportInfoChatList");
  57. foreach (var item in list.EnumerateArray())
  58. {
  59. var report = item.GetRawText().Convert2Object<SpreadReportInfoDTO>();
  60. if (report.clickNum == 0 && report.orderNum == 0 &&
  61. report.cosFee == 0 && report.finishOrderNum == 0) continue;
  62. if (DateTime.TryParse(report.accountDate, out DateTime report_date))
  63. {
  64. changed_dates.Add(report_date.Date);
  65. var exist = new DBContext.Table("jd_spread_report")
  66. .Fields("id")
  67. .Get<dynamic>("report_date=@report_date AND accountId=@accountId",
  68. new { report_date = report_date.Date, accountId = _account.id });
  69. var update = new DBContext.Table("jd_spread_report")
  70. .Add("accountId", _account.id)
  71. .Add("accountName", _account.name)
  72. .Add("clickNum", report.clickNum)
  73. .Add("cosFee", report.cosFee)
  74. .Add("cosPrice", report.cosPrice)
  75. .Add("finishCosFee", report.finishCosFee)
  76. .Add("finishCosPrice", report.finishCosPrice)
  77. .Add("finishOrderNum", report.finishOrderNum)
  78. .Add("orderNum", report.orderNum)
  79. .Add("last_time", DateTime.Now);
  80. if (exist == null)
  81. {
  82. update.Add("report_date", report_date.Date)
  83. .Add("create_time", DateTime.Now)
  84. .Create();
  85. }
  86. else
  87. {
  88. update.Where("id=@id", new { exist.id }).Update();
  89. }
  90. }
  91. }
  92. }
  93. else
  94. {
  95. throw new APIException(body);
  96. }
  97. }
  98. return changed_dates;
  99. }
  100. }
  101. }