UnionCouponCore.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 Spire.Pdf.Exporting.XPS.Schema;
  10. using System.Xml.Linq;
  11. using System.Net;
  12. using TencentCloud.Mrs.V20200910.Models;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Microsoft.AspNetCore.Authentication;
  15. using Spire.Pdf.Graphics;
  16. using Google.Protobuf.WellKnownTypes;
  17. using System.Threading.Channels;
  18. using TencentCloud.Weilingwith.V20230427.Models;
  19. using System.Threading;
  20. namespace molilian.core
  21. {
  22. public partial class UnionCouponCore
  23. {
  24. private static string _end_point;
  25. static UnionCouponCore()
  26. {
  27. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  28. }
  29. public static async Task<ActionResult> UnionCouponAsync(string content, string dplink, string channel, string ip, string oaid)
  30. {
  31. return channel switch
  32. {
  33. "jd" => await JdCouponAsync(content, dplink, ip, oaid),
  34. _ => await TaobaoCouponAsync(content, dplink, ip, oaid),
  35. };
  36. }
  37. public static async Task<APIResult> TaobaoCouponAsync(string content, string dplink, string ip = "", string oaid = "")
  38. {
  39. AlimamaPlus alimama = null;
  40. content = content.UrlDecode();
  41. dplink = dplink.UrlDecode();
  42. UnionCouponDTO result = new()
  43. {
  44. channel = TkChannelEnum.tb,
  45. rawContent = content,
  46. rawContent2 = dplink,
  47. end_point = _end_point,
  48. ip = ip,
  49. oaid = oaid
  50. };
  51. content = AlimamaPlus.FakeItemUrl(content, dplink);
  52. if (string.IsNullOrEmpty(content))
  53. {
  54. result.success = false;
  55. result.message = "放弃转链";
  56. result.reason = "无效的参数";
  57. result.rawContent = content;
  58. _ = TkLogCore.CouponLogAsync(result);
  59. return new APIResult(new
  60. {
  61. result.success,
  62. result.reason,
  63. result.message,
  64. channel = result.channel.ToString(),
  65. });
  66. }
  67. try
  68. {
  69. string reason;
  70. if (AlimamaPlus.FilterRiskLink(ip, oaid, content, out reason) ||
  71. AlimamaPlus.ShouldIgnoreRequest(ip, oaid, out reason))
  72. {
  73. result.success = false;
  74. result.message = "放弃转链";
  75. result.reason = reason;
  76. _ = TkLogCore.CouponLogAsync(result);
  77. return new APIResult(new
  78. {
  79. result.success,
  80. result.message,
  81. result.reason,
  82. channel = result.channel.ToString(),
  83. });
  84. }
  85. var account = TkPoolCore.GetOne(TkPoolCore.TkAction.coupon);
  86. if (account == null)
  87. {
  88. result.success = false;
  89. result.message = "放弃转链";
  90. result.reason = "没有匹配账号";
  91. _ = TkLogCore.CouponLogAsync(result);
  92. return new APIResult(new
  93. {
  94. result.success,
  95. result.message,
  96. result.reason,
  97. channel = result.channel.ToString(),
  98. });
  99. }
  100. result.accountId = account.id;
  101. result.accountName = account.name;
  102. alimama = new AlimamaPlus(account);
  103. result = await alimama.UnionCoupon2Async(content, result);
  104. }
  105. catch (Exception ex)
  106. {
  107. if (ex.Message.Contains("was canceled"))
  108. {
  109. result.success = false;
  110. result.message = "放弃转链";
  111. result.reason = "请求超时";
  112. }
  113. else
  114. {
  115. _ = new LoggerLibrary("unionCoupon", "tb_error")
  116. .Info(ip, oaid)
  117. .Info(content)
  118. .Info(ex.Message, ex.StackTrace)
  119. .SaveAsync();
  120. NotifyCore.Notify(new NifyMessage
  121. {
  122. message = $"【转链异常TB】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
  123. priority = NifyMessagePriority.high,
  124. tags = ["red_circle"]
  125. });
  126. result.success = false;
  127. result.message = "放弃转链";
  128. result.reason = "转链接口异常";
  129. }
  130. }
  131. _ = TkLogCore.CouponLogAsync(result, alimama);
  132. if (!result.success)
  133. {
  134. return new APIResult(new
  135. {
  136. result.success,
  137. result.message,
  138. result.reason,
  139. channel = result?.channel.ToString(),
  140. });
  141. }
  142. int couponAmount = (int)result.couponAmount * 100;
  143. return new APIResult(new
  144. {
  145. result.success,
  146. result.message,
  147. channel = result?.channel.ToString(),
  148. result?.itemId,
  149. couponAmount,
  150. result?.deeplink_url,
  151. result?.pic,
  152. result?.couponEffectiveStartTime,
  153. result?.couponEffectiveEndTime,
  154. });
  155. }
  156. public static async Task<APIResult> JdCouponAsync(string content, string dplink, string ip, string oaid)
  157. {
  158. content = content.UrlDecode();
  159. dplink = dplink.UrlDecode();
  160. UnionCouponDTO result = new()
  161. {
  162. channel = TkChannelEnum.jd,
  163. rawContent = content,
  164. rawContent2 = dplink,
  165. end_point = _end_point,
  166. };
  167. if (string.IsNullOrEmpty(content))
  168. {
  169. content = dplink.Replace("taobao://", "https://");
  170. }
  171. if (string.IsNullOrEmpty(content))
  172. {
  173. result.success = false;
  174. result.message = "放弃转链";
  175. result.reason = "无效的参数";
  176. result.rawContent = content;
  177. _ = TkLogCore.CouponLogAsync(result);
  178. return new APIResult(new
  179. {
  180. result.success,
  181. result.reason,
  182. result.message,
  183. channel = result.channel.ToString(),
  184. });
  185. }
  186. try
  187. {
  188. var config = TkConfigCore.Get();
  189. if (JdUnionPlus.ShouldIgnoreRequest(config, ip, oaid, out string reason))
  190. {
  191. result.success = false;
  192. result.message = "放弃转链";
  193. result.reason = reason;
  194. _ = TkLogCore.CouponLogAsync(result);
  195. return new APIResult(new
  196. {
  197. result.success,
  198. result.reason,
  199. result.message,
  200. channel = result.channel.ToString(),
  201. });
  202. }
  203. var api = DataokeCore.GetOne();
  204. if (api == null)
  205. {
  206. result.success = false;
  207. result.message = "放弃转链";
  208. result.reason = "没有匹配账号";
  209. result.rawContent = content;
  210. _ = TkLogCore.CouponLogAsync(result);
  211. return new APIResult(new
  212. {
  213. result.success,
  214. result.reason,
  215. result.message,
  216. channel = result.channel.ToString(),
  217. });
  218. }
  219. var account = JdPoolCore.GetOne();
  220. if (account == null)
  221. {
  222. result.success = false;
  223. result.message = "放弃转链";
  224. result.reason = "没有匹配账号";
  225. _ = TkLogCore.CouponLogAsync(result);
  226. return new APIResult(new
  227. {
  228. result.success,
  229. result.reason,
  230. result.message,
  231. channel = result.channel.ToString(),
  232. });
  233. }
  234. result = await JdUnionPlus.JdCouponAsync(account, api, content, result);
  235. if (result.success && result.couponAmount == 0)
  236. {
  237. result.success = false;
  238. result.message = "没有优惠券";
  239. result.reason = "";
  240. }
  241. }
  242. catch (Exception ex)
  243. {
  244. if (ex.Message.Contains("was canceled"))
  245. {
  246. result.success = false;
  247. result.message = "放弃转链";
  248. result.reason = "请求超时";
  249. }
  250. else
  251. {
  252. _ = new LoggerLibrary("unionCoupon", "jd_error")
  253. .Info(ip, oaid)
  254. .Info(content)
  255. .Info(ex.Message, ex.StackTrace)
  256. .SaveAsync();
  257. NotifyCore.Notify(new NifyMessage
  258. {
  259. message = $"【转链异常JD】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
  260. priority = NifyMessagePriority.high,
  261. tags = ["red_circle"]
  262. });
  263. result.success = false;
  264. result.message = "内部错误";
  265. result.reason = "转链接口异常";
  266. }
  267. }
  268. _ = TkLogCore.CouponLogAsync(result);
  269. if (!result.success)
  270. {
  271. return new APIResult(new
  272. {
  273. result.success,
  274. result.reason,
  275. result.message,
  276. channel = result?.channel.ToString(),
  277. });
  278. }
  279. int couponAmount = (int)(result.couponAmount * 100);
  280. return new APIResult(new
  281. {
  282. result.success,
  283. result.message,
  284. channel = result?.channel.ToString(),
  285. result?.itemId,
  286. couponAmount,
  287. result?.deeplink_url,
  288. result?.pic,
  289. result?.couponEffectiveStartTime,
  290. result?.couponEffectiveEndTime,
  291. });
  292. }
  293. }
  294. }