UnionCouponCore.cs 9.3 KB

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