Browse Source

将公用数据与方法封装入 QuestionMixin.js 中

dusenyao 1 year ago
parent
commit
e29baef464

+ 55 - 0
src/views/exercise_questions/create/components/common/QuestionMixin.js

@@ -0,0 +1,55 @@
+// 题目混入
+import QuestionBase from './QuestionBase.vue';
+import RichText from '@/components/common/RichText.vue';
+
+import {
+  stemTypeList,
+  scoreTypeList,
+  switchOption,
+  questionNumberTypeList,
+  computedQuestionNumber,
+} from '@/views/exercise_questions/data/common';
+
+const mixin = {
+  data() {
+    return {
+      stemTypeList,
+      scoreTypeList,
+      switchOption,
+      questionNumberTypeList,
+      computedQuestionNumber,
+    };
+  },
+  components: {
+    QuestionBase,
+    RichText,
+  },
+  methods: {
+    upload(file_id) {
+      this.data.file_id_list.push(file_id);
+    },
+    deleteFile(file_id) {
+      let index = this.data.file_id_list.indexOf(file_id);
+      if (index !== -1) {
+        this.data.file_id_list.splice(index, 1);
+      }
+    },
+    /**
+     * 设置题目内容
+     * @param {object} param
+     * @param {string} param.content 题目内容
+     */
+    setQuestion({ content }) {
+      this.data = JSON.parse(content);
+    },
+    /**
+     * 删除选项
+     * @param {Number} i 索引
+     */
+    deleteOption(i) {
+      this.data.option_list.splice(i, 1);
+    },
+  },
+};
+
+export default mixin;

+ 6 - 2
src/views/exercise_questions/create/components/common/UploadAudio.vue

@@ -4,6 +4,7 @@
       ref="upload"
       :limit="1"
       action="no"
+      accept="audio/*"
       :show-file-list="false"
       :before-upload="beforeUpload"
       :http-request="upload"
