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