DataokeCore.cs 5.4 KB

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