UnionCouponCore.cs 9.5 KB

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