XunFeilJYYController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using TEAMModelOS.SDK.DI;
  5. using TEAMModelOS.SDK;
  6. using TEAMModelOS.Models;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.Logging;
  9. using Microsoft.Extensions.Options;
  10. using Microsoft.AspNetCore.Authorization;
  11. using System.Threading.Tasks;
  12. using TEAMModelOS.Controllers.Third.LePei;
  13. using Microsoft.Azure.Amqp.Encoding;
  14. using System;
  15. using TEAMModelOS.SDK.Extension;
  16. using System.Text.Json;
  17. using System.Web;
  18. using System.Text;
  19. using System.Net.Http;
  20. using System.Net.Http.Json;
  21. using Azure.Core;
  22. using Microsoft.Azure.Cosmos;
  23. namespace TEAMModelOS.Controllers.Third.XunFeiJYY
  24. {
  25. /// <summary>
  26. /// 讯飞教育云
  27. /// </summary>
  28. [ProducesResponseType(StatusCodes.Status200OK)]
  29. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  30. [ApiController]
  31. [Route("xunfei-jjy")]
  32. public class XunFeilJYYController : ControllerBase
  33. {
  34. private static readonly string authurl = "http://www.jyyun.com/sso-oauth";
  35. private static readonly string authorize = "authorize";
  36. private static readonly string getOpenId = "getOpenId";
  37. private static readonly string getUserInfo = "getUserInfo";
  38. private static readonly string url= "https://open.jyyun.com";
  39. private static readonly string getUserByUserId = "getUserByUserId";
  40. private static readonly string appid = "ea4e44e1dea54caba7b06bdea72970fa";
  41. private static readonly string appsecret = "bb79a189fb9c22e9ae1a62b279a112c0";
  42. private static readonly string apikey = "305C300D06092A864886F70D0101010500034B003048024100C362EAB80DDFD682367592DD8274A40A9FE9F37DDEC56AC8E0FBE4A5A6D9F6FDAAA495460821CA94EF4A955B582822D7BB289142F2C562FC04DA2D3B940C3D730203010001";
  43. private static readonly string testuserid = "fbc284072a40da84adbe206f721b3285bc5101240c006447328c7afa3b7ae20d122a14a73144eea261756cce3e83c3f6ef940d9e8d823c7c";
  44. private static readonly string testschlid = "fbc284072a40da841b643aa367013eb18a1d351ed46b0feb15f629530e2f3c1137ae30823af5c69d3b88ef0fa3c761efee102032fc5d2a5a";
  45. private readonly AzureCosmosFactory _azureCosmos;
  46. private readonly DingDing _dingDing;
  47. private readonly Option _option;
  48. private readonly AzureStorageFactory _azureStorage;
  49. private readonly CoreAPIHttpService _coreAPIHttpService;
  50. private readonly IWebHostEnvironment _environment;
  51. private readonly IConfiguration _configuration;
  52. private readonly ILogger<XunFeilJYYController> _logger;
  53. private readonly IHttpClientFactory _httpClientFactory;
  54. public XunFeilJYYController(ILogger<XunFeilJYYController> logger, IConfiguration configuration, IWebHostEnvironment environment,
  55. CoreAPIHttpService coreAPIHttpService, AzureStorageFactory azureStorage, DingDing dingDing, AzureCosmosFactory azureCosmos, IOptionsSnapshot<Option> option)
  56. {
  57. _logger = logger;
  58. _configuration = configuration;
  59. _environment = environment;
  60. _coreAPIHttpService = coreAPIHttpService;
  61. _azureStorage = azureStorage;
  62. _dingDing = dingDing;
  63. _azureCosmos = azureCosmos;
  64. _option = option?.Value;
  65. }
  66. [HttpPost("gen-sso")]
  67. [AllowAnonymous]
  68. public async Task<IActionResult> GenSso(JsonElement json )
  69. {
  70. json.TryGetProperty("p", out var p);
  71. if ($"{p}".Equals("xunfei-jjy-sso"))
  72. {
  73. string HostName = HttpContext.GetHostName();
  74. if (!string.IsNullOrWhiteSpace(_option.HostName))
  75. {
  76. HostName = _option.HostName;
  77. }
  78. string xunfei_url = $"{authurl}/{authorize}?appkey={appid}&redirectUri={HttpUtility.UrlEncode($"https://{HostName}/xunfei-jjy/sso", Encoding.UTF8)}&state={DateTimeOffset.Now.ToUnixTimeMilliseconds()}&display=web";
  79. return Ok(new { xunfei_url, code = 200 });
  80. }
  81. else
  82. {
  83. return Ok(new { code = 400, msg = "参数错误" });
  84. }
  85. }
  86. [HttpGet("sso")]
  87. [AllowAnonymous]
  88. public async Task<IActionResult> Sso([FromQuery]string accesstoken,[FromQuery]string state)
  89. {
  90. //前端缓存accesstoken,用于做免登录验证。直接获取用户的Token .
  91. var httpClient= _httpClientFactory.CreateClient();
  92. HttpResponseMessage response_getOpenId = await httpClient.PostAsJsonAsync($"{authurl}/{getOpenId}",new { appkey= appid , appsecret= appsecret, accessToken= accesstoken });
  93. if (response_getOpenId.IsSuccessStatusCode)
  94. {
  95. var result = await response_getOpenId.Content.ReadAsStringAsync();
  96. var resultData= result.ToObject<JsonElement>();
  97. if (resultData.TryGetProperty("data", out JsonElement data))
  98. {
  99. }
  100. }
  101. return Ok(new { code = 200 });
  102. }
  103. }
  104. }