JdUnionPlus.cs 9.9 KB

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