JdUnionPlus.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. private static string _end_point;
  25. static JdUnionPlus()
  26. {
  27. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  28. }
  29. public static string GetLink(string content)
  30. {
  31. if (string.IsNullOrEmpty(content)) return content;
  32. string result = content;
  33. //string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  34. string pattern = @"https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
  35. Regex regex = new(pattern);
  36. MatchCollection matches = regex.Matches(content);
  37. foreach (Match m in matches.Cast<Match>())
  38. {
  39. string url = m.Value;
  40. return url;
  41. }
  42. return string.Empty;
  43. }
  44. public static string GetItemId(string url)
  45. {
  46. if (string.IsNullOrEmpty(url)) return string.Empty;
  47. // 定义正则表达式模式,使用捕获组提取数字ID
  48. string pattern = @"https?://item(?:\.m)?\.jd\.com/(?:product/)?(\d+)\.html(?:\?.*)?";
  49. // 使用正则表达式进行匹配
  50. Match match = Regex.Match(url, pattern);
  51. if (match.Success)
  52. {
  53. string idNumber = match.Groups[1].Value; // 捕获组1包含数字ID
  54. return idNumber;
  55. }
  56. return string.Empty;
  57. }
  58. public static string GetDeeplink(string url)
  59. {
  60. if (string.IsNullOrEmpty(url)) return "openapp.jdmobile://virtual?params=";
  61. string virtual_params = $"{{\"category\":\"jump\",\"des\":\"m\",\"sourceValue\":\"babel-act\",\"sourceType\":\"babel\",\"url\":\"{url}\",\"M_sourceFrom\":\"h5auto\",\"msf_type\":\"auto\"}}";
  62. string result = $"openapp.jdmobile://virtual?params={virtual_params.UrlEncode()}";
  63. return result;
  64. }
  65. public static JdDataDTO GetFormattedObject(string content, string ip = "", string oaid = "")
  66. {
  67. string shortLinkurl = GetLink(content);
  68. string deeplink_url = GetDeeplink(shortLinkurl);
  69. return new JdDataDTO()
  70. {
  71. message = string.Empty,
  72. link_type = LinkTypeEnum.unknown,
  73. channel = TkChannelEnum.jd,
  74. accountName = string.Empty,
  75. success = false,
  76. content = content,
  77. ip = ip,
  78. oaid = oaid,
  79. elapsedTime = 0,
  80. itemName = "点击打开京东APP",
  81. shortLinkurl = shortLinkurl,
  82. deeplink_url = deeplink_url,
  83. create_time = DateTime.Now,
  84. end_point = _end_point,
  85. };
  86. }
  87. public static bool IsAffLlink(string url)
  88. {
  89. Uri uri = new(url);
  90. if (uri.Host.Equals("u.jd.com", StringComparison.OrdinalIgnoreCase))
  91. {
  92. return true;
  93. }
  94. return false;
  95. }
  96. public static string? GetDesiredUrl(string url)
  97. {
  98. try
  99. {
  100. WebClientUtility client = new()
  101. {
  102. AllowAutoRedirect = false
  103. };
  104. var response = client.Request(url);
  105. if (!response.Successed) return null;
  106. var target_url = response.ResponseMessage?.Headers?.Location?.ToString();
  107. if (!url.StartsWith("https://3.cn")) return null;
  108. return target_url;
  109. }
  110. catch (Exception ex)
  111. {
  112. }
  113. return null;
  114. }
  115. public static LinkTypeEnum GetLinkType(string url, out string desiredUrl, out string itemId)
  116. {
  117. desiredUrl = url;
  118. itemId = string.Empty;
  119. if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
  120. if (url.StartsWith("https://u.jd.com") ||
  121. url.StartsWith("http://u.jd.com"))
  122. return LinkTypeEnum.other_aff;
  123. if (url.StartsWith("https://lives.jd.com/") ||
  124. url.StartsWith("http://lives.jd.com/"))
  125. return LinkTypeEnum.live;
  126. if (url.StartsWith("https://h5.m.jd.com/active/faxian/video/index.html") ||
  127. url.StartsWith("http://h5.m.jd.com/active/faxian/video/index.html"))
  128. return LinkTypeEnum.video;
  129. if (url.StartsWith("https://eco.m.jd.com/content/dr_home/index.html") ||
  130. url.StartsWith("http://eco.m.jd.com/content/dr_home/index.html")) return LinkTypeEnum.profile;
  131. if (url.StartsWith("https://3.cn") ||
  132. url.StartsWith("http://3.cn"))
  133. {
  134. var target_url = GetDesiredUrl(url);
  135. return GetLinkType(target_url, out desiredUrl, out itemId);
  136. }
  137. if (url.Contains("jd.com"))
  138. {
  139. itemId = GetItemId(url);
  140. if (!string.IsNullOrEmpty(itemId)) return LinkTypeEnum.goods;
  141. return LinkTypeEnum.baseDomain;
  142. }
  143. return LinkTypeEnum.unknown;
  144. }
  145. static bool ContainsSpecialFormat(string content)
  146. {
  147. // 使用正则表达式匹配内容中是否含有特定格式的字符串
  148. string pattern = @"[!$¥%]+[a-zA-Z0-9]+[!$¥%]]*";
  149. return Regex.IsMatch(content, pattern);
  150. }
  151. //public static JdDataDTO JdParse(JdPoolDTO account, string content, ref JdDataDTO result)
  152. //{
  153. // string message = "OK";
  154. // string url = GetLink(content);
  155. // var plus = new JdUnionPlus(account.app_key, account.app_secret);
  156. // if (ContainsSpecialFormat(content))
  157. // {
  158. // result.link_type = LinkTypeEnum.other_aff;
  159. // result.success = false;
  160. // result.link_type = LinkTypeEnum.unknown;
  161. // result.channel_type = ChannelTypeEnum.jd;
  162. // result.message = "其他推广链接";
  163. // result.accountName = string.Empty;
  164. // result.content = content;
  165. // result.shortLinkurl = url;
  166. // result.deeplink_url = GetDeeplink(null);
  167. // return result;
  168. // }
  169. // //result.link_type = plus.IsAffLlink(url) ? LinkTypeEnum.other_aff : LinkTypeEnum.goods;
  170. // if (!string.IsNullOrEmpty(url))
  171. // {
  172. // (result.link_type, result.content) = GetLinkType(url);
  173. // if (result.link_type == LinkTypeEnum.other_aff)
  174. // {
  175. // result.success = false;
  176. // result.link_type = LinkTypeEnum.unknown;
  177. // result.channel_type = ChannelTypeEnum.jd;
  178. // result.message = "其他推广链接";
  179. // result.accountName = string.Empty;
  180. // result.content = content;
  181. // result.shortLinkurl = url;
  182. // result.deeplink_url = GetDeeplink(null);
  183. // return result;
  184. // }
  185. // }
  186. // string shortLinkurl = plus.promotion_get(account.site_id, string.IsNullOrEmpty(url) ? content : url);
  187. // var success = !string.IsNullOrEmpty(shortLinkurl);
  188. // if (!success) message = "转链失败";
  189. // result.content = shortLinkurl;
  190. // result.shortLinkurl = shortLinkurl;
  191. // result.success = success;
  192. // result.message = message;
  193. // result.deeplink_url = GetDeeplink(url);
  194. // return result;
  195. //}
  196. public static async Task<JdDataDTO> JdParseAsync(JdPoolDTO account, string content, JdDataDTO result, CancellationToken cancellationToken = default)
  197. {
  198. string message = "OK";
  199. string url = GetLink(content);
  200. string shortLinkurl = url;
  201. var plus = new JdUnionPlus(account.app_key, account.app_secret);
  202. //result.link_type = plus.IsAffLlink(url) ? LinkTypeEnum.other_aff : LinkTypeEnum.goods;
  203. result.link_type = GetLinkType(url, out string out_url, out string itemId);
  204. url = out_url;
  205. result.itemId = itemId;
  206. if (result.link_type == LinkTypeEnum.other_aff)
  207. {
  208. result.success = false;
  209. result.link_type = LinkTypeEnum.unknown;
  210. result.channel_type = ChannelTypeEnum.jd;
  211. result.message = "其他推广链接";
  212. result.accountName = string.Empty;
  213. result.content = content;
  214. result.shortLinkurl = url;
  215. result.deeplink_url = GetDeeplink(url);
  216. return result;
  217. }
  218. result.shortLinkurl = await plus.GetPromotionAsync(account.site_id, url, cancellationToken);
  219. var success = !string.IsNullOrEmpty(result.shortLinkurl);
  220. if (!success)
  221. {
  222. result.shortLinkurl = shortLinkurl;
  223. message = "转链失败";
  224. }
  225. result.content = result.shortLinkurl;
  226. result.shortLinkurl = result.shortLinkurl;
  227. result.success = success;
  228. result.message = message;
  229. result.deeplink_url = GetDeeplink(result.shortLinkurl);
  230. return result;
  231. }
  232. }
  233. }