TkPoolCore.cs 8.3 KB

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