OtherApiPlus.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using dodohold.core;
  2. using Org.BouncyCastle.Ocsp;
  3. using System.Text.RegularExpressions;
  4. namespace molilian.core
  5. {
  6. public partial class OtherApiPlus
  7. {
  8. private string _accountName;
  9. private string _key;
  10. private string _sessionKey;
  11. private string _style;
  12. private string _api_url;
  13. public OtherApiPlus(string accountName, string key, string sessionKey, string style, string api_url)
  14. {
  15. _accountName = accountName;
  16. _key = key;
  17. _sessionKey = sessionKey;
  18. _style = style;
  19. _api_url = api_url;
  20. }
  21. public TkDataDTO UnionParse(string content, ref TkDataDTO result)
  22. {
  23. string para = content.UrlEncode();
  24. string url = $"http://api.veapi.cn/tbk/hcapi?vekey={_key}&sessionkey={_sessionKey}&para={para}";
  25. var response = new WebClientUtility().Request(url);
  26. var body = response.Body();
  27. var root = body.Convert2JsonElement();
  28. int error = root.Read<int>("error", 0);
  29. bool success = error == 0;
  30. string message = root.Read("msg", string.Empty);
  31. string taoToken = root.PathRead("data.ios_tbk_pwd", string.Empty);
  32. string num_iid = root.PathRead("data.num_iid", string.Empty);
  33. decimal couponAmount = root.PathRead<decimal>("data.coupon_amount", 0);
  34. string shortLinkurl = root.PathRead("data.coupon_short_url", string.Empty);
  35. string shortLink = AlimamaPlus.GetLink(taoToken);
  36. content = AlimamaPlus.ReplaceUrls(content, shortLink);
  37. if (!success)
  38. {
  39. shortLinkurl = AlimamaPlus.GetLink(content);
  40. }
  41. string deeplink_url = AlimamaPlus.GetDeeplink(shortLinkurl);
  42. result.channel = TkChannelEnum.tb;
  43. result.accountName = _accountName;
  44. result.success = success;
  45. result.message = message;
  46. result.content = content;
  47. result.couponAmount = couponAmount;
  48. result.taoToken = taoToken;
  49. result.shortLinkurl = shortLinkurl;
  50. result.deeplink_url = deeplink_url;
  51. result.num_iid = num_iid;
  52. return result;
  53. }
  54. public PromotionQueryDTO picSimilaritem(string pic, ref PromotionQueryDTO result)
  55. {
  56. string url = $"http://api.veapi.cn/tbk/picsimilaritem";
  57. string base64 = $"data:image/jpeg;base64,{pic}";
  58. var client = new WebClientUtility();
  59. client.SetContentType("application/x-www-form-urlencoded");
  60. client.Post("vekey", _key);
  61. client.Post("pic", base64);
  62. var response = client.Request(url);
  63. var body = response.Body();
  64. var root = body.Convert2JsonElement();
  65. int error = root.Read<int>("error", 0);
  66. bool success = error == 0;
  67. string message = root.Read("msg", string.Empty);
  68. result.success = success;
  69. if (!success)
  70. {
  71. result.message = "调用失败";
  72. result.reason = message;
  73. return result;
  74. }
  75. List<PromotionQueryItemDTO> arr = [];
  76. var list = root.ElementRead("data");
  77. foreach (var e in list.EnumerateArray())
  78. {
  79. var item = e.ElementRead("Result");
  80. string itemId = item.Read<string>("ItemId", string.Empty);
  81. string h5_url = $"https://uland.taobao.com/item/edetail?id={itemId}";
  82. string deeplink_url = AlimamaPlus.GetDeeplink(h5_url);
  83. PromotionQueryItemDTO newItem = new PromotionQueryItemDTO();
  84. newItem.itemId = itemId;
  85. newItem.h5_url = h5_url;
  86. newItem.deeplink_url = deeplink_url;
  87. newItem.userType = item.Read<string>("UserType", string.Empty);
  88. newItem.shopTitle = item.Read<string>("ShopTitle", string.Empty);
  89. newItem.itemName = item.Read<string>("Title", string.Empty);
  90. newItem.pic = item.Read<string>("PicUrl", string.Empty);
  91. newItem.price = item.Read<string>("ReservePrice", string.Empty);
  92. newItem.promotionPrice = item.Read<string>("PriceAfterCoupon", string.Empty);
  93. newItem.couponAmount = item.Read<string>("CouponAmount", string.Empty);
  94. newItem.couponEffectiveStartTime = item.Read<string>("CouponStartTime", string.Empty);
  95. newItem.couponEffectiveEndTime = item.Read<string>("CouponEndTime", string.Empty);
  96. newItem.sellCountStr = item.Read<string>("sellCountStr", string.Empty);
  97. newItem.provcity = item.Read<string>("Provcity", string.Empty);
  98. arr.Add(newItem);
  99. }
  100. result.message = "OK";
  101. result.reason = string.Empty;
  102. result.PromotionImg = arr;
  103. //PromotionImg
  104. /*
  105. *
  106. public string h5_url { get; set; } = string.Empty;
  107. public string deeplink_url { get; set; } = string.Empty;
  108. /// <summary>
  109. /// 店铺信息-卖家类型:1天猫,0淘宝,3淘特
  110. /// </summary>
  111. public string userType { get; set; } = string.Empty;
  112. public string shopTitle { get; set; } = string.Empty;
  113. public string itemId { get; set; } = string.Empty;
  114. public string itemName { get; set; } = string.Empty;
  115. public string pic { get; set; } = string.Empty;
  116. public string price { get; set; } = string.Empty;
  117. public string promotionPrice { get; set; } = string.Empty;
  118. public string couponAmount { get; set; } = string.Empty;
  119. public string couponEffectiveStartTime { get; set; } = string.Empty;
  120. public string couponEffectiveEndTime { get; set; } = string.Empty;
  121. public string sellCountStr { get; set; } = string.Empty;
  122. public string provcity { get; set; } = string.Empty;
  123. {
  124. "error": "0",
  125. "msg": "查询成功",
  126. "data": [
  127. {
  128. "RankScore": "0.8065",
  129. "Result": {
  130. "CategoryName": "跑步鞋",
  131. "CommissionRate": "135",
  132. "CouponAmount": 50,
  133. "CouponEndTime": "2025-01-31",
  134. "CouponInfo": "满99.0元减50.0元",
  135. "CouponRemainCount": 99656,
  136. "CouponStartFee": "99.0",
  137. "CouponStartTime": "2024-12-24",
  138. "CouponTotalCount": "100000",
  139. "ItemId": "jOZk7gxcAfJZ6zGz8bFKjwSJte-Z9Z023UAg5djJmJSRp",
  140. "LevelOneCategoryName": "运动鞋new",
  141. "MaxCommission": {},
  142. "Nick": "",
  143. "PicUrl": "https://img.alicdn.com/i2/1994113638/O1CN015M0Aqf1ckFs5UI2eJ_!!0-item_pic.jpg",
  144. "PriceAfterCoupon": "109.00",
  145. "Provcity": "福建 泉州",
  146. "ReservePrice": "259.00",
  147. "SellerId": "1994113638",
  148. "ShopTitle": "361度户外旗舰店",
  149. "ShortTitle": "加绒保暖老爹鞋361女鞋运动鞋女",
  150. "SubTitle": "新款加绒女鞋运动鞋",
  151. "Title": "361女鞋运动鞋女2024冬季新款加绒保暖棉鞋轻便老爹鞋休闲跑步鞋",
  152. "UserType": 1,
  153. "Volume": 10,
  154. "ZkFinalPrice": "159"
  155. }
  156. }
  157. ]
  158. ],
  159. "request_id": "4eZfpBu"
  160. }
  161. */
  162. return result;
  163. }
  164. public async Task<TkDataDTO> UnionParseAsync(string content, string pid, TkDataDTO result, CancellationToken cancellationToken)
  165. {
  166. switch (_style)
  167. {
  168. case "ddx":
  169. return await DdxParseAsync(content, pid, result, cancellationToken);
  170. case "veapi":
  171. default:
  172. return await VeapiParseAsync(content, pid, result, cancellationToken);
  173. }
  174. }
  175. private async Task<TkDataDTO> VeapiParseAsync(string content, string pid, TkDataDTO result, CancellationToken cancellationToken)
  176. {
  177. string para = content.UrlEncode();
  178. string url = $"http://api.veapi.cn/tbk/generalconvert?vekey={_key}&para={para}&pid={pid}";
  179. if (!string.IsNullOrEmpty(_api_url))
  180. {
  181. url = _api_url;
  182. url = url.Replace("{apikey}", _key);
  183. url = url.Replace("{pid}", pid);
  184. url = url.Replace("{content}", para);
  185. }
  186. var response = await new WebClientUtility().RequestAsync(url, "GET", cancellationToken);
  187. var body = response.Body();
  188. var root = body.Convert2JsonElement();
  189. int error = root.Read<int>("error", 0);
  190. bool success = error == 0;
  191. string message = root.Read("msg", string.Empty);
  192. var data = root.ElementRead("data");
  193. result.channel = TkChannelEnum.tb;
  194. result.accountName = _accountName;
  195. result.success = success;
  196. result.message = message;
  197. if (success)
  198. {
  199. result.content = content;
  200. result.message = string.Empty;
  201. result.commercial = true;
  202. result.couponAmount = data.Read<decimal>("coupon_amount", 0);
  203. result.mktId = data.Read<string>("isv_mktid", string.Empty);
  204. result.itemId = data.Read<string>("item_id", string.Empty);
  205. result.itemId = content.GetContentPart("item.htm?id=", "");
  206. // veapi 缺少商品pic 和 promotionPrice
  207. result.taoToken = data.Read<string>("cps_full_tpwd", string.Empty);
  208. result.item_url = AlimamaPlus.GetLink(result.taoToken);
  209. result.item_deeplink_url = AlimamaPlus.GetDeeplink(result.shortLinkurl);
  210. string coupon_full_tpwd = data.Read<string>("coupon_full_tpwd", string.Empty);
  211. if (!string.IsNullOrEmpty(coupon_full_tpwd)) result.taoToken = coupon_full_tpwd;
  212. result.shortLinkurl = AlimamaPlus.GetLink(result.taoToken);
  213. result.deeplink_url = AlimamaPlus.GetDeeplink(result.shortLinkurl);
  214. result.itemName = result.taoToken.GetContentPart(result.shortLinkurl, "").Trim();
  215. return result;
  216. }
  217. else
  218. {
  219. result.message= "转链失败";
  220. result.reason = message;
  221. }
  222. return result;
  223. }
  224. private async Task<TkDataDTO> DdxParseAsync(string content, string pid, TkDataDTO result, CancellationToken cancellationToken)
  225. {
  226. string para = result.rawContent.UrlEncode();
  227. string url = $"https://api.tbk.dingdanxia.com/tbk/tkl_privilege?apikey={_key}&pid={pid}&relation_id=3197256735&activityId=&extspk=&tb_auth_id=&tkl=XXX+{para}";
  228. if (!string.IsNullOrEmpty(_api_url))
  229. {
  230. url = _api_url;
  231. url = url.Replace("{apikey}", _key);
  232. url = url.Replace("{pid}", pid);
  233. url = url.Replace("{content}", para);
  234. }
  235. var client = new WebClientUtility();
  236. #if DEBUG
  237. var response = await client.RequestAsync(url, "GET");
  238. #else
  239. var response = await client.RequestAsync(url, "GET", cancellationToken);
  240. #endif
  241. var body = response.Body();
  242. var root = body.Convert2JsonElement();
  243. int code = root.Read<int>("code", 0);
  244. bool success = code == 200;
  245. string message = root.Read("msg", string.Empty);
  246. if (message.StartsWith("数据返回失败【"))
  247. {
  248. message = message.GetContentPart("数据返回失败【", "】");
  249. }
  250. var data = root.ElementRead("data");
  251. result.channel = TkChannelEnum.tb;
  252. result.accountName = _accountName;
  253. result.success = success;
  254. result.message = message;
  255. if (success)
  256. {
  257. result.content = content;
  258. result.message = string.Empty;
  259. result.commercial = true;
  260. result.couponAmount = data.Read<decimal>("coupon", 0);
  261. result.couponEffectiveStartTime = data.Read<string>("coupon_start_time", string.Empty);
  262. result.couponEffectiveEndTime = data.Read<string>("coupon_end_time", string.Empty);
  263. result.mktId = data.Read<string>("item_id", string.Empty);
  264. result.itemId = content.GetContentPart("item.htm?id=", "");
  265. result.pic = data.PathRead<string>("itemInfo.pict_url", string.Empty);
  266. result.sellerNickName = data.PathRead<string>("itemInfo.nick", string.Empty);
  267. result.promotionPrice = data.PathRead<decimal>("itemInfo.qh_final_price", 0);
  268. if (result.promotionPrice == 0)
  269. {
  270. result.promotionPrice = data.PathRead<decimal>("itemInfo.zk_final_price", 0);
  271. }
  272. result.taoToken = data.Read<string>("long_item_tpwd", string.Empty);
  273. //string long_coupon_tpwd = data.Read<string>("long_coupon_tpwd", string.Empty);
  274. //if (!string.IsNullOrEmpty(long_coupon_tpwd)) result.taoToken = long_coupon_tpwd;
  275. result.shortLinkurl = AlimamaPlus.GetLink(result.taoToken);
  276. result.itemName = result.taoToken.GetContentPart(result.shortLinkurl, "").Trim();
  277. result.deeplink_url = AlimamaPlus.GetDeeplink(result.shortLinkurl);
  278. return result;
  279. }
  280. else
  281. {
  282. result.content = result.rawContent;
  283. switch (message)
  284. {
  285. case "该口令无效,请检查后重新提交":
  286. case "该商品已下架":
  287. result.message = "转链失败";
  288. result.reason = message;
  289. result.subCode = TkSubCodeEnum.NoConvert;
  290. break;
  291. default:
  292. result.message = "转链失败";
  293. result.reason = message;
  294. result.subCode = TkSubCodeEnum.NetError;
  295. break;
  296. }
  297. return result;
  298. }
  299. }
  300. }
  301. }