JdPoolCore.cs 14 KB

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