UnionCouponCore.cs 9.3 KB

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