JdUnionPlus.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Dataoke;
  10. using System.Text.RegularExpressions;
  11. using Org.BouncyCastle.Ocsp;
  12. using System.Diagnostics;
  13. using TencentCloud.Fmu.V20191213.Models;
  14. using Microsoft.VisualBasic;
  15. using System.Text.Json;
  16. using static System.Runtime.InteropServices.JavaScript.JSType;
  17. using System.Security.Cryptography;
  18. using Google.Protobuf.WellKnownTypes;
  19. using Microsoft.Extensions.FileSystemGlobbing.Internal;
  20. using System.Web;
  21. namespace molilian.core
  22. {
  23. public partial class JdUnionPlus
  24. {
  25. private const string VIDEO_URL_PREFIX = "https://h5.m.jd.com/active/faxian/video/index.html";
  26. public static string GetLink(string content)
  27. {
  28. if (string.IsNullOrEmpty(content)) return content;
  29. string result = content;
  30. //string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  31. Regex regex = new(AlimamaPlus._url_pattern);
  32. MatchCollection matches = regex.Matches(content);
  33. foreach (Match m in matches.Cast<Match>())
  34. {
  35. string url = m.Value;
  36. return url;
  37. }
  38. return string.Empty;
  39. }
  40. public static string GetItemId(string url)
  41. {
  42. if (string.IsNullOrEmpty(url)) return string.Empty;
  43. string[] patterns = [
  44. @"https?://(?:i-)?item(?:\.m)?\.jd\.com/(?:product/)?(\d+)\.html(?:\?.*)?"
  45. //@"https?:\/\/b2b\.m\.jd\.com\/goods-detail\/.*[?&]skuId=(\d+)"
  46. ];
  47. foreach (var pattern in patterns)
  48. {
  49. Match match = Regex.Match(url, pattern);
  50. if (match.Success)
  51. {
  52. string idNumber = match.Groups[1].Value; // 捕获组1包含数字ID
  53. return idNumber;
  54. }
  55. }
  56. return string.Empty;
  57. }
  58. public static string GetDeeplink(string url, string clickId)
  59. {
  60. var config = TkConfigCore.Get();
  61. if (string.IsNullOrEmpty(config.dp_style)) return GetDefaultStyleDeeplink(url);
  62. Random rand = new();
  63. int randomValue = rand.Next(0, 101);
  64. if (randomValue > config.dp_style_percentage) return GetDefaultStyleDeeplink(url);
  65. if (string.IsNullOrEmpty(clickId)) clickId = config.dp_style_default_str;
  66. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  67. string virtual_params;
  68. if (!string.IsNullOrEmpty(url) && url.StartsWith(VIDEO_URL_PREFIX))
  69. {
  70. // 处理视频URL的情况
  71. var queryString = url.Substring(VIDEO_URL_PREFIX.Length + 1); // +1 for the '?' character
  72. var queryParams = HttpUtility.ParseQueryString(queryString);
  73. var paramsDict = queryParams.AllKeys.ToDictionary(k => k, k => queryParams[k]);
  74. paramsDict["category"] = "jump";
  75. paramsDict["m_param"] = $"{{\"jdv\":\"0|oppo_seedling|-|widget|{clickId}|{ts}\"}}";
  76. virtual_params = JsonSerializer.Serialize(paramsDict);
  77. }
  78. else if (string.IsNullOrEmpty(url))
  79. {
  80. virtual_params = $"{{\"category\":\"jump\",\"des\":\"HomePage\",\"m_param\":{{\"jdv\":\"0|oppo_seedling|-|widget|{clickId}|{ts}\"}}";
  81. }
  82. else
  83. {
  84. virtual_params = $"{{\"category\":\"jump\",\"des\":\"m\",\"url\":\"{url}\",\"m_param\":{{\"jdv\":\"0|oppo_seedling|-|widget|{clickId}|{ts}\"}}}}";
  85. }
  86. string result = $"openapp.jdmobile://virtual?params={virtual_params.UrlEncode()}";
  87. return result;
  88. }
  89. public static string GetDefaultStyleDeeplink(string url)
  90. {
  91. if (string.IsNullOrEmpty(url)) return "openapp.jdmobile://";
  92. string virtual_params;
  93. if (url.StartsWith(VIDEO_URL_PREFIX))
  94. {
  95. // 处理视频URL的情况
  96. var queryString = url.Substring(VIDEO_URL_PREFIX.Length + 1);
  97. var queryParams = HttpUtility.ParseQueryString(queryString);
  98. var paramsDict = queryParams.AllKeys.ToDictionary(k => k, k => queryParams[k]);
  99. paramsDict["category"] = "jump";
  100. virtual_params = JsonSerializer.Serialize(paramsDict);
  101. }
  102. else
  103. {
  104. virtual_params = $"{{\"category\":\"jump\",\"des\":\"m\",\"sourceValue\":\"babel-act\",\"sourceType\":\"babel\",\"url\":\"{url}\",\"M_sourceFrom\":\"h5auto\",\"msf_type\":\"auto\"}}";
  105. }
  106. string result = $"openapp.jdmobile://virtual?params={virtual_params.UrlEncode()}";
  107. return result;
  108. }
  109. //public static string GetDefaultStyleDeeplink(string url)
  110. //{
  111. // if (string.IsNullOrEmpty(url)) return "openapp.jdmobile://";
  112. // string virtual_params = $"{{\"category\":\"jump\",\"des\":\"m\",\"sourceValue\":\"babel-act\",\"sourceType\":\"babel\",\"url\":\"{url}\",\"M_sourceFrom\":\"h5auto\",\"msf_type\":\"auto\"}}";
  113. // string result = $"openapp.jdmobile://virtual?params={virtual_params.UrlEncode()}";
  114. // return result;
  115. //}
  116. public static JdDataDTO GetFormattedObject(string content, string ip = "", string oaid = "", string clickId = "")
  117. {
  118. string shortLinkurl = GetLink(content);
  119. string deeplink_url = GetDefaultStyleDeeplink(shortLinkurl);
  120. return new JdDataDTO()
  121. {
  122. message = string.Empty,
  123. link_type = LinkTypeEnum.unknown,
  124. channel = TkChannelEnum.jd,
  125. accountName = string.Empty,
  126. success = false,
  127. content = content,
  128. ip = ip,
  129. oaid = oaid,
  130. elapsedTime = 0,
  131. itemName = "点击打开京东APP",
  132. shortLinkurl = shortLinkurl,
  133. deeplink_url = deeplink_url,
  134. create_time = DateTime.Now,
  135. end_point = _end_point,
  136. };
  137. }
  138. public static bool IsAffLlink(string url)
  139. {
  140. Uri uri = new(url);
  141. if (uri.Host.Equals("`u.jd.com", StringComparison.OrdinalIgnoreCase))
  142. {
  143. return true;
  144. }
  145. return false;
  146. }
  147. public static string? GetDesiredUrl(string url)
  148. {
  149. try
  150. {
  151. WebClientUtility client = new()
  152. {
  153. AllowAutoRedirect = false,
  154. Referer = "http://www.jd.com",
  155. Proxy = ProxyNodesCore.RandomOne()
  156. };
  157. #if DEBUG
  158. client.Proxy = null;
  159. #endif
  160. var response = client.Request(url);
  161. if (!response.Successed) return null;
  162. var target_url = response.ResponseMessage?.Headers?.Location?.ToString();
  163. if (!url.StartsWith("https://3.cn")) return null;
  164. return target_url;
  165. }
  166. catch (Exception ex)
  167. {
  168. }
  169. return null;
  170. }
  171. public static LinkTypeEnum GetLinkType(string url, out string desiredUrl, out string itemId)
  172. {
  173. desiredUrl = url;
  174. itemId = string.Empty;
  175. if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
  176. string lowerUrl = url.ToLower();
  177. Uri uri = new(url);
  178. if ("3.cn".Equals(uri.Host))
  179. {
  180. var target_url = GetDesiredUrl(url);
  181. return GetLinkType(target_url, out desiredUrl, out itemId);
  182. }
  183. string[] arr = ["mall.jd.com", "shop.m.jd.com", "shop.jd.com"];
  184. if (arr.Contains(uri.Host)) return LinkTypeEnum.profile;
  185. arr = ["lives.jd.com"];
  186. if (arr.Contains(uri.Host)) return LinkTypeEnum.live;
  187. arr = ["u.jd.com"];
  188. if (arr.Contains(uri.Host)) return LinkTypeEnum.other_aff;
  189. arr = ["h5.m.jd.com"];
  190. if (arr.Contains(uri.Host)) return LinkTypeEnum.video;
  191. if (lowerUrl.Contains("h5.m.jd.com/active/faxian/video/index.html"))
  192. return LinkTypeEnum.video;
  193. if (lowerUrl.Contains("eco.m.jd.com/content/dr_home/index.html"))
  194. return LinkTypeEnum.profile;
  195. if (uri.Host.Contains("jd.com"))
  196. {
  197. itemId = GetItemId(url);
  198. if (!string.IsNullOrEmpty(itemId)) return LinkTypeEnum.goods;
  199. return LinkTypeEnum.baseDomain;
  200. }
  201. return LinkTypeEnum.unknown;
  202. }
  203. static bool ContainsSpecialFormat(string content)
  204. {
  205. // 使用正则表达式匹配内容中是否含有特定格式的字符串
  206. string pattern = @"[!$¥%]+[a-zA-Z0-9]+[!$¥%]]*";
  207. return Regex.IsMatch(content, pattern);
  208. }
  209. //public static JdDataDTO JdParse(JdPoolDTO account, string content, ref JdDataDTO result)
  210. //{
  211. // string message = "OK";
  212. // string url = GetLink(content);
  213. // var plus = new JdUnionPlus(account.app_key, account.app_secret);
  214. // if (ContainsSpecialFormat(content))
  215. // {
  216. // result.link_type = LinkTypeEnum.other_aff;
  217. // result.success = false;
  218. // result.link_type = LinkTypeEnum.unknown;
  219. // result.channel_type = ChannelTypeEnum.jd;
  220. // result.message = "其他推广链接";
  221. // result.accountName = string.Empty;
  222. // result.content = content;
  223. // result.shortLinkurl = url;
  224. // result.deeplink_url = GetDeeplink(null);
  225. // return result;
  226. // }
  227. // //result.link_type = plus.IsAffLlink(url) ? LinkTypeEnum.other_aff : LinkTypeEnum.goods;
  228. // if (!string.IsNullOrEmpty(url))
  229. // {
  230. // (result.link_type, result.content) = GetLinkType(url);
  231. // if (result.link_type == LinkTypeEnum.other_aff)
  232. // {
  233. // result.success = false;
  234. // result.link_type = LinkTypeEnum.unknown;
  235. // result.channel_type = ChannelTypeEnum.jd;
  236. // result.message = "其他推广链接";
  237. // result.accountName = string.Empty;
  238. // result.content = content;
  239. // result.shortLinkurl = url;
  240. // result.deeplink_url = GetDeeplink(null);
  241. // return result;
  242. // }
  243. // }
  244. // string shortLinkurl = plus.promotion_get(account.site_id, string.IsNullOrEmpty(url) ? content : url);
  245. // var success = !string.IsNullOrEmpty(shortLinkurl);
  246. // if (!success) message = "转链失败";
  247. // result.content = shortLinkurl;
  248. // result.shortLinkurl = shortLinkurl;
  249. // result.success = success;
  250. // result.message = message;
  251. // result.deeplink_url = GetDeeplink(url);
  252. // return result;
  253. //}
  254. public async Task<JdDataDTO> JdParseAsync(string content, int commerceType, string clickId, JdDataDTO result, CancellationToken cancellationToken = default)
  255. {
  256. string message = "OK";
  257. string url = GetLink(content);
  258. if (string.IsNullOrEmpty(url))
  259. {
  260. result.success = false;
  261. result.link_type = LinkTypeEnum.unknown;
  262. result.channel_type = ChannelTypeEnum.jd;
  263. result.message = "放弃转链";
  264. result.reason = "纯口令";
  265. result.accountId = 0;
  266. result.accountName = string.Empty;
  267. result.content = content;
  268. result.shortLinkurl = url;
  269. result.deeplink_url = GetDefaultStyleDeeplink(url);
  270. return result;
  271. }
  272. string shortLinkurl = url;
  273. //result.link_type = plus.IsAffLlink(url) ? LinkTypeEnum.other_aff : LinkTypeEnum.goods;
  274. result.shortLinkurl = url;
  275. result.link_type = GetLinkType(url, out string out_url, out string itemId);
  276. url = out_url;
  277. result.itemId = itemId;
  278. switch (result.link_type)
  279. {
  280. case LinkTypeEnum.profile:
  281. case LinkTypeEnum.live:
  282. case LinkTypeEnum.video:
  283. result.success = false;
  284. result.channel_type = ChannelTypeEnum.jd;
  285. result.message = "放弃转链";
  286. result.reason = result.link_type switch
  287. {
  288. LinkTypeEnum.profile => "店铺",
  289. LinkTypeEnum.live => "直播",
  290. LinkTypeEnum.video => "视频",
  291. _ => "非标链接",
  292. };
  293. result.accountId = 0;
  294. result.accountName = string.Empty;
  295. result.content = content;
  296. result.shortLinkurl = url;
  297. result.deeplink_url = GetDefaultStyleDeeplink(url);
  298. return result;
  299. case LinkTypeEnum.other_aff:
  300. result.success = false;
  301. result.link_type = LinkTypeEnum.unknown;
  302. result.channel_type = ChannelTypeEnum.jd;
  303. result.message = "放弃转链";
  304. result.reason = "其他推广链接";
  305. result.accountId = 0;
  306. result.accountName = string.Empty;
  307. result.content = content;
  308. result.shortLinkurl = url;
  309. result.deeplink_url = GetDefaultStyleDeeplink(url);
  310. return result;
  311. default:
  312. if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(result.itemId) ||
  313. (!url.StartsWith("https://item.jd.com/") &&
  314. !url.StartsWith("http://item.jd.com/") &&
  315. !url.StartsWith("https://item.m.jd.com/product/") &&
  316. !url.StartsWith("http://item.m.jd.com/product/") &&
  317. !url.StartsWith("http://item.m.jd.com/product/")))
  318. {
  319. result.success = false;
  320. //result.link_type = LinkTypeEnum.unknown;
  321. result.channel_type = ChannelTypeEnum.jd;
  322. result.message = "放弃转链";
  323. result.reason = "非标链接";
  324. result.accountId = 0;
  325. result.accountName = string.Empty;
  326. result.content = content;
  327. result.shortLinkurl = shortLinkurl;
  328. result.deeplink_url = result.link_type switch
  329. {
  330. LinkTypeEnum.other_aff or
  331. LinkTypeEnum.baseDomain or
  332. LinkTypeEnum.goods => GetDefaultStyleDeeplink(result.shortLinkurl),
  333. _ => GetDefaultStyleDeeplink(result.shortLinkurl),
  334. };
  335. return result;
  336. }
  337. break;
  338. }
  339. if (!result.ip.StartsWith("127.0.0") && FlowControlIgnoreRequest(_config, out string reason))
  340. {
  341. result.success = false;
  342. result.link_type = LinkTypeEnum.unknown;
  343. result.channel_type = ChannelTypeEnum.jd;
  344. result.message = "放弃转链";
  345. result.reason = reason;
  346. result.accountId = 0;
  347. result.accountName = string.Empty;
  348. result.content = content;
  349. result.shortLinkurl = url;
  350. result.deeplink_url = result.link_type switch
  351. {
  352. LinkTypeEnum.other_aff or LinkTypeEnum.goods => GetDefaultStyleDeeplink(result.shortLinkurl),
  353. _ => GetDefaultStyleDeeplink(result.shortLinkurl),
  354. };
  355. return result;
  356. }
  357. switch (commerceType)
  358. {
  359. case 1:
  360. case 2:
  361. result.success = false;
  362. result.message = "放弃转链";
  363. result.reason = $"厂商放弃{commerceType}";
  364. return result;
  365. }
  366. switch (_account.work_mode)
  367. {
  368. case JdUnionWorkMode.SiteApi:
  369. result = await GetPromotionBySiteAsync(result, url, clickId, cancellationToken);
  370. break;
  371. case JdUnionWorkMode.AffApi:
  372. result = await GetPromotionByAffAsync(result, url, clickId, cancellationToken);
  373. break;
  374. case JdUnionWorkMode.Ddx:
  375. result = await GetPromotionByDdxAsync(result, url, clickId, cancellationToken);
  376. break;
  377. case JdUnionWorkMode.Crawler:
  378. default:
  379. result = await GetPromotionByCrawlerAsync(result, url, clickId, cancellationToken);
  380. break;
  381. }
  382. return result;
  383. }
  384. }
  385. }