orders.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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_orders_url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/report.getTbkOrderDetails.json";
  8. public async Task<int> GetHistoryOrdersAsync(int sleep, DateTime startTime, DateTime endTime)
  9. {
  10. string lockKey = $"lock:GetHistoryOrders:{_accountId}";
  11. if (!RedisHelper.Set(lockKey, "1", 1800))
  12. {
  13. string message = $"【GetHistoryOrders】【{_accountId}:{_accountName}】任务正在执行中,跳过本次调用";
  14. _ = new LoggerLibrary("task_lock", "skip")
  15. .Info(message)
  16. .SaveAsync();
  17. return 0;
  18. }
  19. var startTimestamp = DateTime.Now;
  20. try
  21. {
  22. //DateTime endTime = DateTime.Now;
  23. //DateTime startTime = endTime.AddDays(-90);
  24. //if (_accountId == 3) endTime = DateTime.Parse("2024-04-07");
  25. //#if DEBUG
  26. // startTime = DateTime.Parse("2024-05-01");
  27. // endTime = DateTime.Parse("2024-06-01");
  28. // int total = 0;
  29. // List<DateTime> orderTimes = new List<DateTime>();
  30. // for (DateTime date = startTime; date <= endTime; date = date.AddDays(1))
  31. // {
  32. // orderTimes.Add(date);
  33. // }
  34. //#else
  35. //#endif
  36. //当日往前查询
  37. (int total, DateTime max_modifiedTime, List<DateTime> orderTimes) = await GetTkOrdersAsync(startTime, endTime, DateTime.MinValue, false, sleep);
  38. if (_account.is_hide) return total;
  39. string sql = @"
  40. UPDATE tk_report tr
  41. JOIN (
  42. SELECT
  43. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  44. COUNT(*) AS order_ord_num, SUM(pubShareFeeForCommission) AS order_ord_tfee
  45. FROM tk_order_details
  46. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=3 AND accountId=@accountId
  47. GROUP BY DATE(tbPaidTime), accountId
  48. ) AS sub
  49. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  50. SET tr.order_ord_amt = sub.order_ord_amt, tr.order_ord_num_3 = sub.order_ord_num,
  51. tr.order_ord_tfee = sub.order_ord_tfee;";
  52. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  53. sql = @"
  54. UPDATE tk_report tr
  55. JOIN (
  56. SELECT
  57. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  58. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  59. FROM tk_order_details
  60. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=12 AND accountId=@accountId
  61. GROUP BY DATE(tbPaidTime), accountId
  62. ) AS sub
  63. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  64. SET tr.order_ord_amt_12 = sub.order_ord_amt, tr.order_ord_num_12 = sub.order_ord_num,
  65. tr.order_ord_tfee_12 = sub.order_ord_tfee;";
  66. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  67. sql = @"
  68. UPDATE tk_report tr
  69. JOIN (
  70. SELECT
  71. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  72. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  73. FROM tk_order_details
  74. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=13 AND accountId=@accountId
  75. GROUP BY DATE(tbPaidTime), accountId
  76. ) AS sub
  77. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  78. SET tr.order_ord_amt_13 = sub.order_ord_amt, tr.order_ord_num_13 = sub.order_ord_num,
  79. tr.order_ord_tfee_13 = sub.order_ord_tfee;";
  80. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  81. sql = @"
  82. UPDATE tk_report tr
  83. JOIN (
  84. SELECT
  85. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  86. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  87. FROM tk_order_details
  88. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=14 AND accountId=@accountId
  89. GROUP BY DATE(tbPaidTime), accountId
  90. ) AS sub
  91. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  92. SET tr.order_ord_amt_14 = sub.order_ord_amt, tr.order_ord_num_14 = sub.order_ord_num,
  93. tr.order_ord_tfee_14 = sub.order_ord_tfee;";
  94. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  95. sql = @"UPDATE tk_report
  96. SET order_ord_num = order_ord_num_3 + order_ord_num_12 + order_ord_num_13 + order_ord_num_14
  97. WHERE DATE(report_date) IN @orderTimes AND accountId=@accountId;";
  98. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  99. return total;
  100. }
  101. catch (Exception ex)
  102. {
  103. string errorMessage = $"【GetHistoryOrders】【{_accountId}:{_accountName}】执行异常: {ex.Message}";
  104. _ = new LoggerLibrary("task_error", "GetHistoryOrders")
  105. .Info(errorMessage, ex.StackTrace)
  106. .SaveAsync();
  107. throw;
  108. }
  109. finally
  110. {
  111. var executionTime = DateTime.Now - startTimestamp;
  112. string logMessage = $"【GetHistoryOrders】【{_accountId}:{_accountName}】执行完成,耗时: {executionTime.TotalSeconds:F2}秒";
  113. _ = new LoggerLibrary("task_performance", "GetHistoryOrders")
  114. .Info(logMessage)
  115. .SaveAsync();
  116. RedisHelper.Del(lockKey);
  117. }
  118. }
  119. public async Task<int> GetIncyOrdersAsync(int sleep, int retry_days = 0)
  120. {
  121. string lockKey = $"lock:GetIncyOrders:{_accountId}";
  122. if (!RedisHelper.Set(lockKey, "1", 1800))
  123. {
  124. string message = $"【GetIncyOrders】【{_accountId}:{_accountName}】任务正在执行中,跳过本次调用";
  125. _ = new LoggerLibrary("task_lock", "skip")
  126. .Info(message)
  127. .SaveAsync();
  128. return 0;
  129. }
  130. var startTimestamp = DateTime.Now;
  131. try
  132. {
  133. string cacheKey = $":cache:GetIncyOrders:{_accountName}";
  134. string cacheKey2 = $":cache:GetIncyOrders:{_accountId}";
  135. DateTime endTime = DateTime.Now;
  136. DateTime startTime = endTime.AddDays(-1);
  137. DateTime last_modifiedTime = RedisHelper.Get<DateTime>(cacheKey);
  138. ////找出数据库最新的一天
  139. //var last = new DBContext.Table("tk_order_details")
  140. // .Order("modifiedTime DESC")
  141. // .Get<TkOrderDetailDTO>("accountId=@accountId", new { accountId = _accountId });
  142. if (last_modifiedTime != DateTime.MinValue)
  143. {
  144. startTime = last_modifiedTime;
  145. }
  146. else
  147. {
  148. last_modifiedTime = startTime;
  149. }
  150. if (retry_days > 0)
  151. {
  152. last_modifiedTime = DateTime.Now.AddDays(-retry_days);
  153. }
  154. //当日往前查询
  155. (int total, DateTime max_modifiedTime, List<DateTime> orderTimes) = await GetTkOrdersAsync(startTime, endTime, last_modifiedTime, true, sleep);
  156. if (max_modifiedTime != DateTime.MinValue)
  157. {
  158. RedisHelper.Set(cacheKey, max_modifiedTime);
  159. RedisHelper.Set(cacheKey2, max_modifiedTime);
  160. }
  161. if (_account.is_hide) return total;
  162. if (total == 0) return 0;
  163. //todo 根据orderTimes、settlementTimes 更新被影响的相关行
  164. string sql = @"
  165. UPDATE tk_report tr
  166. JOIN (
  167. SELECT
  168. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  169. COUNT(*) AS order_ord_num, SUM(pubShareFeeForCommission) AS order_ord_tfee
  170. FROM tk_order_details
  171. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=3 AND accountId=@accountId
  172. GROUP BY DATE(tbPaidTime), accountId
  173. ) AS sub
  174. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  175. SET tr.order_ord_amt = sub.order_ord_amt, tr.order_ord_num_3 = sub.order_ord_num,
  176. tr.order_ord_tfee = sub.order_ord_tfee;";
  177. int x = DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  178. sql = @"
  179. UPDATE tk_report tr
  180. JOIN (
  181. SELECT
  182. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  183. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  184. FROM tk_order_details
  185. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=12 AND accountId=@accountId
  186. GROUP BY DATE(tbPaidTime), accountId
  187. ) AS sub
  188. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  189. SET tr.order_ord_amt_12 = sub.order_ord_amt, tr.order_ord_num_12 = sub.order_ord_num,
  190. tr.order_ord_tfee_12 = sub.order_ord_tfee;";
  191. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  192. sql = @"
  193. UPDATE tk_report tr
  194. JOIN (
  195. SELECT
  196. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  197. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  198. FROM tk_order_details
  199. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=13 AND accountId=@accountId
  200. GROUP BY DATE(tbPaidTime), accountId
  201. ) AS sub
  202. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  203. SET tr.order_ord_amt_13 = sub.order_ord_amt, tr.order_ord_num_13 = sub.order_ord_num,
  204. tr.order_ord_tfee_13 = sub.order_ord_tfee;";
  205. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  206. sql = @"
  207. UPDATE tk_report tr
  208. JOIN (
  209. SELECT
  210. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  211. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  212. FROM tk_order_details
  213. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=14 AND accountId=@accountId
  214. GROUP BY DATE(tbPaidTime), accountId
  215. ) AS sub
  216. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  217. SET tr.order_ord_amt_14 = sub.order_ord_amt, tr.order_ord_num_14 = sub.order_ord_num,
  218. tr.order_ord_tfee_14 = sub.order_ord_tfee;";
  219. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  220. sql = @"UPDATE tk_report
  221. SET order_ord_num = order_ord_num_3 + order_ord_num_12 + order_ord_num_13 + order_ord_num_14
  222. WHERE DATE(report_date) IN @orderTimes AND accountId=@accountId;";
  223. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  224. return total;
  225. }
  226. catch (Exception ex)
  227. {
  228. string errorMessage = $"【GetIncyOrders】【{_accountId}:{_accountName}】执行异常: {ex.Message}";
  229. _ = new LoggerLibrary("task_error", "GetIncyOrders")
  230. .Info(errorMessage, ex.StackTrace)
  231. .SaveAsync();
  232. throw;
  233. }
  234. finally
  235. {
  236. var executionTime = DateTime.Now - startTimestamp;
  237. string logMessage = $"【GetIncyOrders】【{_accountId}:{_accountName}】执行完成,耗时: {executionTime.TotalSeconds:F2}秒";
  238. _ = new LoggerLibrary("task_performance", "GetIncyOrders")
  239. .Info(logMessage)
  240. .SaveAsync();
  241. RedisHelper.Del(lockKey);
  242. }
  243. }
  244. public async Task<int> GetOrdersByAdZoneAsync(long adzoneId, DateTime startTime, DateTime endTime, int sleep)
  245. {
  246. string cacheKey = $":cache:GetOrdersByAdZone:{_accountName}";
  247. string cacheKey2 = $":cache:GetOrdersByAdZone:{_accountId}";
  248. DateTime last_modifiedTime = RedisHelper.Get<DateTime>(cacheKey);
  249. if (last_modifiedTime != DateTime.MinValue)
  250. {
  251. startTime = last_modifiedTime;
  252. }
  253. else
  254. {
  255. last_modifiedTime = startTime;
  256. }
  257. //当日往前查询
  258. (int total, DateTime max_modifiedTime, List<DateTime> orderTimes) = await GetTkOrdersByAdZoneAsync(adzoneId, startTime, endTime, last_modifiedTime, true, sleep);
  259. if (max_modifiedTime != DateTime.MinValue)
  260. {
  261. RedisHelper.Set(cacheKey, max_modifiedTime);
  262. RedisHelper.Set(cacheKey2, max_modifiedTime);
  263. }
  264. return total;
  265. }
  266. public async Task<(int, DateTime, List<DateTime>)> GetTkOrdersByAdZoneAsync(long adzoneId, DateTime startTime, DateTime endTime, DateTime last_modifiedTime, bool desc = true, int sleep = 100)
  267. {
  268. List<DateTime> orderTimes = [];
  269. if (startTime > endTime)
  270. {
  271. (startTime, endTime) = (endTime, startTime);
  272. }
  273. DateTime max_modifiedTime = last_modifiedTime;
  274. int pageNo = 1;
  275. string positionIndex = string.Empty;
  276. int pageSize = 100;
  277. bool hasNext = true;
  278. int total = 0;
  279. while (hasNext)
  280. {
  281. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  282. string jumpType = pageNo == 1 ? "0" : "1";
  283. #if DEBUG
  284. jumpType = pageNo == 1 ? "0" : "1";
  285. #endif
  286. string queryType = desc ? "4" : "2";
  287. 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}&positionIndex={positionIndex.UrlEncode()}";
  288. string body = "";
  289. JsonElement data;
  290. try
  291. {
  292. var client = new WebClientUtility().SetContentType("application/x-www-form-urlencoded; charset=UTF-8")
  293. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  294. .AddHeaders("Cookie", _cookies);
  295. #if DEBUG
  296. #else
  297. client.Proxy = _proxy;
  298. #endif
  299. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  300. var response = client.Request(url);
  301. if (response.ResponseException != null)
  302. {
  303. _ = new LoggerLibrary("api_error", "fail")
  304. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  305. .SaveAsync();
  306. throw response.ResponseException;
  307. }
  308. body = response.Body();
  309. #if DEBUG
  310. _ = new LoggerLibrary("debug", "GetTkOrdersByAdZone")
  311. .Info(body)
  312. .SaveAsync();
  313. #endif
  314. if (body.Contains("{\"action\":\"captcha\""))
  315. {
  316. RiskControlCore.SetRiskCookie($"tb:{_accountId}", "滑块");
  317. string message = $"【GetTkOrdersByAdZone】【{_accountId}:{_accountName}】出现滑动验证码";
  318. NotifyCore.Notify(new NifyMessage
  319. {
  320. message = message,
  321. priority = NifyMessagePriority.high,
  322. tags = ["red_circle"]
  323. });
  324. if (!_account.is_hide)
  325. {
  326. _ = NotifyCore.QYWeixinPushNotifyAsync(message, message);
  327. }
  328. throw new Exception(message);
  329. }
  330. if (body.Contains("\"resultCode\":500"))
  331. {
  332. string message = $"【GetTkOrdersByAdZone】【{_accountId}:{_accountName}】出现错误\n{body}";
  333. NotifyCore.Notify(new NifyMessage
  334. {
  335. message = message,
  336. priority = NifyMessagePriority.high,
  337. tags = ["red_circle"]
  338. });
  339. throw new Exception(message);
  340. }
  341. var root = body.Convert2JsonElement();
  342. var success = root.Read<bool>("success");
  343. data = root.ElementRead("data");
  344. if (!success && body.Contains("nologin"))
  345. {
  346. (success, string message) = RenewCookie();
  347. if (!success)
  348. {
  349. TkPoolCore.Disabled(_accountId, _accountName, $"{body}", _account.is_hide);
  350. }
  351. return (0, DateTime.MinValue, new List<DateTime>());
  352. }
  353. if (!success)
  354. {
  355. throw new APIException(body);
  356. }
  357. }
  358. catch (Exception ex)
  359. {
  360. throw new APIException(body);
  361. }
  362. using var conn = DBContext.GetOpenConnection();
  363. conn.Open();
  364. try
  365. {
  366. foreach (var order in data.ElementRead("result").EnumerateArray())
  367. {
  368. TkOrderDetailAdZoneDTO item = order.GetRawText().Convert2Object<TkOrderDetailAdZoneDTO>();
  369. // 处理每个订单数据
  370. if (item.adzoneId != adzoneId) continue;
  371. item.accountId = _accountId;
  372. item.accountName = _company;
  373. if (item.modifiedTime > max_modifiedTime) max_modifiedTime = item.modifiedTime;
  374. if (last_modifiedTime != DateTime.MinValue && last_modifiedTime >= item.modifiedTime)
  375. {
  376. return (total, max_modifiedTime, orderTimes);
  377. }
  378. if (item.tkStatus == 3 && item.tbPaidTime != DateTime.MinValue) orderTimes.Add(item.tbPaidTime.Date);
  379. int? id = conn.Replace(item);
  380. total++;
  381. }
  382. }
  383. catch (Exception ex)
  384. {
  385. throw;
  386. }
  387. finally
  388. {
  389. conn.Dispose();
  390. }
  391. pageNo++;
  392. hasNext = data.Read<bool>("hasNext");
  393. positionIndex = data.Read<string>("positionIndex");
  394. if (sleep > 0) await Task.Delay(sleep);
  395. }
  396. return (total, max_modifiedTime, orderTimes);
  397. }
  398. public async Task<(int, DateTime, List<DateTime>)> GetTkOrdersAsync(DateTime startTime, DateTime endTime, DateTime last_modifiedTime, bool desc = true, int sleep = 100)
  399. {
  400. List<DateTime> orderTimes = [];
  401. // 确保 startTime 小于 endTime
  402. if (startTime > endTime)
  403. {
  404. (startTime, endTime) = (endTime, startTime);
  405. }
  406. DateTime max_modifiedTime = last_modifiedTime;
  407. int pageNo = 1;
  408. string positionIndex = string.Empty;
  409. int pageSize = 100;
  410. bool hasNext = true;
  411. int total = 0;
  412. while (hasNext)
  413. {
  414. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  415. //string jumpType = desc ? pageNo == 1 ? "0" : "1" : "-1";
  416. string jumpType = pageNo == 1 ? "0" : "1";
  417. #if DEBUG
  418. jumpType = pageNo == 1 ? "0" : "1";
  419. #endif
  420. //string queryType = "1";
  421. //1 创建时间;3结算时间 2付款时间 4更新时间
  422. //string queryType = "2";
  423. string queryType = desc ? "4" : "2";
  424. 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}&positionIndex={positionIndex.UrlEncode()}";
  425. string body = "";
  426. JsonElement data;
  427. try
  428. {
  429. bool success = false;
  430. #if DEBUG
  431. _cookies = "t_alimama=265947ef5452cb1dbc3a85423cb0b4ac; xlly_s=1; wk_cookie2=153667c5e7c346da3c64cc8be5db65b5; wk_unb=UUpjNmHYrAUDFe1XcQ%3D%3D; arms_uid=50fb4ec1-5342-4774-9b30-822cfb71ac29; cookie2_alimama=1939cd118da29bee099e20497d91d195; lid=tb464407448627; cookie3_bak=17c2085f3ca7b7e1d9932184a321265b; env_bak=FM%2BgwZ4N3DW5Nit1vtAN%2BnGo%2FlvOs%2BhcfJ4%2F0uUixKyv; cookie3_bak_exp=1753443569194; havana_lgc_exp=1786690298961; dnk=tb464407448627; tracknick=tb464407448627; lgc=; cookie2=17c2085f3ca7b7e1d9932184a321265b; cancelledSubSites=empty; t=b0f1900bb872fb73431103fd9fd685da; sn=; _tb_token_=e3be75e77317e; uc1=cookie14=UoYbzibV4BRygw%3D%3D&pas=0&cookie16=WqG3DMC9UpAPBHGz5QBErFxlCA%3D%3D&existShop=false&cookie15=WqG3DMC9VAQiUQ%3D%3D&cookie21=V32FPkk%2FhSt4; sgcookie=E100maFa2uGfFmlaN000MHH4oUxgMX%2Fe%2FWHW96Um0swftox2LuxNNHviK0VTqoHo%2F1NuX%2BYAGoSBwA8mzA3bTrtPkhdwXGBaIPytPt3iO9Ay%2Bh8%3D; csg=8a980a9f; login=; rurl=aHR0cHM6Ly9wdWIuYWxpbWFtYS5jb20vaW5kZXguaHRtP2ZvcndhcmQ9aHR0cHMlM0ElMkYlMkZwdWIuYWxpbWFtYS5jb20lMkZwb3J0YWwlMkZ2MiUyRmVmZmVjdCUyRm9yZGVyJTJGb3ZlcnZpZXdPcmRlciUyRnBhZ2UlMkZpbmRleC5odG0%3D; v_alimama=0; JSESSIONID=5529D2F4B4C1D00E84AEBD50280FB383; cna=BfkxIaeXgCYCAXs5yWAuAa9d; x5sec=7b22733b32223a2263613337363636363063313961373062222c2277616762726964676561643b32223a2232643831656432356532633264343332333737306462646238363761376562664350476374385547454a612f707654392f2f2f2f2f774561454449794d6a41794e544d314d5445314e446b374d545977332f6e4f3651553d227d; tfstk=gEftPuYb-kqMTmoZWi2noACNV9Uh0JbN8G7SinxihMIduaU2hRS0MsIcvPJbbCfAvZSeoj2NmMHXkHWGnrzwDEtCYEf07KjdDaIJoxXg7msFkg6MoCODMmdNtOcDjlRAlgAYquVuZN7wMIZuq5jhO1dWrcMsmnOCRIYvWC9CtN7wg2unc8XC7qEntGc6lnapOELocIT6lBapkUKXGh9jAvTWAnOfcn_BOF8r5FTsGXapuHT6GIsfRyLDAnOXGi_ITUU9YVTtMOE1kwEYa-WPI6L9B3_LUbczeTol8NGsajfdp4xR5xljG6L9B6M24HxwIt_yU1bUMXRGy9OBWiN7fpW1JspPg-cMQ9_hwe17oY-h9hjf6paowKACZLO1KJrMnCLRc1StkjsXshOG1IumgaO9ACf9pqGAVwbl_p5Y2DtVIURBWiN7vgWFZ_L570xJoAaLJxk2CezoM3hD13hyWeKu5dMq3KYeJ34LJxk2Ce8pqPGj3xJD8";
  432. #endif
  433. var client = new WebClientUtility().SetContentType("application/x-www-form-urlencoded; charset=UTF-8")
  434. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  435. .AddHeaders("Cookie", _cookies);
  436. #if DEBUG
  437. #else
  438. client.Proxy = _proxy;
  439. #endif
  440. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  441. client.Referer = "https://pub.alimama.com/portal/v2/effect/order/overviewOrder/page/index.htm";
  442. var response = client.Request(url);
  443. if (response.ResponseException != null)
  444. {
  445. _ = new LoggerLibrary("api_error", "fail")
  446. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  447. .SaveAsync();
  448. throw response.ResponseException;
  449. }
  450. body = response.Body();
  451. _ = new LoggerLibrary("debug", "GetTkOrders")
  452. .Info(body)
  453. .SaveAsync();
  454. if (body.Contains("{\"action\":\"login\"") || body.Contains("\"action\": \"login\""))
  455. {
  456. (success, string message) = RenewCookie();
  457. if (!success)
  458. {
  459. TkPoolCore.Disabled(_accountId, _accountName, $"{body}", _account.is_hide);
  460. }
  461. return (0, DateTime.MinValue, new List<DateTime>());
  462. }
  463. if (body.Contains("{\"action\":\"captcha\""))
  464. {
  465. RiskControlCore.SetRiskCookie($"tb:{_accountId}", "滑块");
  466. string message = $"【GetTkOrders】【{_accountId}:{_accountName}】出现滑动验证码";
  467. NotifyCore.Notify(new NifyMessage
  468. {
  469. message = message,
  470. priority = NifyMessagePriority.high,
  471. tags = ["red_circle"]
  472. });
  473. if (!_account.is_hide)
  474. {
  475. _ = NotifyCore.QYWeixinPushNotifyAsync(message, message);
  476. }
  477. return (0, max_modifiedTime, new List<DateTime>());
  478. //throw new Exception(message);
  479. }
  480. if (body.Contains("\"resultCode\":500"))
  481. {
  482. //{"success":false,"resultCode":500,"bizErrorDesc":"系统繁忙,请稍后重试!","bizErrorCode":3001}
  483. string message = $"【GetTkOrders】【{_accountId}:{_accountName}】出现错误\n{body}";
  484. NotifyCore.Notify(new NifyMessage
  485. {
  486. message = message,
  487. priority = NifyMessagePriority.high,
  488. tags = ["red_circle"]
  489. });
  490. throw new Exception(message);
  491. }
  492. /*
  493. <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--> */
  494. var root = body.Convert2JsonElement();
  495. success = root.Read<bool>("success");
  496. data = root.ElementRead("data");
  497. //{"code":601,"info":{"ok":false,"message":"nologin"}}
  498. if (!success && body.Contains("nologin"))
  499. {
  500. (success, string message) = RenewCookie();
  501. if (!success)
  502. {
  503. TkPoolCore.Disabled(_accountId, _accountName, $"{body}", _account.is_hide);
  504. }
  505. return (0, DateTime.MinValue, new List<DateTime>());
  506. }
  507. if (!success)
  508. {
  509. throw new APIException(body);
  510. }
  511. }
  512. catch (Exception ex)
  513. {
  514. _ = new LoggerLibrary("debug", "GetTkOrders")
  515. .Info(body)
  516. .Info(ex.Message, ex.StackTrace)
  517. .SaveAsync();
  518. string message = $"【GetRawItemId】{_accountId}:{_accountName}\t{_proxy?.Address}\n" +
  519. $"{body}\n{ex.Message}\n{ex.StackTrace}";
  520. if (sleep > 0) await Task.Delay(sleep);
  521. throw new APIException(body);
  522. }
  523. try
  524. {
  525. foreach (var order in data.ElementRead("result").EnumerateArray())
  526. {
  527. TkOrderDetailDTO item = order.GetRawText().Convert2Object<TkOrderDetailDTO>();
  528. // 处理每个订单数据
  529. item.accountId = _accountId;
  530. item.accountName = _company;
  531. if (item.modifiedTime > max_modifiedTime) max_modifiedTime = item.modifiedTime;
  532. if (last_modifiedTime != DateTime.MinValue && last_modifiedTime >= item.modifiedTime)
  533. {
  534. return (total, max_modifiedTime, orderTimes);
  535. }
  536. //进行订单和转链匹配
  537. (int state, item.itemId) = GetRawItemId(item.mktId);
  538. if (item.tkStatus == 3 && item.tbPaidTime != DateTime.MinValue) orderTimes.Add(item.tbPaidTime.Date);
  539. using var conn = DBContext.GetOpenConnection();
  540. int? id = conn.Replace(item);
  541. if (id != null)
  542. {
  543. item.id = (int)id;
  544. TkOrderTrackingCore.MatchOrder(_account, item);
  545. }
  546. total++;
  547. }
  548. }
  549. catch (Exception ex)
  550. {
  551. if (sleep > 0) await Task.Delay(sleep);
  552. throw;
  553. }
  554. pageNo++;
  555. hasNext = data.Read<bool>("hasNext");
  556. positionIndex = data.Read<string>("positionIndex");
  557. if (sleep > 0) await Task.Delay(sleep);
  558. }
  559. return (total, max_modifiedTime, orderTimes);
  560. }
  561. public (int, string) GetRawItemId(string mktId)
  562. {
  563. ////
  564. ///
  565. string body = string.Empty, location = string.Empty;
  566. var proxy = ProxyNodesCore.RandomOne();
  567. try
  568. {
  569. string cacheKey = $":cache:mkt2itemId:{mktId}";
  570. string itemId = RedisHelper.Get(cacheKey);
  571. if (itemId != null) return (1, itemId);
  572. string mktItemLink = $"https://uland.taobao.com/item/edetail?id={mktId}";
  573. ////uland.taobao.com/item/edetail?id=98t7UOZmNWX4dh2eJSQt6-b7Y8yZVuBQ5ywoz7TDA
  574. ////uland.taobao.com/item/edetail?id=98t7UOZmNWX4dh2eJSQt6-b7Y8yZVuBQ5ywoz7TD
  575. ///
  576. var client = new WebClientUtility();
  577. client.Proxy = proxy;
  578. client.AddHeaders("accept-language", "zh-CN,zh;q=0.9");
  579. client.AllowAutoRedirect = false;
  580. var response = client.Request(mktItemLink);
  581. if (!response.Successed)
  582. {
  583. throw response.ResponseException;
  584. }
  585. location = response.ResponseMessage?.Headers?.Location?.ToString();
  586. string message;
  587. if (!string.IsNullOrEmpty(location) && location.Contains("err.taobao.com/error1.html"))
  588. {
  589. message = $"【GetRawItemId】{_accountId}:{_accountName}\t{proxy?.Address}\n" +
  590. $"{mktId}\n{location}";
  591. //NotifyCore.Notify(new NifyMessage
  592. //{
  593. // message = message,
  594. // priority = NifyMessagePriority.high,
  595. // tags = ["red_circle"]
  596. //});
  597. return (0, null);
  598. }
  599. if (!string.IsNullOrEmpty(location) && location.Contains("item.htm?id="))
  600. {
  601. itemId = location.GetContentPart("?id=", "");
  602. }
  603. if (!string.IsNullOrEmpty(itemId))
  604. {
  605. RedisHelper.Set(cacheKey, itemId, 86400 * 7);
  606. }
  607. else
  608. {
  609. }
  610. return (1, itemId);
  611. }
  612. catch (Exception ex)
  613. {
  614. string message = $"【GetRawItemId】{_accountId}:{_accountName}\t{proxy?.Address}\n" +
  615. $"{mktId}\n{location}\n{body}\n{ex.Message}";
  616. NotifyCore.Notify(new NifyMessage
  617. {
  618. message = message,
  619. priority = NifyMessagePriority.high,
  620. tags = ["red_circle"]
  621. });
  622. }
  623. return (0, null);
  624. }
  625. public (int, string) GetRawItemId22(string mktId)
  626. {
  627. string body = string.Empty, location = string.Empty;
  628. try
  629. {
  630. string lock_key = $"lock:GetRawItemId:disabled";
  631. int count = RedisHelper.Get<int>(lock_key);
  632. if (count > 0) return (0, null);
  633. string cacheKey = $":cache:mkt2itemId:{mktId}";
  634. string itemId = RedisHelper.Get(cacheKey);
  635. if (itemId != null) return (1, itemId);
  636. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  637. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/union.moose.content.entry";
  638. //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}";
  639. var client = new WebClientUtility()
  640. .SetContentType("application/x-www-form-urlencoded")
  641. .AddHeaders("Cookie", _cookies)
  642. .AddHeaders("Origin", "https://pub.alimama.com")
  643. .Post("_tb_token_", _tb_token)
  644. .Post("t", ts.ToString())
  645. .Post("bizType", "detailPage")
  646. .Post("bizParam", $"{{\"itemId\":\"{mktId}\"}}");
  647. //_tb_token_=3b88755bbe3f7&t=1718896771391&bizType=detailPage&bizParam=%7B%22itemId%22%3A%22JbtMUqkGMQvagTNb2Fvta-9ag242pu7MOX5e0bsrY%22%7D
  648. //_tb_token_=361e383eb61e5&t=1718895935097&bizType=detailPage&bizParam=%7B%22itemId%22%3A%22JbtMUqkGMQvagTNb2Fvta-9ag242pu7MOX5e0bsrY%22%7D
  649. #if DEBUG
  650. #else
  651. client.Proxy = _proxy;
  652. #endif
  653. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  654. var response = client.Request(url);
  655. if (!response.Successed)
  656. {
  657. throw response.ResponseException;
  658. }
  659. location = response.ResponseMessage?.Headers?.Location?.ToString();
  660. string message;
  661. if (!string.IsNullOrEmpty(location) && location.Contains("err.taobao.com/error1.html"))
  662. {
  663. RedisHelper.Set(lock_key, 1, 60);
  664. message = $"【GetRawItemId】{_accountId}:{_accountName}\t{_proxy?.Address}\n" +
  665. $"{mktId}\n{location}";
  666. //NotifyCore.Notify(new NifyMessage
  667. //{
  668. // message = message,
  669. // priority = NifyMessagePriority.high,
  670. // tags = ["red_circle"]
  671. //});
  672. return (0, null);
  673. }
  674. body = response.Body();
  675. var root = body.Convert2JsonElement();
  676. int code = root.PathRead<int>("code");
  677. message = root.PathRead<string>("info.message");
  678. //if ("nologin".Equals(message))
  679. //{
  680. // (bool success, message) = RenewCookie();
  681. // if (!success)
  682. // {
  683. // TkPoolCore.Disabled(_accountId, _accountName, body);
  684. // throw new Exception("nologin");
  685. // }
  686. // return (-1, null);
  687. //}
  688. itemId = root.PathRead<string>("model.item.itemId");
  689. if (!string.IsNullOrEmpty(itemId))
  690. {
  691. RedisHelper.Set(cacheKey, itemId, 86400 * 7);
  692. }
  693. else
  694. {
  695. //RedisHelper.Set(lock_key, 1, 3600);
  696. }
  697. return (1, itemId);
  698. }
  699. catch (Exception ex)
  700. {
  701. string message = $"【GetRawItemId】{_accountId}:{_accountName}\t{_proxy?.Address}\n" +
  702. $"{mktId}\n{location}\n{body}\n{ex.Message}";
  703. NotifyCore.Notify(new NifyMessage
  704. {
  705. message = message,
  706. priority = NifyMessagePriority.high,
  707. tags = ["red_circle"]
  708. });
  709. }
  710. return (0, null);
  711. }
  712. public void GetTkOrders222()
  713. {
  714. try
  715. {
  716. string url = "https://media.alimama.com/violationu2/warning_instance_page_v2.json?r=mx_208&toPage=1&pageSize=40&params=";
  717. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  718. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  719. .AddHeaders("Cookie", _cookies);
  720. client.Proxy = _proxy;
  721. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  722. var response = client.Request(url);
  723. var body = response.Body();
  724. var root = body.Convert2JsonElement();
  725. int resultCode = root.Read<int>("resultCode", 0);
  726. int totalCount = root.PathRead<int>("data.totalCount", 0);
  727. bool success = root.Read("success", false);
  728. if (!success)
  729. {
  730. _ = new LoggerLibrary("violationu", "warning_error")
  731. .Info(url, body)
  732. .SaveAsync();
  733. throw new Exception(body);
  734. }
  735. if (totalCount == 0) return;
  736. _ = new LoggerLibrary("violationu", "warning").Info(url, body).SaveAsync();
  737. var list = root.ElementRead("data").ElementRead("result").EnumerateArray();
  738. foreach (var data in list)
  739. {
  740. var violation_commission = data.PathRead<decimal>("mediaInfo.订单虚假交易违规佣金", 0);
  741. new DBContext.Table("alimama_warning")
  742. .Fill(data)
  743. .Loader<int>("filterRuleId", 0)
  744. .Loader<long>("gmtCreate", 0)
  745. .Loader<long>("id", 0, "ali_id")
  746. .Loader<string>("idStr", string.Empty)
  747. .Loader<int>("mediaDimension", 0)
  748. .Loader<long>("mediaId", 0)
  749. .Loader<int>("mediaInfo.abn_cnt", 0, "abn_cnt")
  750. .Add("violation_commission", violation_commission)
  751. .Loader<int>("mediaInfo.warning_cnt", 0, "warning_cnt")
  752. .Loader<string>("mediaName", string.Empty)
  753. .Loader<long>("memberId", 0)
  754. .Loader<string>("noticeCategory", string.Empty)
  755. .Loader<int>("noticeTemplateId", 0)
  756. .Loader<string>("noticeTemplateName", string.Empty)
  757. .Loader<string>("orderFile", string.Empty)
  758. .Loader<string>("pid", string.Empty)
  759. .Loader<int>("punishRuleId", 0)
  760. .Loader<string>("riskType", string.Empty)
  761. .Loader<string>("roleName", string.Empty)
  762. .Loader<string>("ruleLink", string.Empty)
  763. .Loader<bool>("showOrderDetail", false)
  764. .Loader<int>("status", 0, "ali_status")
  765. .Loader<string>("warningText", string.Empty)
  766. .Add("accountName", _accountName)
  767. .Add("status", true)
  768. .Add("create_time", DateTime.Now)
  769. .Add("last_time", DateTime.Now)
  770. .Create(DBContext.InsertType.REPLACE);
  771. }
  772. }
  773. catch (Exception ex) { }
  774. }
  775. }
  776. }