ApiController.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Org.BouncyCastle.Ocsp;
  5. using System.Text.Json;
  6. using System.Runtime.InteropServices;
  7. using System.Net;
  8. using System.Security.Cryptography;
  9. using Microsoft.AspNetCore.Authentication;
  10. using TencentCloud.Ecm.V20190719.Models;
  11. using COSXML.Network;
  12. using System.Linq;
  13. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  14. using TencentCloud.Lighthouse.V20200324.Models;
  15. using TencentCloud.Ame.V20190916.Models;
  16. using TencentCloud.Omics.V20221128.Models;
  17. using System.Threading.Channels;
  18. namespace molilian.api.Controllers
  19. {
  20. [ApiController]
  21. [Route("api/[action]")]
  22. public class ApiController : ControllerBase
  23. {
  24. protected IHttpContextAccessor _accessor;
  25. TaokeOpenCore core;
  26. public ApiController(IHttpContextAccessor accessor)
  27. {
  28. _accessor = accessor;
  29. core = new TaokeOpenCore();
  30. }
  31. //[HttpGet]
  32. //public async Task<ActionResult> RequestAsync([FromQuery] string url, [FromQuery] int proxyId)
  33. //{
  34. // if (string.IsNullOrEmpty(url))
  35. // {
  36. // return BadRequest("URL cannot be empty");
  37. // }
  38. // var proxy = ProxyNodesCore.GetOne(proxyId);
  39. // if (proxy == null)
  40. // {
  41. // return Unauthorized("Invalid proxy configuration");
  42. // }
  43. // WebClientUtility cli = new()
  44. // {
  45. // Proxy = proxy
  46. // };
  47. // await cli.RequestAsync(url);
  48. // var content = cli.Body();
  49. // var contentType = cli.ResponseMessage.Content.Headers.ContentType?.MediaType ?? "text/plain";
  50. // return Content(content, contentType);
  51. //}
  52. [HttpGet]
  53. public async Task<ActionResult> RequestAsync([FromQuery] string url, [FromQuery] string proxy)
  54. {
  55. if (string.IsNullOrEmpty(url))
  56. {
  57. return BadRequest("URL cannot be empty");
  58. }
  59. WebProxy _proxy = ProxyManager.Parse(proxy);
  60. WebClientUtility cli = new();
  61. cli.Proxy = _proxy;
  62. cli.Timeout = TimeSpan.FromSeconds(10);
  63. await cli.RequestAsync(url);
  64. if (cli.Successed)
  65. {
  66. var content = cli.Body();
  67. var contentType = cli.ResponseMessage.Content.Headers.ContentType?.MediaType ?? "text/plain";
  68. return Content(content, contentType);
  69. }
  70. throw cli.ResponseException;
  71. }
  72. [HttpPost]
  73. public async Task<ActionResult> PromotionQuery([FromBody] JsonElement form, [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
  74. {
  75. var img = form.Read("img", string.Empty);
  76. var url = form.Read("url", string.Empty);
  77. var ip = form.Read("ip", string.Empty);
  78. var oaid = form.Read("oaid", string.Empty);
  79. var scene = form.Read("scene", string.Empty);
  80. var recommand = form.Read("recommand", true);
  81. var useCouponLinkFirst = form.Read("useCouponLinkFirst", true);
  82. var aid = form.Read("aid", 0);
  83. //验证签名
  84. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  85. {
  86. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  87. }
  88. DateTime time1 = t.Convert2Datetime();
  89. if (time1 < DateTime.Now.AddMinutes(-10))
  90. {
  91. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  92. }
  93. var account = await ApiAccountCore.GetOneAsync(a);
  94. if (account == null)
  95. {
  96. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  97. }
  98. string app_sign = $"{account.api_secret}@{t}".MD5();
  99. if (sign != app_sign)
  100. {
  101. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  102. }
  103. if (!string.IsNullOrEmpty(url))
  104. {
  105. var bytes = new WebClientUtility().Request(url).ResponseBody;
  106. img = Convert.ToBase64String(bytes);
  107. }
  108. return await core.PromotionQueryAsync(img, oaid, ip, scene, recommand, false, aid, useCouponLinkFirst);
  109. }
  110. [HttpPost]
  111. public async Task<ActionResult> unsafePromotionQuery([FromBody] JsonElement form)
  112. {
  113. var img = form.Read("img", string.Empty);
  114. var url = form.Read("url", string.Empty);
  115. var tkl = form.Read("tkl", string.Empty);
  116. var itemid = form.Read("itemid", string.Empty);
  117. var ip = form.Read("ip", string.Empty);
  118. var oaid = form.Read("oaid", string.Empty);
  119. var scene = form.Read("scene", string.Empty);
  120. var recommand = form.Read("recommand", true);
  121. var debug = form.Read("debug", false);
  122. var useCouponLinkFirst = form.Read("useCouponLinkFirst", true);
  123. var aid = form.Read("aid", 0);
  124. int count = form.Read("count", 0);
  125. if (!string.IsNullOrEmpty(url))
  126. {
  127. var bytes = new WebClientUtility().Request(url).ResponseBody;
  128. img = Convert.ToBase64String(bytes);
  129. }
  130. if (string.IsNullOrEmpty(oaid))
  131. {
  132. Random rand = new Random();
  133. oaid = $"{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}";
  134. }
  135. if (count > 1)
  136. {
  137. for (int i = 0; i < count; i++)
  138. {
  139. _ = core.PromotionQueryAsync(img, oaid, ip, scene, recommand, debug, aid, useCouponLinkFirst);
  140. }
  141. return new APIResult(new { msg = "ok" });
  142. }
  143. // 从口令解析商品图 临时需求
  144. if (!string.IsNullOrEmpty(tkl))
  145. {
  146. string goods_url = AlimamaPlus.GetLink(tkl);
  147. (bool successed, string itemUrl) = AlimamaPlus.GetItemUrl(goods_url);
  148. if (successed && !string.IsNullOrEmpty(itemUrl))
  149. {
  150. itemid = AlimamaPlus.ExtractItemId(itemUrl);
  151. (APIResult target, PromotionQueryDTO data) = await core.PromotionQueryByItemIdAsync(itemid, oaid, ip, scene, recommand, debug, aid);
  152. if (data != null)
  153. {
  154. url = data.PromotionImg[0].pic;
  155. if (!string.IsNullOrEmpty(url))
  156. {
  157. var bytes = new WebClientUtility().Request(url).ResponseBody;
  158. img = Convert.ToBase64String(bytes);
  159. }
  160. }
  161. return await core.PromotionQueryAsync(img, oaid, ip, scene, recommand, debug, aid, useCouponLinkFirst);
  162. }
  163. if (!string.IsNullOrEmpty(itemid))
  164. {
  165. (APIResult result, PromotionQueryDTO data) = await core.PromotionQueryByItemIdAsync(itemid, oaid, ip, scene, recommand, debug, aid, useCouponLinkFirst);
  166. return result;
  167. }
  168. }
  169. return await core.PromotionQueryAsync(img, oaid, ip, scene, recommand, debug, aid, useCouponLinkFirst);
  170. }
  171. [HttpGet]
  172. public async Task<ActionResult> PromotionQuerySampleLogs([FromQuery] int n = 20)
  173. {
  174. int count = Math.Clamp(n, 1, 100);
  175. string logDir = Path.Combine(AppContext.BaseDirectory, "log", "promotion_query_samples");
  176. if (!Directory.Exists(logDir))
  177. {
  178. return new APIResult(new
  179. {
  180. success = true,
  181. count = 0,
  182. logs = Array.Empty<object>()
  183. });
  184. }
  185. var files = Directory.EnumerateFiles(logDir, "*.log", SearchOption.TopDirectoryOnly)
  186. .Where(file => !Path.GetFileName(file).StartsWith("write_error_", StringComparison.OrdinalIgnoreCase))
  187. .Select(file => new FileInfo(file))
  188. .OrderByDescending(file => file.LastWriteTime)
  189. .Take(count)
  190. .ToList();
  191. var logs = new List<object>();
  192. foreach (var file in files)
  193. {
  194. logs.Add(new
  195. {
  196. fileName = file.Name,
  197. writeTime = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
  198. content = await System.IO.File.ReadAllTextAsync(file.FullName)
  199. });
  200. }
  201. return new APIResult(new
  202. {
  203. success = true,
  204. count = logs.Count,
  205. logs
  206. });
  207. }
  208. }
  209. }