UnionCouponCore.cs 11 KB

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