TaokeOpenCore.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using dodohold.core;
  2. using System.Text.Json;
  3. namespace molilian.core
  4. {
  5. public partial class TaokeOpenCore
  6. {
  7. static string _test_base64String = string.Empty;
  8. public TkConfigDTO _config;
  9. private static string _end_point;
  10. static TaokeOpenCore()
  11. {
  12. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  13. }
  14. public TaokeOpenCore()
  15. {
  16. _config = TkConfigCore.Get();
  17. }
  18. public async Task<APIResult> PromotionQueryAsync(string img, string oaid = "", string ip = "")
  19. {
  20. PromotionQueryDTO result = new()
  21. {
  22. end_point = _end_point,
  23. ip = ip,
  24. oaid = oaid,
  25. img = img
  26. };
  27. if (string.IsNullOrEmpty(img))
  28. {
  29. result.success = false;
  30. result.message = "调用失败";
  31. result.reason = "无效图片";
  32. _ = TkLogCore.PromotionImgLogAsync(result);
  33. return new APIResult(new
  34. {
  35. result.success,
  36. result.message,
  37. result.reason,
  38. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  39. {
  40. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  41. });
  42. }
  43. try
  44. {
  45. var account = TkPoolCore.GetOne(TkPoolCore.TkAction.promotionQuery);
  46. if (account == null)
  47. {
  48. result.success = false;
  49. result.message = "调用失败";
  50. result.reason = "没有匹配账号";
  51. _ = TkLogCore.PromotionImgLogAsync(result);
  52. return new APIResult(new
  53. {
  54. result.success,
  55. result.message,
  56. result.reason,
  57. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  58. {
  59. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  60. });
  61. }
  62. TaobaoOpenPlus taobao = new(account.appkey, account.appSecret, account.open_pid);
  63. var data = taobao.PromotionQueryRequest(img, oaid);
  64. if (data.IsError)
  65. {
  66. result.success = false;
  67. result.message = "调用失败";
  68. result.reason = data.ErrMsg;
  69. _ = TkLogCore.PromotionImgLogAsync(result);
  70. return new APIResult(new
  71. {
  72. result.success,
  73. result.message,
  74. result.reason,
  75. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  76. {
  77. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  78. });
  79. }
  80. if (!"0".Equals(data.Status))
  81. {
  82. result.success = false;
  83. result.message = "调用失败";
  84. result.reason = data.Desc;
  85. _ = TkLogCore.PromotionImgLogAsync(result);
  86. return new APIResult(new
  87. {
  88. result.success,
  89. result.message,
  90. result.reason,
  91. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  92. {
  93. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  94. });
  95. }
  96. result.url = data.Url;
  97. oaid = data.Url.GetContentPart("oaid=", "&");
  98. string cookies = string.Empty;
  99. int timeout = _config.similar_timeout;
  100. #if DEBUG
  101. timeout = 5000;
  102. #endif
  103. using var cts = new CancellationTokenSource();
  104. cts.CancelAfter(timeout);
  105. var requestTask = Task.Run(async () =>
  106. {
  107. (result.SimilarPromotion, cookies) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies, cts.Token);
  108. (result.PromotionImg, cookies) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
  109. result.similarPromotion = result.SimilarPromotion.Convert2Json();
  110. result.promotionImg = result.PromotionImg.Convert2Json();
  111. return result;
  112. }, cts.Token);
  113. var delayTask = Task.Delay(timeout, cts.Token);
  114. var completedTask = await Task.WhenAny(requestTask, delayTask);
  115. if (completedTask == requestTask)
  116. {
  117. result = await requestTask;
  118. }
  119. else
  120. {
  121. //============================== 放弃转链-请求超时 ==============================
  122. cts.Cancel();
  123. result.success = false;
  124. result.message = "调用失败";
  125. result.reason = "请求超时";
  126. _ = TkLogCore.PromotionImgLogAsync(result);
  127. return new APIResult(new
  128. {
  129. result.success,
  130. result.message,
  131. result.reason,
  132. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  133. {
  134. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  135. });
  136. }
  137. //(result.SimilarPromotion, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies);
  138. //(result.PromotionImg, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies);
  139. //result.similarPromotion = result.SimilarPromotion.Convert2Json();
  140. //result.promotionImg = result.PromotionImg.Convert2Json();
  141. result.success = true;
  142. result.message = "调用成功";
  143. _ = TkLogCore.PromotionImgLogAsync(result);
  144. return new APIResult(new
  145. {
  146. result.success,
  147. result.message,
  148. result.reason,
  149. similarPromotion = result.SimilarPromotion,
  150. promotionImg = result.PromotionImg,
  151. result.url,
  152. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  153. {
  154. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  155. });
  156. }
  157. catch (Exception ex)
  158. {
  159. result.success = false;
  160. result.message = "调用失败";
  161. result.reason = ex.Message;
  162. _ = TkLogCore.PromotionImgLogAsync(result);
  163. return new APIResult(new
  164. {
  165. result.success,
  166. result.message,
  167. result.reason,
  168. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  169. {
  170. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  171. });
  172. }
  173. //try
  174. //{
  175. // var account = TkPoolCore.GetOne(TkPoolCore.TkAction.promotionQuery);
  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.accountId = account.id;
  190. // result.accountName = account.name;
  191. // alimama = new AlimamaPlus(account);
  192. // result = await alimama.PromotionQueryAsync(result);
  193. // if (result.success && result.couponAmount == 0)
  194. // {
  195. // result.success = false;
  196. // result.message = "没有优惠券";
  197. // result.reason = "没有优惠券";
  198. // }
  199. //}
  200. //catch (Exception ex)
  201. //{
  202. // if (ex.Message.Contains("was canceled"))
  203. // {
  204. // result.success = false;
  205. // result.message = "放弃转链";
  206. // result.reason = "请求超时";
  207. // }
  208. // else
  209. // {
  210. // _ = new LoggerLibrary("unionCoupon", "tb_error")
  211. // .Info(ip, oaid)
  212. // .Info(content)
  213. // .Info(ex.Message, ex.StackTrace)
  214. // .SaveAsync();
  215. // NotifyCore.Notify(new NifyMessage
  216. // {
  217. // message = $"【转链异常TB】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
  218. // priority = NifyMessagePriority.high,
  219. // tags = ["red_circle"]
  220. // });
  221. // result.success = false;
  222. // result.message = "放弃转链";
  223. // result.reason = "转链接口异常";
  224. // }
  225. //}
  226. //_ = TkLogCore.CouponLogAsync(result, alimama);
  227. //if (!result.success)
  228. //{
  229. // return new APIResult(new
  230. // {
  231. // result.success,
  232. // message = "没有优惠券",
  233. // channel = result?.channel.ToString(),
  234. // });
  235. //}
  236. //int couponAmount = (int)result.couponAmount * 100;
  237. //return new APIResult(new
  238. //{
  239. // result.success,
  240. // result.message,
  241. // channel = result?.channel.ToString(),
  242. // result?.itemId,
  243. // result?.deeplink_url,
  244. // result?.pic,
  245. // result?.couponEffectiveStartTime,
  246. // result?.couponEffectiveEndTime,
  247. //});
  248. }
  249. }
  250. }