HomeController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using IES.ExamServer.Helper;
  2. using Microsoft.AspNetCore.Http.HttpResults;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Caching.Memory;
  5. using Microsoft.Extensions.Configuration;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net.Http;
  9. using System.Text.Json.Nodes;
  10. using System.DrawingCore;
  11. using System.DrawingCore.Imaging;
  12. using System.DirectoryServices.ActiveDirectory;
  13. using ZXing.QrCode.Internal;
  14. using System.Text.Json;
  15. using ZXing.Aztec.Internal;
  16. using System.IdentityModel.Tokens.Jwt;
  17. using IES.ExamServer.Models;
  18. using System.Net.Http.Json;
  19. namespace IES.ExamServer.Controllers
  20. {
  21. [Route("core")]
  22. [ApiController]
  23. public class HomeController : ControllerBase
  24. {
  25. private readonly IConfiguration _configuration;
  26. private readonly IHttpClientFactory _httpClientFactory;
  27. private readonly IMemoryCache _memoryCache;
  28. public HomeController(IConfiguration configuration, IHttpClientFactory httpClientFactory, IMemoryCache memoryCache)
  29. {
  30. _configuration=configuration;
  31. _httpClientFactory=httpClientFactory;
  32. _memoryCache=memoryCache;
  33. }
  34. [HttpGet("/init")]
  35. public async Task<IActionResult> Init()
  36. {
  37. int code = 0;
  38. string msg = string.Empty;
  39. try {
  40. _memoryCache.TryGetValue(Constant._KeyServerCenter, out JsonNode? data);
  41. if (data!=null)
  42. {
  43. return Ok(new { code = 200, msg = "云端服务连接成功!", data = data });
  44. }
  45. else {
  46. code=500;
  47. msg="云端服务未连接!";
  48. }
  49. } catch (Exception ex)
  50. {
  51. code=500;
  52. msg="云端服务未连接!";
  53. }
  54. return Ok(new { code, msg });
  55. }
  56. /**
  57. {
  58. "type":"sms",//qrcode二维码扫码登录:randomCode必传; sms 短信验证登录:randomCode必传,mobile必传
  59. "randomCode",
  60. "mobile":"1528377****"
  61. }
  62. **/
  63. /// <summary>
  64. /// 登录验证
  65. /// </summary>
  66. /// <param name="randomCode"></param>
  67. /// <returns></returns>
  68. [HttpPost("/login-check")]
  69. public async Task<IActionResult> LoginCheck(JsonNode json)
  70. {
  71. string randomCode = $"{json["randomCode"]}";
  72. System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
  73. string? CenterUrl = _configuration.GetValue<string>("ExamServer:CenterUrl");
  74. var response = await _httpClientFactory.CreateClient().GetAsync($"{CenterUrl}/hita/check-login?code={randomCode}");
  75. if (response.IsSuccessStatusCode)
  76. {
  77. string content = await response.Content.ReadAsStringAsync();
  78. if (!string.IsNullOrWhiteSpace(content))
  79. {
  80. var json = JsonSerializer.Deserialize<JsonNode>(content);
  81. if (json != null)
  82. {
  83. TmdidImplicit? token = JsonSerializer.Deserialize<TmdidImplicit>(json["implicit_token"]);
  84. string x_auth_token = $"{json["x_auth_token"]}";
  85. List<School>? schools = JsonSerializer.Deserialize<List<School>>(json["schools"]);
  86. var jwt = new JwtSecurityToken(token?.id_token);
  87. var id = jwt.Payload.Sub;
  88. jwt.Payload.TryGetValue("name", out object? name);
  89. jwt.Payload.TryGetValue("picture", out object? picture);
  90. }
  91. }
  92. }
  93. return Ok();
  94. }
  95. /// <summary>
  96. /// 登录模式初始化
  97. /// </summary>
  98. /// <returns></returns>
  99. [HttpGet("/login-init")]
  100. public async Task<IActionResult> LoginInit(JsonNode json)
  101. {
  102. var type = json["type"];
  103. string qrcode = string.Empty;
  104. string randomCode = "";
  105. switch (true)
  106. {
  107. case bool when $"{type}".Equals("qrcode"):
  108. {
  109. // 生成二维码图片
  110. Random random = new Random();
  111. randomCode = $"{random.Next(1000, 9999)}";
  112. string? CenterUrl = _configuration.GetValue<string>("ExamServer:CenterUrl");
  113. string content = $"{CenterUrl}/joinSchool?schoolCode=login:{randomCode}&m=%E7%99%BB%E5%BD%95&o=1";
  114. Bitmap qrCodeImage = QRCodeHelper.GetBitmap(content, 200, 200);
  115. using (MemoryStream stream = new MemoryStream())
  116. {
  117. qrCodeImage.Save(stream, ImageFormat.Png);
  118. byte[] data = stream.ToArray();
  119. qrcode=$"data:image/png;base64,{Convert.ToBase64String(data)}";
  120. }
  121. return Ok(new { code = 200, randomCode = randomCode, qrcode, type });
  122. }
  123. case bool when $"{type}".Equals("smspin"):
  124. {
  125. int send = 0;
  126. if ( !string.IsNullOrWhiteSpace($"{json["area"]}") && !string.IsNullOrWhiteSpace($"{json["to"]}"))
  127. {
  128. string? CenterUrl = _configuration.GetValue<string>("ExamServer:CenterUrl");
  129. string url = $"{CenterUrl}/core/sendsms/pin";
  130. HttpResponseMessage message= await _httpClientFactory.CreateClient().PostAsJsonAsync(url, new { });
  131. if (message.IsSuccessStatusCode)
  132. {
  133. string content =await message.Content.ReadAsStringAsync();
  134. JsonNode? jsonNode = JsonSerializer.Deserialize<JsonNode>(content);
  135. if (jsonNode!=null && int.TryParse($"{jsonNode["send"]}", out int s))
  136. {
  137. send = s;
  138. }
  139. }
  140. }
  141. return Ok(new { code = 200, send, type });
  142. }
  143. }
  144. return Ok(new { code = 400});
  145. }
  146. }
  147. }