JdUnionPlus.cs 9.1 KB

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