JdPoolCore.cs 14 KB

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