OrderTrackingCore.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 Dataoke;
  10. using Google.Protobuf.WellKnownTypes;
  11. using System.Diagnostics;
  12. using System.Text.Json;
  13. using TencentCloud.Tcm.V20210413.Models;
  14. using System.Security.Cryptography;
  15. using System.Data;
  16. using System.Threading.Channels;
  17. using YunhuiKit;
  18. using System.IO;
  19. namespace molilian.core
  20. {
  21. public partial class TkOrderTrackingCore
  22. {
  23. private const int DefaultLinkSummaryTtlSeconds = 24 * 3600;
  24. private const int MaxLinkSummaryTtlSeconds = 7 * 24 * 3600;
  25. private const string LinkSummaryCacheVersion = "v2";
  26. public class TkOrderLinkSummaryDTO
  27. {
  28. public TkChannelEnum channel { get; set; }
  29. public int accountId { get; set; } = 0;
  30. public string ip { get; set; } = string.Empty;
  31. public string oaid { get; set; } = string.Empty;
  32. public string mktId { get; set; } = string.Empty;
  33. public string itemId { get; set; } = string.Empty;
  34. public string itemName { get; set; } = string.Empty;
  35. public string taoToken { get; set; } = string.Empty;
  36. public string shortLinkurl { get; set; } = string.Empty;
  37. public string deeplink_url { get; set; } = string.Empty;
  38. public string item_url { get; set; } = string.Empty;
  39. public string item_deeplink_url { get; set; } = string.Empty;
  40. public DateTime create_time { get; set; } = DateTime.Now;
  41. }
  42. public static void SaveLinkSummary(TkDataDTO item, TkPoolDTO? account = null)
  43. {
  44. if (!ShouldSaveLinkSummary(item, account)) return;
  45. TkOrderLinkSummaryDTO newData = new()
  46. {
  47. accountId = item.accountId,
  48. channel = item.channel,
  49. ip = item.ip,
  50. oaid = item.oaid,
  51. mktId = item.mktId,
  52. itemId = item.itemId,
  53. itemName = item.itemName,
  54. taoToken = item.taoToken,
  55. shortLinkurl = item.shortLinkurl,
  56. deeplink_url = item.deeplink_url,
  57. item_url = item.item_url,
  58. item_deeplink_url = item.item_deeplink_url,
  59. create_time = item.create_time,
  60. };
  61. var config = TkConfigCore.Get();
  62. int ttl = GetLinkSummaryTtlSeconds(config);
  63. string cacheKey = BuildItemSummaryKey(item.accountId, item.itemId);
  64. if (!string.IsNullOrEmpty(cacheKey)) RedisHelper.Set(cacheKey, newData, ttl);
  65. string mktId = item.mktId;
  66. if (!string.IsNullOrEmpty(mktId) && mktId.Contains('-'))
  67. {
  68. mktId = mktId.Split('-')[^1];
  69. cacheKey = BuildMktSummaryKey(item.accountId, mktId);
  70. RedisHelper.Set(cacheKey, newData, ttl);
  71. }
  72. cacheKey = BuildTitleSummaryKey(item.accountId, item.itemName);
  73. if (!string.IsNullOrEmpty(cacheKey)) RedisHelper.Set(cacheKey, newData, ttl);
  74. }
  75. private static bool ShouldSaveLinkSummary(TkDataDTO item, TkPoolDTO? account)
  76. {
  77. if (item == null || item.accountId <= 0) return false;
  78. if (!item.success) return false;
  79. if (string.IsNullOrEmpty(item.itemId)) return false;
  80. bool hasLink = !string.IsNullOrEmpty(item.shortLinkurl) ||
  81. !string.IsNullOrEmpty(item.deeplink_url) ||
  82. !string.IsNullOrEmpty(item.item_url) ||
  83. !string.IsNullOrEmpty(item.item_deeplink_url);
  84. if (!hasLink) return false;
  85. account ??= GetCachedAccount(item.accountId);
  86. return account?.enable_order_summary_cache ?? true;
  87. }
  88. private static TkPoolDTO? GetCachedAccount(int accountId)
  89. {
  90. try
  91. {
  92. var list = TkPoolCore.GetCachedAccounts();
  93. return list?.FirstOrDefault(e => e.id == accountId);
  94. }
  95. catch
  96. {
  97. return null;
  98. }
  99. }
  100. private static int GetLinkSummaryTtlSeconds(TkConfigDTO config)
  101. {
  102. long configuredHours = config?.fake_click_ttl ?? 0;
  103. long ttl = configuredHours > 0 ? configuredHours * 3600 : DefaultLinkSummaryTtlSeconds;
  104. return (int)Math.Clamp(ttl, 3600, MaxLinkSummaryTtlSeconds);
  105. }
  106. private static string BuildItemSummaryKey(int accountId, string itemId)
  107. {
  108. if (accountId <= 0 || string.IsNullOrEmpty(itemId)) return string.Empty;
  109. return $":cache:order_summary:{LinkSummaryCacheVersion}:{accountId}:item:{itemId}";
  110. }
  111. private static string BuildMktSummaryKey(int accountId, string mktId)
  112. {
  113. if (accountId <= 0 || string.IsNullOrEmpty(mktId)) return string.Empty;
  114. return $":cache:order_summary:{LinkSummaryCacheVersion}:{accountId}:mktId:{mktId}";
  115. }
  116. private static string BuildTitleSummaryKey(int accountId, string itemTitle)
  117. {
  118. if (accountId <= 0 || string.IsNullOrEmpty(itemTitle)) return string.Empty;
  119. return $":cache:order_summary:{LinkSummaryCacheVersion}:{accountId}:title:{Sha256Hex(itemTitle)}";
  120. }
  121. private static string BuildLegacyItemSummaryKey(int accountId, string itemId)
  122. {
  123. if (accountId <= 0 || string.IsNullOrEmpty(itemId)) return string.Empty;
  124. return $":cache:order_summary:{accountId}:{itemId}";
  125. }
  126. private static string BuildLegacyMktSummaryKey(int accountId, string mktId)
  127. {
  128. if (accountId <= 0 || string.IsNullOrEmpty(mktId)) return string.Empty;
  129. return $":cache:order_summary:{accountId}:mktId:{mktId}";
  130. }
  131. private static string BuildLegacyTitleSummaryKey(int accountId, string itemTitle)
  132. {
  133. if (accountId <= 0 || string.IsNullOrEmpty(itemTitle)) return string.Empty;
  134. return $":cache:order_summary:{accountId}:{itemTitle}";
  135. }
  136. private static string Sha256Hex(string value)
  137. {
  138. byte[] hash = SHA256.HashData(Encoding.UTF8.GetBytes(value));
  139. return Convert.ToHexString(hash).ToLowerInvariant();
  140. }
  141. private static async Task<TkOrderLinkSummaryDTO?> GetFirstSummaryAsync(YunhuiKit.RedisClient redis, IEnumerable<string> keys)
  142. {
  143. foreach (var key in keys.Where(e => !string.IsNullOrEmpty(e)))
  144. {
  145. var result = await redis.GetAsync<TkOrderLinkSummaryDTO>(key);
  146. if (IsUsableOrderSummary(result)) return result;
  147. }
  148. return null;
  149. }
  150. private static bool IsUsableOrderSummary(TkOrderLinkSummaryDTO? summary)
  151. {
  152. if (summary == null) return false;
  153. return !string.IsNullOrEmpty(summary.shortLinkurl) ||
  154. !string.IsNullOrEmpty(summary.deeplink_url) ||
  155. !string.IsNullOrEmpty(summary.item_url) ||
  156. !string.IsNullOrEmpty(summary.item_deeplink_url);
  157. }
  158. public static async Task<(int, TkOrderLinkSummaryDTO)> GetLinkSummaryAsync(TkPoolDTO account, string itemId, string mktId, string itemTitle)
  159. {
  160. try
  161. {
  162. int accountId = account.id;
  163. mktId = !string.IsNullOrEmpty(mktId) && mktId.Contains('-')
  164. ? mktId.Split('-')[^1]
  165. : mktId;
  166. // 获取所有关联账户ID(包括当前账户)
  167. var relatedAccountIds = new List<int> { accountId };
  168. if (!string.IsNullOrEmpty(account.related_account_ids))
  169. {
  170. relatedAccountIds.AddRange(account.related_account_ids
  171. .Split(',')
  172. .Where(id => !string.IsNullOrWhiteSpace(id))
  173. .Select(int.Parse));
  174. }
  175. var tasks = EndPointCore.List()
  176. .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
  177. .Select(async node =>
  178. {
  179. try
  180. {
  181. var redisServer = EndPointCore.GetRedisServer(node);
  182. if (string.IsNullOrEmpty(redisServer)) return (0, null);
  183. using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
  184. var redis = scope.Client;
  185. // 按优先级依次查询不同的缓存key
  186. TkOrderLinkSummaryDTO result = null;
  187. // 遍历所有关联账户ID
  188. foreach (var relatedId in relatedAccountIds)
  189. {
  190. if (!string.IsNullOrEmpty(mktId))
  191. {
  192. result = await GetFirstSummaryAsync(redis, new[]
  193. {
  194. BuildMktSummaryKey(relatedId, mktId),
  195. BuildLegacyMktSummaryKey(relatedId, mktId)
  196. });
  197. if (IsUsableOrderSummary(result)) return (1, result);
  198. }
  199. if (!string.IsNullOrEmpty(itemId))
  200. {
  201. result = await GetFirstSummaryAsync(redis, new[]
  202. {
  203. BuildItemSummaryKey(relatedId, itemId),
  204. BuildLegacyItemSummaryKey(relatedId, itemId)
  205. });
  206. if (IsUsableOrderSummary(result)) return (2, result);
  207. if (!string.IsNullOrEmpty(itemTitle))
  208. {
  209. result = await GetFirstSummaryAsync(redis, new[]
  210. {
  211. BuildTitleSummaryKey(relatedId, itemTitle),
  212. BuildLegacyTitleSummaryKey(relatedId, itemTitle)
  213. });
  214. if (IsUsableOrderSummary(result)) return (3, result);
  215. }
  216. }
  217. }
  218. }
  219. catch (Exception ex)
  220. {
  221. }
  222. return (0, null);
  223. });
  224. var results = await Task.WhenAll(tasks);
  225. return results.FirstOrDefault(r => r != (0, null));
  226. }
  227. catch (Exception)
  228. {
  229. // TODO: 添加日志记录
  230. return (0, null);
  231. }
  232. }
  233. public static async Task RemoveLinkSummaryAsync(int accountId, string itemId, string mktId, string itemTitle)
  234. {
  235. try
  236. {
  237. var tasks = EndPointCore.List()
  238. .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
  239. .Select(async node =>
  240. {
  241. try
  242. {
  243. var redisServer = EndPointCore.GetRedisServer(node);
  244. if (string.IsNullOrEmpty(redisServer)) return;
  245. using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
  246. var redis = scope.Client;
  247. mktId = !string.IsNullOrEmpty(mktId) && mktId.Contains('-')
  248. ? mktId.Split('-')[^1]
  249. : mktId;
  250. var keys = new[]
  251. {
  252. BuildItemSummaryKey(accountId, itemId),
  253. BuildLegacyItemSummaryKey(accountId, itemId),
  254. BuildMktSummaryKey(accountId, mktId),
  255. BuildLegacyMktSummaryKey(accountId, mktId),
  256. BuildTitleSummaryKey(accountId, itemTitle),
  257. BuildLegacyTitleSummaryKey(accountId, itemTitle)
  258. }.Where(k => !string.IsNullOrEmpty(k));
  259. // 批量删除所有相关缓存
  260. await redis.DelAsync(keys.ToArray());
  261. }
  262. catch (Exception ex) { }
  263. });
  264. await Task.WhenAll(tasks);
  265. }
  266. catch (Exception)
  267. {
  268. // TODO: 添加日志记录
  269. }
  270. }
  271. //public static async void RemoveLinkSummary(int accountId, string itemId, string mktId, string itemTitle)
  272. //{
  273. // await EndPointCore.ProcessEndPointNodesAsync(node =>
  274. // {
  275. // if (!node.is_public_api) return Task.CompletedTask;
  276. // if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  277. // var redis = RedisClientManager.GetRedisClient(node.redis_server);
  278. // string cacheKey = $":cache:order_summary:{accountId}:{itemId}";
  279. // redis.Del(cacheKey);
  280. // cacheKey = $":cache:order_summary:{accountId}:mktId:{mktId}";
  281. // redis.Del(cacheKey);
  282. // cacheKey = $":cache:order_summary:{accountId}:{itemTitle}";
  283. // redis.Del(cacheKey);
  284. // return Task.CompletedTask;
  285. // });
  286. //}
  287. public static async Task<bool> MatchOrder(TkPoolDTO account, TkOrderDetailDTO item)
  288. {
  289. DateTime now = DateTime.Now;
  290. if (item.tbPaidTime < now.AddDays(-24)) return false;
  291. (int match_type, var summary) = await GetLinkSummaryAsync(account, item.itemId, item.mktId, item.itemTitle);
  292. if (summary == null) return false;
  293. var config = TkConfigCore.Get();
  294. Random rand = new();
  295. int click_num = rand.Next(config.fake_click_min, config.fake_click_max + 1);
  296. if (account.fake_click_min != 0 && account.fake_click_max != 0)
  297. click_num = rand.Next(account.fake_click_min, account.fake_click_max + 1);
  298. DateTime paidTime = item.tbPaidTime;
  299. if (paidTime < summary.create_time) return false;
  300. if (paidTime < now.AddDays(-24)) return false;
  301. int minutes = now.Minute / 10 * 10;
  302. DateTime rounded = new(now.Year, now.Month, now.Day, now.Hour, minutes, 0);
  303. string batchId = rounded.ToString("yyyyMMddHHmm");
  304. string accountName = item.accountName;
  305. string shortLinkUrl = !string.IsNullOrEmpty(summary.item_url) ? summary.item_url : summary.shortLinkurl;
  306. string deeplinkUrl = !string.IsNullOrEmpty(summary.item_deeplink_url) ? summary.item_deeplink_url : summary.deeplink_url;
  307. switch (account.fake_click_link_type)
  308. {
  309. case FakeClickLinkType.Deeplink:
  310. shortLinkUrl = string.Empty;
  311. break;
  312. case FakeClickLinkType.H5:
  313. deeplinkUrl = string.Empty;
  314. break;
  315. }
  316. var exist = new DBContext.Table("tk_order_tracking").Fields("id").Get<dynamic>("tradeId=@tradeId", new { item.tradeId });
  317. DateTime expTime = now.Hour >= 21 ? now.AddHours(3) : now.Date.AddDays(1);
  318. var data = new TkOrderTrackingDTO()
  319. {
  320. match_type = match_type,
  321. channel = summary.channel,
  322. accountId = item.accountId,
  323. accountName = accountName,
  324. batchId = batchId,
  325. ip = summary.ip,
  326. oaid = summary.oaid,
  327. mktId = summary.mktId,
  328. itemId = summary.itemId,
  329. itemName = summary.itemName,
  330. taoToken = summary.taoToken,
  331. shortLinkUrl = shortLinkUrl,
  332. deeplinkUrl = deeplinkUrl,
  333. tradeId = item.tradeId,
  334. tradeParentId = item.tradeParentId,
  335. create_time = summary.create_time,
  336. click_time = item.clickTime,
  337. paid_time = item.tbPaidTime,
  338. exp_time = expTime,
  339. click_num = click_num,
  340. };
  341. if (exist == null)
  342. {
  343. using var conn = DBContext.GetOpenConnection();
  344. conn.Insert(data);
  345. }
  346. //RemoveLinkSummary(item.accountId, item.itemId, item.mktId, item.itemTitle);
  347. await RemoveLinkSummaryAsync(item.accountId, item.itemId, item.mktId, item.itemTitle);
  348. return true;
  349. }
  350. }
  351. }