H5Controller.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Dataoke;
  12. using System.Web;
  13. namespace molilian.api.Controllers
  14. {
  15. [ApiController]
  16. [Route("[controller]/[action]")]
  17. public class H5Controller : ControllerBase
  18. {
  19. protected IHttpContextAccessor _accessor;
  20. public H5Controller(IHttpContextAccessor accessor)
  21. {
  22. _accessor = accessor;
  23. }
  24. [HttpGet]
  25. public ActionResult sign()
  26. {
  27. string referer = _accessor.HttpContext.Request.Referer();
  28. var api = DataokeCore.GetOne(referer);
  29. if (api == null)
  30. {
  31. return new APIResult(new
  32. {
  33. success = false,
  34. message = "没有可用签名账户"
  35. });
  36. }
  37. int nonce;
  38. string timer;
  39. DtkApp app = new(api.app_key, api.app_secret);
  40. string sign = app.MakeSign(out nonce, out timer);
  41. string sign_url = $"appKey={api.app_key}&nonce={nonce}&signRan={sign}&timer={timer}";
  42. return new APIResult(new
  43. {
  44. success = true,
  45. message = "ok",
  46. sign_url
  47. });
  48. }
  49. [HttpPost]
  50. public ActionResult ddsign([FromBody] JsonElement form)
  51. {
  52. var api = PddPoolCore.GetOne(PddUnionWorkMode.SiteApi);
  53. if (api == null)
  54. {
  55. return new APIResult(new
  56. {
  57. success = false,
  58. message = "没有可用签名账户"
  59. });
  60. }
  61. string ts = DateTime.Now.Convert2UnixTimestamp().ToString();
  62. string url = $"https://gw-api.pinduoduo.com/api/router";
  63. Dictionary<string, string> args = new Dictionary<string, string>
  64. {
  65. { "client_id", api.app_key },
  66. { "timestamp", ts }
  67. };
  68. // 提取客户端的业务参数并排除 "client_id" 和 "timestamp"
  69. foreach (JsonProperty prop in form.EnumerateObject())
  70. {
  71. if (prop.Name != "client_id" && prop.Name != "timestamp")
  72. {
  73. args.Add(prop.Name, prop.Value.GetString());
  74. }
  75. }
  76. ////其他业务参数接收客户端的form,记得排除client_id 和 timestamp
  77. //args.Add("type", "pdd.ddk.goods.recommend.get");
  78. //args.Add("data_type", "JSON");
  79. //args.Add("custom_parameters", "5");
  80. args = PddPoolCore.GenerateSignature(api, args);
  81. string queryString = string.Join("&", args.Select(kv => $"{HttpUtility.UrlEncode(kv.Key)}={HttpUtility.UrlEncode(kv.Value)}"));
  82. string sign_url = $"{url}?{queryString}";
  83. return new APIResult(new
  84. {
  85. success = true,
  86. message = "ok",
  87. sign_url
  88. });
  89. //WebClientUtility client = new WebClientUtility();
  90. //var response = client.Request(url);
  91. //var body = response.Body();
  92. //return Content(body);
  93. }
  94. }
  95. }