JdPoolCore.cs 9.0 KB

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