bills.cs 4.2 KB

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