orders.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. using dodohold.core;
  2. using Spire.Pdf.Xmp;
  3. using System.Data;
  4. using System.Drawing.Printing;
  5. using System.Runtime.InteropServices;
  6. using System.Text.Json;
  7. using System.Text.RegularExpressions;
  8. using static System.Runtime.InteropServices.JavaScript.JSType;
  9. namespace molilian.core
  10. {
  11. public partial class AlimamaPlus
  12. {
  13. private const string base_orders_url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/report.getTbkOrderDetails.json";
  14. public int GetHistoryOrders(int sleep, DateTime startTime, DateTime endTime)
  15. {
  16. //DateTime endTime = DateTime.Now;
  17. //DateTime startTime = endTime.AddDays(-90);
  18. //if (_accountId == 3) endTime = DateTime.Parse("2024-04-07");
  19. //#if DEBUG
  20. // startTime = DateTime.Parse("2024-05-01");
  21. // endTime = DateTime.Parse("2024-06-01");
  22. // int total = 0;
  23. // List<DateTime> orderTimes = new List<DateTime>();
  24. // for (DateTime date = startTime; date <= endTime; date = date.AddDays(1))
  25. // {
  26. // orderTimes.Add(date);
  27. // }
  28. //#else
  29. //#endif
  30. //当日往前查询
  31. (int total, DateTime max_modifiedTime, List<DateTime> orderTimes) = GetTkOrders(startTime, endTime, DateTime.MinValue, false, sleep);
  32. string sql = @"
  33. UPDATE tk_report tr
  34. JOIN (
  35. SELECT
  36. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  37. COUNT(*) AS order_ord_num, SUM(pubShareFeeForCommission) AS order_ord_tfee
  38. FROM tk_order_details
  39. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=3 AND accountId=@accountId
  40. GROUP BY DATE(tbPaidTime), accountId
  41. ) AS sub
  42. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  43. SET tr.order_ord_amt = sub.order_ord_amt, tr.order_ord_num_3 = sub.order_ord_num,
  44. tr.order_ord_tfee = sub.order_ord_tfee;";
  45. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  46. sql = @"
  47. UPDATE tk_report tr
  48. JOIN (
  49. SELECT
  50. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  51. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  52. FROM tk_order_details
  53. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=12 AND accountId=@accountId
  54. GROUP BY DATE(tbPaidTime), accountId
  55. ) AS sub
  56. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  57. SET tr.order_ord_amt_12 = sub.order_ord_amt, tr.order_ord_num_12 = sub.order_ord_num,
  58. tr.order_ord_tfee_12 = sub.order_ord_tfee;";
  59. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  60. sql = @"
  61. UPDATE tk_report tr
  62. JOIN (
  63. SELECT
  64. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  65. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  66. FROM tk_order_details
  67. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=13 AND accountId=@accountId
  68. GROUP BY DATE(tbPaidTime), accountId
  69. ) AS sub
  70. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  71. SET tr.order_ord_amt_13 = sub.order_ord_amt, tr.order_ord_num_13 = sub.order_ord_num,
  72. tr.order_ord_tfee_13 = sub.order_ord_tfee;";
  73. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  74. sql = @"
  75. UPDATE tk_report tr
  76. JOIN (
  77. SELECT
  78. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  79. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  80. FROM tk_order_details
  81. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=14 AND accountId=@accountId
  82. GROUP BY DATE(tbPaidTime), accountId
  83. ) AS sub
  84. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  85. SET tr.order_ord_amt_14 = sub.order_ord_amt, tr.order_ord_num_14 = sub.order_ord_num,
  86. tr.order_ord_tfee_14 = sub.order_ord_tfee;";
  87. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  88. sql = @"UPDATE tk_report
  89. SET order_ord_num = order_ord_num_3 + order_ord_num_12 + order_ord_num_13 + order_ord_num_14
  90. WHERE DATE(report_date) IN @orderTimes AND accountId=@accountId;";
  91. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  92. return total;
  93. }
  94. public int GetIncyOrders(int sleep)
  95. {
  96. string cacheKey = $":cache:GetIncyOrders:{_accountName}";
  97. string cacheKey2 = $":cache:GetIncyOrders:{_accountId}";
  98. DateTime endTime = DateTime.Now;
  99. DateTime startTime = endTime.AddDays(-1);
  100. DateTime last_modifiedTime = RedisHelper.Get<DateTime>(cacheKey);
  101. ////找出数据库最新的一天
  102. //var last = new DBContext.Table("tk_order_details")
  103. // .Order("modifiedTime DESC")
  104. // .Get<TkOrderDetailDTO>("accountId=@accountId", new { accountId = _accountId });
  105. if (last_modifiedTime != DateTime.MinValue)
  106. {
  107. startTime = last_modifiedTime;
  108. }
  109. else
  110. {
  111. last_modifiedTime = startTime;
  112. }
  113. //当日往前查询
  114. (int total, DateTime max_modifiedTime, List<DateTime> orderTimes) = GetTkOrders(startTime, endTime, last_modifiedTime, true, sleep);
  115. if (max_modifiedTime != DateTime.MinValue)
  116. {
  117. RedisHelper.Set(cacheKey, max_modifiedTime);
  118. RedisHelper.Set(cacheKey2, max_modifiedTime);
  119. }
  120. //todo 根据orderTimes、settlementTimes 更新被影响的相关行
  121. string sql = @"
  122. UPDATE tk_report tr
  123. JOIN (
  124. SELECT
  125. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  126. COUNT(*) AS order_ord_num, SUM(pubShareFeeForCommission) AS order_ord_tfee
  127. FROM tk_order_details
  128. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=3 AND accountId=@accountId
  129. GROUP BY DATE(tbPaidTime), accountId
  130. ) AS sub
  131. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  132. SET tr.order_ord_amt = sub.order_ord_amt, tr.order_ord_num_3 = sub.order_ord_num,
  133. tr.order_ord_tfee = sub.order_ord_tfee;";
  134. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  135. sql = @"
  136. UPDATE tk_report tr
  137. JOIN (
  138. SELECT
  139. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  140. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  141. FROM tk_order_details
  142. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=12 AND accountId=@accountId
  143. GROUP BY DATE(tbPaidTime), accountId
  144. ) AS sub
  145. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  146. SET tr.order_ord_amt_12 = sub.order_ord_amt, tr.order_ord_num_12 = sub.order_ord_num,
  147. tr.order_ord_tfee_12 = sub.order_ord_tfee;";
  148. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  149. sql = @"
  150. UPDATE tk_report tr
  151. JOIN (
  152. SELECT
  153. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  154. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  155. FROM tk_order_details
  156. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=13 AND accountId=@accountId
  157. GROUP BY DATE(tbPaidTime), accountId
  158. ) AS sub
  159. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  160. SET tr.order_ord_amt_13 = sub.order_ord_amt, tr.order_ord_num_13 = sub.order_ord_num,
  161. tr.order_ord_tfee_13 = sub.order_ord_tfee;";
  162. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  163. sql = @"
  164. UPDATE tk_report tr
  165. JOIN (
  166. SELECT
  167. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  168. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  169. FROM tk_order_details
  170. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=14 AND accountId=@accountId
  171. GROUP BY DATE(tbPaidTime), accountId
  172. ) AS sub
  173. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  174. SET tr.order_ord_amt_14 = sub.order_ord_amt, tr.order_ord_num_14 = sub.order_ord_num,
  175. tr.order_ord_tfee_14 = sub.order_ord_tfee;";
  176. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  177. sql = @"UPDATE tk_report
  178. SET order_ord_num = order_ord_num_3 + order_ord_num_12 + order_ord_num_13 + order_ord_num_14
  179. WHERE DATE(report_date) IN @orderTimes AND accountId=@accountId;";
  180. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  181. return total;
  182. }
  183. public int GetOrdersByAdZone(long adzoneId, DateTime startTime, DateTime endTime, int sleep)
  184. {
  185. string cacheKey = $":cache:GetOrdersByAdZone:{_accountName}";
  186. string cacheKey2 = $":cache:GetOrdersByAdZone:{_accountId}";
  187. DateTime last_modifiedTime = RedisHelper.Get<DateTime>(cacheKey);
  188. if (last_modifiedTime != DateTime.MinValue)
  189. {
  190. startTime = last_modifiedTime;
  191. }
  192. else
  193. {
  194. last_modifiedTime = startTime;
  195. }
  196. //当日往前查询
  197. (int total, DateTime max_modifiedTime, List<DateTime> orderTimes) = GetTkOrdersByAdZone(adzoneId, startTime, endTime, last_modifiedTime, true, sleep);
  198. if (max_modifiedTime != DateTime.MinValue)
  199. {
  200. RedisHelper.Set(cacheKey, max_modifiedTime);
  201. RedisHelper.Set(cacheKey2, max_modifiedTime);
  202. }
  203. return total;
  204. }
  205. public (int, DateTime, List<DateTime>) GetTkOrdersByAdZone(long adzoneId, DateTime startTime, DateTime endTime, DateTime last_modifiedTime, bool desc = true, int sleep = 100)
  206. {
  207. List<DateTime> orderTimes = [];
  208. if (startTime > endTime)
  209. {
  210. (startTime, endTime) = (endTime, startTime);
  211. }
  212. DateTime max_modifiedTime = last_modifiedTime;
  213. int pageNo = 1;
  214. string positionIndex = string.Empty;
  215. int pageSize = 100;
  216. bool hasNext = true;
  217. int total = 0;
  218. while (hasNext)
  219. {
  220. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  221. string jumpType = pageNo == 1 ? "0" : "1";
  222. #if DEBUG
  223. jumpType = pageNo == 1 ? "0" : "1";
  224. #endif
  225. string queryType = desc ? "4" : "2";
  226. string url = $"{base_orders_url}?t={ts}&_tb_token_={_tb_token}&pageNo={pageNo}&pageSize={pageSize}&startTime={startTime:yyyy-MM-dd}&endTime={endTime:yyyy-MM-dd}&payStatus=&queryType={queryType}&jumpType={jumpType}&tkTradeId=&positionIndex={positionIndex.UrlEncode()}";
  227. string body = "";
  228. JsonElement data;
  229. try
  230. {
  231. for (var i = 1; i <= 5; i++)
  232. {
  233. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  234. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  235. .AddHeaders("Cookie", _cookies);
  236. #if DEBUG
  237. #else
  238. client.Proxy = _proxy;
  239. #endif
  240. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  241. var response = client.Request(url);
  242. if (response.ResponseException != null)
  243. {
  244. _ = new LoggerLibrary("api_error", "fail")
  245. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  246. .SaveAsync();
  247. throw response.ResponseException;
  248. }
  249. body = response.Body();
  250. #if DEBUG
  251. _ = new LoggerLibrary("debug", "GetTkOrdersByAdZone")
  252. .Info(body)
  253. .SaveAsync();
  254. #endif
  255. if (body.Contains("{\"action\":\"captcha\""))
  256. {
  257. string message = $"【GetTkOrdersByAdZone】【{_accountId}:{_accountName}】第 {i} 次出现滑动验证码";
  258. NotifyCore.Notify(new NifyMessage
  259. {
  260. message = message,
  261. priority = NifyMessagePriority.high,
  262. tags = ["red_circle"]
  263. });
  264. Thread.Sleep(i * 60 * 1000);
  265. continue;
  266. }
  267. if (body.Contains("\"resultCode\":500"))
  268. {
  269. string message = $"【GetTkOrdersByAdZone】【{_accountId}:{_accountName}】第 {i} 次出现错误\n{body}";
  270. NotifyCore.Notify(new NifyMessage
  271. {
  272. message = message,
  273. priority = NifyMessagePriority.high,
  274. tags = ["red_circle"]
  275. });
  276. Thread.Sleep(i * 60 * 1000);
  277. continue;
  278. }
  279. break;
  280. }
  281. var root = body.Convert2JsonElement();
  282. var success = root.Read<bool>("success");
  283. data = root.ElementRead("data");
  284. if (!success && body.Contains("nologin"))
  285. {
  286. (success, string message) = RenewCookie();
  287. if (!success)
  288. {
  289. TkPoolCore.Disabled(_accountId, _accountName, $"{body}");
  290. }
  291. return (0, DateTime.MinValue, new List<DateTime>());
  292. }
  293. if (!success)
  294. {
  295. throw new APIException(body);
  296. }
  297. }
  298. catch (Exception ex)
  299. {
  300. throw new APIException(body);
  301. }
  302. using var conn = DBContext.GetOpenConnection();
  303. conn.Open();
  304. try
  305. {
  306. foreach (var order in data.ElementRead("result").EnumerateArray())
  307. {
  308. TkOrderDetailAdZoneDTO item = order.GetRawText().Convert2Object<TkOrderDetailAdZoneDTO>();
  309. // 处理每个订单数据
  310. if (item.adzoneId != adzoneId) continue;
  311. item.accountId = _accountId;
  312. item.accountName = _company;
  313. if (item.modifiedTime > max_modifiedTime) max_modifiedTime = item.modifiedTime;
  314. if (last_modifiedTime != DateTime.MinValue && last_modifiedTime >= item.modifiedTime)
  315. {
  316. return (total, max_modifiedTime, orderTimes);
  317. }
  318. if (item.tkStatus == 3 && item.tbPaidTime != DateTime.MinValue) orderTimes.Add(item.tbPaidTime.Date);
  319. int? id = conn.Replace(item);
  320. total++;
  321. }
  322. }
  323. catch (Exception ex)
  324. {
  325. throw;
  326. }
  327. finally
  328. {
  329. conn.Dispose();
  330. }
  331. pageNo++;
  332. hasNext = data.Read<bool>("hasNext");
  333. positionIndex = data.Read<string>("positionIndex");
  334. if (sleep > 0) Thread.Sleep(sleep);
  335. }
  336. return (total, max_modifiedTime, orderTimes);
  337. }
  338. public (int, DateTime, List<DateTime>) GetTkOrders(DateTime startTime, DateTime endTime, DateTime last_modifiedTime, bool desc = true, int sleep = 100)
  339. {
  340. List<DateTime> orderTimes = [];
  341. // 确保 startTime 小于 endTime
  342. if (startTime > endTime)
  343. {
  344. (startTime, endTime) = (endTime, startTime);
  345. }
  346. DateTime max_modifiedTime = last_modifiedTime;
  347. int pageNo = 1;
  348. string positionIndex = string.Empty;
  349. int pageSize = 100;
  350. bool hasNext = true;
  351. int total = 0;
  352. while (hasNext)
  353. {
  354. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  355. //string jumpType = desc ? pageNo == 1 ? "0" : "1" : "-1";
  356. string jumpType = pageNo == 1 ? "0" : "1";
  357. #if DEBUG
  358. jumpType = pageNo == 1 ? "0" : "1";
  359. #endif
  360. //string queryType = "1";
  361. //1 创建时间;3结算时间 2付款时间 4更新时间
  362. //string queryType = "2";
  363. string queryType = desc ? "4" : "2";
  364. string url = $"{base_orders_url}?t={ts}&_tb_token_={_tb_token}&pageNo={pageNo}&pageSize={pageSize}&startTime={startTime:yyyy-MM-dd}&endTime={endTime:yyyy-MM-dd}&payStatus=&queryType={queryType}&jumpType={jumpType}&tkTradeId=&positionIndex={positionIndex.UrlEncode()}";
  365. //https://pub.alimama.com/openapi/param2/1/gateway.unionpub/report.getTbkOrderDetails.json?t=1719477837298&_tb_token_=7537e7ed93338&pageNo=92&pageSize=20&startTime=2024-03-31&endTime=2024-03-31&payStatus=&queryType=2&jumpType=1&tkTradeId=&positionIndex=1711883373_2vtPPr7CyYX2%7C1711883522_4zcICUeUevh2
  366. //https://pub.alimama.com/openapi/param2/1/gateway.unionpub/report.getTbkOrderDetails.json?t=1719477942492&_tb_token_=7537e7ed93338&pageNo=7&pageSize=20&startTime=2024-03-31&endTime=2024-03-31&payStatus=&queryType=2&jumpType=1&tkTradeId=&positionIndex=1711899163_4zdpOH1mbTQ2%7C1711899483_4zbH9BHqgl82
  367. string body = "";
  368. JsonElement data;
  369. try
  370. {
  371. for (var i = 1; i <= 5; i++)
  372. {
  373. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  374. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  375. .AddHeaders("Cookie", _cookies);
  376. #if DEBUG
  377. #else
  378. client.Proxy = _proxy;
  379. #endif
  380. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  381. var response = client.Request(url);
  382. if (response.ResponseException != null)
  383. {
  384. _ = new LoggerLibrary("api_error", "fail")
  385. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  386. .SaveAsync();
  387. throw response.ResponseException;
  388. }
  389. body = response.Body();
  390. #if DEBUG
  391. _ = new LoggerLibrary("debug", "GetTkOrders")
  392. .Info(body)
  393. .SaveAsync();
  394. #endif
  395. if (body.Contains("{\"action\":\"captcha\""))
  396. {
  397. string message = $"【GetTkOrders】【{_accountId}:{_accountName}】第 {i} 次出现滑动验证码";
  398. NotifyCore.Notify(new NifyMessage
  399. {
  400. message = message,
  401. priority = NifyMessagePriority.high,
  402. tags = ["red_circle"]
  403. });
  404. Thread.Sleep(i * 60 * 1000);
  405. continue;
  406. }
  407. if (body.Contains("\"resultCode\":500"))
  408. {
  409. //{"success":false,"resultCode":500,"bizErrorDesc":"系统繁忙,请稍后重试!","bizErrorCode":3001}
  410. string message = $"【GetTkOrders】【{_accountId}:{_accountName}】第 {i} 次出现错误\n{body}";
  411. NotifyCore.Notify(new NifyMessage
  412. {
  413. message = message,
  414. priority = NifyMessagePriority.high,
  415. tags = ["red_circle"]
  416. });
  417. Thread.Sleep(i * 60 * 1000);
  418. continue;
  419. }
  420. break;
  421. }
  422. /*
  423. <script>sessionStorage.x5referer = window.location.href;var url = window.location.protocol + "//pub.alimama.com//openapi/param2/1/gateway.unionpub/report.getTbkOrderDetails.json/_____tmd_____/punish?x5secdata=xc1iwR0XfHknuQ2tGhtadtTAvEt14YScfTDHt2ZiSRqroaYZK98uykgsVOy7o75Rw8uuycKT3Rpx30%2fB5H0gRc0%2fJVGQUcV5g%2fsAmZ%2bc46eyD7DXSdA4R3mLnmtbp9wuZ1vmrG3ZPlH7brUilFbXBdZ7EBrru1X7wPfC05Eq3g4mhDFiGq39xUtwQcC%2f%2bPbtekcm8%2fVJ%2brVt8aExunwzXzJPJJTAvEt14YScfTDHt2ZiSRqroaYZK98uykgsiXIkw%2ffMWwebJ3zxCja5CzSu%2ffTA%3d%3d__bx__pub.alimama.com%2fopenapi%2fparam2%2f1%2fgateway.unionpub%2freport.getTbkOrderDetails.json&x5step=1";window.location.replace(url);window._config_ = {"action":"captcha","url":"https://pub.alimama.com//openapi/param2/1/gateway.unionpub/report.getTbkOrderDetails.json/_____tmd_____/punish?x5secdata=xc1iwR0XfHknuQ2tGhtadtTAvEt14YScfTDHt2ZiSRqroaYZK98uykgsVOy7o75Rw8uuycKT3Rpx30%2fB5H0gRc0%2fJVGQUcV5g%2fsAmZ%2bc46eyD7DXSdA4R3mLnmtbp9wuZ1vmrG3ZPlH7brUilFbXBdZ7EBrru1X7wPfC05Eq3g4mhDFiGq39xUtwQcC%2f%2bPbtekcm8%2fVJ%2brVt8aExunwzXzJPJJTAvEt14YScfTDHt2ZiSRqroaYZK98uykgsiXIkw%2ffMWwebJ3zxCja5CzSu%2ffTA%3d%3d__bx__pub.alimama.com%2fopenapi%2fparam2%2f1%2fgateway.unionpub%2freport.getTbkOrderDetails.json&x5step=1"};</script><!--rgv587_flag:sm--> */
  424. var root = body.Convert2JsonElement();
  425. var success = root.Read<bool>("success");
  426. data = root.ElementRead("data");
  427. //{"code":601,"info":{"ok":false,"message":"nologin"}}
  428. if (!success && body.Contains("nologin"))
  429. {
  430. (success, string message) = RenewCookie();
  431. if (!success)
  432. {
  433. TkPoolCore.Disabled(_accountId, _accountName, $"{body}");
  434. }
  435. return (0, DateTime.MinValue, new List<DateTime>());
  436. }
  437. if (!success)
  438. {
  439. throw new APIException(body);
  440. }
  441. }
  442. catch (Exception ex)
  443. {
  444. throw new APIException(body);
  445. }
  446. using var conn = DBContext.GetOpenConnection();
  447. conn.Open();
  448. try
  449. {
  450. foreach (var order in data.ElementRead("result").EnumerateArray())
  451. {
  452. TkOrderDetailDTO item = order.GetRawText().Convert2Object<TkOrderDetailDTO>();
  453. // 处理每个订单数据
  454. item.accountId = _accountId;
  455. item.accountName = _company;
  456. if (item.modifiedTime > max_modifiedTime) max_modifiedTime = item.modifiedTime;
  457. if (last_modifiedTime != DateTime.MinValue && last_modifiedTime >= item.modifiedTime)
  458. {
  459. return (total, max_modifiedTime, orderTimes);
  460. }
  461. //进行订单和转链匹配
  462. item.itemId = GetRawItemId(item.mktId);
  463. if (item.tkStatus == 3 && item.tbPaidTime != DateTime.MinValue) orderTimes.Add(item.tbPaidTime.Date);
  464. int? id = conn.Replace(item);
  465. if (id != null)
  466. {
  467. item.id = (int)id;
  468. TkOrderTrackingCore.MatchOrder(_account, item, conn);
  469. }
  470. total++;
  471. }
  472. }
  473. catch (Exception ex)
  474. {
  475. throw;
  476. }
  477. finally
  478. {
  479. conn.Dispose();
  480. }
  481. pageNo++;
  482. hasNext = data.Read<bool>("hasNext");
  483. positionIndex = data.Read<string>("positionIndex");
  484. if (sleep > 0) Thread.Sleep(sleep);
  485. }
  486. return (total, max_modifiedTime, orderTimes);
  487. }
  488. string GetRawItemId(string mktId)
  489. {
  490. try
  491. {
  492. string cacheKey = $":cache:mkt2itemId:{mktId}";
  493. string itemId = RedisHelper.Get(cacheKey);
  494. if (itemId != null) return itemId;
  495. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  496. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/union.moose.content.entry";
  497. //string url = $"{base_orders_url}?t={ts}&_tb_token_={_tb_token}&pageNo={pageNo}&pageSize={pageSize}&startTime={startTime:yyyy-MM-dd}&endTime={endTime:yyyy-MM-dd}&payStatus=&queryType={queryType}&jumpType={jumpType}&tkTradeId=&positionIndex={positionIndex}";
  498. var client = new WebClientUtility()
  499. .SetContentType("application/x-www-form-urlencoded")
  500. .AddHeaders("Cookie", _cookies)
  501. .AddHeaders("Origin", "https://pub.alimama.com")
  502. .Post("_tb_token_", _tb_token)
  503. .Post("t", ts.ToString())
  504. .Post("bizType", "detailPage")
  505. .Post("bizParam", $"{{\"itemId\":\"{mktId}\"}}");
  506. //_tb_token_=3b88755bbe3f7&t=1718896771391&bizType=detailPage&bizParam=%7B%22itemId%22%3A%22JbtMUqkGMQvagTNb2Fvta-9ag242pu7MOX5e0bsrY%22%7D
  507. //_tb_token_=361e383eb61e5&t=1718895935097&bizType=detailPage&bizParam=%7B%22itemId%22%3A%22JbtMUqkGMQvagTNb2Fvta-9ag242pu7MOX5e0bsrY%22%7D
  508. #if DEBUG
  509. #else
  510. client.Proxy = _proxy;
  511. #endif
  512. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  513. var response = client.Request(url);
  514. if (!response.Successed)
  515. {
  516. throw response.ResponseException;
  517. }
  518. var body = response.Body();
  519. var root = body.Convert2JsonElement();
  520. itemId = root.PathRead<string>("model.item.itemId");
  521. if (!string.IsNullOrEmpty(itemId))
  522. {
  523. RedisHelper.Set(cacheKey, itemId, 86400 * 7);
  524. }
  525. return itemId;
  526. }
  527. catch (Exception ex)
  528. {
  529. string message = $"【GetRawItemId】[{mktId}\n{ex.Message}\n{ex.StackTrace}";
  530. NotifyCore.Notify(new NifyMessage
  531. {
  532. message = message,
  533. priority = NifyMessagePriority.high,
  534. tags = ["red_circle"]
  535. });
  536. }
  537. return null;
  538. }
  539. public void GetTkOrders222()
  540. {
  541. try
  542. {
  543. string url = "https://media.alimama.com/violationu2/warning_instance_page_v2.json?r=mx_208&toPage=1&pageSize=40&params=";
  544. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  545. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  546. .AddHeaders("Cookie", _cookies);
  547. client.Proxy = _proxy;
  548. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  549. var response = client.Request(url);
  550. var body = response.Body();
  551. var root = body.Convert2JsonElement();
  552. int resultCode = root.Read<int>("resultCode", 0);
  553. int totalCount = root.PathRead<int>("data.totalCount", 0);
  554. bool success = root.Read("success", false);
  555. if (!success)
  556. {
  557. _ = new LoggerLibrary("violationu", "warning_error")
  558. .Info(url, body)
  559. .SaveAsync();
  560. throw new Exception(body);
  561. }
  562. if (totalCount == 0) return;
  563. _ = new LoggerLibrary("violationu", "warning").Info(url, body).SaveAsync();
  564. var list = root.ElementRead("data").ElementRead("result").EnumerateArray();
  565. foreach (var data in list)
  566. {
  567. var violation_commission = data.PathRead<decimal>("mediaInfo.订单虚假交易违规佣金", 0);
  568. new DBContext.Table("alimama_warning")
  569. .Fill(data)
  570. .Loader<int>("filterRuleId", 0)
  571. .Loader<long>("gmtCreate", 0)
  572. .Loader<long>("id", 0, "ali_id")
  573. .Loader<string>("idStr", string.Empty)
  574. .Loader<int>("mediaDimension", 0)
  575. .Loader<long>("mediaId", 0)
  576. .Loader<int>("mediaInfo.abn_cnt", 0, "abn_cnt")
  577. .Add("violation_commission", violation_commission)
  578. .Loader<int>("mediaInfo.warning_cnt", 0, "warning_cnt")
  579. .Loader<string>("mediaName", string.Empty)
  580. .Loader<long>("memberId", 0)
  581. .Loader<string>("noticeCategory", string.Empty)
  582. .Loader<int>("noticeTemplateId", 0)
  583. .Loader<string>("noticeTemplateName", string.Empty)
  584. .Loader<string>("orderFile", string.Empty)
  585. .Loader<string>("pid", string.Empty)
  586. .Loader<int>("punishRuleId", 0)
  587. .Loader<string>("riskType", string.Empty)
  588. .Loader<string>("roleName", string.Empty)
  589. .Loader<string>("ruleLink", string.Empty)
  590. .Loader<bool>("showOrderDetail", false)
  591. .Loader<int>("status", 0, "ali_status")
  592. .Loader<string>("warningText", string.Empty)
  593. .Add("accountName", _accountName)
  594. .Add("status", true)
  595. .Add("create_time", DateTime.Now)
  596. .Add("last_time", DateTime.Now)
  597. .Create(DBContext.InsertType.REPLACE);
  598. }
  599. }
  600. catch (Exception ex) { }
  601. }
  602. }
  603. }