TkPoolCore.cs 7.0 KB

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