TkPoolCore.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. private static Dictionary<string, decimal> _incomeAmt = new();
  32. public static TkPoolDTO? GetOne(TkAction action)
  33. {
  34. var list = List();
  35. if (!list.Any()) return null;
  36. return list.Where(e => FilterNodes(e, action)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  37. }
  38. public static TkPoolDTO? GetOne(int id)
  39. {
  40. var list = List();
  41. if (!list.Any()) return null;
  42. return list.Where(e => e.id == id).FirstOrDefault();
  43. }
  44. internal static void SaveIncomeAmt(string accountName, decimal income_amt)
  45. {
  46. string key = $"{accountName}:{DateTime.Now:yyyyMMdd}";
  47. if (_incomeAmt.ContainsKey(key))
  48. {
  49. _incomeAmt[key] = income_amt;
  50. }
  51. else
  52. {
  53. _incomeAmt.Add(key, income_amt);
  54. }
  55. string cache_key = $"cache:tk_pool:{key}:income_amt";
  56. var result = EndPointCore.ProcessEndPointNodes<bool>(node =>
  57. {
  58. if (!node.is_public_api) return true;
  59. if (string.IsNullOrEmpty(node.redis_server)) return true;
  60. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  61. return redis.Set(cache_key, income_amt, 3 * 86400);
  62. });
  63. }
  64. public static decimal GetIncomeAmt(string accountName)
  65. {
  66. try
  67. {
  68. string key = $"{accountName}:{DateTime.Now:yyyyMMdd}";
  69. if (_incomeAmt.ContainsKey(key)) return _incomeAmt[key];
  70. string cache_key = $"cache:tk_pool:{key}:income_amt";
  71. return RedisHelper.Get<decimal>(cache_key);
  72. }
  73. catch (Exception ex) { return 0; }
  74. }
  75. private static bool FilterNodes(TkPoolDTO item, TkAction action)
  76. {
  77. if (!string.IsNullOrEmpty(item.suspended_endpoint) && item.suspended_endpoint.Contains($"{_end_point}|")) return false;
  78. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  79. {
  80. if (item.end_point != _end_point) return false;
  81. }
  82. switch (action)
  83. {
  84. case TkAction.parse:
  85. if (!item.enable_parse) return false;
  86. break;
  87. case TkAction.promotionQuery:
  88. if (!item.enable_promotionQuery) return false;
  89. break;
  90. case TkAction.coupon:
  91. if (!item.enable_coupon) return false;
  92. break;
  93. }
  94. if (item.daily_income_limit == 0) return true;
  95. decimal income_amt = GetIncomeAmt($"{item.id}");
  96. return income_amt < item.daily_income_limit;
  97. }
  98. public static IEnumerable<TkPoolDTO> List(bool force = false)
  99. {
  100. if (!force && _cached != null) return _cached;
  101. string cache_key = $"cache:tk_pool";
  102. var list = RedisHelper.Get<IEnumerable<TkPoolDTO>>(cache_key);
  103. if (force || list == null)
  104. {
  105. lock (_lockObj)
  106. {
  107. list = new DBContext.Table("tk_pool")
  108. .Where("status=@status", new { status = 1 })
  109. .Select<TkPoolDTO>();
  110. if (list == null) return default;
  111. RedisHelper.Set(cache_key, list, 30 * 86400);
  112. }
  113. }
  114. _cached = list;
  115. return list;
  116. }
  117. public static void Refresh()
  118. {
  119. _incomeAmt = [];
  120. _ = List(true);
  121. }
  122. public static void UpdateDrawBalance(string name, decimal amout)
  123. {
  124. new DBContext.Table("tk_pool")
  125. .Add("draw_balance", amout)
  126. .Where("name=@name", new { name })
  127. .Update();
  128. _ = List(true);
  129. }
  130. public static void Suspend(string endpoint, int accountId, string name, string content)
  131. {
  132. string cache_key = $"cache:tk_pool:{accountId}:suspend";
  133. long count = RedisHelper.IncrBy(cache_key);
  134. RedisHelper.Expire(cache_key, 10);
  135. if (count > 1) return;
  136. var update = new DBContext.Table("tk_pool").Add("suspended_endpoint:", $"CONCAT(suspended_endpoint, '{endpoint},')");
  137. if (accountId > 0)
  138. {
  139. update.Where("id=@accountId", new { accountId }).Update();
  140. }
  141. else
  142. {
  143. update.Where("name=@name", new { name }).Update();
  144. }
  145. _ = List(true);
  146. NotifyCore.Notify(new NifyMessage
  147. {
  148. message = $"【淘客{accountId}:{name}】{endpoint} 暂停",
  149. priority = NifyMessagePriority.high,
  150. tags = ["red_circle"]
  151. });
  152. NotifyCore.AnPushNotify("暂停", $"【淘客{accountId}:{name}】{endpoint} 暂停");
  153. EndPointCore.NotifyReload(true);
  154. }
  155. public static void Disabled(int accountId, string name, string content)
  156. {
  157. #if DEBUG
  158. #else
  159. #endif
  160. string cache_key = $"cache:tk_pool:{accountId}:disabled";
  161. long count = RedisHelper.IncrBy(cache_key);
  162. RedisHelper.Expire(cache_key, 10);
  163. if (count > 1) return;
  164. var update = new DBContext.Table("tk_pool").Add("status", 0);
  165. if (accountId > 0)
  166. {
  167. update.Where("id=@accountId", new { accountId }).Update();
  168. }
  169. else
  170. {
  171. update.Where("name=@name", new { name }).Update();
  172. }
  173. _ = List(true);
  174. NotifyCore.Notify(new NifyMessage
  175. {
  176. message = $"【淘客{accountId}:{name}】cookie 掉线\n\n{content}",
  177. priority = NifyMessagePriority.high,
  178. tags = ["red_circle"]
  179. });
  180. NotifyCore.AnPushNotify("掉线", $"【淘客{accountId}:{name}】cookie 掉线");
  181. EndPointCore.NotifyReload(true);
  182. }
  183. public static int UpdateCookies(string cookies, string user_agent)
  184. {
  185. if (string.IsNullOrEmpty(cookies)) return 0;
  186. cookies += ";";
  187. string dnk = cookies.GetContentPart("dnk=", ";");
  188. string company = dnk;
  189. int accountId = 0;
  190. string tb_token = cookies.GetContentPart("_tb_token_=", ";");
  191. if (string.IsNullOrEmpty(dnk) || string.IsNullOrEmpty(tb_token)) return 0;
  192. var exist = new DBContext.Table("tk_pool").Fields("id, name, company, refpid, status").Get<dynamic>("name=@dnk", new { dnk });
  193. if (exist != null)
  194. {
  195. int status = exist.status;
  196. string refpid = exist.refpid;
  197. company = exist.company;
  198. accountId = exist.id;
  199. if (!string.IsNullOrEmpty(refpid)) status = 1;
  200. new DBContext.Table("tk_pool")
  201. .Add("name", dnk)
  202. .Add("tb_token", tb_token)
  203. .Add("cookies", cookies)
  204. .Add("user_agent", user_agent)
  205. .Add("status", status)
  206. .Add("suspended_endpoint", string.Empty)
  207. .Add("last_time", DateTime.Now)
  208. .Add("login_time", DateTime.Now)
  209. .Where("id=@id", new { exist.id })
  210. .Update();
  211. if (status == 1) _ = List(true);
  212. }
  213. else
  214. {
  215. accountId = new DBContext.Table("tk_pool")
  216. .Add("name", dnk)
  217. .Add("description", "由cookies上报创建此记录")
  218. .Add("tb_token", tb_token)
  219. .Add("refpid", string.Empty)
  220. .Add("cookies", cookies)
  221. .Add("user_agent", user_agent)
  222. .Add("create_time", DateTime.Now)
  223. .Add("last_time", DateTime.Now)
  224. .Add("login_time", DateTime.Now)
  225. .Add("status", 0)
  226. .Create();
  227. }
  228. NotifyCore.Notify(new NifyMessage
  229. {
  230. message = $"【淘客{accountId}:{company}】cookie 上线",
  231. tags = ["green_circle"]
  232. });
  233. EndPointCore.NotifyReload(true);
  234. //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
  235. return accountId;
  236. }
  237. internal static void AccountExhausted()
  238. {
  239. string cache_key = $"cache:tk_pool:account:exhausted";
  240. long count = RedisHelper.IncrBy(cache_key);
  241. if (count > 1) return;
  242. RedisHelper.Expire(cache_key, 3600);
  243. NotifyCore.Notify(new NifyMessage
  244. {
  245. message = $"【淘客】没有匹配账号",
  246. priority = NifyMessagePriority.high,
  247. tags = ["red_circle"]
  248. });
  249. NotifyCore.AnPushNotify("没账号", $"【淘客】没有匹配账号");
  250. }
  251. }
  252. }