dp2dp.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. account = PddPoolCore.GetOne(PddUnionWorkMode.All, accountid, "dp");
  602. if (account == null) continue;
  603. if (account.cis_limit > 0)
  604. {
  605. lockKey = $"pdd_cis_limit_{result.accountId}";
  606. cis_num = RedisHelper.Get<long>(lockKey);
  607. if (cis_num > 0) continue;
  608. }
  609. break;
  610. }
  611. if (account == null)
  612. {
  613. result.success = false;
  614. result.message = "放弃转链";
  615. result.reason = "没有匹配账号";
  616. result.deeplink_url = PddUnionPlus.GetDeeplink(result.rawContent);
  617. _ = TkLogCore.ParseLogAsync(result);
  618. return PddParseOutput(result);
  619. }
  620. result.accountId = account.id;
  621. result.accountName = account.name;
  622. result.proxy_node = account.nodeName;
  623. if (account.cis_limit > 0)
  624. {
  625. // 更新当前分钟的使用统计
  626. PddPoolCore.UpdateAccountUsage(account.id);
  627. lockKey = $"pdd_cis_limit_{result.accountId}";
  628. cis_num = RedisHelper.IncrBy(lockKey);
  629. if (cis_num > 1)
  630. {
  631. result.success = false;
  632. result.message = "放弃转链";
  633. result.reason = $"没有匹配账号{cis_num}";
  634. result.deeplink_url = PddUnionPlus.GetDeeplink(result.rawContent);
  635. _ = TkLogCore.ParseLogAsync(result);
  636. return PddParseOutput(result);
  637. }
  638. RedisHelper.Expire(lockKey, TimeSpan.FromMilliseconds(account.cis_limit));
  639. }
  640. var plus = new PddUnionPlus(account);
  641. result = await plus.PddParseAsync(content, commerceType, result, cancellationToken);
  642. }
  643. catch (Exception ex)
  644. {
  645. if (ex.Message.Contains("was canceled"))
  646. {
  647. result.success = false;
  648. result.message = "放弃转链";
  649. result.reason = "请求超时";
  650. }
  651. else
  652. {
  653. IfExceptional = true;
  654. _ = new LoggerLibrary("unionParse", "pdd_error")
  655. .Info(ip, oaid)
  656. .Info(content)
  657. .Info(ex.Message, ex.StackTrace)
  658. .SaveAsync();
  659. NotifyCore.Notify(new NifyMessage
  660. {
  661. message = $"【转链异常PDD】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
  662. priority = NifyMessagePriority.high,
  663. tags = ["red_circle"]
  664. });
  665. result.success = false;
  666. result.message = "内部错误";
  667. result.reason = "转链接口异常";
  668. }
  669. }
  670. _ = TkLogCore.ParseLogAsync(result);
  671. return PddParseOutput(result, IfExceptional ? APIResultCodeEnum.NotAcceptable : APIResultCodeEnum.OK);
  672. }
  673. private static ParseDeeplinkResust parseJdDeeplinkInput(string content)
  674. {
  675. ParseDeeplinkResust resust = new();
  676. if (!content.StartsWith("openapp.jdmobile://"))
  677. {
  678. resust.success = false;
  679. resust.reason = "不是有效DP";
  680. return resust;
  681. }
  682. try
  683. {
  684. string param = content.GetContentPart("params=", "&");
  685. if (string.IsNullOrEmpty(param))
  686. {
  687. param = content.GetContentPart("params=", "");
  688. }
  689. param = param.UrlDecode();
  690. var root = param.Convert2JsonElement();
  691. string category = root.Read("category", string.Empty);
  692. string sourceType = root.Read("sourceType", string.Empty);
  693. string skuId = root.Read("skuId", string.Empty);
  694. string url = root.Read("url", string.Empty);
  695. string des = root.Read("des", string.Empty);
  696. switch (sourceType)
  697. {
  698. case "Item":
  699. resust.url = $"https://item.jd.com/{skuId}.html";
  700. resust.success = true;
  701. return resust;
  702. }
  703. switch (category)
  704. {
  705. case "jump":
  706. switch (des)
  707. {
  708. case "getCoupon":
  709. case "union":
  710. resust.success = false;
  711. resust.reason = "其他推广链接";
  712. return resust;
  713. case "m":
  714. resust.url = url;
  715. break;
  716. case "productDetail":
  717. resust.success = true;
  718. resust.url = $"https://item.jd.com/{skuId}.html";
  719. return resust;
  720. }
  721. break;
  722. }
  723. if (string.IsNullOrEmpty(url))
  724. {
  725. resust.success = false;
  726. resust.reason = "未知链接";
  727. }
  728. string utm_campaign = resust.url.GetContentPart("utm_campaign=", "&");
  729. string utm_term = resust.url.GetContentPart("utm_term=", "&");
  730. if (url.Contains("union-click.jd.com") || url.Contains("union-activity") ||
  731. !string.IsNullOrEmpty(utm_campaign) || !string.IsNullOrEmpty(utm_term))
  732. {
  733. resust.success = false;
  734. resust.reason = "其他推广链接";
  735. return resust;
  736. }
  737. resust.success = true;
  738. return resust;
  739. }
  740. catch (Exception ex)
  741. {
  742. resust.success = false;
  743. resust.reason = "内部错误";
  744. return resust;
  745. }
  746. }
  747. private static ParseDeeplinkResust parseTaobaoDeeplinkInput(string content)
  748. {
  749. ParseDeeplinkResust resust = new();
  750. if (!content.StartsWith("taobao://") && !content.StartsWith("tbopen://") && !content.StartsWith("tmall://"))
  751. {
  752. resust.success = false;
  753. resust.reason = "不是有效DP";
  754. return resust;
  755. }
  756. if (content.Contains("ali_trackid="))
  757. {
  758. resust.success = false;
  759. resust.reason = "其他推广链接";
  760. return resust;
  761. }
  762. string itemid_pattern = "(?:pc_detail\\.itemId\\.\\d+&id=|(?:detail|item).*?[?&]id=)(\\d+)";
  763. //https://h5.m.taobao.com/awp/core/detail.htm?id=951950415328
  764. //taobao://item.taobao.com/item.htm?id=950190032209
  765. //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
  766. //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
  767. //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
  768. var regex = new Regex(itemid_pattern);
  769. var match = regex.Match(content);
  770. if (match.Success)
  771. {
  772. string itemId = match.Groups[1].Value;
  773. string item_url = $"https://item.taobao.com/item.htm?id={itemId}";
  774. resust.success = true;
  775. resust.url = item_url;
  776. return resust;
  777. }
  778. /*
  779. * 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
  780. * 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
  781. *
  782. 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
  783. 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
  784. */
  785. string url = content.GetContentPart("h5Url=", "&");
  786. if (string.IsNullOrEmpty(url))
  787. {
  788. url = content.GetContentPart("h5Url=", "");
  789. }
  790. url = url.UrlDecode();
  791. if (string.IsNullOrEmpty(url))
  792. {
  793. resust.success = false;
  794. resust.reason = "未知链接";
  795. return resust;
  796. }
  797. if (AlimamaPlus.IsAffLink(url))
  798. {
  799. resust.success = false;
  800. resust.reason = "其他推广链接";
  801. return resust;
  802. }
  803. if (url.Contains("ali_trackid="))
  804. {
  805. resust.success = false;
  806. resust.reason = "其他推广链接";
  807. return resust;
  808. }
  809. if (AlimamaPlus.IsShortLink(url))
  810. {
  811. resust.success = true;
  812. resust.url = url;
  813. return resust;
  814. }
  815. regex = new Regex(itemid_pattern);
  816. match = regex.Match(url);
  817. if (match.Success)
  818. {
  819. string itemId = match.Groups[1].Value;
  820. string item_url = $"https://item.taobao.com/item.htm?id={itemId}";
  821. resust.success = true;
  822. resust.url = item_url;
  823. return resust;
  824. }
  825. resust.success = false;
  826. resust.reason = "未知链接";
  827. return resust;
  828. }
  829. private static string parseDeeplinkInput(string content)
  830. {
  831. if (content.StartsWith("tbopen://") || content.StartsWith("tmall://"))
  832. {
  833. string url = content.GetContentPart("h5Url=", "&");
  834. if (string.IsNullOrEmpty(url))
  835. {
  836. url = content.GetContentPart("h5Url=", "");
  837. }
  838. if (string.IsNullOrEmpty(url)) return content;
  839. content = url.UrlDecode();
  840. }
  841. if (content.StartsWith("openapp.jdmobile://"))
  842. {
  843. try
  844. {
  845. string param = content.GetContentPart("params=", "");
  846. param = param.UrlDecode();
  847. var root = param.Convert2JsonElement();
  848. string category = root.Read("category", string.Empty);
  849. string sourceType = root.Read("sourceType", string.Empty);
  850. string skuId = root.Read("skuId", string.Empty);
  851. switch (sourceType)
  852. {
  853. case "Item":
  854. string url_content = $"https://item.jd.com/{skuId}.html";
  855. return url_content;
  856. }
  857. switch (category)
  858. {
  859. case "jump":
  860. string url = root.Read("url", string.Empty);
  861. string url_content = url;
  862. return url_content;
  863. }
  864. }
  865. catch (Exception ex)
  866. {
  867. }
  868. }
  869. return content;
  870. }
  871. }
  872. }