H5Controller.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 = PddUnionPlus.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. [HttpPost]
  95. public ActionResult jdsign([FromBody] JsonElement form)
  96. {
  97. var accountid = form.Read<int>("aid", 4);
  98. var method = form.Read<string>("method", string.Empty);
  99. var version = form.Read<string>("v", string.Empty);
  100. var param_json = form.Read<string>("param_json", string.Empty);
  101. var api = new DBContext.Table("jd_pool").Get<JdPoolDTO>("id=@id", new { id = accountid });
  102. string url = "https://api.jd.com/routerjson";
  103. string ts = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  104. Dictionary<string, string> args = new()
  105. {
  106. { "access_token", ""},
  107. { "app_key", api.app_key },
  108. { "method", method },
  109. { "v", version },
  110. { "360buy_param_json", param_json },
  111. { "timestamp", ts }
  112. };
  113. var plus = new JdUnionPlus(api);
  114. string sign = plus._sign(method, version, param_json, ts);
  115. args.Add("sign", sign);
  116. string queryString = string.Join("&", args.Select(kv => $"{HttpUtility.UrlEncode(kv.Key)}={HttpUtility.UrlEncode(kv.Value)}"));
  117. string sign_url = $"{url}?{queryString}";
  118. return new APIResult(new
  119. {
  120. success = true,
  121. message = "ok",
  122. sign_url
  123. });
  124. }
  125. [HttpPost]
  126. public ActionResult jdrequest([FromBody] JsonElement form)
  127. {
  128. var accountid = form.Read<int>("aid", 4);
  129. var method = form.Read<string>("method", string.Empty);
  130. var version = form.Read<string>("v", string.Empty);
  131. var param_json = form.Read<string>("param_json", string.Empty);
  132. var api = new DBContext.Table("jd_pool").Get<JdPoolDTO>("id=@id", new { id = accountid });
  133. if (api == null)
  134. {
  135. return new APIResult(new
  136. {
  137. success = false,
  138. message = "没有可用签名账户"
  139. });
  140. }
  141. string url = "https://api.jd.com/routerjson";
  142. string ts = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  143. Dictionary<string, string> args = new()
  144. {
  145. { "access_token", ""},
  146. { "app_key", api.app_key },
  147. { "method", method },
  148. { "v", version },
  149. { "360buy_param_json", param_json },
  150. { "timestamp", ts }
  151. };
  152. var plus = new JdUnionPlus(api);
  153. var result = plus.Request(method, version, param_json);
  154. return Content(result, "application/json");
  155. }
  156. }
  157. }