ApiController.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 aid = form.Read("aid", 0);
  82. //验证签名
  83. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  84. {
  85. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  86. }
  87. DateTime time1 = t.Convert2Datetime();
  88. if (time1 < DateTime.Now.AddMinutes(-10))
  89. {
  90. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  91. }
  92. var account = await ApiAccountCore.GetOneAsync(a);
  93. if (account == null)
  94. {
  95. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  96. }
  97. string app_sign = $"{account.api_secret}@{t}".MD5();
  98. if (sign != app_sign)
  99. {
  100. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  101. }
  102. if (!string.IsNullOrEmpty(url))
  103. {
  104. var bytes = new WebClientUtility().Request(url).ResponseBody;
  105. img = Convert.ToBase64String(bytes);
  106. }
  107. return await core.PromotionQueryAsync(img, oaid, ip, scene, recommand, false, aid);
  108. }
  109. [HttpPost]
  110. public async Task<ActionResult> unsafePromotionQuery([FromBody] JsonElement form)
  111. {
  112. var img = form.Read("img", string.Empty);
  113. var url = form.Read("url", string.Empty);
  114. var tkl = form.Read("tkl", string.Empty);
  115. var itemid = form.Read("itemid", string.Empty);
  116. var ip = form.Read("ip", string.Empty);
  117. var oaid = form.Read("oaid", string.Empty);
  118. var scene = form.Read("scene", string.Empty);
  119. var recommand = form.Read("recommand", true);
  120. var debug = form.Read("debug", false);
  121. var aid = form.Read("aid", 0);
  122. int count = form.Read("count", 0);
  123. if (!string.IsNullOrEmpty(url))
  124. {
  125. var bytes = new WebClientUtility().Request(url).ResponseBody;
  126. img = Convert.ToBase64String(bytes);
  127. }
  128. if (string.IsNullOrEmpty(oaid))
  129. {
  130. Random rand = new Random();
  131. oaid = $"{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}";
  132. }
  133. if (count > 1)
  134. {
  135. for (int i = 0; i < count; i++)
  136. {
  137. _ = core.PromotionQueryAsync(img, oaid, ip, scene, recommand, debug, aid);
  138. }
  139. return new APIResult(new { msg = "ok" });
  140. }
  141. // 从口令解析商品图 临时需求
  142. if (!string.IsNullOrEmpty(tkl))
  143. {
  144. string goods_url = AlimamaPlus.GetLink(tkl);
  145. (bool successed, string itemUrl) = AlimamaPlus.GetItemUrl(goods_url);
  146. if (successed && !string.IsNullOrEmpty(itemUrl))
  147. {
  148. itemid = AlimamaPlus.ExtractItemId(itemUrl);
  149. (APIResult target, PromotionQueryDTO data) = await core.PromotionQueryByItemIdAsync(itemid, oaid, ip, scene, recommand, debug, aid);
  150. if (data != null)
  151. {
  152. url = data.PromotionImg[0].pic;
  153. if (!string.IsNullOrEmpty(url))
  154. {
  155. var bytes = new WebClientUtility().Request(url).ResponseBody;
  156. img = Convert.ToBase64String(bytes);
  157. }
  158. }
  159. return await core.PromotionQueryAsync(img, oaid, ip, scene, recommand, debug, aid);
  160. }
  161. if (!string.IsNullOrEmpty(itemid))
  162. {
  163. (APIResult result, PromotionQueryDTO data) = await core.PromotionQueryByItemIdAsync(itemid, oaid, ip, scene, recommand, debug, aid);
  164. return result;
  165. }
  166. }
  167. return await core.PromotionQueryAsync(img, oaid, ip, scene, recommand, debug, aid);
  168. }
  169. [HttpGet]
  170. public async Task<ActionResult> PromotionQuerySampleLogs([FromQuery] int n = 20)
  171. {
  172. int count = Math.Clamp(n, 1, 100);
  173. string logDir = Path.Combine(AppContext.BaseDirectory, "log", "promotion_query_samples");
  174. if (!Directory.Exists(logDir))
  175. {
  176. return new APIResult(new
  177. {
  178. success = true,
  179. count = 0,
  180. logs = Array.Empty<object>()
  181. });
  182. }
  183. var files = Directory.EnumerateFiles(logDir, "*.log", SearchOption.TopDirectoryOnly)
  184. .Where(file => !Path.GetFileName(file).StartsWith("write_error_", StringComparison.OrdinalIgnoreCase))
  185. .Select(file => new FileInfo(file))
  186. .OrderByDescending(file => file.LastWriteTime)
  187. .Take(count)
  188. .ToList();
  189. var logs = new List<object>();
  190. foreach (var file in files)
  191. {
  192. logs.Add(new
  193. {
  194. fileName = file.Name,
  195. writeTime = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
  196. content = await System.IO.File.ReadAllTextAsync(file.FullName)
  197. });
  198. }
  199. return new APIResult(new
  200. {
  201. success = true,
  202. count = logs.Count,
  203. logs
  204. });
  205. }
  206. }
  207. }