ApiController.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. namespace molilian.api.Controllers
  15. {
  16. [ApiController]
  17. [Route("api/[action]")]
  18. public class ApiController : ControllerBase
  19. {
  20. protected IHttpContextAccessor _accessor;
  21. TaokeOpenCore core;
  22. public ApiController(IHttpContextAccessor accessor)
  23. {
  24. _accessor = accessor;
  25. core = new TaokeOpenCore();
  26. }
  27. [HttpPost]
  28. public async Task<ActionResult> PromotionQuery([FromBody] JsonElement form, [FromQuery] string a = "", [FromQuery] int t = 0, [FromQuery] string sign = "")
  29. {
  30. var img = form.Read("img", string.Empty);
  31. var url = form.Read("url", string.Empty);
  32. var ip = form.Read("ip", string.Empty);
  33. var oaid = form.Read("oaid", string.Empty);
  34. //验证签名
  35. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  36. {
  37. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  38. }
  39. DateTime time1 = t.Convert2Datetime();
  40. if (time1 < DateTime.Now.AddMinutes(-10))
  41. {
  42. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  43. }
  44. var account = ApiAccountCore.GetOne(a);
  45. if (account == null)
  46. {
  47. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  48. }
  49. string app_sign = $"{account.api_secret}@{t}".MD5();
  50. if (sign != app_sign)
  51. {
  52. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  53. }
  54. if (!string.IsNullOrEmpty(url))
  55. {
  56. var bytes = new WebClientUtility().Request(url).ResponseBody;
  57. img = Convert.ToBase64String(bytes);
  58. }
  59. return await core.PromotionQueryAsync(img, oaid, ip);
  60. }
  61. [HttpPost]
  62. public async Task<ActionResult> unsafePromotionQuery([FromBody] JsonElement form)
  63. {
  64. var img = form.Read("img", string.Empty);
  65. var url = form.Read("url", string.Empty);
  66. var ip = form.Read("ip", string.Empty);
  67. var oaid = form.Read("oaid", string.Empty);
  68. var recommand = form.Read("recommand", true);
  69. if (!string.IsNullOrEmpty(url))
  70. {
  71. var bytes = new WebClientUtility().Request(url).ResponseBody;
  72. img = Convert.ToBase64String(bytes);
  73. }
  74. if (string.IsNullOrEmpty(oaid))
  75. {
  76. Random rand = new Random();
  77. oaid = $"{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}";
  78. }
  79. return await core.PromotionQueryAsync(img, oaid, ip, recommand);
  80. }
  81. }
  82. }