ApiController.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. var recommand = form.Read("recommand", true);
  35. //验证签名
  36. if (string.IsNullOrEmpty(a) || string.IsNullOrEmpty(sign))
  37. {
  38. return new APIResult(new { success = false, message = "验证错误" }, APIResultCodeEnum.Unauthorized);
  39. }
  40. DateTime time1 = t.Convert2Datetime();
  41. if (time1 < DateTime.Now.AddMinutes(-10))
  42. {
  43. return new APIResult(new { success = false, message = "验证错误2" }, APIResultCodeEnum.Unauthorized);
  44. }
  45. var account = ApiAccountCore.GetOne(a);
  46. if (account == null)
  47. {
  48. return new APIResult(new { success = false, message = "验证错误3" }, APIResultCodeEnum.Unauthorized);
  49. }
  50. string app_sign = $"{account.api_secret}@{t}".MD5();
  51. if (sign != app_sign)
  52. {
  53. return new APIResult(new { success = false, message = "验证错误4" }, APIResultCodeEnum.Unauthorized);
  54. }
  55. if (!string.IsNullOrEmpty(url))
  56. {
  57. var bytes = new WebClientUtility().Request(url).ResponseBody;
  58. img = Convert.ToBase64String(bytes);
  59. }
  60. return await core.PromotionQueryAsync(img, oaid, ip, recommand);
  61. }
  62. [HttpPost]
  63. public async Task<ActionResult> unsafePromotionQuery([FromBody] JsonElement form)
  64. {
  65. var img = form.Read("img", string.Empty);
  66. var url = form.Read("url", string.Empty);
  67. var ip = form.Read("ip", string.Empty);
  68. var oaid = form.Read("oaid", string.Empty);
  69. var recommand = form.Read("recommand", true);
  70. var debug = form.Read("debug", false);
  71. if (!string.IsNullOrEmpty(url))
  72. {
  73. var bytes = new WebClientUtility().Request(url).ResponseBody;
  74. img = Convert.ToBase64String(bytes);
  75. }
  76. if (string.IsNullOrEmpty(oaid))
  77. {
  78. Random rand = new Random();
  79. oaid = $"{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}-{rand.Next(100000, 999999)}";
  80. }
  81. return await core.PromotionQueryAsync(img, oaid, ip, recommand, debug);
  82. }
  83. }
  84. }