JdPoolCore.cs 11 KB

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