Преглед изворни кода

改变播放次数,使用全局保存次数,以免套题的次数不同意。

Michael Wang пре 6 година
родитељ
комит
7beb7f388b

+ 2 - 1
src/features/OnlineExam/Examing/ExamingHome.vue

@@ -222,7 +222,8 @@ export default {
       this.updateExamState({
         exam: exam,
         paperStruct: paperStruct,
-        examQuestionList: examQuestionList
+        examQuestionList: examQuestionList,
+        allAudioPlayTimes: JSON.parse(examQuestionList[0].audioPlayTimes) || []
       });
     },
     updateQuestion: async function(next) {

+ 14 - 9
src/features/OnlineExam/Examing/QuestionBody.vue

@@ -3,9 +3,9 @@
     <div v-html="questionDetail.text"></div>
     <!-- <div v-html="questionDetail.audio"></div> -->
     <div v-for="({src, name}, index) in questionDetail.audio" :key="name" class="audio-div">
-      <audio controls preload="auto" controlsList='nodownload' :name="name" :src="src" @play="($event) => played(index, $event)"></audio>
+      <audio controls preload="auto" controlsList='nodownload' :key="src" :name="name" :src="src" @play="($event) => played(index, $event)"></audio>
       <span>(剩余播放次数:{{examQuestion.limitedPlayTimes -
-        (audioPlayTimes.find(a => a.name === name) || { times: 0 }).times}})</span><br />
+        (allAudioPlayTimes.find(a => a.name === name) || { times: 0 }).times}})</span><br />
     </div>
   </div>
 
@@ -16,7 +16,7 @@
 
 <script>
 import { createNamespacedHelpers } from "vuex";
-const { mapMutations } = createNamespacedHelpers("examingHomeModule");
+const { mapState, mapMutations } = createNamespacedHelpers("examingHomeModule");
 
 export default {
   name: "QuestionBody",
@@ -37,7 +37,7 @@ export default {
   //   }
   // },
   methods: {
-    ...mapMutations(["updateExamQuestion"]),
+    ...mapMutations(["updateExamQuestion", "updateQuestionAudioPlayTimes"]),
     async parseQuestion() {
       let question = {};
       if (this.questionBody.includes("question-audio")) {
@@ -85,7 +85,7 @@ export default {
           // console.log(`Found ${array1[0]}. Next starts at ${re.lastIndex}.`);
           // console.log(array1[1]);
           question.audio.push({ name: array1[1], src: array1[2] });
-          console.log(question.audio);
+          // console.log(question.audio);
           // expected output: "Found foo. Next starts at 9."
           // expected output: "Found foo. Next starts at 19."
         }
@@ -128,6 +128,7 @@ export default {
     //   });
     // },
     played(index, $event) {
+      // $event.target.pause();
       const limitedPlayTimes = this.examQuestion.limitedPlayTimes;
       let audioPlayTimes = this.audioPlayTimes;
       console.log("开始播放");
@@ -141,17 +142,21 @@ export default {
         $event.target.pause();
         return;
       }
+
+      // var audio = new Audio($event.target.src);
+      // audio.play();
+
       if (theAudio) {
         theAudio.times = playedTimes + 1;
       } else {
         this.audioPlayTimes.push({ name: name, times: 1 });
       }
-      this.updateExamQuestion({
-        order: this.examQuestion.order,
-        audioPlayTimes: JSON.stringify(this.audioPlayTimes)
-      });
+      this.updateQuestionAudioPlayTimes(name);
     }
   },
+  computed: {
+    ...mapState(["allAudioPlayTimes"])
+  },
   watch: {
     questionBody() {
       this.parseQuestion();

+ 62 - 1
src/store.js

@@ -28,7 +28,8 @@ const examingHomeModule = {
     questionFilterType: "ALL",
     snapNow: false,
     snapProcessingCount: 0,
-    shouldSubmitPaper: false
+    shouldSubmitPaper: false,
+    allAudioPlayTimes: []
   },
   mutations: {
     toggleSnapNow(state) {
@@ -49,6 +50,51 @@ const examingHomeModule = {
     updateQuestionFilter(state, type) {
       state.questionFilterType = type;
     },
+    updateQuestionAudioPlayTimes(state, payload) {
+      // const cAudio = state.allAudioPlayTimes.find(
+      //   audio => audio.name === payload
+      // ) || { name: payload, times: 1 };
+
+      // console.log(cAudio);
+      // console.log(
+      //   state.allAudioPlayTimes.find(audio => audio.name === payload)
+      // );
+
+      let alreayHas = false;
+      let allAudioPlayTimes = state.allAudioPlayTimes.map(audio => {
+        if (audio.name === payload) {
+          alreayHas = true;
+          const times = audio.times + 1;
+          return { name: payload, times: times };
+        }
+        return audio;
+      });
+      // console.log(allAudioPlayTimes);
+      if (!alreayHas) {
+        // let o = {};
+        // o.name = payload;
+        // o.times = 1;
+        allAudioPlayTimes.push({ name: payload, times: 1 });
+        // allAudioPlayTimes.push(o);
+      }
+      // console.log(allAudioPlayTimes);
+
+      const examQuestionList = state.examQuestionList.map(eq => {
+        // console.log(eq.order, order);
+        if (eq.order == 1) {
+          return Object.assign(
+            {},
+            eq,
+            { dirty: true },
+            { getQuestionContent: true }, // 第一题总是获取
+            { audioPlayTimes: JSON.stringify(allAudioPlayTimes) }
+          );
+        }
+        return eq;
+      });
+
+      state = Object.assign(state, { allAudioPlayTimes, examQuestionList });
+    },
     updateExamQuestion(
       state,
       { order, studentAnswer, isSign, audioPlayTimes, getQuestionContent }
@@ -68,6 +114,21 @@ const examingHomeModule = {
         }
         return eq;
       });
+      // if (audioPlayTimes) {
+      //   const cq = state.examQuestionList[order - 1];
+      //   examQuestionList = examQuestionList.map(eq => {
+      //     if (cq.questionId == eq.questionId) {
+      //       // 保存套题的音频播放次数
+      //       return Object.assign(
+      //         {},
+      //         eq,
+      //         { dirty: true },
+      //         audioPlayTimes !== undefined && { audioPlayTimes }
+      //       );
+      //     }
+      //     return eq;
+      //   });
+      // }
       state = Object.assign(state, { examQuestionList });
     },
     resetExamQuestionDirty(state) {