TkPoolCore.cs 6.7 KB

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