JdPoolCore.cs 6.8 KB

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