| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc.Controllers;
- using Microsoft.AspNetCore.Mvc.Filters;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using dodohold.core;
- using Spire.Pdf.Exporting.XPS.Schema;
- using System.Xml.Linq;
- using System.Net;
- using TencentCloud.Mrs.V20200910.Models;
- namespace molilian.core
- {
- public partial class TkPoolCore
- {
- private static string _end_point;
- static TkPoolCore()
- {
- _end_point = Environment.GetEnvironmentVariable("EndPoint");
- }
- private static readonly object _lockObj = new();
- private static IEnumerable<TkPoolDTO> _cached;
- private static Dictionary<string, decimal> _incomeAmt = new();
- public static TkPoolDTO? GetOne()
- {
- var list = List();
- if (!list.Any()) return null;
- return list.Where(IsNotExceedDailyIncomeLimit).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
- }
- internal static void SaveIncomeAmt(string accountName, decimal income_amt)
- {
- string key = $"{accountName}:{DateTime.Now:yyyyMMdd}";
- if (_incomeAmt.ContainsKey(key))
- {
- _incomeAmt[key] = income_amt;
- }
- else
- {
- _incomeAmt.Add(key, income_amt);
- }
- string cache_key = $"cache:tk_pool:{key}:income_amt";
- RedisHelper.Set(cache_key, income_amt, 3 * 86400);
- }
- public static decimal GetIncomeAmt(string accountName)
- {
- try
- {
- string key = $"{accountName}:{DateTime.Now:yyyyMMdd}";
- if (_incomeAmt.ContainsKey(key)) return _incomeAmt[key];
- string cache_key = $"cache:tk_pool:{key}:income_amt";
- return RedisHelper.Get<decimal>(cache_key);
- }
- catch (Exception ex) { return 0; }
- }
- private static bool IsNotExceedDailyIncomeLimit(TkPoolDTO item)
- {
- if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
- {
- if (item.end_point != _end_point) return false;
- }
- if (item.daily_income_limit == 0) return true;
- decimal income_amt = GetIncomeAmt(item.name);
- return income_amt < item.daily_income_limit;
- }
- public static IEnumerable<TkPoolDTO> List(bool force = false)
- {
- if (!force && _cached != null) return _cached;
- string cache_key = $"cache:tk_pool";
- var list = RedisHelper.Get<IEnumerable<TkPoolDTO>>(cache_key);
- if (force || list == null)
- {
- lock (_lockObj)
- {
- list = new DBContext.Table("tk_pool")
- .Where("status=@status", new { status = 1 })
- .Select<TkPoolDTO>();
- if (list == null) return default;
- RedisHelper.Set(cache_key, list, 30 * 86400);
- }
- }
- _cached = list;
- return list;
- }
- public static void Refresh()
- {
- _cached = null;
- _incomeAmt = [];
- _ = List(true);
- }
- public static void UpdateDrawBalance(string name, decimal amout)
- {
- new DBContext.Table("tk_pool")
- .Add("draw_balance", amout)
- .Where("name=@name", new { name })
- .Update();
- _ = List(true);
- }
- public static void Disabled(string name, string content)
- {
- #if DEBUG
- #else
- string cache_key = $"cache:tk_pool:{name}:disabled";
- long count = RedisHelper.IncrBy(cache_key);
- RedisHelper.Expire(cache_key, 10);
- if (count > 1) return;
- new DBContext.Table("tk_pool")
- .Add("status", 0)
- .Where("name=@name", new { name })
- .Update();
- _ = List(true);
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【淘宝联盟:{name}】cookie 掉线\n\n{content}",
- priority = NifyMessagePriority.high,
- tags = ["red_circle"]
- });
- NotifyCore.AnPushNotify("掉线", $"【淘宝联盟:{name}】cookie 掉线");
- #endif
- }
- public static bool UpdateCookies(string cookies, string user_agent)
- {
- if (string.IsNullOrEmpty(cookies)) return false;
- cookies += ";";
- string dnk = cookies.GetContentPart("dnk=", ";");
- string tb_token = cookies.GetContentPart("_tb_token_=", ";");
- if (string.IsNullOrEmpty(dnk) || string.IsNullOrEmpty(tb_token)) return false;
- var exist = new DBContext.Table("tk_pool").Fields("id, name, refpid, status").Get<dynamic>("name=@dnk", new { dnk });
- if (exist != null)
- {
- int status = exist.status;
- string refpid = exist.refpid;
- if (!string.IsNullOrEmpty(refpid)) status = 1;
- new DBContext.Table("tk_pool")
- .Add("name", dnk)
- .Add("tb_token", tb_token)
- .Add("cookies", cookies)
- .Add("user_agent", user_agent)
- .Add("status", status)
- .Add("last_time", DateTime.Now)
- .Where("id=@id", new { exist.id })
- .Update();
- if (status == 1) _ = List(true);
- }
- else
- {
- new DBContext.Table("tk_pool")
- .Add("name", dnk)
- .Add("description", "由cookies上报创建此记录")
- .Add("tb_token", tb_token)
- .Add("refpid", string.Empty)
- .Add("cookies", cookies)
- .Add("user_agent", user_agent)
- .Add("create_time", DateTime.Now)
- .Add("last_time", DateTime.Now)
- .Add("status", 0)
- .Create();
- }
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【淘宝联盟:{dnk}】cookie 上线",
- tags = ["green_circle"]
- });
- //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
- return true;
- }
- internal static void AccountExhausted()
- {
- string cache_key = $"cache:tk_pool:account:exhausted";
- long count = RedisHelper.IncrBy(cache_key);
- if (count > 1) return;
- RedisHelper.Expire(cache_key, 3600);
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【淘宝联盟】没有匹配账号",
- priority = NifyMessagePriority.high,
- tags = ["red_circle"]
- });
- NotifyCore.AnPushNotify("没账号", $"【淘宝联盟】没有匹配账号");
- }
- }
- }
|