DataokeCore.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 Dataoke;
  10. using Google.Protobuf.WellKnownTypes;
  11. using System.Diagnostics;
  12. using System.Text.Json;
  13. using TencentCloud.Tcm.V20210413.Models;
  14. namespace molilian.core
  15. {
  16. public partial class DataokeCore
  17. {
  18. private static readonly object _lockObj = new();
  19. private static IEnumerable<DataokeDTO> _cached;
  20. public static DataokeDTO? GetOne(string domain = "")
  21. {
  22. var list = List();
  23. if (!list.Any()) return null;
  24. domain = domain.ToLower();
  25. return list.Where(e => FilterReferer(e, domain)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  26. }
  27. public static IEnumerable<DataokeDTO> List(bool force = false)
  28. {
  29. if (!force && _cached != null) return _cached;
  30. string cache_key = $"cache:dataoke_pool";
  31. var list = RedisHelper.Get<IEnumerable<DataokeDTO>>(cache_key);
  32. if (force || list == null)
  33. {
  34. lock (_lockObj)
  35. {
  36. list = new DBContext.Table("dataoke_pool")
  37. .Where("status=@status", new { status = 1 })
  38. .Select<DataokeDTO>();
  39. if (list == null) return default;
  40. RedisHelper.Set(cache_key, list, 30 * 86400);
  41. }
  42. }
  43. _cached = list;
  44. return list;
  45. }
  46. private static bool FilterReferer(DataokeDTO item, string domain)
  47. {
  48. if (string.IsNullOrEmpty(domain)) return true;
  49. if (string.IsNullOrEmpty(item.domain)) return true;
  50. if (domain.Contains(item.domain)) return true;
  51. return false;
  52. }
  53. public static void Refresh()
  54. {
  55. _ = List(true);
  56. }
  57. public static void Disabled(string name)
  58. {
  59. string cache_key = $"cache:dataoke_pool:{name}:disabled";
  60. long count = RedisHelper.IncrBy(cache_key);
  61. RedisHelper.Expire(cache_key, 10);
  62. if (count > 1) return;
  63. new DBContext.Table("dataoke_pool")
  64. .Add("status", 0)
  65. .Where("name=@name", new { name })
  66. .Update();
  67. _ = List(true);
  68. NotifyCore.Notify(new NifyMessage
  69. {
  70. message = $"【大淘客:{name}】调用异常",
  71. priority = NifyMessagePriority.high,
  72. tags = ["red_circle"]
  73. });
  74. }
  75. public static JdDataDTO JdParse(JdDataDTO result, DataokeDTO api, JdPoolDTO account, string content)
  76. {
  77. string message = string.Empty;
  78. string url = JdUnionPlus.GetLink(content);
  79. string shortLinkurl = url;
  80. var plus = new DataokePlus(api.app_key, api.app_secret);
  81. string body = plus.JDParse(account.unionid, url);
  82. //result.link_type = plus.IsAffLlink(url) ? LinkTypeEnum.other_aff : LinkTypeEnum.goods;
  83. result.link_type = JdUnionPlus.GetLinkType(url, out string out_url, out string itemId);
  84. url = out_url;
  85. result.itemId = itemId;
  86. if (result.link_type == LinkTypeEnum.other_aff)
  87. {
  88. result.success = false;
  89. result.link_type = LinkTypeEnum.unknown;
  90. result.channel_type = ChannelTypeEnum.jd;
  91. message = "其他推广链接";
  92. result.accountName = string.Empty;
  93. result.content = content;
  94. result.shortLinkurl = url;
  95. result.deeplink_url = JdUnionPlus.GetDeeplink(null);
  96. }
  97. else
  98. {
  99. JsonElement root;
  100. try
  101. {
  102. root = body.Convert2JsonElement();
  103. }
  104. catch
  105. {
  106. throw new APIException(body);
  107. }
  108. int code = root.Read("code", -1);
  109. bool success = code == 0;
  110. if (success)
  111. {
  112. shortLinkurl = root.PathRead("data.shortUrl", string.Empty);
  113. }
  114. else
  115. {
  116. message = body;
  117. }
  118. result.content = shortLinkurl;
  119. result.shortLinkurl = shortLinkurl;
  120. result.success = success;
  121. result.message = message;
  122. result.accountName = account.name;
  123. GoodsDetailDTO goods = null;
  124. if (success) goods = plus.GetGoods(shortLinkurl);
  125. result.deeplink_url = JdUnionPlus.GetDeeplink(shortLinkurl);
  126. if (goods != null)
  127. {
  128. result.itemName = goods.skuName;
  129. }
  130. }
  131. return result;
  132. }
  133. public static GoodsDetailDTO? GetCoupon(DataokeDTO api, JdPoolDTO account, string url, string itemId)
  134. {
  135. var plus = new DataokePlus(api.app_key, api.app_secret);
  136. var item = plus.GetDetails([itemId]);
  137. if (item != null)
  138. {
  139. try
  140. {
  141. string body = plus.JDParse(account.unionid, url, item.couponLink);
  142. JsonElement root = body.Convert2JsonElement();
  143. int code = root.Read("code", -1);
  144. if (code != 0) return null;
  145. item.couponLink = root.PathRead("data.shortUrl", string.Empty);
  146. }
  147. catch
  148. {
  149. return null;
  150. }
  151. }
  152. return item;
  153. }
  154. }
  155. }