JdPoolCore.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. string cache_key = $"cache:jd_pool";
  143. var list = await RedisKit.GetAsync<IEnumerable<JdPoolDTO>>(cache_key);
  144. if (force || list == null)
  145. {
  146. list = new DBContext.Table("jd_pool")
  147. .Where("status=@status", new { status = 1 })
  148. .Select<JdPoolDTO>();
  149. if (list != null)
  150. {
  151. foreach (var item in list)
  152. {
  153. _ = RiskControlCore.SetCallsAsync(TkChannelEnum.jd, item.id,
  154. DateTime.Now.ToString("yyyyMMddHH"), item.current_hourly_calls);
  155. _ = RiskControlCore.SetCallsAsync(TkChannelEnum.jd, item.id,
  156. DateTime.Now.ToString("yyyyMMdd"), item.current_daily_calls);
  157. }
  158. await RedisKit.SetAsync(cache_key, list, 30 * 86400);
  159. }
  160. }
  161. _cached = list;
  162. return list;
  163. }
  164. finally
  165. {
  166. _semaphore.Release(); // 确保释放锁
  167. }
  168. }
  169. public static void Refresh()
  170. {
  171. _all_cached = [];
  172. _cached = [];
  173. _ = ListAsync(true);
  174. _ = AllListAsync();
  175. }
  176. public static int Update(JdPoolDTO account)
  177. {
  178. return new DBContext.Table("jd_pool")
  179. .Add("current_hourly_calls", account.current_hourly_calls)
  180. .Add("current_daily_calls", account.current_daily_calls)
  181. .Add("today_clickNum", account.today_clickNum)
  182. .Add("today_cosFee", account.today_cosFee)
  183. .Add("today_cosPrice", account.today_cosPrice)
  184. .Add("today_finishCosFee", account.today_finishCosFee)
  185. .Add("today_finishCosPrice", account.today_finishCosPrice)
  186. .Add("today_finishOrderNum", account.today_finishOrderNum)
  187. .Add("today_orderNum", account.today_orderNum)
  188. .Where("id=@id", new { account.id })
  189. .Update();
  190. }
  191. public static int UpdateCookies(string cookies, string user_agent, string h5st)
  192. {
  193. if (string.IsNullOrEmpty(cookies)) return 0;
  194. string pin = cookies.GetContentPart("pin=", ";");
  195. pin = pin.UrlDecode();
  196. string company = pin;
  197. int accountId = 0;
  198. if (string.IsNullOrEmpty(pin)) return 0;
  199. var exist = new DBContext.Table("jd_pool").Get<JdPoolDTO>("pin=@pin", new { pin });
  200. if (exist != null)
  201. {
  202. var status = exist.status;
  203. var work_mode = exist.work_mode;
  204. accountId = exist.id;
  205. if (work_mode == JdUnionWorkMode.Crawler) status = true;
  206. var update = new DBContext.Table("jd_pool")
  207. .Add("pin", pin)
  208. .Add("cookies", cookies)
  209. .Add("status", status)
  210. .Add("last_time", DateTime.Now)
  211. .Add("login_time", DateTime.Now)
  212. .Where("id=@id", new { exist.id });
  213. if (!string.IsNullOrEmpty(user_agent)) update.Add("user_agent", user_agent);
  214. if (!string.IsNullOrEmpty(h5st)) update.Add("h5st", h5st);
  215. update.Update();
  216. if (status) _ = ListAsync(true);
  217. }
  218. else
  219. {
  220. var update = new DBContext.Table("jd_pool")
  221. .Add("pin", pin)
  222. .Add("name", pin)
  223. .Add("company", pin)
  224. .Add("description", "由cookies上报创建此记录")
  225. .Add("cookies", cookies)
  226. .Add("create_time", DateTime.Now)
  227. .Add("last_time", DateTime.Now)
  228. .Add("login_time", DateTime.Now)
  229. .Add("status", 0);
  230. if (!string.IsNullOrEmpty(user_agent)) update.Add("user_agent", user_agent);
  231. if (!string.IsNullOrEmpty(h5st)) update.Add("h5st", h5st);
  232. accountId = update.Create();
  233. }
  234. NotifyCore.Notify(new NifyMessage
  235. {
  236. message = $"【京东{accountId}:{company}】cookie 上线",
  237. tags = ["green_circle"]
  238. });
  239. _ = EndPointCore.NotifyReload(true);
  240. //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
  241. return accountId;
  242. }
  243. public static async Task DisabledAsync(int accountId, string name, string content)
  244. {
  245. string cache_key = $"cache:jd_pool:{name}:disabled";
  246. long count = await RedisKit.IncrByAsync(cache_key);
  247. _ = RedisKit.ExpireAsync(cache_key, 10);
  248. if (count > 1) return;
  249. var update = new DBContext.Table("jd_pool").Add("status", 0);
  250. if (accountId > 0)
  251. {
  252. update.Where("id=@accountId", new { accountId }).Update();
  253. }
  254. else
  255. {
  256. update.Where("name=@name", new { name }).Update();
  257. }
  258. _ = ListAsync(true);
  259. NotifyCore.Notify(new NifyMessage
  260. {
  261. message = $"【京东联盟{accountId}:{name}】cookie 掉线\n\n{content}",
  262. priority = NifyMessagePriority.high,
  263. tags = ["red_circle"]
  264. });
  265. //NotifyCore.AnPushNotify("掉线", $"【京东{accountId}:{name}】cookie 掉线");
  266. _ = EndPointCore.NotifyReload(true);
  267. }
  268. internal static async Task AccountExhaustedAsync()
  269. {
  270. string cache_key = $"cache:tk_pool:account:exhausted";
  271. long count = await RedisKit.IncrByAsync(cache_key);
  272. if (count > 1) return;
  273. _ = RedisKit.ExpireAsync(cache_key, 3600);
  274. NotifyCore.Notify(new NifyMessage
  275. {
  276. message = $"【京东联盟】没有匹配账号",
  277. priority = NifyMessagePriority.high,
  278. tags = ["red_circle"]
  279. });
  280. NotifyCore.AnPushNotify("没账号", $"【京东联盟】没有匹配账号");
  281. }
  282. }
  283. }