소스 검색

fix: 修正获取空的学生答案之后题目显示出错的问题

ChenYangfan 6 년 전
부모
커밋
2283014262
1개의 변경된 파일27개의 추가작업 그리고 14개의 파일을 삭제
  1. 27 14
      src/views/quiz/StudentDoQuiz.vue

+ 27 - 14
src/views/quiz/StudentDoQuiz.vue

@@ -11,24 +11,24 @@
         <div>最近提交:{{!isEmpty(answersInfo.submitAt) ? momentFromNow(answersInfo.submitAt) : '无提交历史'}}</div>
       </div>
       <div class="quiz-list-item" :key="question.id" v-for="(question, index) in questions"
-           :class="question.studentAnswer.correct ? 'correct' : 'wrong'">
+           :class="question.studentAnswer ? question.studentAnswer.correct === false ? 'wrong' : '' : ''">
         <div class="quiz-list-item__index">{{ index + 1 }}.</div>
         <div>
           <markdown-section class="quiz-list-item-title" :raw="question.stem"/>
-          <div v-if="question.type === QuestionKind.choice">
+          <div v-if="question.type === QuestionKind.choice && question.studentAnswer">
             <label class="option" :key="index" v-for="(item, index) in question.options">
               <input v-model="question.studentAnswer.answer" type="radio" :value="index"/>
               <b class="mr-8">{{ index }}.</b>
               <markdown-section style="display: inline" :raw="item"/>
             </label>
           </div>
-          <div v-else-if="question.type === QuestionKind.trueOrFalse">
+          <div v-else-if="question.type === QuestionKind.trueOrFalse && question.studentAnswer">
             <label class="option">
-              <input v-model="question.studentAnswer" type="radio" :value="true"/>
+              <input v-model="question.studentAnswer.answer" type="radio" :value="true"/>
               <c-icon>check</c-icon>
             </label>
             <label class="option">
-              <input v-model="question.studentAnswer" type="radio" :value="false"/>
+              <input v-model="question.studentAnswer.answer" type="radio" :value="false"/>
               <c-icon>close</c-icon>
             </label>
           </div>
@@ -60,8 +60,11 @@ export default {
       quiz: createDefaultQuiz(),
       hasSubmit: false,
       questions: null,
-      answersInfo: null,
-
+      answersInfo: {
+        submitNumber: 0,
+        score: null,
+        submitAt: null
+      },
       QuestionKind
     }
   },
@@ -87,12 +90,21 @@ export default {
         this.questions = res
         // 获取之前填写的答案
         getOneQuizStudentAnswer({ quizId }).then(answers => {
-          this.answersInfo = answers
-          this.questions = this.questions.map(question => ({
-            ...question,
-            studentAnswer: answers.answers[question.id]
-          }))
-        }).catch(err => { this.$setErrorMessage(err.response.data.msg) })
+          if (!isEmpty(answers) && answers !== '') {
+            this.answersInfo = answers
+            this.questions = this.questions.map(question => ({
+              ...question,
+              studentAnswer: answers.answers[question.id]
+            }))
+          } else {
+            this.questions = this.questions.map(question => ({
+              ...question,
+              studentAnswer: { answer: '' }
+            }))
+          }
+        }).catch(err => {
+          this.$setErrorMessage(err.response.data.msg)
+        })
       }).catch(err => this.$setErrorMessage(err.response.data.msg))
     },
     handleFormSubmit () {
@@ -166,7 +178,8 @@ export default {
   .quiz-list-item__index {
     color: $theme-red;
   }
-  .quiz-list-item-title{
+
+  .quiz-list-item-title {
     color: $theme-red;
   }
 }