StudentController.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. EvaluationClient? evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id!.Equals(evaluationId)&& x.activate==1);
  56. EvaluationRoundSetting? setting = null;
  57. if (!string.IsNullOrWhiteSpace(evaluationClient?.roundId))
  58. {
  59. setting = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().FindOne(x => x.id == evaluationClient.roundId);
  60. if (setting!=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[] { "visitor" }, 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<EvaluationClient>? evaluationClients = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().Find(x =>x.activate==1 );
  96. if (evaluationClients != null && evaluationClients.Count() > 0)
  97. {
  98. if (evaluationClients.Count()>1)
  99. {
  100. msg="有多个正在开考的评测,请监考教师重新设置开考评测。";
  101. code=2;
  102. }
  103. else
  104. {
  105. EvaluationClient evaluationClient = evaluationClients.First();
  106. EvaluationRoundSetting? setting=null;
  107. if (!string.IsNullOrWhiteSpace(evaluationClient.roundId))
  108. {
  109. setting = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().FindOne(x => x.id == evaluationClient.roundId);
  110. }
  111. if (setting!=null)
  112. {
  113. (code ,msg)= CheckActivate(evaluationClient, setting);
  114. evaluationClient.countdownType = setting.countdownType;
  115. evaluationClient.countdown = setting.countdown;
  116. evaluationClient.deadline = setting.deadline;
  117. evaluationClient.startline = setting.startline;
  118. evaluationClient.activate = setting.activate;
  119. evaluationClient.grouplist = setting.groupList;
  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. var anonymousObject = new Dictionary<string, object?>();
  134. var properties = evaluationClient.GetType().GetProperties();
  135. foreach (var property in properties)
  136. {
  137. if (!property.Name.Equals("shortCode") && !property.Name.Equals("openCode"))
  138. {
  139. anonymousObject[property.Name] = property.GetValue(evaluationClient);
  140. }
  141. }
  142. return Ok(new { evaluationClient = anonymousObject, code = code, msg = msg, setting });
  143. }
  144. }
  145. else
  146. {
  147. msg="暂无正在开考的评测";
  148. code=1;
  149. }
  150. }
  151. catch (Exception ex) { }
  152. return Ok(new { msg ,code});
  153. }
  154. private (int code, string msg) CheckActivate(EvaluationClient evaluationClient, EvaluationRoundSetting setting)
  155. {
  156. code = 200;
  157. if (evaluationClient.scope!.Equals("school"))
  158. {
  159. if (!evaluationClient!.ownerId!.Equals($"{_connectionService.serverDevice?.school?.id}"))
  160. {
  161. msg="授权学校与评测归属学校不一致。";
  162. code=3;
  163. }
  164. }
  165. //当前时间
  166. long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  167. if (setting.countdownType>0)
  168. {
  169. if (evaluationClient.startline>now || evaluationClient.stime>now)
  170. {
  171. msg="评测暂未开始。";
  172. code=4;
  173. }
  174. if ((setting.deadline>0 && setting.deadline<now)|| evaluationClient.etime<now)
  175. {
  176. msg="评测已经结束。";
  177. code=5;
  178. }
  179. }
  180. else
  181. {
  182. if (evaluationClient.stime>now)
  183. {
  184. msg="评测暂未开始。";
  185. code=4;
  186. }
  187. if (evaluationClient.etime<now)
  188. {
  189. msg="评测已经结束。";
  190. code=5;
  191. }
  192. }
  193. return (code, msg);
  194. }
  195. }
  196. }