UnionCouponCore.cs 9.5 KB

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