zhangjie 2 jaren geleden
bovenliggende
commit
d6205bcaa9

+ 5 - 5
src/modules/question/components/QuestionImportPaperEdit.vue

@@ -9,8 +9,8 @@
       </div>
       <div class="ep-detail-body">
         <div
-          v-for="question in detail.questions"
-          :key="question.number"
+          v-for="(question, qindex) in detail.questions"
+          :key="qindex"
           class="ep-question"
         >
           <div class="ep-question-type">
@@ -18,7 +18,7 @@
           </div>
           <component
             :is="structTypeComp(question.questionType)"
-            :ref="`QuestionEditDetail${dIndex}-${question.number}`"
+            :ref="`QuestionEditDetail-${dIndex}-${qindex}-${question.id}`"
             :question="question"
           ></component>
         </div>
@@ -98,8 +98,8 @@ export default {
     },
     getData() {
       this.paperData.forEach((detail, dIndex) => {
-        detail.questions.forEach((question) => {
-          const refName = `QuestionEditDetail${dIndex}-${question.number}`;
+        detail.questions.forEach((question, qIndex) => {
+          const refName = `QuestionEditDetail-${dIndex}-${qIndex}-${question.id}`;
           const questionData = this.$refs[refName][0].getData();
           Object.assign(question, questionData);
         });

+ 23 - 4
src/modules/question/components/import-edit/FillBlankQuestion.vue

@@ -51,6 +51,8 @@ import { getInitQuestionModel } from "./model";
 import QuestionInfoEdit from "../QuestionInfoEdit.vue";
 import QuestionInfoView from "./QuestionInfoView.vue";
 import { mapState, mapMutations } from "vuex";
+import { objTypeOf } from "@/plugins/utils";
+import { isAnEmptyRichText } from "@/utils/utils";
 
 export default {
   name: "FillBlankQuestion",
@@ -81,10 +83,7 @@ export default {
     ...mapMutations("import-edit", ["setCurQuestion"]),
     initData() {
       this.modalForm = this.$objAssign(getInitQuestionModel(), this.question);
-      this.modalForm.quesAnswer = [];
-      const quesAnswer = this.question.quesAnswer
-        ? JSON.parse(this.question.quesAnswer)
-        : [];
+      const quesAnswer = this.initAnswer(this.question.quesAnswer);
 
       if (!this.question.body || !this.question.body.sections) return;
       this.question.body.sections.forEach((section) => {
@@ -98,6 +97,26 @@ export default {
         });
       });
     },
+    initAnswer(answer) {
+      let quesAnswer = [];
+      if (answer) {
+        try {
+          quesAnswer = JSON.parse(answer);
+        } catch (err) {
+          console.log(`answer error:${answer}`);
+        }
+      }
+      if (this.checkAnswerFormatValid(quesAnswer)) {
+        return quesAnswer.map((item) =>
+          isAnEmptyRichText(item) ? null : item
+        );
+      } else {
+        return [];
+      }
+    },
+    checkAnswerFormatValid(answer) {
+      return objTypeOf(answer) !== "array";
+    },
     questionInfoChange(questionInfo) {
       this.modalForm = Object.assign({}, this.modalForm, questionInfo);
     },

+ 22 - 3
src/modules/question/components/import-edit/MatchQuestion.vue

@@ -54,6 +54,7 @@ import QuestionInfoEdit from "../QuestionInfoEdit.vue";
 import QuestionInfoView from "./QuestionInfoView.vue";
 import { getInitQuestionModel } from "./model";
 import { mapState, mapMutations } from "vuex";
+import { objTypeOf } from "@/plugins/utils";
 
 export default {
   name: "MatchQuestion",
@@ -111,11 +112,29 @@ export default {
     initData() {
       this.modalForm = this.$objAssign(getInitQuestionModel(), this.question);
       this.modalForm.courseId = this.parentQuestion.courseId;
-      this.modalForm.quesAnswer = this.question.quesAnswer
-        ? JSON.parse(this.question.quesAnswer)
-        : [];
+      this.initAnswer(this.question.quesAnswer);
       this.quesAnswer = this.modalForm.quesAnswer[0] || null;
     },
+    initAnswer(answer) {
+      let quesAnswer = [];
+      if (answer) {
+        try {
+          quesAnswer = JSON.parse(answer);
+        } catch (err) {
+          console.log(`answer error:${answer}`);
+        }
+      }
+      if (this.checkAnswerFormatValid(quesAnswer)) {
+        this.modalForm.quesAnswer = quesAnswer;
+      } else {
+        this.modalForm.quesAnswer = [];
+      }
+    },
+    checkAnswerFormatValid(answer) {
+      if (objTypeOf(answer) !== "array") return;
+
+      return !answer.some((item) => !/^[0-9]{1,2}$/.test(item));
+    },
     answerChange() {
       this.modalForm.quesAnswer =
         this.quesAnswer || this.quesAnswer === 0 ? [this.quesAnswer] : [];

+ 25 - 4
src/modules/question/components/import-edit/SelectQuestion.vue

@@ -76,6 +76,7 @@ import QuestionInfoEdit from "../QuestionInfoEdit.vue";
 import QuestionInfoView from "./QuestionInfoView.vue";
 import { getInitQuestionModel } from "./model";
 import { mapState, mapMutations } from "vuex";
+import { objTypeOf } from "@/plugins/utils";
 
 export default {
   name: "SelectQuestion",
@@ -131,11 +132,11 @@ export default {
     ...mapMutations("import-edit", ["setCurQuestion"]),
     initData() {
       this.modalForm = this.$objAssign(getInitQuestionModel(), this.question);
-      this.modalForm.quesAnswer = this.question.quesAnswer
-        ? JSON.parse(this.question.quesAnswer)
-        : [];
+      // 因为会缓存答案,所以会导致答案的格式完全不确定。
+      // 可能之前识别是判断题,后来识别成选择题。答案格式的不一样,会导致异常
+      this.initAnswer(this.question.quesAnswer);
       if (this.IS_SIMPLE) {
-        this.quesAnswer = this.modalForm.quesAnswer
+        this.quesAnswer = this.modalForm.quesAnswer.length
           ? this.modalForm.quesAnswer[0]
           : null;
       } else {
@@ -147,6 +148,26 @@ export default {
         return nitem;
       });
     },
+    initAnswer(answer) {
+      let quesAnswer = [];
+      if (answer) {
+        try {
+          quesAnswer = JSON.parse(answer);
+        } catch (err) {
+          console.log(`answer error:${answer}`);
+        }
+      }
+      if (this.checkAnswerFormatValid(quesAnswer)) {
+        this.modalForm.quesAnswer = quesAnswer;
+      } else {
+        this.modalForm.quesAnswer = [];
+      }
+    },
+    checkAnswerFormatValid(answer) {
+      if (objTypeOf(answer) !== "array") return;
+
+      return !answer.some((item) => !/^[0-9]{1,2}$/.test(item));
+    },
     answerChange() {
       if (this.IS_SIMPLE) {
         this.modalForm.quesAnswer =

+ 23 - 3
src/modules/question/components/import-edit/TextAnswerQuestion.vue

@@ -45,6 +45,8 @@ import { getInitQuestionModel } from "./model";
 import QuestionInfoEdit from "../QuestionInfoEdit.vue";
 import QuestionInfoView from "./QuestionInfoView.vue";
 import { mapState, mapMutations } from "vuex";
+import { objTypeOf } from "@/plugins/utils";
+import { isAnEmptyRichText } from "@/utils/utils";
 
 export default {
   name: "TextAnswerQuestion",
@@ -76,11 +78,29 @@ export default {
     ...mapMutations("import-edit", ["setCurQuestion"]),
     initData() {
       this.modalForm = this.$objAssign(getInitQuestionModel(), this.question);
-      this.modalForm.quesAnswer = this.question.quesAnswer
-        ? JSON.parse(this.question.quesAnswer)
-        : [];
+      this.initAnswer(this.question.quesAnswer);
       this.quesAnswer = this.modalForm.quesAnswer[0] || null;
     },
+    initAnswer(answer) {
+      let quesAnswer = [];
+      if (answer) {
+        try {
+          quesAnswer = JSON.parse(answer);
+        } catch (err) {
+          console.log(`answer error:${answer}`);
+        }
+      }
+      if (this.checkAnswerFormatValid(quesAnswer)) {
+        this.modalForm.quesAnswer = quesAnswer.map((item) =>
+          isAnEmptyRichText(item) ? null : item
+        );
+      } else {
+        this.modalForm.quesAnswer = [];
+      }
+    },
+    checkAnswerFormatValid(answer) {
+      return objTypeOf(answer) !== "array";
+    },
     answerChange() {
       this.modalForm.quesAnswer = [
         {