JdUnionPlus.cs 15 KB

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