TkPoolCore.cs 8.5 KB

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