StudentController.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using IES.ExamLibrary.Models;
  2. using IES.ExamServer.DI;
  3. using IES.ExamServer.Helper;
  4. using IES.ExamServer.Models;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Caching.Memory;
  7. using Microsoft.Extensions.Hosting;
  8. using Microsoft.VisualBasic;
  9. using System;
  10. using System.Text.Json.Nodes;
  11. namespace IES.ExamServer.Controllers
  12. {
  13. [ApiController]
  14. [Route("student")]
  15. public class StudentController : BaseController
  16. {
  17. private readonly IConfiguration _configuration;
  18. private readonly IHttpClientFactory _httpClientFactory;
  19. private readonly IMemoryCache _memoryCache;
  20. private readonly ILogger<IndexController> _logger;
  21. private readonly CenterServiceConnectionService _connectionService;
  22. private readonly LiteDBFactory _liteDBFactory;
  23. public StudentController(ILogger<IndexController> logger, IConfiguration configuration, IHttpClientFactory httpClientFactory,
  24. IMemoryCache memoryCache, CenterServiceConnectionService connectionService, LiteDBFactory liteDBFactory)
  25. {
  26. _logger = logger;
  27. _configuration=configuration;
  28. _httpClientFactory=httpClientFactory;
  29. _memoryCache=memoryCache;
  30. _connectionService=connectionService;
  31. _liteDBFactory=liteDBFactory;
  32. }
  33. /// <summary>
  34. /// 提交作答
  35. /// </summary>
  36. /// <param name="json"></param>
  37. /// <returns></returns>
  38. [HttpPost("submit-answer")]
  39. public IActionResult SubmitAnswer(JsonNode json)
  40. {
  41. //提交作答,多种保存方式,在数据库,文件中,缓存中,以及日志中都记录防止丢失。
  42. return Ok();
  43. }
  44. /// <summary>
  45. /// 登录
  46. /// </summary>
  47. /// <param name="json"></param>
  48. /// <returns></returns>
  49. [HttpPost("login")]
  50. public IActionResult Login(JsonNode json)
  51. {
  52. string? studentId = json["studentId"]?.ToString();
  53. string? studentName = json["studentName"]?.ToString();
  54. string? evaluationId = json["evaluationId"]?.ToString();
  55. EvaluationRoundSetting? setting = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().FindOne(x => x.id!.Equals(evaluationId)&& x.activate==1);
  56. EvaluationClient? evaluationClient = null;
  57. if (setting!=null)
  58. {
  59. evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id == setting.evaluationId);
  60. if (evaluationClient!=null)
  61. {
  62. (code, msg)= CheckActivate(evaluationClient, setting);
  63. if (code==200)
  64. {
  65. long time = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  66. _memoryCache.TryGetValue(Constant._KeyServerDevice, out ServerDevice? server);
  67. School? school = null;
  68. ///获取名单文件,解析信息,应该在设置开考轮次的时候进行解析,并放在数据库中,并且在此时看是否是需要 分配试卷,还是在登录的时候获取试卷。
  69. if (server!=null)
  70. {
  71. school = server.school;
  72. }
  73. string id = $"{studentId}";
  74. string name = $"{studentName}";
  75. string x_auth_token = JwtAuthExtension.CreateAuthToken("www.teammodel.cn", id, name, picture: string.Empty
  76. , ExamConstant.JwtSecretKey, ExamConstant.ScopeVisitor, 8, schoolID: school?.id, new string[] { "student" }, expire: 1);
  77. _liteDBFactory.GetLiteDatabase().GetCollection<Teacher>().Upsert(new Teacher { id = id, name = $"{name}", picture = null, x_auth_token = x_auth_token, loginTime=time });
  78. return Ok(new { code = 200, x_auth_token = x_auth_token });
  79. }
  80. }
  81. }
  82. return Ok(new { msg =msg, code = 200 });
  83. }
  84. /// <summary>
  85. /// 学生端获取激活的考试
  86. /// </summary>
  87. /// <param name="json"></param>
  88. /// <returns></returns>
  89. [HttpPost("get-activate-evaluation")]
  90. public IActionResult GetActivateEvaluation(JsonNode json)
  91. {
  92. // _connectionService.serverDevice.school.id?
  93. try
  94. {
  95. IEnumerable<EvaluationRoundSetting>? settings = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().Find(x =>x.activate==1 );
  96. if (settings != null && settings.Count() > 0)
  97. {
  98. if (settings.Count()>1)
  99. {
  100. msg="有多个正在开考的评测,请监考教师重新设置开考评测。";
  101. code=2;
  102. }
  103. else
  104. {
  105. EvaluationRoundSetting? setting = settings.First(); ;
  106. EvaluationClient evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id ==setting.evaluationId);
  107. if (evaluationClient!=null)
  108. {
  109. (code ,msg)= CheckActivate(evaluationClient, setting);
  110. var anonymousObject = new Dictionary<string, object?>();
  111. var properties = evaluationClient.GetType().GetProperties();
  112. foreach (var property in properties)
  113. {
  114. if (!property.Name.Equals("shortCode") && !property.Name.Equals("openCode"))
  115. {
  116. anonymousObject[property.Name] = property.GetValue(evaluationClient);
  117. }
  118. }
  119. return Ok(new { evaluationClient = anonymousObject, code = code, msg = msg, setting });
  120. }
  121. else
  122. {
  123. msg="未找到评测信息。";
  124. code=6;
  125. }
  126. //foreach (var subject in evaluationClient.subjects)
  127. //{
  128. // foreach (var paper in subject.papers)
  129. // {
  130. // paper.blob=$"package/{evaluationClient.id}/papers/{paper.paperId}";
  131. // }
  132. //}
  133. }
  134. }
  135. else
  136. {
  137. msg="暂无正在开考的评测";
  138. code=1;
  139. }
  140. }
  141. catch (Exception ex) { }
  142. return Ok(new { msg ,code});
  143. }
  144. private (int code, string msg) CheckActivate(EvaluationClient evaluationClient, EvaluationRoundSetting setting)
  145. {
  146. code = 200;
  147. if (evaluationClient.scope!.Equals("school"))
  148. {
  149. if (!evaluationClient!.ownerId!.Equals($"{_connectionService.serverDevice?.school?.id}"))
  150. {
  151. msg="授权学校与评测归属学校不一致。";
  152. code=3;
  153. }
  154. }
  155. //当前时间
  156. long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  157. if (setting.countdownType>0)
  158. {
  159. if (setting.startline>now || evaluationClient.stime>now)
  160. {
  161. msg="评测暂未开始。";
  162. code=4;
  163. }
  164. if ((setting.deadline>0 && setting.deadline<now)|| evaluationClient.etime<now)
  165. {
  166. msg="评测已经结束。";
  167. code=5;
  168. }
  169. }
  170. else
  171. {
  172. if (evaluationClient.stime>now)
  173. {
  174. msg="评测暂未开始。";
  175. code=4;
  176. }
  177. if (evaluationClient.etime<now)
  178. {
  179. msg="评测已经结束。";
  180. code=5;
  181. }
  182. }
  183. return (code, msg);
  184. }
  185. }
  186. }