123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- using IES.ExamLibrary.Models;
- using IES.ExamServer.DI;
- using IES.ExamServer.Helper;
- using IES.ExamServer.Models;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Caching.Memory;
- using Microsoft.Extensions.Hosting;
- using Microsoft.VisualBasic;
- using System;
- using System.Text.Json.Nodes;
- namespace IES.ExamServer.Controllers
- {
- [ApiController]
- [Route("student")]
- public class StudentController : BaseController
- {
- private readonly IConfiguration _configuration;
- private readonly IHttpClientFactory _httpClientFactory;
- private readonly IMemoryCache _memoryCache;
- private readonly ILogger<IndexController> _logger;
- private readonly CenterServiceConnectionService _connectionService;
- private readonly LiteDBFactory _liteDBFactory;
- public StudentController(ILogger<IndexController> logger, IConfiguration configuration, IHttpClientFactory httpClientFactory,
- IMemoryCache memoryCache, CenterServiceConnectionService connectionService, LiteDBFactory liteDBFactory)
- {
- _logger = logger;
- _configuration=configuration;
- _httpClientFactory=httpClientFactory;
- _memoryCache=memoryCache;
- _connectionService=connectionService;
- _liteDBFactory=liteDBFactory;
- }
- /// <summary>
- /// 提交作答
- /// </summary>
- /// <param name="json"></param>
- /// <returns></returns>
- [HttpPost("submit-answer")]
- public IActionResult SubmitAnswer(JsonNode json)
- {
- //提交作答,多种保存方式,在数据库,文件中,缓存中,以及日志中都记录防止丢失。
- return Ok();
- }
- /// <summary>
- /// 登录
- /// </summary>
- /// <param name="json"></param>
- /// <returns></returns>
- [HttpPost("login")]
- public IActionResult Login(JsonNode json)
- {
- string? studentId = json["studentId"]?.ToString();
- string? studentName = json["studentName"]?.ToString();
- string? evaluationId = json["evaluationId"]?.ToString();
- EvaluationRoundSetting? setting = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().FindOne(x => x.id!.Equals(evaluationId)&& x.activate==1);
- EvaluationClient? evaluationClient = null;
- if (setting!=null)
- {
- evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id == setting.evaluationId);
- if (evaluationClient!=null)
- {
- (code, msg)= CheckActivate(evaluationClient, setting);
- if (code==200)
- {
- long time = DateTimeOffset.Now.ToUnixTimeMilliseconds();
- _memoryCache.TryGetValue(Constant._KeyServerDevice, out ServerDevice? server);
- School? school = null;
- ///获取名单文件,解析信息,应该在设置开考轮次的时候进行解析,并放在数据库中,并且在此时看是否是需要 分配试卷,还是在登录的时候获取试卷。
- if (server!=null)
- {
- school = server.school;
- }
- string id = $"{studentId}";
- string name = $"{studentName}";
- string x_auth_token = JwtAuthExtension.CreateAuthToken("www.teammodel.cn", id, name, picture: string.Empty
- , ExamConstant.JwtSecretKey, ExamConstant.ScopeVisitor, 8, schoolID: school?.id, new string[] { "student" }, expire: 1);
- _liteDBFactory.GetLiteDatabase().GetCollection<Teacher>().Upsert(new Teacher { id = id, name = $"{name}", picture = null, x_auth_token = x_auth_token, loginTime=time });
- return Ok(new { code = 200, x_auth_token = x_auth_token });
- }
- }
- }
- return Ok(new { msg =msg, code = 200 });
- }
- /// <summary>
- /// 学生端获取激活的考试
- /// </summary>
- /// <param name="json"></param>
- /// <returns></returns>
- [HttpPost("get-activate-evaluation")]
- public IActionResult GetActivateEvaluation(JsonNode json)
- {
- // _connectionService.serverDevice.school.id?
- try
- {
- IEnumerable<EvaluationRoundSetting>? settings = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationRoundSetting>().Find(x =>x.activate==1 );
- if (settings != null && settings.Count() > 0)
- {
- if (settings.Count()>1)
- {
- msg="有多个正在开考的评测,请监考教师重新设置开考评测。";
- code=2;
- }
- else
- {
- EvaluationRoundSetting? setting = settings.First(); ;
- EvaluationClient evaluationClient = _liteDBFactory.GetLiteDatabase().GetCollection<EvaluationClient>().FindOne(x => x.id ==setting.evaluationId);
- if (evaluationClient!=null)
- {
- (code ,msg)= CheckActivate(evaluationClient, setting);
- var anonymousObject = new Dictionary<string, object?>();
- var properties = evaluationClient.GetType().GetProperties();
- foreach (var property in properties)
- {
- if (!property.Name.Equals("shortCode") && !property.Name.Equals("openCode"))
- {
- anonymousObject[property.Name] = property.GetValue(evaluationClient);
- }
- }
- return Ok(new { evaluationClient = anonymousObject, code = code, msg = msg, setting });
- }
- else
- {
- msg="未找到评测信息。";
- code=6;
- }
- //foreach (var subject in evaluationClient.subjects)
- //{
- // foreach (var paper in subject.papers)
- // {
- // paper.blob=$"package/{evaluationClient.id}/papers/{paper.paperId}";
- // }
- //}
-
- }
- }
- else
- {
- msg="暂无正在开考的评测";
- code=1;
- }
- }
- catch (Exception ex) { }
- return Ok(new { msg ,code});
- }
- private (int code, string msg) CheckActivate(EvaluationClient evaluationClient, EvaluationRoundSetting setting)
- {
- code = 200;
- if (evaluationClient.scope!.Equals("school"))
- {
- if (!evaluationClient!.ownerId!.Equals($"{_connectionService.serverDevice?.school?.id}"))
- {
- msg="授权学校与评测归属学校不一致。";
- code=3;
- }
- }
- //当前时间
- long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
- if (setting.countdownType>0)
- {
- if (setting.startline>now || evaluationClient.stime>now)
- {
- msg="评测暂未开始。";
- code=4;
- }
- if ((setting.deadline>0 && setting.deadline<now)|| evaluationClient.etime<now)
- {
- msg="评测已经结束。";
- code=5;
- }
- }
- else
- {
- if (evaluationClient.stime>now)
- {
- msg="评测暂未开始。";
- code=4;
- }
- if (evaluationClient.etime<now)
- {
- msg="评测已经结束。";
- code=5;
- }
- }
- return (code, msg);
- }
-
- }
- }
|