TkPoolCore.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. }
  23. private static string _end_point;
  24. static TkPoolCore()
  25. {
  26. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  27. }
  28. private static readonly object _lockObj = new();
  29. private static IEnumerable<TkPoolDTO> _cached;
  30. private static Dictionary<string, decimal> _incomeAmt = new();
  31. public static TkPoolDTO? GetOne(TkAction action)
  32. {
  33. var list = List();
  34. if (!list.Any()) return null;
  35. return list.Where(e => IsNotExceedDailyIncomeLimit(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. internal static void SaveIncomeAmt(string accountName, decimal income_amt)
  44. {
  45. string key = $"{accountName}:{DateTime.Now:yyyyMMdd}";
  46. if (_incomeAmt.ContainsKey(key))
  47. {
  48. _incomeAmt[key] = income_amt;
  49. }
  50. else
  51. {
  52. _incomeAmt.Add(key, income_amt);
  53. }
  54. string cache_key = $"cache:tk_pool:{key}:income_amt";
  55. RedisHelper.Set(cache_key, income_amt, 3 * 86400);
  56. }
  57. public static decimal GetIncomeAmt(string accountName)
  58. {
  59. try
  60. {
  61. string key = $"{accountName}:{DateTime.Now:yyyyMMdd}";
  62. if (_incomeAmt.ContainsKey(key)) return _incomeAmt[key];
  63. string cache_key = $"cache:tk_pool:{key}:income_amt";
  64. return RedisHelper.Get<decimal>(cache_key);
  65. }
  66. catch (Exception ex) { return 0; }
  67. }
  68. private static bool IsNotExceedDailyIncomeLimit(TkPoolDTO item, TkAction action)
  69. {
  70. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  71. {
  72. if (item.end_point != _end_point) return false;
  73. }
  74. switch (action)
  75. {
  76. case TkAction.parse:
  77. if (!item.enable_parse) return false;
  78. break;
  79. case TkAction.coupon:
  80. if (!item.enable_coupon) return false;
  81. break;
  82. }
  83. if (item.daily_income_limit == 0) return true;
  84. decimal income_amt = GetIncomeAmt(item.name);
  85. return income_amt < item.daily_income_limit;
  86. }
  87. public static IEnumerable<TkPoolDTO> List(bool force = false)
  88. {
  89. if (!force && _cached != null) return _cached;
  90. string cache_key = $"cache:tk_pool";
  91. var list = RedisHelper.Get<IEnumerable<TkPoolDTO>>(cache_key);
  92. if (force || list == null)
  93. {
  94. lock (_lockObj)
  95. {
  96. list = new DBContext.Table("tk_pool")
  97. .Where("status=@status", new { status = 1 })
  98. .Select<TkPoolDTO>();
  99. if (list == null) return default;
  100. RedisHelper.Set(cache_key, list, 30 * 86400);
  101. }
  102. }
  103. _cached = list;
  104. return list;
  105. }
  106. public static void Refresh()
  107. {
  108. _incomeAmt = [];
  109. _ = List(true);
  110. }
  111. public static void UpdateDrawBalance(string name, decimal amout)
  112. {
  113. new DBContext.Table("tk_pool")
  114. .Add("draw_balance", amout)
  115. .Where("name=@name", new { name })
  116. .Update();
  117. _ = List(true);
  118. }
  119. public static void Disabled(string name, string content)
  120. {
  121. #if DEBUG
  122. #else
  123. #endif
  124. string cache_key = $"cache:tk_pool:{name}:disabled";
  125. long count = RedisHelper.IncrBy(cache_key);
  126. RedisHelper.Expire(cache_key, 10);
  127. if (count > 1) return;
  128. new DBContext.Table("tk_pool")
  129. .Add("status", 0)
  130. .Where("name=@name", new { name })
  131. .Update();
  132. _ = List(true);
  133. NotifyCore.Notify(new NifyMessage
  134. {
  135. message = $"【淘宝联盟:{name}】cookie 掉线\n\n{content}",
  136. priority = NifyMessagePriority.high,
  137. tags = ["red_circle"]
  138. });
  139. NotifyCore.AnPushNotify("掉线", $"【淘宝联盟:{name}】cookie 掉线");
  140. EndPointCore.NotifyReload(true);
  141. }
  142. public static bool UpdateCookies(string cookies, string user_agent)
  143. {
  144. if (string.IsNullOrEmpty(cookies)) return false;
  145. cookies += ";";
  146. string dnk = cookies.GetContentPart("dnk=", ";");
  147. string tb_token = cookies.GetContentPart("_tb_token_=", ";");
  148. if (string.IsNullOrEmpty(dnk) || string.IsNullOrEmpty(tb_token)) return false;
  149. var exist = new DBContext.Table("tk_pool").Fields("id, name, refpid, status").Get<dynamic>("name=@dnk", new { dnk });
  150. if (exist != null)
  151. {
  152. int status = exist.status;
  153. string refpid = exist.refpid;
  154. if (!string.IsNullOrEmpty(refpid)) status = 1;
  155. new DBContext.Table("tk_pool")
  156. .Add("name", dnk)
  157. .Add("tb_token", tb_token)
  158. .Add("cookies", cookies)
  159. .Add("user_agent", user_agent)
  160. .Add("status", status)
  161. .Add("last_time", DateTime.Now)
  162. .Where("id=@id", new { exist.id })
  163. .Update();
  164. if (status == 1) _ = List(true);
  165. }
  166. else
  167. {
  168. new DBContext.Table("tk_pool")
  169. .Add("name", dnk)
  170. .Add("description", "由cookies上报创建此记录")
  171. .Add("tb_token", tb_token)
  172. .Add("refpid", string.Empty)
  173. .Add("cookies", cookies)
  174. .Add("user_agent", user_agent)
  175. .Add("create_time", DateTime.Now)
  176. .Add("last_time", DateTime.Now)
  177. .Add("status", 0)
  178. .Create();
  179. }
  180. NotifyCore.Notify(new NifyMessage
  181. {
  182. message = $"【淘宝联盟:{dnk}】cookie 上线",
  183. tags = ["green_circle"]
  184. });
  185. EndPointCore.NotifyReload(true);
  186. //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
  187. return true;
  188. }
  189. internal static void AccountExhausted()
  190. {
  191. string cache_key = $"cache:tk_pool:account:exhausted";
  192. long count = RedisHelper.IncrBy(cache_key);
  193. if (count > 1) return;
  194. RedisHelper.Expire(cache_key, 3600);
  195. NotifyCore.Notify(new NifyMessage
  196. {
  197. message = $"【淘宝联盟】没有匹配账号",
  198. priority = NifyMessagePriority.high,
  199. tags = ["red_circle"]
  200. });
  201. NotifyCore.AnPushNotify("没账号", $"【淘宝联盟】没有匹配账号");
  202. }
  203. }
  204. }