@@ -53,11 +54,14 @@ export default {
   },
   methods: {
     beforeUpload(file) {
-      if (this.file_id.length > 0) return;
+      if (this.file_id.length > 0) {
+        this.$message.warning('只能上传一个音频文件');
+        return false;
+      }
       const fileName = file.name;
       const suffix = fileName.slice(fileName.lastIndexOf('.') + 1, fileName.length).toLowerCase();
       if (!['mp3', 'wav', 'aac', 'm4a'].includes(suffix)) {
-        this.$message.error('音频格式不正确');
+        this.$message.warning('音频格式不正确');
         return false;
       }
     },

+ 3 - 37
src/views/exercise_questions/create/components/exercises/JudgeQuestion.vue

@@ -125,35 +125,21 @@
 </template>
 
 <script>
-import RichText from '@/components/common/RichText.vue';
 import UploadAudio from '../common/UploadAudio.vue';
-import QuestionBase from '../common/QuestionBase.vue';
+import QuestionMixin from '../common/QuestionMixin.js';
 
-import {
-  stemTypeList,
-  questionNumberTypeList,
-  switchOption,
-  scoreTypeList,
-  computedQuestionNumber,
-  changeOptionType,
-} from '@/views/exercise_questions/data/common';
+import { changeOptionType } from '@/views/exercise_questions/data/common';
 import { judgeData, option_type_list, option_type_value_list, getOption } from '@/views/exercise_questions/data/judge';
 
 export default {
   name: 'JudgeQuestion',
   components: {
-    QuestionBase,
-    RichText,
     UploadAudio,
   },
+  mixins: [QuestionMixin],
   data() {
     return {
-      stemTypeList,
       option_type_list,
-      questionNumberTypeList,
-      switchOption,
-      scoreTypeList,
-      computedQuestionNumber,
       changeOptionType,
       data: JSON.parse(JSON.stringify(judgeData)),
     };
@@ -180,26 +166,6 @@ export default {
     },
   },
   methods: {
-    upload(file_id) {
-      this.data.file_id_list.push(file_id);
-    },
-    deleteFile(file_id) {
-      let index = this.data.file_id_list.indexOf(file_id);
-      if (index !== -1) {
-        this.data.file_id_list.splice(index, 1);
-      }
-    },
-    /**
-     * 设置题目内容
-     * @param {object} param
-     * @param {string} param.content 题目内容
-     */
-    setQuestion({ content }) {
-      this.data = JSON.parse(content);
-    },
-    deleteOption(i) {
-      this.data.option_list.splice(i, 1);
-    },
     addOption() {
       this.data.option_list.push(getOption());
     },

+ 3 - 24
src/views/exercise_questions/create/components/exercises/MatchingQuestion.vue

@@ -29,41 +29,20 @@
 </template>
 
 <script>
-import QuestionBase from '../common/QuestionBase.vue';
-import RichText from '@/components/common/RichText.vue';
+import QuestionMixin from '../common/QuestionMixin.js';
 
-import {
-  stemTypeList,
-  switchOption,
-  scoreTypeList,
-  computedQuestionNumber,
-  changeOptionType,
-  questionNumberTypeList,
-} from '@/views/exercise_questions/data/common';
 import { matchingTypeList, matchingData } from '@/views/exercise_questions/data/matching';
 
 export default {
   name: 'MatchingQuestion',
-  components: {
-    QuestionBase,
-    RichText,
-  },
+  mixins: [QuestionMixin],
   data() {
     return {
       matchingTypeList,
       data: JSON.parse(JSON.stringify(matchingData)),
     };
   },
-  methods: {
-    /**
-     * 设置题目内容
-     * @param {object} param
-     * @param {string} param.content 题目内容
-     */
-    setQuestion({ content }) {
-      this.data = JSON.parse(content);
-    },
-  },
+  methods: {},
 };
 </script>
 

+ 6 - 50
src/views/exercise_questions/create/components/exercises/SelectQuestion.vue

@@ -129,51 +129,26 @@
 </template>
 
 <script>
-import QuestionBase from '../common/QuestionBase.vue';
-import RichText from '@/components/common/RichText.vue';
 import UploadAudio from '../common/UploadAudio.vue';
+import QuestionMixin from '../common/QuestionMixin.js';
 
-import {
-  stemTypeList,
-  selectTypeList,
-  switchOption,
-  scoreTypeList,
-  computedQuestionNumber,
-  changeOptionType,
-  questionNumberTypeList,
-} from '@/views/exercise_questions/data/common';
-import { selectData } from '@/views/exercise_questions/data/select';
-import { getRandomNumber } from '@/utils/index';
+import { selectTypeList, scoreTypeList, changeOptionType } from '@/views/exercise_questions/data/common';
+import { selectData, getOption } from '@/views/exercise_questions/data/select';
 
 export default {
   name: 'SelectQuestion',
   components: {
-    QuestionBase,
-    RichText,
     UploadAudio,
   },
+  mixins: [QuestionMixin],
   data() {
     return {
-      stemTypeList,
       selectTypeList,
-      switchOption,
-      scoreTypeList,
-      questionNumberTypeList,
       changeOptionType,
-      computedQuestionNumber,
       data: JSON.parse(JSON.stringify(selectData)),
     };
   },
   methods: {
-    upload(file_id) {
-      this.data.file_id_list.push(file_id);
-    },
-    deleteFile(file_id) {
-      let index = this.data.file_id_list.indexOf(file_id);
-      if (index !== -1) {
-        this.data.file_id_list.splice(index, 1);
-      }
-    },
     /**
      * 智能识别
      * @param {String} text 识别数据
@@ -186,25 +161,9 @@ export default {
 
       if (arr.length > 0) {
         this.data.stem = arr[0];
-        this.data.option_list = arr.slice(1).map((content) => {
-          return {
-            mark: getRandomNumber(),
-            content,
-          };
-        });
+        this.data.option_list = arr.slice(1).map((content) => getOption(content));
       }
     },
-    /**
-     * 设置题目内容
-     * @param {object} param
-     * @param {string} param.content 题目内容
-     */
-    setQuestion({ content }) {
-      this.data = JSON.parse(content);
-    },
-    deleteOption(i) {
-      this.data.option_list.splice(i, 1);
-    },
     changeSelectType(val) {
       if (val === selectTypeList[0].value && this.data.answer.select_list.length > 1) {
         this.data.answer.select_list = [this.data.answer.select_list[0]];
@@ -231,10 +190,7 @@ export default {
       }
     },
     addOption() {
-      this.data.option_list.push({
-        mark: getRandomNumber(),
-        content: '',
-      });
+      this.data.option_list.push(getOption());
     },
   },
 };

+ 4 - 0
src/views/exercise_questions/data/select.js

@@ -1,6 +1,10 @@
 import { optionTypeList, stemTypeList, selectTypeList, scoreTypeList, questionNumberTypeList } from './common';
 import { getRandomNumber } from '@/utils/index';
 
+export function getOption(content = '') {
+  return { content, mark: getRandomNumber() };
+}
+
 // 选择题数据模板
 export const selectData = {
   type: 'select', // 题型