TkPoolCore.cs 9.7 KB

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