JdPoolCore.cs 14 KB

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