dp2dp.cs 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. using dodohold.core;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System.Diagnostics;
  4. using System.Text.RegularExpressions;
  5. namespace molilian.core
  6. {
  7. public class ParseDeeplinkResust
  8. {
  9. public bool success { get; set; } = false;
  10. public string reason { get; set; } = string.Empty;
  11. public string url { get; set; } = string.Empty;
  12. }
  13. public partial class UnionParseCore
  14. {
  15. public static async Task<APIResult> DeeplinkTaobaoParseAsync(string content, int commerceType,
  16. string ip = "", string oaid = "", string riskStrategy = "", int launchScene = -1,
  17. int accountid = 0)
  18. {
  19. bool other_aff = false;
  20. AlimamaPlus alimama = null;
  21. var result = AlimamaPlus.GetFormattedObject(content, ip, oaid);
  22. result.riskStrategy = riskStrategy;
  23. result.launchScene = launchScene;
  24. result.rawContent = content;
  25. result.parse_type = "dp";
  26. Dictionary<string, long> swData = new();
  27. swData.Add("StartNew", 0);
  28. Stopwatch stopwatch = Stopwatch.StartNew();
  29. try
  30. {
  31. var parseResult = parseTaobaoDeeplinkInput(content);
  32. if (!parseResult.success)
  33. {
  34. result.channel = TkChannelEnum.tb;
  35. result.channel_type = ChannelTypeEnum.tb;
  36. result.link_type = LinkTypeEnum.other_aff;
  37. result.success = false;
  38. result.message = "放弃转链";
  39. result.reason = parseResult.reason;
  40. result.subCode = TkSubCodeEnum.OtherPromo;
  41. result.accountName = string.Empty;
  42. result.content = content;
  43. result.deeplink_url = string.Empty;
  44. _ = TkLogCore.ParseLogAsync(result);
  45. return TaobaoParseOutput(result, swData, new
  46. {
  47. result.success,
  48. result.commercial,
  49. result.message,
  50. result.content,
  51. result.itemName,
  52. other_aff = true,
  53. result.deeplink_url,
  54. });
  55. }
  56. if (!AlimamaPlus.IsShortLink(parseResult.url))
  57. {
  58. (bool isTaobaUrl, string shortLinkurl) = AlimamaPlus.IsTaobaoUrl(parseResult.url);
  59. if (!isTaobaUrl)
  60. {
  61. result.success = false;
  62. result.channel = TkChannelEnum.tb;
  63. result.link_type = LinkTypeEnum.unknown;
  64. result.success = false;
  65. result.message = "放弃转链";
  66. result.reason = "无效商品id";
  67. if (!result.success)
  68. {
  69. result.deeplink_url = string.Empty;
  70. }
  71. _ = TkLogCore.ParseLogAsync(result);
  72. return TaobaoParseOutput(result, swData, new
  73. {
  74. result.success,
  75. result.commercial,
  76. result.message,
  77. result.content,
  78. result.itemName,
  79. other_aff,
  80. result.deeplink_url,
  81. });
  82. }
  83. result.shortLinkurl = shortLinkurl;
  84. result.content = shortLinkurl;
  85. content = shortLinkurl;
  86. }
  87. else
  88. {
  89. result.content = parseResult.url;
  90. result.shortLinkurl = parseResult.url;
  91. content = parseResult.url;
  92. }
  93. result.deeplink_url = string.Empty;
  94. string reason = string.Empty;
  95. var config = TkConfigCore.Get();
  96. var arr = config.tk_whitelist_regular.Split('\n')
  97. .Where(line => !string.IsNullOrWhiteSpace(line))
  98. .ToList();
  99. var arr2 = config.tk_blacklist_regular.Split('\n')
  100. .Where(line => !string.IsNullOrWhiteSpace(line))
  101. .ToList();
  102. var arr3 = config.multi_token_regular.Split('\n')
  103. .Where(line => !string.IsNullOrWhiteSpace(line))
  104. .ToList();
  105. bool is_tao_url = !string.IsNullOrEmpty(result.shortLinkurl);
  106. bool is_tao_token = AlimamaPlus.MatchRegexes(content, arr);
  107. bool is_other = AlimamaPlus.MatchOtherInfo(content, arr2);
  108. //============================== 放弃转链-初步筛选1 ==============================
  109. if (!is_tao_url && !is_tao_token || is_other)
  110. {
  111. //没有链接都放弃
  112. result.success = false;
  113. result.message = "放弃转链";
  114. result.reason = "初步筛选1";
  115. result.subCode = TkSubCodeEnum.Prelim1;
  116. result.itemName = "";
  117. result.deeplink_url = string.Empty;
  118. _ = TkLogCore.ParseLogAsync(result);
  119. return TaobaoParseOutput(result, swData, new
  120. {
  121. result.success,
  122. result.commercial,
  123. result.message,
  124. result.content,
  125. result.itemName,
  126. other_aff,
  127. result.deeplink_url,
  128. });
  129. }
  130. bool is_multi_token = AlimamaPlus.MatchMultiToken(content, arr3);
  131. //============================== 放弃转链-初步筛选1.5 ==============================
  132. if (is_multi_token)
  133. {
  134. //没有链接都放弃
  135. result.success = false;
  136. result.message = "放弃转链";
  137. result.reason = "初步筛选1.5";
  138. result.subCode = TkSubCodeEnum.Prelim1_5;
  139. result.itemName = "";
  140. result.deeplink_url = string.Empty;
  141. _ = TkLogCore.ParseLogAsync(result);
  142. return TaobaoParseOutput(result, swData, new
  143. {
  144. result.success,
  145. result.commercial,
  146. result.message,
  147. result.content,
  148. result.itemName,
  149. other_aff,
  150. result.deeplink_url,
  151. });
  152. }
  153. //============================== 放弃转链-初步筛选2 ==============================
  154. bool is_aff_link = false;
  155. if (!string.IsNullOrEmpty(result.shortLinkurl)) is_aff_link = AlimamaPlus.IsAffLink(result.shortLinkurl);
  156. if (!is_tao_url && is_tao_token)
  157. {
  158. //淘宝口令
  159. result.success = false;
  160. result.message = "放弃转链";
  161. result.reason = "初步筛选2";
  162. result.subCode = TkSubCodeEnum.Prelim2;
  163. result.itemName = "点击打开淘宝APP";
  164. if (!result.success)
  165. {
  166. result.deeplink_url = string.Empty;
  167. }
  168. _ = TkLogCore.ParseLogAsync(result);
  169. //20241229 特例
  170. //if ("bj".Equals(EndPointCore.CurrentEndPoint))
  171. //{
  172. // string ruleKey = $"tmp_rules2:tb:{DateTime.Now:yyyyMMdd}";
  173. // var count = RedisHelper.Get<int>(ruleKey);
  174. // var now = DateTime.Now;
  175. // if (now > new DateTime(2024, 12, 19) && now <= new DateTime(2024, 12, 22) && now.Hour >= 7 && count < 1000)
  176. // {
  177. // result.deeplink_url = "tbopen://m.taobao.com/tbopen/index.html?source=auto&action=ali.open.nav&module=h5&bootImage=0&afc_route=1&bc_fl_src=growth_dhh_2200803434968_100-1930985-1595034112&dpa_Inid=159503411212096&dpa_material_id=530637422585&dpa_material_type=1&dpa_source_code=12096&force_no_smb=true&h5Url=https%3A%2F%2Fs.m.taobao.com%2Fh5%3Ffrom%3Dcustoms%26g_channelSrp%3Ddhhdpa_1232552832%26dpa_material_type%3D1%26ut_sk%3Dsearch%26spm%3Da2141.mb819t211c7%26sLaunch%3D0%26sKeep%3D1%26sModuleName%3Dsearch%26spm_back_up%3Da2141.mb819t211c7%26bc_fl_src%3Dgrowth_dhh_2200803434968_100-1930985-1595034112%26dpa_Inid%3D159503411212096%26dpa_material_id%3D530637422585%26dpa_material_type%3D1%26dpa_source_code%3D12096%26force_no_smb%3Dtrue%26itemIds%3D530637422585%26slk_actid%3D100000000207%26spm%3D2014.ugdhh.2200803434968.100-1930985-1595034112%26wh_biz%3Dtm&itemIds=530637422585&slk_actid=100000000207&spm=2014.ugdhh.2200803434968.100-1930985-1595034112&wh_biz=tm";
  178. // RedisHelper.IncrBy(ruleKey);
  179. // RedisHelper.Expire(ruleKey, 86400 * 1);
  180. // }
  181. //}
  182. other_aff = true;
  183. return TaobaoParseOutput(result, swData, new
  184. {
  185. result.success,
  186. result.commercial,
  187. result.message,
  188. result.content,
  189. result.itemName,
  190. other_aff,
  191. result.deeplink_url,
  192. });
  193. }
  194. if ((!is_tao_url && is_tao_token) || is_aff_link)
  195. {
  196. //没有短链接都放弃
  197. result.success = false;
  198. result.message = "放弃转链";
  199. result.reason = "初步筛选2";
  200. result.subCode = TkSubCodeEnum.Prelim2;
  201. result.itemName = "点击打开淘宝APP";
  202. if (!result.success)
  203. {
  204. result.deeplink_url = string.Empty;
  205. }
  206. _ = TkLogCore.ParseLogAsync(result);
  207. //20241229 特例
  208. //if ("bj".Equals(EndPointCore.CurrentEndPoint))
  209. //{
  210. // string ruleKey = $"tmp_rules2:tb:{DateTime.Now:yyyyMMdd}";
  211. // var count = RedisHelper.Get<int>(ruleKey);
  212. // var now = DateTime.Now;
  213. // if (now > new DateTime(2024, 12, 19) && now <= new DateTime(2024, 12, 22) && now.Hour >= 7 && count < 1000)
  214. // {
  215. // result.deeplink_url = "tbopen://m.taobao.com/tbopen/index.html?source=auto&action=ali.open.nav&module=h5&bootImage=0&afc_route=1&bc_fl_src=growth_dhh_2200803434968_100-1930985-1595034112&dpa_Inid=159503411212096&dpa_material_id=530637422585&dpa_material_type=1&dpa_source_code=12096&force_no_smb=true&h5Url=https%3A%2F%2Fs.m.taobao.com%2Fh5%3Ffrom%3Dcustoms%26g_channelSrp%3Ddhhdpa_1232552832%26dpa_material_type%3D1%26ut_sk%3Dsearch%26spm%3Da2141.mb819t211c7%26sLaunch%3D0%26sKeep%3D1%26sModuleName%3Dsearch%26spm_back_up%3Da2141.mb819t211c7%26bc_fl_src%3Dgrowth_dhh_2200803434968_100-1930985-1595034112%26dpa_Inid%3D159503411212096%26dpa_material_id%3D530637422585%26dpa_material_type%3D1%26dpa_source_code%3D12096%26force_no_smb%3Dtrue%26itemIds%3D530637422585%26slk_actid%3D100000000207%26spm%3D2014.ugdhh.2200803434968.100-1930985-1595034112%26wh_biz%3Dtm&itemIds=530637422585&slk_actid=100000000207&spm=2014.ugdhh.2200803434968.100-1930985-1595034112&wh_biz=tm";
  216. // RedisHelper.IncrBy(ruleKey);
  217. // RedisHelper.Expire(ruleKey, 86400 * 1);
  218. // }
  219. //}
  220. other_aff = true;
  221. return TaobaoParseOutput(result, swData, new
  222. {
  223. result.success,
  224. result.commercial,
  225. result.message,
  226. result.content,
  227. result.itemName,
  228. other_aff,
  229. result.deeplink_url,
  230. });
  231. }
  232. //那就初步筛选3 有tb.cn 并且有 or 手淘发现
  233. string pattern = @"^(?!【淘宝】).*https?:\/\/m\.tb\.cn.*(?:手淘发现|手淘搜索)";
  234. bool isMatch = Regex.IsMatch(content, pattern);
  235. if (isMatch)
  236. {
  237. result.success = false;
  238. result.message = "放弃转链";
  239. result.reason = "初步筛选3";
  240. result.subCode = TkSubCodeEnum.Prelim3;
  241. result.itemName = "点击打开淘宝APP";
  242. if (!result.success)
  243. {
  244. result.deeplink_url = string.Empty;
  245. }
  246. _ = TkLogCore.ParseLogAsync(result);
  247. return TaobaoParseOutput(result, swData, new
  248. {
  249. result.success,
  250. result.commercial,
  251. result.message,
  252. result.content,
  253. result.itemName,
  254. other_aff,
  255. result.deeplink_url,
  256. });
  257. }
  258. //============================== 放弃转链-地区过滤 ==============================
  259. bool is_ignore2 = AlimamaPlus.ShouldIgnoreRequest(ip, oaid, riskStrategy, launchScene, out reason);
  260. if (is_ignore2)
  261. {
  262. result.success = false;
  263. result.message = "放弃转链";
  264. result.itemName = "点击打开淘宝APP";
  265. result.reason = reason;
  266. result.subCode = TkSubCodeEnum.TrafficCtrl;
  267. if (!result.success)
  268. {
  269. result.deeplink_url = string.Empty;
  270. }
  271. _ = TkLogCore.ParseLogAsync(result);
  272. return TaobaoParseOutput(result, swData, new
  273. {
  274. result.success,
  275. result.commercial,
  276. result.message,
  277. result.content,
  278. result.itemName,
  279. other_aff,
  280. result.deeplink_url,
  281. });
  282. }
  283. (bool isTaobaoUrl, string url) = AlimamaPlus.IsTaobaoUrl(content);
  284. //============================== 放弃转链-没有匹配账号 ==============================
  285. TkPoolDTO? account = accountid > 0 ?
  286. await TkPoolCore.GetOneAsync(accountid) :
  287. await TkPoolCore.GetOneAsync(TkPoolCore.TkAction.parse, isTaobaoUrl, riskStrategy, launchScene, "dp");
  288. if (account == null)
  289. {
  290. result.success = false;
  291. result.message = "放弃转链";
  292. result.reason = "没有匹配账号";
  293. result.subCode = TkSubCodeEnum.Other;
  294. result.itemName = "点击打开淘宝APP";
  295. if (!result.success)
  296. {
  297. result.deeplink_url = string.Empty;
  298. }
  299. _ = TkLogCore.ParseLogAsync(result);
  300. return TaobaoParseOutput(result, swData, new
  301. {
  302. result.success,
  303. result.commercial,
  304. result.message,
  305. result.content,
  306. result.couponAmount,
  307. result.taoToken,
  308. result.shortLinkurl,
  309. other_aff,
  310. result.deeplink_url,
  311. });
  312. }
  313. result.accountId = account.id;
  314. result.accountName = account.company;
  315. alimama = new AlimamaPlus(account);
  316. int timeout = alimama._config.rt_max;
  317. #if DEBUG
  318. timeout = 10000;
  319. #endif
  320. using var cts = new CancellationTokenSource();
  321. cts.CancelAfter(timeout);
  322. var requestTask = Task.Run(() => alimama.UnionParseAsync(ip, oaid, content, commerceType, result, isTaobaoUrl, riskStrategy, launchScene, cts.Token, swData), cts.Token);
  323. var delayTask = Task.Delay(timeout, cts.Token);
  324. var completedTask = await Task.WhenAny(requestTask, delayTask);
  325. if (completedTask == requestTask)
  326. {
  327. result = await requestTask;
  328. }
  329. else
  330. {
  331. //============================== 放弃转链-请求超时 ==============================
  332. cts.Cancel();
  333. result.success = false;
  334. result.message = "放弃转链";
  335. result.reason = "请求超时";
  336. result.subCode = TkSubCodeEnum.Other;
  337. result.itemName = "点击打开淘宝APP";
  338. }
  339. result.accountName = account.company;
  340. //result = await alimama.UnionParse2Async(content, result);
  341. }
  342. catch (Exception ex)
  343. {
  344. //============================== 放弃转链-请求超时 ==============================
  345. if (ex.Message.Contains("was canceled"))
  346. {
  347. result.success = false;
  348. result.message = "放弃转链";
  349. result.reason = "请求超时";
  350. result.subCode = TkSubCodeEnum.Other;
  351. result.itemName = "点击打开淘宝APP";
  352. }
  353. else
  354. {
  355. //============================== 转链接口异常 ==============================
  356. _ = new LoggerLibrary("unionParse", "tb_error")
  357. .Info(ip, oaid)
  358. .Info(content)
  359. .Info(ex.Message, ex.StackTrace)
  360. .SaveAsync();
  361. NotifyCore.Notify(new NifyMessage
  362. {
  363. message = $"【转链异常TB】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
  364. priority = NifyMessagePriority.high,
  365. tags = ["red_circle"]
  366. });
  367. result.success = false;
  368. result.subCode = TkSubCodeEnum.Other;
  369. result.message = "转链接口异常";
  370. result.itemName = "点击打开淘宝APP";
  371. }
  372. }
  373. if (!result.success)
  374. {
  375. result.deeplink_url = string.Empty;
  376. }
  377. _ = TkLogCore.ParseLogAsync(result, alimama);
  378. switch (result.reason)
  379. {
  380. case "其他推广链接":
  381. //case "排除淘口令":
  382. case "纯口令 没链接":
  383. other_aff = true;
  384. break;
  385. }
  386. return TaobaoParseOutput(result, swData, new
  387. {
  388. result.success,
  389. result.commercial,
  390. result.message,
  391. link_type = result.link_type.ToString(),
  392. channel = result?.channel.ToString(),
  393. result.channel_type,
  394. result.itemId,
  395. result.itemName,
  396. result.pic,
  397. result.promotionPrice,
  398. result.couponAmount,
  399. result.couponEffectiveStartTime,
  400. result?.couponEffectiveEndTime,
  401. result.shortLinkurl,
  402. result.deeplink_url,
  403. result.riskStrategy,
  404. result.launchScene,
  405. result.content,
  406. result.taoToken,
  407. other_aff,
  408. });
  409. }
  410. public static async Task<APIResult> DeeplinkJdParseAsync(string content, int commerceType, string ip, string oaid,
  411. string riskStrategy = "", int launchScene = -1,
  412. int accountid = 0, string clickId = "", CancellationToken cancellationToken = default)
  413. {
  414. var config = TkConfigCore.Get();
  415. var result = JdUnionPlus.GetFormattedObject(content, ip, oaid, clickId);
  416. result.rawContent = content;
  417. result.parse_type = "dp";
  418. result.riskStrategy = riskStrategy;
  419. result.launchScene = launchScene;
  420. bool IfExceptional = false;
  421. try
  422. {
  423. var parseResult = parseJdDeeplinkInput(content);
  424. if (!parseResult.success)
  425. {
  426. result.channel = TkChannelEnum.jd;
  427. result.channel_type = ChannelTypeEnum.jd;
  428. result.link_type = LinkTypeEnum.other_aff;
  429. result.success = false;
  430. result.message = "放弃转链";
  431. result.reason = parseResult.reason;
  432. result.accountName = string.Empty;
  433. result.content = content;
  434. result.deeplink_url = string.Empty;
  435. _ = TkLogCore.ParseLogAsync(result, false);
  436. return JdParseOutput(result);
  437. }
  438. result.shortLinkurl = parseResult.url;
  439. result.content = parseResult.url;
  440. result.deeplink_url = string.Empty;
  441. content = parseResult.url;
  442. if (!string.IsNullOrEmpty(config.jd_blacklist_regular))
  443. {
  444. var blacklist = config.jd_blacklist_regular.Split('\n')
  445. .Where(line => !string.IsNullOrWhiteSpace(line))
  446. .ToList();
  447. bool anyMatch = false;
  448. foreach (string pattern in blacklist)
  449. {
  450. if (Regex.IsMatch(content, pattern, RegexOptions.IgnoreCase)) anyMatch = true;
  451. }
  452. if (anyMatch)
  453. {
  454. result.success = false;
  455. result.deeplink_url = string.Empty;
  456. result.message = "放弃转链";
  457. result.reason = "规则排除";
  458. _ = TkLogCore.ParseLogAsync(result, false);
  459. return JdParseOutput(result);
  460. }
  461. }
  462. string out_url = null;
  463. string itemId = null;
  464. //============================== 放弃转链-地区过滤 ==============================
  465. if (!TestParseCore.InWhitelist(result.ip, result.oaid) && accountid == 0 && JdUnionPlus.ShouldIgnoreRequest(config, ip, oaid, riskStrategy, launchScene, out string reason))
  466. {
  467. result.link_type = JdUnionPlus.GetLinkType(result.shortLinkurl, out out_url, out itemId);
  468. result.deeplink_url = result.link_type switch
  469. {
  470. LinkTypeEnum.other_aff or
  471. LinkTypeEnum.live or
  472. LinkTypeEnum.video or
  473. LinkTypeEnum.profile or
  474. LinkTypeEnum.goods => JdUnionPlus.GetDefaultStyleDeeplink(result.shortLinkurl),
  475. _ => JdUnionPlus.GetDefaultStyleDeeplink(result.shortLinkurl),
  476. };
  477. result.success = false;
  478. result.message = "放弃转链";
  479. result.reason = reason;
  480. if (!result.success)
  481. {
  482. result.deeplink_url = string.Empty;
  483. }
  484. _ = TkLogCore.ParseLogAsync(result, false);
  485. return JdParseOutput(result);
  486. }
  487. result.shortLinkurl = JdUnionPlus.GetLink(content);
  488. result.content = result.shortLinkurl;
  489. result.link_type = JdUnionPlus.GetLinkType(result.shortLinkurl, out out_url, out itemId);
  490. bool is_numeric = result.shortLinkurl.Contains(itemId);
  491. var account = accountid > 0 ? await JdPoolCore.GetOneAsync(accountid, JdPoolCore.JdAction.parse) : await JdPoolCore.GetOneAsync(JdPoolCore.JdAction.parse, riskStrategy, launchScene, is_numeric, "dp");
  492. if (account == null)
  493. {
  494. result.success = false;
  495. result.message = "放弃转链";
  496. result.reason = "没有匹配账号";
  497. result.deeplink_url = result.link_type switch
  498. {
  499. LinkTypeEnum.other_aff or LinkTypeEnum.goods => JdUnionPlus.GetDefaultStyleDeeplink(result.shortLinkurl),
  500. _ => JdUnionPlus.GetDefaultStyleDeeplink(result.shortLinkurl),
  501. };
  502. if (!result.success)
  503. {
  504. result.deeplink_url = string.Empty;
  505. }
  506. _ = TkLogCore.ParseLogAsync(result, false);
  507. return JdParseOutput(result);
  508. }
  509. result.accountId = account.id;
  510. result.accountName = account.name;
  511. var plus = new JdUnionPlus(account);
  512. result.proxy_name = plus._proxyName;
  513. result.proxy_node = plus._proxy?.Address?.Host;
  514. result = await plus.JdParseAsync(content, commerceType, clickId, result, cancellationToken);
  515. }
  516. catch (Exception ex)
  517. {
  518. if (ex.Message.Contains("was canceled"))
  519. {
  520. result.success = false;
  521. result.message = "放弃转链";
  522. result.reason = "请求超时";
  523. }
  524. else
  525. {
  526. IfExceptional = true;
  527. _ = new LoggerLibrary("unionParse", "jd_error")
  528. .Info(ip, oaid)
  529. .Info(content)
  530. .Info(ex.Message, ex.StackTrace)
  531. .SaveAsync();
  532. NotifyCore.Notify(new NifyMessage
  533. {
  534. message = $"【转链异常JD】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
  535. priority = NifyMessagePriority.high,
  536. tags = ["red_circle"]
  537. });
  538. result.success = false;
  539. result.message = "内部错误";
  540. result.reason = "转链接口异常";
  541. }
  542. result.deeplink_url = result.link_type switch
  543. {
  544. LinkTypeEnum.other_aff or LinkTypeEnum.goods => JdUnionPlus.GetDefaultStyleDeeplink(result.shortLinkurl),
  545. _ => JdUnionPlus.GetDefaultStyleDeeplink(result.shortLinkurl),
  546. };
  547. }
  548. if (!result.success)
  549. {
  550. result.deeplink_url = string.Empty;
  551. }
  552. _ = TkLogCore.ParseLogAsync(result, false);
  553. return JdParseOutput(result, IfExceptional ? APIResultCodeEnum.NotAcceptable : APIResultCodeEnum.OK);
  554. }
  555. public static async Task<APIResult> DeeplinkPddParseAsync(string content, int commerceType, string ip, string oaid,
  556. int accountid = 0, CancellationToken cancellationToken = default)
  557. {
  558. var result = PddUnionPlus.GetFormattedObject(content, ip, oaid);
  559. content = content.UrlDecode();
  560. result.rawContent = content;
  561. result.parse_type = "dp";
  562. bool IfExceptional = false;
  563. try
  564. {
  565. var config = TkConfigCore.Get();
  566. if (!string.IsNullOrEmpty(config.pdd_blacklist_regular))
  567. {
  568. var blacklist = config.pdd_blacklist_regular.Split('\n')
  569. .Where(line => !string.IsNullOrWhiteSpace(line))
  570. .ToList();
  571. bool anyMatch = false;
  572. foreach (string pattern in blacklist)
  573. {
  574. if (Regex.IsMatch(content, pattern, RegexOptions.IgnoreCase)) anyMatch = true;
  575. }
  576. if (anyMatch)
  577. {
  578. result.success = false;
  579. result.deeplink_url = string.Empty;
  580. result.message = "放弃转链";
  581. result.reason = "规则排除";
  582. _ = TkLogCore.ParseLogAsync(result);
  583. return PddParseOutput(result);
  584. }
  585. }
  586. //============================== 放弃转链-地区过滤 ==============================
  587. if (accountid == 0 && PddUnionPlus.ShouldIgnoreRequest(config, ip, oaid, out string reason))
  588. {
  589. result.success = false;
  590. result.message = "放弃转链";
  591. result.reason = reason;
  592. result.deeplink_url = PddUnionPlus.GetDeeplink(result.rawContent);
  593. _ = TkLogCore.ParseLogAsync(result);
  594. return PddParseOutput(result);
  595. }
  596. PddPoolDTO account = null;
  597. string lockKey;
  598. long cis_num = 0;
  599. for (int i = 0; i < 3; i++)
  600. {
  601. for (int j = 0; j < 3; j++)
  602. {
  603. account = PddPoolCore.GetOne(PddUnionWorkMode.All, accountid, "dp");
  604. if (account == null) continue;
  605. if (account.cis_limit > 0)
  606. {
  607. lockKey = $"pdd_cis_limit_{result.accountId}";
  608. cis_num = RedisHelper.Get<long>(lockKey);
  609. if (cis_num > 0) continue;
  610. }
  611. break;
  612. }
  613. if (account == null)
  614. {
  615. result.success = false;
  616. result.message = "放弃转链";
  617. result.reason = "没有匹配账号";
  618. result.deeplink_url = PddUnionPlus.GetDeeplink(result.rawContent);
  619. _ = TkLogCore.ParseLogAsync(result);
  620. return PddParseOutput(result);
  621. }
  622. result.accountId = account.id;
  623. result.accountName = account.name;
  624. result.proxy_node = account.nodeName;
  625. if (account.cis_limit > 0)
  626. {
  627. // 更新当前分钟的使用统计
  628. PddPoolCore.UpdateAccountUsage(account.id);
  629. lockKey = $"pdd_cis_limit_{result.accountId}";
  630. cis_num = RedisHelper.IncrBy(lockKey);
  631. if (cis_num > 1) continue;
  632. RedisHelper.Expire(lockKey, TimeSpan.FromMilliseconds(account.cis_limit));
  633. }
  634. break;
  635. }
  636. if (account == null)
  637. {
  638. result.success = false;
  639. result.message = "放弃转链";
  640. result.reason = "没有匹配账号";
  641. result.deeplink_url = PddUnionPlus.GetDeeplink(result.rawContent);
  642. _ = TkLogCore.ParseLogAsync(result);
  643. return PddParseOutput(result);
  644. }
  645. if (account?.cis_limit > 0)
  646. {
  647. lockKey = $"pdd_cis_limit_{result.accountId}";
  648. cis_num = RedisHelper.Get<int>(lockKey);
  649. if (cis_num > 1)
  650. {
  651. result.success = false;
  652. result.message = "放弃转链";
  653. result.reason = $"没有匹配账号{cis_num}";
  654. result.deeplink_url = PddUnionPlus.GetDeeplink(result.rawContent);
  655. _ = TkLogCore.ParseLogAsync(result);
  656. return PddParseOutput(result);
  657. }
  658. }
  659. var plus = new PddUnionPlus(account);
  660. result = await plus.PddParseAsync(content, commerceType, result, cancellationToken);
  661. }
  662. catch (Exception ex)
  663. {
  664. if (ex.Message.Contains("was canceled"))
  665. {
  666. result.success = false;
  667. result.message = "放弃转链";
  668. result.reason = "请求超时";
  669. }
  670. else
  671. {
  672. IfExceptional = true;
  673. _ = new LoggerLibrary("unionParse", "pdd_error")
  674. .Info(ip, oaid)
  675. .Info(content)
  676. .Info(ex.Message, ex.StackTrace)
  677. .SaveAsync();
  678. NotifyCore.Notify(new NifyMessage
  679. {
  680. message = $"【转链异常PDD】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
  681. priority = NifyMessagePriority.high,
  682. tags = ["red_circle"]
  683. });
  684. result.success = false;
  685. result.message = "内部错误";
  686. result.reason = "转链接口异常";
  687. }
  688. }
  689. _ = TkLogCore.ParseLogAsync(result);
  690. return PddParseOutput(result, IfExceptional ? APIResultCodeEnum.NotAcceptable : APIResultCodeEnum.OK);
  691. }
  692. private static ParseDeeplinkResust parseJdDeeplinkInput(string content)
  693. {
  694. ParseDeeplinkResust resust = new();
  695. if (!content.StartsWith("openapp.jdmobile://"))
  696. {
  697. resust.success = false;
  698. resust.reason = "不是有效DP";
  699. return resust;
  700. }
  701. try
  702. {
  703. string param = content.GetContentPart("params=", "&");
  704. if (string.IsNullOrEmpty(param))
  705. {
  706. param = content.GetContentPart("params=", "");
  707. }
  708. param = param.UrlDecode();
  709. var root = param.Convert2JsonElement();
  710. string category = root.Read("category", string.Empty);
  711. string sourceType = root.Read("sourceType", string.Empty);
  712. string skuId = root.Read("skuId", string.Empty);
  713. string url = root.Read("url", string.Empty);
  714. string des = root.Read("des", string.Empty);
  715. switch (sourceType)
  716. {
  717. case "Item":
  718. resust.url = $"https://item.jd.com/{skuId}.html";
  719. resust.success = true;
  720. return resust;
  721. }
  722. switch (category)
  723. {
  724. case "jump":
  725. switch (des)
  726. {
  727. case "getCoupon":
  728. case "union":
  729. resust.success = false;
  730. resust.reason = "其他推广链接";
  731. return resust;
  732. case "m":
  733. resust.url = url;
  734. break;
  735. case "productDetail":
  736. resust.success = true;
  737. resust.url = $"https://item.jd.com/{skuId}.html";
  738. return resust;
  739. }
  740. break;
  741. }
  742. if (string.IsNullOrEmpty(url))
  743. {
  744. resust.success = false;
  745. resust.reason = "未知链接";
  746. }
  747. string utm_campaign = resust.url.GetContentPart("utm_campaign=", "&");
  748. string utm_term = resust.url.GetContentPart("utm_term=", "&");
  749. if (url.Contains("union-click.jd.com") || url.Contains("union-activity") ||
  750. !string.IsNullOrEmpty(utm_campaign) || !string.IsNullOrEmpty(utm_term))
  751. {
  752. resust.success = false;
  753. resust.reason = "其他推广链接";
  754. return resust;
  755. }
  756. resust.success = true;
  757. return resust;
  758. }
  759. catch (Exception ex)
  760. {
  761. resust.success = false;
  762. resust.reason = "内部错误";
  763. return resust;
  764. }
  765. }
  766. private static ParseDeeplinkResust parseTaobaoDeeplinkInput(string content)
  767. {
  768. ParseDeeplinkResust resust = new();
  769. if (!content.StartsWith("taobao://") && !content.StartsWith("tbopen://") && !content.StartsWith("tmall://"))
  770. {
  771. resust.success = false;
  772. resust.reason = "不是有效DP";
  773. return resust;
  774. }
  775. if (content.Contains("ali_trackid="))
  776. {
  777. resust.success = false;
  778. resust.reason = "其他推广链接";
  779. return resust;
  780. }
  781. string itemid_pattern = "(?:pc_detail\\.itemId\\.\\d+&id=|(?:detail|item).*?[?&]id=)(\\d+)";
  782. //https://h5.m.taobao.com/awp/core/detail.htm?id=951950415328
  783. //taobao://item.taobao.com/item.htm?id=950190032209
  784. //https://main.m.taobao.com/detail/index.html?id=908945171430&price=148.9&sourceType=item&suid=9a9921f6-5610-497c-9bb3-94427da5fbc0&ut_sk=1.aFUBza6VAN8DAOC0tQdyzNek_21646297_1753594384153.Copy.ShareGlobalNavigation_1&un=8ad28c612754fb28d9e5b32ae03d1ada&share_crt_v=1&un_site=0&spm=a2159r.13376460.0.0&tbSocialPopKey=shareItem&sp_tk=MnluVjQ0N2tDUXU%3D&cpp=1&shareurl=true&short_name=h.hOFywxY0R9ZuBR4&bxsign=scduUbJTmvx63UiuFdNYy6FDvPx0iaKQu9uyc1zfURgLpCZqHIO1Aqj8fgZ6_ZUy-rJeS1ZH3PiAGcemxyFqpeN5BTKnDbDuU77d6sqWD2TVok-8K3nspP6YVfAZBvzfx7WLVAMu3X6119Ru_1ooSt9ug&tk=2ynV447kCQu&app=chrome&x-ssr=true&slk_gid=gid_er_sidebar_0&action=ali.open.nav&module=h5&bootImage=0&slk_sid=2nf7IO8XhWICAd9KM5hOtK8k_1753783603680&slk_t=1753783605292&slk_gid=gid_er_sidebar_0&afcPromotionOpen=false&bc_fl_src=h5_huanduan&source=slk_dp
  785. //https://main.m.taobao.com/detail/index.html?id=937709627060&price=208.9&sourceType=item&suid=48a4d791-127c-45d7-b580-a63097897697&ut_sk=1.aFUBza6VAN8DAOC0tQdyzNek_21646297_1753594384153.Copy.ShareGlobalNavigation_1&un=8ad28c612754fb28d9e5b32ae03d1ada&share_crt_v=1&un_site=0&spm=a2159r.13376460.0.0&tbSocialPopKey=shareItem&sp_tk=SG42aTQ0NzFMc3Y%3D&cpp=1&shareurl=true&short_name=h.hOCuiAd32dv9wBK&bxsign=scd0KcwU22FkrzJ77bVvMLnP0u4_5wpLz1ERfC-eCQE1wk21BYNMuT-w0I07gisSMdabKmlWh9F6DwW5DXLagikYvHjSD7Eh5n2n6kmyw1aF-dZWuqzGkI9Z-AHw-rffwjrPx1cXEsQ35HqOwmmkMEiYw&tk=Hn6i4471Lsv&app=chrome&x-ssr=true&slk_gid=gid_er_sidebar_0&action=ali.open.nav&module=h5&bootImage=0&slk_sid=580NIcIpZVQBASQOANYD2H43_1753783601070&slk_t=1753783605251&slk_gid=gid_er_sidebar_0&afcPromotionOpen=false&bc_fl_src=h5_huanduan&source=slk_dp
  786. //https://main.m.taobao.com/detail/index.html?spm=a224e.7913437.1.1849716&id=672205270263&sku_properties=210158085%3A170720673&_bind=true&bc_fl_src=tmall_market_llb_1_1849716&llbPlatform=pc&agentId=1685089&llbIsd=1&bxsign=llbirWpI8yJlJLibTbWOXuYbuykv0WCTORO2Hj7JqVtdGau62UJiz2xO%2FZSIjdwbOudIUUsdN51H41YisrOLI53ZMhblPS9ZMyVhFDisOySHmk%3D&ntfMsId=65df6cbb682a0779f23141a7efd9c391&llbOsd=1&mm_unid=1_11192113_560501015e6d5754040c0255026f5353086656040b075e&jlogid=0718172753b3bfbd&x-ssr=true&slk_gid=gid_er_sidebar_0&bc_fl_src=tmall_market_llb_1_1849716&action=ali.open.nav&module=h5&bootImage=0&slk_sid=rnd07d9b9_1753784332640&slk_t=1753784332860&slk_gid=gid_er_sidebar_0&afcPromotionOpen=false&source=slk_dp
  787. var regex = new Regex(itemid_pattern);
  788. var match = regex.Match(content);
  789. if (match.Success)
  790. {
  791. string itemId = match.Groups[1].Value;
  792. string item_url = $"https://item.taobao.com/item.htm?id={itemId}";
  793. resust.success = true;
  794. resust.url = item_url;
  795. return resust;
  796. }
  797. /*
  798. * tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&source=alimama&bc_fl_src=tunion_vipmedia_sy&h5Url=https%3A%2F%2Fs.click.taobao.com%2Ff8sLf2s
  799. * tbopen://m.taobao.com/tbopen/index.html?h5Url=https%3A%2F%2Fmain.m.taobao.com%2Fsearch%2Findex.html%3Fspm%3Da215s.7406091.topbar.1.560c6770snz1OF%26pageType%3D3%26q%3D%25E6%2598%259F%25E8%25BF%2590%25E5%258D%25A1
  800. *
  801. tbopen://m.taobao.com/tbopen/index.html?h5Url=https%3A%2F%2Fmain.m.taobao.com%2Fsearch%2Findex.html%3Fspm%3Da215s.7406091.topbar.1.560c6770snz1OF%26pageType%3D3%26q%3D%25E9%25A1%25BA%25E6%2597%25BA%25E5%258D%25A1
  802. tbopen://m.taobao.com/tbopen/index.html?h5Url=https%3A%2F%2Fku.m.taobao.com%2Fmix.htm%3Fdf_sid%3D215f61a7f2d0d7006883872e0fa2df3d%26_oneId%3DBD00ECC981654638911F62B6CCF30F30C261E65C5A593BD4E4C78E7F0B05AA0B%26kn%3D521909AA62D8712AA77BD65C878A07BF59834B3D18D1D24D2C0E947EC545D6EA%26package_name%3D2%2FYVzIkqDv9cGePpY%2FRKjGIB7bMMF5twBLhjEiOCk9g%26refpid%3Dmm_26632268_153800213_74441300498%26itemid%3D646763119270%26adgroupid%3D74846357352%26catid%3D1801%26adsrc%3D2%26tk%3D1.1_rUEipiu4lQnCUAcGljdHYD4C6l8ikkERn4TzMch3-OXcoJ3I2L9ZMNENrcyuEA6d%26bc_fl_src%3Dtanx_df_74441300498_215f61a7f2d0d7006883872e0fa2df3d_0_646763119270_74846357352_74867406695_0_1_0_2_0_0_2211829026650_0_646763119270_0_0_0_0_1%26mip%3DXmhaREh7ZFVhSH13Xg%26pdiv%3Dqt3%26project_id%3D1%26ali_trackid%3D158_471853809d7b199af8af80e276b7edb9%26bxsign%3DtanTcJ_vnwBjiXMx6dXdbwNtN0NYe4Dxw8jdMFUfthOcBEBDMKekKKeyFJVBC1HN90EhBu4bnt877-6MRM7VMJfXPrwh_1B7nUf6olHer4TLXU%26slk_gid%3Dgid_er_sidebar_0%26tkSid%3D1752753038137_556550289_0.0%26tk_cps_ut%3D12%26e%3DEb2ey4TKM3Zwv7nGTZ2_-rX3DTSVrV-TdPW7X9DGPerTBdT1uMUzpLB4tW1Ia-SYSzILWu-4Ylw1xiku1Ij7RdZ9rtbmhnxUlYC60BGXgC5f6ehAkwDioKUmn1Ujh2ntIbOKDm8SGGXFqv6GieJi2KXc4rofFFSJZ6rS0GtzoCFGKDxAFeCMxblh5_iK7tdUOCg-a4qwrTN2_AL2y2LOy60-b68KW7cRvO7dXGStqJAigOjjYrz2j5MUyWjCKPHVoZD9rN1I0mU8VGta6ZddcKfRgMu9M8FAVWx6XRy23oSLkoMYS_fbzlZlGzzVHt-JtOrywR95lxHccBzKc7kLgBvZbuLTRODDNWlsmyjpbuya2Ddv0ls-v1Y0FiUPJBa3VjcpA4WsRFam6zFndQ3HZaZPpBJnnE487qeUylWc6TSX3cwyLTlAhlg4CzRoOGUPfG2M1DxYVhIM-7XZOBKdtm1-MNutVMsdVL5FrN2D3QFIHauVi2R1R4T_8p273pSzV1XCG8Ub_zp6czCzvmBAnuTHBUPfjEfH31tdnUQbdFwwPrn00QwN2w%26type%3D2%26tkFlag%3D1&bc_fl_src=tanx_df_74441300498_215f61a7f2d0d7006883872e0fa2df3d_0_646763119270_74846357352_74867406695_0_1_0_2_0_0_2211829026650_0_646763119270_0_0_0_0_1&action=ali.open.nav&module=h5&bootImage=0&slk_sid=j9H/IH/fa3kBASQJiigVFV2u_1753450292601&slk_t=1753450293764&slk_gid=gid_er_sidebar_0&afcPromotionOpen=false&ali_trackid=158_471853809d7b199af8af80e276b7edb9&source=slk_dp
  803. */
  804. string url = content.GetContentPart("h5Url=", "&");
  805. if (string.IsNullOrEmpty(url))
  806. {
  807. url = content.GetContentPart("h5Url=", "");
  808. }
  809. url = url.UrlDecode();
  810. if (string.IsNullOrEmpty(url))
  811. {
  812. resust.success = false;
  813. resust.reason = "未知链接";
  814. return resust;
  815. }
  816. if (AlimamaPlus.IsAffLink(url))
  817. {
  818. resust.success = false;
  819. resust.reason = "其他推广链接";
  820. return resust;
  821. }
  822. if (url.Contains("ali_trackid="))
  823. {
  824. resust.success = false;
  825. resust.reason = "其他推广链接";
  826. return resust;
  827. }
  828. if (AlimamaPlus.IsShortLink(url))
  829. {
  830. resust.success = true;
  831. resust.url = url;
  832. return resust;
  833. }
  834. regex = new Regex(itemid_pattern);
  835. match = regex.Match(url);
  836. if (match.Success)
  837. {
  838. string itemId = match.Groups[1].Value;
  839. string item_url = $"https://item.taobao.com/item.htm?id={itemId}";
  840. resust.success = true;
  841. resust.url = item_url;
  842. return resust;
  843. }
  844. resust.success = false;
  845. resust.reason = "未知链接";
  846. return resust;
  847. }
  848. private static string parseDeeplinkInput(string content)
  849. {
  850. if (content.StartsWith("tbopen://") || content.StartsWith("tmall://"))
  851. {
  852. string url = content.GetContentPart("h5Url=", "&");
  853. if (string.IsNullOrEmpty(url))
  854. {
  855. url = content.GetContentPart("h5Url=", "");
  856. }
  857. if (string.IsNullOrEmpty(url)) return content;
  858. content = url.UrlDecode();
  859. }
  860. if (content.StartsWith("openapp.jdmobile://"))
  861. {
  862. try
  863. {
  864. string param = content.GetContentPart("params=", "");
  865. param = param.UrlDecode();
  866. var root = param.Convert2JsonElement();
  867. string category = root.Read("category", string.Empty);
  868. string sourceType = root.Read("sourceType", string.Empty);
  869. string skuId = root.Read("skuId", string.Empty);
  870. switch (sourceType)
  871. {
  872. case "Item":
  873. string url_content = $"https://item.jd.com/{skuId}.html";
  874. return url_content;
  875. }
  876. switch (category)
  877. {
  878. case "jump":
  879. string url = root.Read("url", string.Empty);
  880. string url_content = url;
  881. return url_content;
  882. }
  883. }
  884. catch (Exception ex)
  885. {
  886. }
  887. }
  888. return content;
  889. }
  890. }
  891. }