JdPoolCore.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. namespace molilian.core
  14. {
  15. public partial class JdPoolCore
  16. {
  17. public enum JdAction
  18. {
  19. all,
  20. api,
  21. parse,
  22. coupon,
  23. promotionQuery
  24. }
  25. private static string _end_point;
  26. private static Dictionary<string, decimal> _incomeAmt = new();
  27. static JdPoolCore()
  28. {
  29. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  30. }
  31. private static readonly object _lockObj = new();
  32. private static IEnumerable<JdPoolDTO> _cached;
  33. private static IEnumerable<JdPoolDTO> _all_cached;
  34. public static JdPoolDTO? GetOne()
  35. {
  36. var list = List();
  37. if (!list.Any()) return null;
  38. return list.OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  39. }
  40. public static JdPoolDTO? GetSpecialOne(int accountid)
  41. {
  42. var list = AllList();
  43. if (!list.Any()) return null;
  44. return list.Where(e => e.id == accountid).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  45. }
  46. public static JdPoolDTO? GetOne(int id, JdAction action)
  47. {
  48. var list = List();
  49. if (!list.Any()) return null;
  50. return list.Where(e => e.id == id && IsNotExceedDailyIncomeLimit(e, action)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  51. }
  52. public static JdPoolDTO? GetOne(JdAction action)
  53. {
  54. var list = List();
  55. if (!list.Any()) return null;
  56. return list.Where(e => IsNotExceedDailyIncomeLimit(e, action)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  57. }
  58. private static bool IsNotExceedDailyIncomeLimit(JdPoolDTO item, JdAction action)
  59. {
  60. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  61. {
  62. if (item.end_point != _end_point) return false;
  63. }
  64. switch (action)
  65. {
  66. case JdAction.api:
  67. if (item.work_mode != JdUnionWorkMode.SiteApi) return false;
  68. break;
  69. case JdAction.parse:
  70. if (!item.enable_parse) return false;
  71. break;
  72. case JdAction.coupon:
  73. if (!item.enable_coupon) return false;
  74. break;
  75. }
  76. // 使用初始化参数创建工作时间表
  77. if (!new WorkSchedule(item.time_range).IsWorkHour()) return false;
  78. if (item.daily_calls_limit > 0)
  79. {
  80. int daily_num = RiskControlCore.GetCalls(TkChannelEnum.jd, item.id, DateTime.Now.ToString("yyyyMMdd"));
  81. if (daily_num >= item.daily_calls_limit) return false;
  82. }
  83. if (item.hourly_calls_limit > 0)
  84. {
  85. int hourly_num = RiskControlCore.GetCalls(TkChannelEnum.jd, item.id, DateTime.Now.ToString("yyyyMMddHH"));
  86. if (hourly_num >= item.hourly_calls_limit) return false;
  87. }
  88. return true;
  89. }
  90. public static IEnumerable<JdPoolDTO> AllList(bool force = false)
  91. {
  92. if (!force && _all_cached != default) return _all_cached;
  93. string cache_key = $"cache:all_jd_pool";
  94. var list = RedisHelper.Get<IEnumerable<JdPoolDTO>>(cache_key);
  95. if (force || list == default)
  96. {
  97. lock (_lockObj)
  98. {
  99. list = new DBContext.Table("jd_pool").Select<JdPoolDTO>();
  100. if (list == null) return default;
  101. RedisHelper.Set(cache_key, list, 30 * 86400);
  102. }
  103. }
  104. _all_cached = list;
  105. return list;
  106. }
  107. public static IEnumerable<JdPoolDTO> List(bool force = false)
  108. {
  109. if (!force && _cached != null) return _cached;
  110. string cache_key = $"cache:jd_pool";
  111. var list = RedisHelper.Get<IEnumerable<JdPoolDTO>>(cache_key);
  112. if (force || list == null)
  113. {
  114. lock (_lockObj)
  115. {
  116. list = new DBContext.Table("jd_pool")
  117. .Where("status=@status", new { status = 1 })
  118. .Select<JdPoolDTO>();
  119. if (list == null) return default;
  120. foreach (var item in list)
  121. {
  122. RiskControlCore.SetCallsAsync(TkChannelEnum.jd, item.id, DateTime.Now.ToString("yyyyMMddHH"), item.current_hourly_calls);
  123. RiskControlCore.SetCallsAsync(TkChannelEnum.jd, item.id, DateTime.Now.ToString("yyyyMMdd"), item.current_daily_calls);
  124. }
  125. RedisHelper.Set(cache_key, list, 30 * 86400);
  126. }
  127. }
  128. _cached = list;
  129. return list;
  130. }
  131. public static void Refresh()
  132. {
  133. _all_cached = [];
  134. _cached = [];
  135. _ = List(true);
  136. _ = AllList();
  137. }
  138. public static int Update(JdPoolDTO account)
  139. {
  140. return new DBContext.Table("jd_pool")
  141. .Add("current_hourly_calls", account.current_hourly_calls)
  142. .Add("current_daily_calls", account.current_daily_calls)
  143. .Add("today_clickNum", account.today_clickNum)
  144. .Add("today_cosFee", account.today_cosFee)
  145. .Add("today_cosPrice", account.today_cosPrice)
  146. .Add("today_finishCosFee", account.today_finishCosFee)
  147. .Add("today_finishCosPrice", account.today_finishCosPrice)
  148. .Add("today_finishOrderNum", account.today_finishOrderNum)
  149. .Add("today_orderNum", account.today_orderNum)
  150. .Where("id=@id", new { account.id })
  151. .Update();
  152. }
  153. public static int UpdateCookies(string cookies, string user_agent, string h5st)
  154. {
  155. if (string.IsNullOrEmpty(cookies)) return 0;
  156. string pin = cookies.GetContentPart("pin=", ";");
  157. pin = pin.UrlDecode();
  158. string company = pin;
  159. int accountId = 0;
  160. if (string.IsNullOrEmpty(pin)) return 0;
  161. var exist = new DBContext.Table("jd_pool").Get<JdPoolDTO>("pin=@pin", new { pin });
  162. if (exist != null)
  163. {
  164. var status = exist.status;
  165. var work_mode = exist.work_mode;
  166. accountId = exist.id;
  167. if (work_mode == JdUnionWorkMode.Crawler) status = true;
  168. var update = new DBContext.Table("jd_pool")
  169. .Add("pin", pin)
  170. .Add("cookies", cookies)
  171. .Add("status", status)
  172. .Add("last_time", DateTime.Now)
  173. .Add("login_time", DateTime.Now)
  174. .Where("id=@id", new { exist.id });
  175. if (!string.IsNullOrEmpty(user_agent)) update.Add("user_agent", user_agent);
  176. if (!string.IsNullOrEmpty(h5st)) update.Add("h5st", h5st);
  177. update.Update();
  178. if (status) _ = List(true);
  179. }
  180. else
  181. {
  182. var update = new DBContext.Table("jd_pool")
  183. .Add("pin", pin)
  184. .Add("name", pin)
  185. .Add("company", pin)
  186. .Add("description", "由cookies上报创建此记录")
  187. .Add("cookies", cookies)
  188. .Add("create_time", DateTime.Now)
  189. .Add("last_time", DateTime.Now)
  190. .Add("login_time", DateTime.Now)
  191. .Add("status", 0);
  192. if (!string.IsNullOrEmpty(user_agent)) update.Add("user_agent", user_agent);
  193. if (!string.IsNullOrEmpty(h5st)) update.Add("h5st", h5st);
  194. accountId = update.Create();
  195. }
  196. NotifyCore.Notify(new NifyMessage
  197. {
  198. message = $"【京东{accountId}:{company}】cookie 上线",
  199. tags = ["green_circle"]
  200. });
  201. EndPointCore.NotifyReload(true);
  202. //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
  203. return accountId;
  204. }
  205. public static void Disabled(int accountId, string name, string content)
  206. {
  207. string cache_key = $"cache:jd_pool:{name}:disabled";
  208. long count = RedisHelper.IncrBy(cache_key);
  209. RedisHelper.Expire(cache_key, 10);
  210. if (count > 1) return;
  211. var update = new DBContext.Table("jd_pool").Add("status", 0);
  212. if (accountId > 0)
  213. {
  214. update.Where("id=@accountId", new { accountId }).Update();
  215. }
  216. else
  217. {
  218. update.Where("name=@name", new { name }).Update();
  219. }
  220. _ = List(true);
  221. NotifyCore.Notify(new NifyMessage
  222. {
  223. message = $"【京东联盟{accountId}:{name}】cookie 掉线\n\n{content}",
  224. priority = NifyMessagePriority.high,
  225. tags = ["red_circle"]
  226. });
  227. //NotifyCore.AnPushNotify("掉线", $"【京东{accountId}:{name}】cookie 掉线");
  228. EndPointCore.NotifyReload(true);
  229. }
  230. internal static void AccountExhausted()
  231. {
  232. string cache_key = $"cache:tk_pool:account:exhausted";
  233. long count = RedisHelper.IncrBy(cache_key);
  234. if (count > 1) return;
  235. RedisHelper.Expire(cache_key, 3600);
  236. NotifyCore.Notify(new NifyMessage
  237. {
  238. message = $"【京东联盟】没有匹配账号",
  239. priority = NifyMessagePriority.high,
  240. tags = ["red_circle"]
  241. });
  242. NotifyCore.AnPushNotify("没账号", $"【京东联盟】没有匹配账号");
  243. }
  244. }
  245. }