JdPoolCore.cs 8.7 KB

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