UserExam-exam.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <el-dialog v-model="state.dialogVisible" draggable append-to-body destroy-on-close :show-close="false" width="80%" :before-close="closeDialog">
  3. <template #header="{ close, titleId, titleClass }">
  4. <div class="topContent">
  5. <div class="surplusBox" v-if="state.examType == 0">
  6. <el-countdown title="剩余时间:" :value="state.surplusTime" @finish="onSurplusTimeFinish" />
  7. </div>
  8. <div class="titleBox">
  9. <span class="title">统一考试</span>
  10. </div>
  11. <div class="submitBox">
  12. <el-button type="info" @click="closeDialog">关 闭</el-button>
  13. <el-button type="primary" @click="onSubmit" :loading="state.loading">交 卷</el-button>
  14. </div>
  15. </div>
  16. </template>
  17. <el-row :gutter="10" v-loading="state.loading">
  18. <el-col :xs="6" :sm="6" :md="6" :lg="6" :xl="6">
  19. <div class="examListContent" v-for="(item) in state.questionTypeData">
  20. <p class="examType">{{item.questionTypeDesc}}</p>
  21. <ul class="examList">
  22. <li class="examListItem" :class="questionsItem.id == state.selQuestionID ? 'select' : ''" v-for="(questionsItem) in item.questions" @click="onSelQuestion(questionsItem.id)">{{questionsItem.sortIndex}}</li>
  23. </ul>
  24. </div>
  25. </el-col>
  26. <el-col :xs="18" :sm="18" :md="18" :lg="18" :xl="18">
  27. <div class="questionBox" v-if="state.questionDetail">
  28. <p class="questionTitle">题干:{{ state.questionDetail.title }}({{ state.questionDetail.score }}分)</p>
  29. <div class="questionAnswer">
  30. <div v-if="[0,2].includes(state.questionDetail.questionType)">
  31. <el-radio-group style="display: block;" v-model="state.selRadioValue">
  32. <el-form-item :label="item.label + '. '" v-for="item in state.questionDetail.questionOptions" style="margin-bottom: 10px;">
  33. <el-radio :label="item.content" :value="item.id" />
  34. </el-form-item>
  35. </el-radio-group>
  36. </div>
  37. <div v-else-if="state.questionDetail.questionType == 1">
  38. <el-checkbox-group v-model="state.selCheckboxValue">
  39. <el-form-item :label="item.label + '. '" v-for="item in state.questionDetail.questionOptions" style="margin-bottom: 10px;">
  40. <el-checkbox :label="item.content" :value="item.id" />
  41. </el-form-item>
  42. </el-checkbox-group>
  43. </div>
  44. <!-- <div v-else-if="state.questionDetail.questionType == 2"></div> -->
  45. <div v-else-if="state.questionDetail.questionType == 3">
  46. <el-form-item label="答:">
  47. <el-input type="textarea" v-model="state.answerValue" :rows="3" placeholder="请输入答案,如果有多个请依照顺序用英文 , 隔开" clearable></el-input>
  48. </el-form-item>
  49. </div>
  50. <div v-else-if="state.questionDetail.questionType == 4">
  51. <el-form-item label="答:">
  52. <el-input type="textarea" v-model="state.answerValue" :rows="3" placeholder="请输入答案" clearable></el-input>
  53. </el-form-item>
  54. </div>
  55. </div>
  56. </div>
  57. </el-col>
  58. </el-row>
  59. <template #footer>
  60. <span class="dialog-footer" v-if="state.selQuestionID">
  61. <el-button type="primary" @click="onLastQuestion" :loading="state.loading">上一题</el-button>
  62. <el-button type="primary" @click="onNextQuestion" :loading="state.loading">下一题</el-button>
  63. </span>
  64. </template>
  65. </el-dialog>
  66. </template>
  67. <script setup lang="ts">
  68. import { reactive, ref } from 'vue';
  69. import { ElMessage, FormInstance } from 'element-plus';
  70. import { excludeSelfById } from '@/utils/tools';
  71. import other from '@/utils/other';
  72. import {startUserExam, getExamQuestionGroup, getExamQuestion, answerUserExam, submitUserExam } from '@/api/examTrain/userExam';
  73. // 定义子组件向父组件传值/事件
  74. const emit = defineEmits(['updateList']);
  75. // 定义变量内容
  76. const state = reactive<any>({
  77. dialogVisible: false, // 弹窗
  78. loading: false, // 加载
  79. userExamId: '', // 用户考试id
  80. examId: '', // 考试id
  81. examType: null, // 考试类型
  82. surplusTime: '00:00:00', // 剩余时间
  83. questionTypeData: [] as any[], // 试卷题型数据
  84. questionsData: [] as any[], // 试题集合
  85. selQuestionID: '', // 选择的问题id
  86. questionDetail: null, // 选择的问题明细
  87. selRadioValue: '', // 单选题选择的值
  88. selCheckboxValue: [] as any[], // 多选题选择的值
  89. answerValue: '', // 填空问答填写的值
  90. });
  91. // 打开弹窗
  92. const ruleFormRef = ref<any>(); // 表单ref
  93. const openDialog = async (row: any) => {
  94. state.dialogVisible = true;
  95. state.userExamId = row.id;
  96. state.examId = row.examId;
  97. state.examType = row.examType;
  98. state.loading = true;
  99. startUserExam({
  100. id: state.userExamId
  101. }).then((res) => {
  102. if (onStateControl(res.result)){
  103. getQuestionsData();
  104. }else {
  105. closeDialog();
  106. state.loading = false;
  107. }
  108. }).catch((error) => {
  109. console.error(error);
  110. });
  111. };
  112. const onStateControl = (obj) => {
  113. console.log(obj);
  114. if (!obj.isStart){
  115. ElMessage.warning('考试尚未开始!')
  116. return false;
  117. }else if (obj.isCompleted){
  118. ElMessage.warning('考试已经结束!')
  119. return false;
  120. }else if (!obj.isJoin){
  121. ElMessage.warning('您不用参加此考试!')
  122. return false;
  123. }else{
  124. if (state.examType == 0){
  125. state.surplusTime = new Date(obj.startTime).getTime() + obj.timeSpan * 60 * 1000;
  126. if (state.surplusTime <= new Date().getTime()){
  127. ElMessage.warning('考试已经结束!')
  128. return false;
  129. }else {
  130. return true;
  131. }
  132. }else {
  133. return true;
  134. }
  135. }
  136. };
  137. const onSurplusTimeFinish = () => {
  138. ElMessage.warning('考试已经结束!')
  139. closeDialog();
  140. }
  141. const getQuestionsData = async () => {
  142. try {
  143. const { result } = await getExamQuestionGroup(state.examId);
  144. let sortIndexNum = 1;
  145. result.forEach(item => {
  146. switch(item.questionType){
  147. case 0: item.questionTypeDesc = '单选题'; break;
  148. case 1: item.questionTypeDesc = '多选题'; break;
  149. case 2: item.questionTypeDesc = '判断题'; break;
  150. case 3: item.questionTypeDesc = '填空题'; break;
  151. case 4: item.questionTypeDesc = '问答题'; break;
  152. default: break;
  153. }
  154. item.questions.forEach(it => {
  155. it.sortIndex = sortIndexNum;
  156. it.questionType = item.questionType;
  157. state.questionsData.push(it)
  158. sortIndexNum++ ;
  159. });
  160. })
  161. state.questionTypeData = result;
  162. state.loading = false;
  163. } catch (error) {
  164. // 打印错误信息
  165. console.error(error);
  166. }
  167. }
  168. // 关闭弹窗
  169. const closeDialog = () => {
  170. state.dialogVisible = false;
  171. state.questionTypeData = [];
  172. state.questionsData = [];
  173. state.selQuestionID = '';
  174. state.questionDetail = null;
  175. state.selRadioValue = '';
  176. state.selCheckboxValue = [];
  177. state.answerValue = '';
  178. };
  179. // 上一题下一题
  180. const onLastQuestion = () => {
  181. let index:number = state.questionsData.findIndex(it => it.id == state.selQuestionID);
  182. index = (index - 1) == -1 ? state.questionsData.length : index - 1;
  183. onSelQuestion(state.questionsData[index].id);
  184. }
  185. const onNextQuestion = () => {
  186. let index:number = state.questionsData.findIndex(it => it.id == state.selQuestionID);
  187. index = (index + 1) == state.questionsData.length ? 0 : index + 1;
  188. onSelQuestion(state.questionsData[index].id);
  189. }
  190. // 选择的问题id 提交当前试题答案,再请求下一道题详情 value: 请求下一题详情id
  191. const onSelQuestion = async (value: any) => {
  192. state.loading = true;
  193. if (state.questionDetail){
  194. // 保存当前题目选项
  195. const selQuestionItem = state.questionsData.find(it => it.id == state.selQuestionID);
  196. const submitObj = {
  197. userExamId: state.userExamId,
  198. questionId: selQuestionItem.id,
  199. questionType: selQuestionItem.questionType,
  200. userExamItemOptionDtos: [] as any[],
  201. answer: ''
  202. };
  203. if ([0,2].includes(state.questionDetail.questionType)){
  204. if (state.selRadioValue){
  205. submitObj.userExamItemOptionDtos.push({questionOptionId: state.selRadioValue})
  206. }
  207. }else if (state.questionDetail.questionType == 1){
  208. if (state.selCheckboxValue.length > 0){
  209. state.selCheckboxValue.forEach(it => {
  210. submitObj.userExamItemOptionDtos.push({questionOptionId: it})
  211. })
  212. }
  213. }else if ([3,4].includes(state.questionDetail.questionType)){
  214. submitObj.answer = state.answerValue;
  215. }
  216. answerUserExam(submitObj)
  217. .then((res) => {
  218. if (onStateControl(res.result)){
  219. onGetQuestionDetail(value);
  220. }else {
  221. closeDialog();
  222. }
  223. })
  224. .catch(() => {
  225. state.loading = false;
  226. });
  227. }else {
  228. onGetQuestionDetail(value);
  229. }
  230. }
  231. const onGetQuestionDetail = async (value) => {
  232. if (!value){
  233. state.loading = false;
  234. return;
  235. }
  236. // 初始化选择选择/填写的答案
  237. state.selRadioValue = '';
  238. state.selCheckboxValue = [];
  239. state.answerValue = '';
  240. // 请求下一道题
  241. state.selQuestionID = value;
  242. try {
  243. const { result } = await getExamQuestion(state.selQuestionID, state.examId);
  244. state.questionDetail = result;
  245. if ([0,2].includes(state.questionDetail.questionType)){
  246. let obj = state.questionDetail.questionOptions.find((x: any) => x.isSelected === true);
  247. state.selRadioValue = obj ? obj.id : '';
  248. }else if (state.questionDetail.questionType == 1){
  249. let arr = [] as any[];
  250. state.questionDetail.questionOptions.forEach(it => {if (it.isSelected) arr.push(it);})
  251. console.log(arr);
  252. state.selCheckboxValue = arr ? arr.map((x: any) => x.id) : [];
  253. console.log(state.selCheckboxValue);
  254. }else {
  255. state.answerValue = state.questionDetail.answer || '';
  256. }
  257. state.loading = false;
  258. } catch (error) {
  259. // 打印错误信息
  260. console.error(error);
  261. }
  262. }
  263. // 结束考试
  264. const onSubmit = async () => {
  265. state.loading = true;
  266. await onSelQuestion('');
  267. submitUserExam({
  268. isSubmit: 1
  269. }).then(() => {
  270. emit('updateList');
  271. state.loading = false;
  272. closeDialog(); // 关闭弹窗
  273. ElMessage.success('操作成功');
  274. }).catch(() => {
  275. state.loading = false;
  276. });
  277. };
  278. // 暴露变量
  279. defineExpose({
  280. openDialog,
  281. closeDialog,
  282. });
  283. </script>
  284. <style lang="scss">
  285. .topContent{padding: 10px 30px;display: flex;justify-content: space-between;align-items: center;}
  286. .topContent .surplusBox{text-align: center;}
  287. .topContent .surplusBox .surplus{font-size: 18px;color: #000000;}
  288. .topContent .titleBox{text-align: center;}
  289. .topContent .titleBox .title{font-size: 20px;font-weight: bold;letter-spacing: 1px;color: #000000;}
  290. .topContent .submitBox{text-align: right;vertical-align: top;}
  291. .examListContent .examType{font-weight: bold;color: #000000;}
  292. .examListContent .examList{padding: 5px 0 10px;}
  293. .examListContent .examList .examListItem{display: inline-block;margin: 5px 10px 5px 0;text-align: center;width: 50px;height:35px;line-height: 35px;cursor: pointer;position: relative;border: #f2f2f2 1px solid;}
  294. .examListContent .examList .marked::after{content: "*";width: 5px;height: 5px;position: absolute;top: -3px;right: 4px;color: #FF5722;font-size: 25px;}
  295. .examListContent .examList .select{background-color: #1890ff;color: #fff;}
  296. .questionBox{padding: 20px 10px 0;}
  297. .questionBox .questionTitle{margin-bottom: 10px;font-size: 16px;}
  298. .questionBox .questionAnswer{padding: 0 20px;}
  299. .questionBox .questionAnswer .answerItem{width:calc(100%);}
  300. .questionBox .questionAnswer .answerItem .optionIndex{vertical-align: sub;font-size: 16px;display: inline-block;}
  301. </style>