JdPoolCore.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Dataoke;
  10. using Google.Protobuf.WellKnownTypes;
  11. using System.Xml.Linq;
  12. using Mysqlx.Crud;
  13. using ZstdSharp.Unsafe;
  14. using YunhuiKit;
  15. namespace molilian.core
  16. {
  17. public partial class JdPoolCore
  18. {
  19. public enum JdAction
  20. {
  21. all,
  22. api,
  23. parse,
  24. coupon,
  25. promotionQuery
  26. }
  27. private static string _end_point;
  28. private static Dictionary<string, decimal> _incomeAmt = new();
  29. static JdPoolCore()
  30. {
  31. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  32. }
  33. private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
  34. private static IEnumerable<JdPoolDTO> _cached;
  35. private static IEnumerable<JdPoolDTO> _all_cached;
  36. public static async Task<JdPoolDTO?> GetOneAsync()
  37. {
  38. var list = await ListAsync();
  39. if (!list.Any()) return null;
  40. return list.OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  41. }
  42. public static async Task<JdPoolDTO?> GetSpecialOneAsync(int accountid)
  43. {
  44. var list = await AllListAsync();
  45. if (!list.Any()) return null;
  46. return list.Where(e => e.id == accountid).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  47. }
  48. public static async Task<JdPoolDTO?> GetOneAsync(int id, JdAction action)
  49. {
  50. var list = await ListAsync();
  51. if (!list.Any()) return null;
  52. var eligibleItems = new List<JdPoolDTO>();
  53. foreach (var item in list.Where(e => e.id == id))
  54. {
  55. if (await IsNotExceedDailyIncomeLimitAsync(item, action))
  56. {
  57. eligibleItems.Add(item);
  58. }
  59. }
  60. return eligibleItems.OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  61. }
  62. public static async Task<JdPoolDTO?> GetOneAsync(JdAction action, string riskStrategy, int launchScene, bool? is_numeric, string parse_type = "")
  63. {
  64. var list = await ListAsync();
  65. if (!list.Any()) return null;
  66. if ("dp".Equals(parse_type))
  67. {
  68. list = list.Where(item => "dp".Equals(item.parse_type)).ToList();
  69. if (!list.Any()) return null;
  70. }
  71. else
  72. {
  73. if ("brw".Equals(riskStrategy) && launchScene != 0)
  74. {
  75. list = list.Where(item => string.IsNullOrEmpty(item.parse_type) && "brw".Equals(item.riskStrategy) && item.launchScene == launchScene).ToList();
  76. if (!list.Any()) return null;
  77. }
  78. else
  79. {
  80. list = list.Where(item => string.IsNullOrEmpty(item.parse_type) && !"brw".Equals(item.riskStrategy)).ToList();
  81. if (!list.Any()) return null;
  82. }
  83. }
  84. var eligibleItems = new List<JdPoolDTO>();
  85. foreach (var item in list)
  86. {
  87. if (await IsNotExceedDailyIncomeLimitAsync(item, action))
  88. {
  89. // 如果 is_numeric 为 true,则排除 work_mode = Ddx (4) 的记录
  90. if (is_numeric == true && item.work_mode == JdUnionWorkMode.Ddx)
  91. continue;
  92. eligibleItems.Add(item);
  93. }
  94. }
  95. return eligibleItems.OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  96. }
  97. private static async Task<bool> IsNotExceedDailyIncomeLimitAsync(JdPoolDTO item, JdAction action)
  98. {
  99. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  100. {
  101. if (item.end_point != _end_point) return false;
  102. }
  103. switch (action)
  104. {
  105. case JdAction.api:
  106. if (item.work_mode != JdUnionWorkMode.SiteApi) return false;
  107. break;
  108. case JdAction.parse:
  109. if (!item.enable_parse) return false;
  110. break;
  111. case JdAction.coupon:
  112. if (!item.enable_coupon) return false;
  113. break;
  114. }
  115. // 使用初始化参数创建工作时间表
  116. if (!new WorkSchedule(item.time_range).IsWorkHour()) return false;
  117. if (item.daily_calls_limit > 0)
  118. {
  119. int daily_num = RiskControlCore.GetCalls(TkChannelEnum.jd, item.id, DateTime.Now.ToString("yyyyMMdd"));
  120. if (daily_num >= item.daily_calls_limit) return false;
  121. }
  122. if (item.hourly_calls_limit > 0)
  123. {
  124. int hourly_num = RiskControlCore.GetCalls(TkChannelEnum.jd, item.id, DateTime.Now.ToString("yyyyMMddHH"));
  125. if (hourly_num >= item.hourly_calls_limit) return false;
  126. }
  127. return true;
  128. }
  129. public static async Task<IEnumerable<JdPoolDTO>> AllListAsync(bool force = false)
  130. {
  131. if (!force && _all_cached != default) return _all_cached;
  132. try
  133. {
  134. await _semaphore.WaitAsync();
  135. string cache_key = $"cache:all_jd_pool";
  136. var list = await RedisKit.GetAsync<IEnumerable<JdPoolDTO>>(cache_key);
  137. if (force || list == default)
  138. {
  139. list = new DBContext.Table("jd_pool").Select<JdPoolDTO>();
  140. if (list == null) return default;
  141. // 等待 Redis 写入完成
  142. await RedisKit.SetAsync(cache_key, list, 30 * 86400);
  143. }
  144. _all_cached = list;
  145. return list;
  146. }
  147. catch (Exception)
  148. {
  149. // 发生异常时返回上一次的缓存,如果没有则返回默认值
  150. return _all_cached ?? default;
  151. }
  152. finally
  153. {
  154. _semaphore.Release();
  155. }
  156. }
  157. public static async Task<IEnumerable<JdPoolDTO>> ListAsync(bool force = false)
  158. {
  159. if (!force && _cached != null) return _cached;
  160. try
  161. {
  162. await _semaphore.WaitAsync();
  163. // 如果缓存存在且未强制刷新,直接返回
  164. if (!force && _cached != null) return _cached;
  165. string cache_key = $"cache:jd_pool";
  166. IEnumerable<JdPoolDTO>? list = null;
  167. // 尝试从Redis获取数据
  168. try
  169. {
  170. list = await RedisKit.GetAsync<IEnumerable<JdPoolDTO>>(cache_key);
  171. }
  172. catch (Exception ex)
  173. {
  174. // 记录Redis错误
  175. _ = new LoggerLibrary("JdPool", "Redis").Info(ex.Message, ex.StackTrace).SaveAsync();
  176. }
  177. // 如果Redis获取失败或需要强制刷新
  178. if (force || list == null)
  179. {
  180. try
  181. {
  182. list = new DBContext.Table("jd_pool")
  183. .Where("status=@status", new { status = 1 })
  184. .Select<JdPoolDTO>();
  185. if (list != null && list.Any())
  186. {
  187. // 更新计数器
  188. foreach (var item in list)
  189. {
  190. try
  191. {
  192. await RiskControlCore.SetCallsAsync(TkChannelEnum.jd, item.id,
  193. DateTime.Now.ToString("yyyyMMddHH"), item.current_hourly_calls);
  194. await RiskControlCore.SetCallsAsync(TkChannelEnum.jd, item.id,
  195. DateTime.Now.ToString("yyyyMMdd"), item.current_daily_calls);
  196. }
  197. catch (Exception ex)
  198. {
  199. // 记录计数器更新错误
  200. _ = new LoggerLibrary("JdPool", "Counter").Info(ex.Message, ex.StackTrace).SaveAsync();
  201. }
  202. }
  203. // 尝试更新Redis缓存
  204. try
  205. {
  206. await RedisKit.SetAsync(cache_key, list, 30 * 86400);
  207. }
  208. catch (Exception ex)
  209. {
  210. // 记录Redis更新错误
  211. _ = new LoggerLibrary("JdPool", "Redis").Info(ex.Message, ex.StackTrace).SaveAsync();
  212. }
  213. }
  214. }
  215. catch (Exception ex)
  216. {
  217. // 记录数据库查询错误
  218. _ = new LoggerLibrary("JdPool", "Database").Info(ex.Message, ex.StackTrace).SaveAsync();
  219. // 如果数据库查询失败但缓存还在,继续使用缓存
  220. if (_cached != null) return _cached;
  221. throw; // 如果没有任何可用数据,则抛出异常
  222. }
  223. }
  224. _cached = list;
  225. return list ?? [];
  226. }
  227. finally
  228. {
  229. _semaphore.Release();
  230. }
  231. }
  232. public static void Refresh()
  233. {
  234. _all_cached = null;
  235. _cached = null;
  236. _ = ListAsync(true);
  237. _ = AllListAsync();
  238. }
  239. public static int Update(JdPoolDTO account)
  240. {
  241. return new DBContext.Table("jd_pool")
  242. .Add("current_hourly_calls", account.current_hourly_calls)
  243. .Add("current_daily_calls", account.current_daily_calls)
  244. .Add("today_clickNum", account.today_clickNum)
  245. .Add("today_cosFee", account.today_cosFee)
  246. .Add("today_cosPrice", account.today_cosPrice)
  247. .Add("today_finishCosFee", account.today_finishCosFee)
  248. .Add("today_finishCosPrice", account.today_finishCosPrice)
  249. .Add("today_finishOrderNum", account.today_finishOrderNum)
  250. .Add("today_orderNum", account.today_orderNum)
  251. .Where("id=@id", new { account.id })
  252. .Update();
  253. }
  254. public static int UpdateCookies(string cookies, string union_cookies, string user_agent, string h5st)
  255. {
  256. if (string.IsNullOrEmpty(cookies)) return 0;
  257. string pin = cookies.GetContentPart("pin=", ";");
  258. pin = pin.UrlDecode();
  259. string company = pin;
  260. int accountId = 0;
  261. if (string.IsNullOrEmpty(pin)) return 0;
  262. var exist = new DBContext.Table("jd_pool").Get<JdPoolDTO>("pin=@pin", new { pin });
  263. if (exist != null)
  264. {
  265. var status = exist.status;
  266. var work_mode = exist.work_mode;
  267. accountId = exist.id;
  268. if (work_mode == JdUnionWorkMode.Crawler) status = true;
  269. var update = new DBContext.Table("jd_pool")
  270. .Add("pin", pin)
  271. .Add("cookies", cookies)
  272. .Add("union_cookies", union_cookies)
  273. .Add("status", status)
  274. .Add("last_time", DateTime.Now)
  275. .Add("login_time", DateTime.Now)
  276. .Where("id=@id", new { exist.id });
  277. if (!string.IsNullOrEmpty(user_agent)) update.Add("user_agent", user_agent);
  278. if (!string.IsNullOrEmpty(h5st)) update.Add("h5st", h5st);
  279. update.Update();
  280. if (status) _ = ListAsync(true);
  281. }
  282. else
  283. {
  284. var update = new DBContext.Table("jd_pool")
  285. .Add("pin", pin)
  286. .Add("name", pin)
  287. .Add("company", pin)
  288. .Add("description", "由cookies上报创建此记录")
  289. .Add("cookies", cookies)
  290. .Add("union_cookies", union_cookies)
  291. .Add("create_time", DateTime.Now)
  292. .Add("last_time", DateTime.Now)
  293. .Add("login_time", DateTime.Now)
  294. .Add("status", 0);
  295. if (!string.IsNullOrEmpty(user_agent)) update.Add("user_agent", user_agent);
  296. if (!string.IsNullOrEmpty(h5st)) update.Add("h5st", h5st);
  297. accountId = update.Create();
  298. }
  299. NotifyCore.Notify(new NifyMessage
  300. {
  301. message = $"【京东{accountId}:{company}】cookie 上线",
  302. tags = ["green_circle"]
  303. });
  304. _ = EndPointCore.NotifyReload(true);
  305. //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
  306. return accountId;
  307. }
  308. public static async Task DisabledAsync(int accountId, string name, string content)
  309. {
  310. string cache_key = $"cache:jd_pool:{name}:disabled";
  311. long count = await RedisKit.IncrByAsync(cache_key);
  312. _ = RedisKit.ExpireAsync(cache_key, 10);
  313. if (count > 1) return;
  314. var update = new DBContext.Table("jd_pool").Add("status", 0);
  315. if (accountId > 0)
  316. {
  317. update.Where("id=@accountId", new { accountId }).Update();
  318. }
  319. else
  320. {
  321. update.Where("name=@name", new { name }).Update();
  322. }
  323. _ = ListAsync(true);
  324. NotifyCore.Notify(new NifyMessage
  325. {
  326. message = $"【京东联盟{accountId}:{name}】cookie 掉线\n\n{content}",
  327. priority = NifyMessagePriority.high,
  328. tags = ["red_circle"]
  329. });
  330. //NotifyCore.AnPushNotify("掉线", $"【京东{accountId}:{name}】cookie 掉线");
  331. _ = EndPointCore.NotifyReload(true);
  332. }
  333. internal static async Task AccountExhaustedAsync()
  334. {
  335. string cache_key = $"cache:tk_pool:account:exhausted";
  336. long count = await RedisKit.IncrByAsync(cache_key);
  337. if (count > 1) return;
  338. _ = RedisKit.ExpireAsync(cache_key, 3600);
  339. NotifyCore.Notify(new NifyMessage
  340. {
  341. message = $"【京东联盟】{EndPointCore.CurrentEndPoint}没有匹配账号",
  342. priority = NifyMessagePriority.high,
  343. tags = ["red_circle"]
  344. });
  345. NotifyCore.AnPushNotify("没账号", $"【京东联盟】没有匹配账号");
  346. }
  347. }
  348. }