bills.cs 4.3 KB

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