UnionCouponCore.cs 11 KB

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