DataokeCore.cs 5.4 KB

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