TaokeOpenCore.cs 9.1 KB

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