TkPoolCore.cs 7.0 KB

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