bills.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using dodohold.core;
  2. using Spire.Pdf.Xmp;
  3. using System.Data;
  4. using System.Drawing.Printing;
  5. using System.Text.Json;
  6. namespace molilian.core
  7. {
  8. public partial class AlimamaPlus
  9. {
  10. private const string base_bill_url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/new.settle.media.pre.income.daily.bill.detail.json";
  11. public int GetSettleBills(int sleep)
  12. {
  13. DateTime startTime = DateTime.Parse("2024-03-01");
  14. DateTime endTime = DateTime.Now;
  15. // 确保 startTime 小于 endTime
  16. if (startTime > endTime)
  17. {
  18. (startTime, endTime) = (endTime, startTime);
  19. }
  20. int pageNo = 1;
  21. string dataVersion = string.Empty;
  22. int pageSize = 20;
  23. bool hasNext = true;
  24. int total = 0;
  25. while (hasNext)
  26. {
  27. // ?t=1717528348646&_tb_token_=576b361b883b8&pageNo=2&pageSize=20&endTime=2024-06-04&startTime=2024-05-05&dataVersion=20240603
  28. var ts = DateTime.Now.Convert2UnixTimestamp();
  29. string url = $"{base_bill_url}?t={ts}&_tb_token_={_tb_token}&pageNo={pageNo}&pageSize={pageSize}&startTime={startTime:yyyy-MM-dd}&endTime={endTime:yyyy-MM-dd}&dataVersion={dataVersion}";
  30. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  31. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  32. .AddHeaders("Cookie", _cookies);
  33. #if DEBUG
  34. #else
  35. client.Proxy = _proxy;
  36. #endif
  37. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  38. var response = client.Request(url);
  39. if (response.ResponseException != null)
  40. {
  41. _ = new LoggerLibrary("api_error", "fail")
  42. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  43. .SaveAsync();
  44. throw response.ResponseException;
  45. }
  46. var body = response.Body();
  47. JsonElement data;
  48. try
  49. {
  50. var root = body.Convert2JsonElement();
  51. var success = root.Read<bool>("success");
  52. data = root.ElementRead("data");
  53. //{"code":601,"info":{"ok":false,"message":"nologin"}}
  54. if (!success && body.Contains("nologin"))
  55. {
  56. (success, string message) = RenewCookie();
  57. if (!success)
  58. {
  59. TkPoolCore.Disabled(_accountId, _accountName, $"{body}");
  60. }
  61. return 0;
  62. }
  63. if (!success)
  64. {
  65. throw new APIException(body);
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. throw new APIException(body);
  71. }
  72. using var conn = DBContext.GetOpenConnection();
  73. conn.Open();
  74. try
  75. {
  76. foreach (var order in data.ElementRead("result").EnumerateArray())
  77. {
  78. TkSettleBillDTO item = order.GetRawText().Convert2Object<TkSettleBillDTO>();
  79. // 处理每个订单数据
  80. item.accountId = _accountId;
  81. item.accountName = _company;
  82. int? id = conn.Replace(item);
  83. total++;
  84. }
  85. }
  86. catch (Exception ex)
  87. {
  88. throw;
  89. }
  90. finally
  91. {
  92. conn.Dispose();
  93. }
  94. pageNo++;
  95. hasNext = data.Read<bool>("hasNext");
  96. dataVersion = data.Read<string>("dataVersion");
  97. if (sleep > 0) Thread.Sleep(sleep);
  98. }
  99. return total;
  100. }
  101. }
  102. }