dp2dp.cs 48 KB

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