TEStudentController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package com.qmth.themis.exam.api;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.google.gson.Gson;
  4. import com.qmth.themis.business.annotation.ApiJsonObject;
  5. import com.qmth.themis.business.annotation.ApiJsonProperty;
  6. import com.qmth.themis.business.bean.exam.ExamActivityUnFinishBean;
  7. import com.qmth.themis.business.bean.exam.ExamUnFinishBean;
  8. import com.qmth.themis.business.cache.ExamRecordCacheUtil;
  9. import com.qmth.themis.business.cache.ExamingDataCacheUtil;
  10. import com.qmth.themis.business.cache.bean.ExamActivityCacheBean;
  11. import com.qmth.themis.business.cache.bean.ExamCacheBean;
  12. import com.qmth.themis.business.cache.bean.ExamCourseCacheBean;
  13. import com.qmth.themis.business.cache.bean.ExamStudentCacheBean;
  14. import com.qmth.themis.business.constant.SystemConstant;
  15. import com.qmth.themis.business.dto.AuthDto;
  16. import com.qmth.themis.business.dto.ExpireTimeDTO;
  17. import com.qmth.themis.business.dto.MqDto;
  18. import com.qmth.themis.business.dto.cache.TEStudentCacheDto;
  19. import com.qmth.themis.business.dto.response.TEExamActivityWaitDto;
  20. import com.qmth.themis.business.dto.response.TEExamResultDto;
  21. import com.qmth.themis.business.dto.response.TEExamWaitDto;
  22. import com.qmth.themis.business.entity.TBOrg;
  23. import com.qmth.themis.business.entity.TBSession;
  24. import com.qmth.themis.business.entity.TEConfig;
  25. import com.qmth.themis.business.entity.TEStudent;
  26. import com.qmth.themis.business.enums.ExamRecordStatusEnum;
  27. import com.qmth.themis.business.enums.MqTagEnum;
  28. import com.qmth.themis.business.enums.SystemOperationEnum;
  29. import com.qmth.themis.business.enums.WebsocketStatusEnum;
  30. import com.qmth.themis.business.service.*;
  31. import com.qmth.themis.business.util.*;
  32. import com.qmth.themis.common.enums.ExceptionResultEnum;
  33. import com.qmth.themis.common.enums.Platform;
  34. import com.qmth.themis.common.enums.Source;
  35. import com.qmth.themis.common.exception.BusinessException;
  36. import com.qmth.themis.common.util.Result;
  37. import com.qmth.themis.common.util.ResultUtil;
  38. import com.qmth.themis.exam.config.ExamConstant;
  39. import io.swagger.annotations.*;
  40. import org.apache.commons.lang3.StringUtils;
  41. import org.slf4j.Logger;
  42. import org.slf4j.LoggerFactory;
  43. import org.springframework.web.bind.annotation.RequestBody;
  44. import org.springframework.web.bind.annotation.RequestMapping;
  45. import org.springframework.web.bind.annotation.RequestMethod;
  46. import org.springframework.web.bind.annotation.RestController;
  47. import javax.annotation.Resource;
  48. import java.io.File;
  49. import java.security.NoSuchAlgorithmException;
  50. import java.util.HashMap;
  51. import java.util.List;
  52. import java.util.Map;
  53. import java.util.Objects;
  54. /**
  55. * @Description: 考生 前端控制器
  56. * @Param:
  57. * @return:
  58. * @Author: wangliang
  59. * @Date: 2020/6/25
  60. */
  61. @Api(tags = "考生Controller")
  62. @RestController
  63. @RequestMapping("/${prefix.url.exam}/student")
  64. public class TEStudentController {
  65. private final static Logger log = LoggerFactory.getLogger(TEStudentController.class);
  66. @Resource
  67. TEStudentService teStudentService;
  68. @Resource
  69. CacheService cacheService;
  70. @Resource
  71. RedisUtil redisUtil;
  72. @Resource
  73. MqDtoService mqDtoService;
  74. @Resource
  75. TEConfigService teConfigService;
  76. @Resource
  77. TEExamService teExamService;
  78. @Resource
  79. TEExamStudentService teExamStudentService;
  80. @Resource
  81. TEExamActivityService teExamActivityService;
  82. @Resource
  83. TEExamCourseService teExamCourseService;
  84. @Resource
  85. OssUtil ossUtil;
  86. @Resource
  87. TOeExamRecordService tOeExamRecordService;
  88. @Resource
  89. TBOrgService tbOrgService;
  90. @Resource
  91. MqUtil mqUtil;
  92. @ApiOperation(value = "学生登录接口")
  93. @RequestMapping(value = "/login", method = RequestMethod.POST)
  94. @ApiResponses({@ApiResponse(code = 200, message = "学生信息", response = TEExamResultDto.class)})
  95. public Result login(
  96. @ApiJsonObject(name = "loginStudent", value = {@ApiJsonProperty(key = "identity", description = "证件号"),
  97. @ApiJsonProperty(key = "password", description = "密码"),
  98. @ApiJsonProperty(key = "orgId", type = "long", example = "1", description = "机构id"),
  99. @ApiJsonProperty(key = "examId", type = "long", example = "1", description = "批次id")}) @ApiParam(value = "学生信息", required = true) @RequestBody Map<String, Object> mapParameter)
  100. throws NoSuchAlgorithmException {
  101. if (Objects.isNull(mapParameter)) {
  102. throw new BusinessException(ExceptionResultEnum.STUDENT_IS_NULL);
  103. }
  104. if (Objects.isNull(mapParameter.get("identity")) || Objects.equals(mapParameter.get("identity"), "")) {
  105. throw new BusinessException(ExceptionResultEnum.LOGIN_NAME_IS_NULL);
  106. }
  107. if (Objects.isNull(mapParameter.get("password")) || Objects.equals(mapParameter.get("password"), "")) {
  108. throw new BusinessException(ExceptionResultEnum.PASSWORD_IS_NULL);
  109. }
  110. Long orgId = null;
  111. if (Objects.nonNull(mapParameter.get(SystemConstant.ORG_ID))) {
  112. orgId = Long.parseLong(String.valueOf(mapParameter.get(SystemConstant.ORG_ID)));
  113. }
  114. Long examId = null;
  115. TBOrg tbOrg = null;
  116. if (Objects.nonNull(mapParameter.get(SystemConstant.EXAM_ID))) {
  117. examId = Long.parseLong(String.valueOf(mapParameter.get(SystemConstant.EXAM_ID)));
  118. ExamCacheBean ec = teExamService.getExamCacheBean(examId);
  119. tbOrg = cacheService.addOrgCache(ec.getOrgId());
  120. }
  121. if (Objects.isNull(orgId) && Objects.isNull(examId)) {
  122. throw new BusinessException(ExceptionResultEnum.ORG_ID_OR_EXAM_ID_NOT_CHOOSE);
  123. }
  124. if (Objects.nonNull(orgId)) {
  125. tbOrg = cacheService.addOrgCache(orgId);
  126. }
  127. if (Objects.isNull(tbOrg)) {
  128. throw new BusinessException(ExceptionResultEnum.ORG_NO);
  129. }
  130. if (Objects.nonNull(tbOrg.getEnable()) && tbOrg.getEnable().intValue() == 0) {
  131. throw new BusinessException(ExceptionResultEnum.ORG_ENABLE);
  132. }
  133. String identity = String.valueOf(mapParameter.get("identity"));
  134. String password = String.valueOf(mapParameter.get("password"));
  135. QueryWrapper<TEStudent> wrapper = new QueryWrapper<>();
  136. wrapper.lambda().eq(TEStudent::getIdentity, identity).eq(TEStudent::getOrgId, tbOrg.getId());
  137. TEStudent user = teStudentService.getOne(wrapper);
  138. //学生不存在
  139. if (Objects.isNull(user)) {
  140. throw new BusinessException(ExceptionResultEnum.STUDENT_NO);
  141. }
  142. if (StringUtils.isNotBlank(user.getBasePhotoPath())) {
  143. user.setBasePhotoPath(
  144. ossUtil.getAliYunOssPublicDomain().getPublicUrl() + File.separator + user.getBasePhotoPath());
  145. }
  146. // String loginPassword = AesUtil.decryptCs7(password, Constants.AES_RULE);
  147. // 密码错误
  148. // String aesPassword = AesUtil.decryptCs7(user.getPassword(), Constants.AES_RULE);
  149. if (!Objects.equals(password, user.getPassword())) {
  150. throw new BusinessException(ExceptionResultEnum.PASSWORD_ERROR);
  151. }
  152. String lockKey = SystemConstant.REDIS_LOCK_STUDENT_PREFIX + user.getId();
  153. redisUtil.waitLock(lockKey, SystemConstant.REDIS_LOCK_STUDENT_TIME_OUT);
  154. try {
  155. //判断是否有正在考试的记录
  156. Long examingRecordId = ExamingDataCacheUtil.getExamingRecordId(user.getId());
  157. if (Objects.nonNull(examingRecordId)) {
  158. WebsocketStatusEnum sta = ExamRecordCacheUtil.getClientWebsocketStatus(examingRecordId);
  159. //有正在考试的记录,且考生端websocket在线则不允许登陆
  160. if (WebsocketStatusEnum.ON_LINE.equals(sta)) {
  161. throw new BusinessException(ExceptionResultEnum.STUDENT_NOT_ALLOW_LOGIN);
  162. }
  163. }
  164. return userLoginCommon(user, examId, orgId);
  165. } finally {
  166. redisUtil.releaseLock(lockKey);
  167. }
  168. }
  169. /**
  170. * 学生登录公用
  171. *
  172. * @param teStudent
  173. * @param examId
  174. * @param orgId
  175. * @return
  176. * @throws NoSuchAlgorithmException
  177. */
  178. private Result userLoginCommon(TEStudent teStudent, Long examId, Long orgId) throws NoSuchAlgorithmException {
  179. //停用
  180. if (teStudent.getEnable() == 0) {
  181. throw new BusinessException(ExceptionResultEnum.STUDENT_ENABLE);
  182. }
  183. Platform platform = ServletUtil.getRequestPlatform();
  184. String deviceId = ServletUtil.getRequestDeviceId();
  185. //添加用户鉴权缓存
  186. AuthDto authDto = cacheService.addStudentAuthCache(teStudent.getId());
  187. //生成token
  188. String token = SystemConstant.getUuid();
  189. cacheService.addStudentAccountCache(teStudent.getId());
  190. //判断会话来源
  191. Source source = null;
  192. if (Platform.WIN == platform || Platform.MAC == platform) {
  193. source = Source.OE_CLIENT;
  194. } else {
  195. throw new BusinessException(ExceptionResultEnum.PLATFORM_INVALID);
  196. }
  197. //添加用户会话缓存
  198. String sessionId = SessionUtil
  199. .digest(teStudent.getId(), Math.abs(authDto.getRoleCodes().toString().hashCode()), source);
  200. ExpireTimeDTO expireTime = SystemConstant.getExpireTime(source, platform);
  201. TBSession tbSession = new TBSession(sessionId, String.valueOf(teStudent.getId()),
  202. authDto.getRoleCodes().toString(), source.name(), platform.name(), deviceId,
  203. ServletUtil.getRequest().getLocalAddr(), token, expireTime.getDate().getTime());
  204. redisUtil.setUserSession(sessionId, tbSession, expireTime.getExpireSeconds());
  205. //mq发送消息start
  206. MqDto mqDto = new MqDto(mqUtil.getMqGroupDomain().getTopic(), platform.name(), SystemOperationEnum.SESSION,
  207. sessionId);
  208. mqDtoService.assembleSendOneWayMsg(mqDto);
  209. MqDto mqDtoLog = new MqDto(mqUtil.getMqGroupDomain().getTopic(), MqTagEnum.STUDENT.name(),
  210. SystemOperationEnum.LOGIN, MqTagEnum.STUDENT, String.valueOf(teStudent.getId()),
  211. teStudent.getIdentity());
  212. mqDtoService.assembleSendOneWayMsg(mqDtoLog);
  213. //mq发送消息end
  214. //测试
  215. // String test = SignatureInfo.build(SignatureType.TOKEN, sessionId, token);
  216. Map<String, Object> map = new HashMap<>();
  217. //获取未完考试
  218. Long recordId = ExamingDataCacheUtil.getExamingRecordId(teStudent.getId());
  219. if (Objects.isNull(recordId)) {
  220. map = this.getWaitList(teStudent.getId(), examId, orgId, map);
  221. } else {
  222. //获取考试记录缓存
  223. ExamRecordStatusEnum status = ExamRecordCacheUtil.getStatus(recordId);
  224. if (Objects.isNull(status)) {
  225. //map = this.getWaitList(teStudent.getId(), examId, orgId, map);
  226. log.error("ExamRecordStatus in cache is null, recordId=" + recordId);
  227. throw new BusinessException(ExceptionResultEnum.EXAM_STATUS_ERROR);
  228. } else {
  229. Long ecExamId = ExamRecordCacheUtil.getExamId(recordId);
  230. Long examStudentId = ExamRecordCacheUtil.getExamStudentId(recordId);
  231. Long examActivityId = ExamRecordCacheUtil.getExamActivityId(recordId);
  232. ExamCacheBean ec = teExamService.getExamCacheBean(ecExamId);//考试缓存
  233. ExamActivityCacheBean examActivityCacheBean = teExamActivityService
  234. .getExamActivityCacheBean(examActivityId);//考试场次缓存
  235. ExamStudentCacheBean examStudentCacheBean = teExamStudentService.getExamStudentCacheBean(examStudentId);
  236. //如果断点时间大于整体断点时间,则强制交卷
  237. if (Objects.equals(status, ExamRecordStatusEnum.ANSWERING) || Objects
  238. .equals(status, ExamRecordStatusEnum.BREAK_OFF) || Objects
  239. .equals(status, ExamRecordStatusEnum.RESUME_PREPARE)) {
  240. //只有ANSWERING状态才生成断点
  241. if (Objects.equals(status, ExamRecordStatusEnum.ANSWERING)) {
  242. ExamConstant.sendExamStopMsg(recordId, true, false);
  243. tOeExamRecordService.setExamBreak(recordId);
  244. }
  245. Boolean finished = tOeExamRecordService.examBreakLogic(recordId, false);
  246. if (finished) {
  247. map = this.getWaitList(teStudent.getId(), examId, orgId, map);
  248. } else {
  249. ExamUnFinishBean examUnFinishBean = this
  250. .unFinishCommon(recordId, ec, examStudentCacheBean, examActivityCacheBean,
  251. examStudentId);
  252. map.put("unFinished", examUnFinishBean);
  253. }
  254. } else {
  255. map = this.getWaitList(teStudent.getId(), examId, orgId, map);
  256. }
  257. }
  258. }
  259. //获取全局考试配置
  260. TEConfig teConfig = teConfigService.getGlobalConfig();
  261. map.put(SystemConstant.ACCESS_TOKEN, token);
  262. map.put(SystemConstant.GLOBAL, teConfig);
  263. // map.put(SystemConstant.ACCESS_TOKEN, test);
  264. map.put(SystemConstant.STUDENT_ACCOUNT, teStudent);
  265. map.put(SystemConstant.SESSION_ID, sessionId);
  266. return ResultUtil.ok(map);
  267. }
  268. @ApiOperation(value = "登出接口")
  269. @RequestMapping(value = "/logout", method = RequestMethod.POST)
  270. @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
  271. public Result logout() throws NoSuchAlgorithmException {
  272. TEStudentCacheDto teStudent = (TEStudentCacheDto) ServletUtil.getRequestStudentAccount();
  273. TBSession tbSession = (TBSession) ServletUtil.getRequestSession();
  274. if (Objects.isNull(tbSession)) {
  275. throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
  276. }
  277. AuthDto authDto = cacheService.addStudentAuthCache(teStudent.getId());
  278. redisUtil.deleteUserSession(tbSession.getId());
  279. //循环检查该用户下其他平台是否存在session,不存在则删除用户缓存和鉴权缓存
  280. boolean delete = true;
  281. for (Source s : Source.values()) {
  282. String sessionId = SessionUtil
  283. .digest(teStudent.getId(), Math.abs(authDto.getRoleCodes().toString().hashCode()), s.name());
  284. if (Objects.nonNull(redisUtil.getUserSession(sessionId))) {
  285. delete = false;
  286. break;
  287. }
  288. }
  289. if (delete) {
  290. cacheService.removeStudentAuthCache(teStudent.getId());
  291. cacheService.removeStudentAccountCache(teStudent.getId());
  292. }
  293. //mq发送消息start
  294. MqDto mqDto = new MqDto(mqUtil.getMqGroupDomain().getTopic(), MqTagEnum.STUDENT.name(),
  295. SystemOperationEnum.LOGOUT, MqTagEnum.STUDENT, String.valueOf(teStudent.getId()),
  296. teStudent.getIdentity());
  297. mqDtoService.assembleSendOneWayMsg(mqDto);
  298. //mq发送消息end
  299. return ResultUtil.ok(true);
  300. }
  301. /**
  302. * 未完成公用
  303. *
  304. * @param recordId
  305. * @param ec
  306. * @param examStudentCacheBean
  307. * @param examActivityCacheBean
  308. * @param examStudentId
  309. * @return
  310. */
  311. private ExamUnFinishBean unFinishCommon(Long recordId, ExamCacheBean ec, ExamStudentCacheBean examStudentCacheBean,
  312. ExamActivityCacheBean examActivityCacheBean, Long examStudentId) {
  313. ExamCourseCacheBean examCourseCacheBean = teExamCourseService
  314. .getExamCourseCacheBean(ec.getId(), examStudentCacheBean.getCourseCode());
  315. TEExamActivityWaitDto teExamActivityWaitDto = new TEExamActivityWaitDto(ec, examActivityCacheBean,
  316. examStudentCacheBean, examStudentId, examCourseCacheBean, ExamRecordCacheUtil.getStartTime(recordId),
  317. ExamRecordCacheUtil.getEndTime(recordId), ExamRecordCacheUtil.getOpeningSeconds(recordId),
  318. ExamRecordCacheUtil.getMinDurationSeconds(recordId),
  319. ExamRecordCacheUtil.getMaxDurationSeconds(recordId), ExamRecordCacheUtil.getForceFinish(recordId));
  320. Gson gson = new Gson();
  321. ExamActivityUnFinishBean examActivityUnFinishBean = gson
  322. .fromJson(gson.toJson(teExamActivityWaitDto), ExamActivityUnFinishBean.class);
  323. examActivityUnFinishBean.setRecordId(recordId);
  324. return new ExamUnFinishBean(ec.getId(), ec.getName(), examActivityUnFinishBean);
  325. }
  326. /**
  327. * 获取待考列表
  328. *
  329. * @param studentId
  330. * @param examId
  331. * @param orgId
  332. * @param map
  333. * @return
  334. */
  335. private Map<String, Object> getWaitList(Long studentId, Long examId, Long orgId, Map<String, Object> map) {
  336. //获取待考列表
  337. List<TEExamWaitDto> list = teExamService.getWaitingExam(studentId, examId, orgId);
  338. if (Objects.nonNull(list) && list.size() > 0) {
  339. map.put("waiting", list);
  340. }
  341. return map;
  342. }
  343. }