123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using IES.ExamServer.Helper;
- using Microsoft.AspNetCore.Http.HttpResults;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Caching.Memory;
- using Microsoft.Extensions.Configuration;
-
- using System.IO;
- using System.Linq;
- using System.Net.Http;
- using System.Text.Json.Nodes;
-
-
- using System.DrawingCore;
- using System.DrawingCore.Imaging;
- using System.DirectoryServices.ActiveDirectory;
- using ZXing.QrCode.Internal;
- using System.Text.Json;
- using ZXing.Aztec.Internal;
- using System.IdentityModel.Tokens.Jwt;
- using IES.ExamServer.Models;
- using System.Net.Http.Json;
- namespace IES.ExamServer.Controllers
- {
- [Route("core")]
- [ApiController]
- public class HomeController : ControllerBase
- {
- private readonly IConfiguration _configuration;
- private readonly IHttpClientFactory _httpClientFactory;
- private readonly IMemoryCache _memoryCache;
- public HomeController(IConfiguration configuration, IHttpClientFactory httpClientFactory, IMemoryCache memoryCache)
- {
- _configuration=configuration;
- _httpClientFactory=httpClientFactory;
- _memoryCache=memoryCache;
- }
- [HttpGet("/init")]
- public async Task<IActionResult> Init()
- {
- int code = 0;
- string msg = string.Empty;
- try {
- _memoryCache.TryGetValue(Constant._KeyServerCenter, out JsonNode? data);
- if (data!=null)
- {
- return Ok(new { code = 200, msg = "云端服务连接成功!", data = data });
- }
- else {
- code=500;
- msg="云端服务未连接!";
- }
- } catch (Exception ex)
- {
- code=500;
- msg="云端服务未连接!";
- }
- return Ok(new { code, msg });
- }
- /**
- {
- "type":"sms",//qrcode二维码扫码登录:randomCode必传; sms 短信验证登录:randomCode必传,mobile必传
- "randomCode",
- "mobile":"1528377****"
- }
- **/
- /// <summary>
- /// 登录验证
- /// </summary>
- /// <param name="randomCode"></param>
- /// <returns></returns>
- [HttpPost("/login-check")]
- public async Task<IActionResult> LoginCheck(JsonNode json)
- {
- string randomCode = $"{json["randomCode"]}";
- System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
- string? CenterUrl = _configuration.GetValue<string>("ExamServer:CenterUrl");
- var response = await _httpClientFactory.CreateClient().GetAsync($"{CenterUrl}/hita/check-login?code={randomCode}");
- if (response.IsSuccessStatusCode)
- {
- string content = await response.Content.ReadAsStringAsync();
- if (!string.IsNullOrWhiteSpace(content))
- {
- var json = JsonSerializer.Deserialize<JsonNode>(content);
- if (json != null)
- {
-
- TmdidImplicit? token = JsonSerializer.Deserialize<TmdidImplicit>(json["implicit_token"]);
- string x_auth_token = $"{json["x_auth_token"]}";
- List<School>? schools = JsonSerializer.Deserialize<List<School>>(json["schools"]);
- var jwt = new JwtSecurityToken(token?.id_token);
- var id = jwt.Payload.Sub;
- jwt.Payload.TryGetValue("name", out object? name);
- jwt.Payload.TryGetValue("picture", out object? picture);
- }
- }
- }
- return Ok();
- }
- /// <summary>
- /// 登录模式初始化
- /// </summary>
- /// <returns></returns>
- [HttpGet("/login-init")]
- public async Task<IActionResult> LoginInit(JsonNode json)
- {
- var type = json["type"];
- string qrcode = string.Empty;
- string randomCode = "";
- switch (true)
- {
- case bool when $"{type}".Equals("qrcode"):
- {
- // 生成二维码图片
- Random random = new Random();
- randomCode = $"{random.Next(1000, 9999)}";
- string? CenterUrl = _configuration.GetValue<string>("ExamServer:CenterUrl");
- string content = $"{CenterUrl}/joinSchool?schoolCode=login:{randomCode}&m=%E7%99%BB%E5%BD%95&o=1";
- Bitmap qrCodeImage = QRCodeHelper.GetBitmap(content, 200, 200);
- using (MemoryStream stream = new MemoryStream())
- {
- qrCodeImage.Save(stream, ImageFormat.Png);
- byte[] data = stream.ToArray();
- qrcode=$"data:image/png;base64,{Convert.ToBase64String(data)}";
- }
- return Ok(new { code = 200, randomCode = randomCode, qrcode, type });
- }
- case bool when $"{type}".Equals("smspin"):
- {
- int send = 0;
- if ( !string.IsNullOrWhiteSpace($"{json["area"]}") && !string.IsNullOrWhiteSpace($"{json["to"]}"))
- {
- string? CenterUrl = _configuration.GetValue<string>("ExamServer:CenterUrl");
- string url = $"{CenterUrl}/core/sendsms/pin";
- HttpResponseMessage message= await _httpClientFactory.CreateClient().PostAsJsonAsync(url, new { });
- if (message.IsSuccessStatusCode)
- {
- string content =await message.Content.ReadAsStringAsync();
- JsonNode? jsonNode = JsonSerializer.Deserialize<JsonNode>(content);
- if (jsonNode!=null && int.TryParse($"{jsonNode["send"]}", out int s))
- {
- send = s;
- }
- }
- }
- return Ok(new { code = 200, send, type });
- }
- }
- return Ok(new { code = 400});
- }
- }
- }
|