Browse Source

问卷部分对接

lrf402788946 5 years ago
parent
commit
8f77491cb1
4 changed files with 53 additions and 12 deletions
  1. 2 2
      src/plugins/check-res.js
  2. 4 0
      src/store/index.js
  3. 12 6
      src/views/question/index.vue
  4. 35 4
      src/views/question/question.vue

+ 2 - 2
src/plugins/check-res.js

@@ -23,7 +23,7 @@ const Plugin = {
           return _okText();
         }
         if (_okText) {
-          Message.success(_okText);
+          // Message.success(_okText);
           Notify({ type: 'success', message: _okText });
         }
         return true;
@@ -31,7 +31,7 @@ const Plugin = {
       if (_.isFunction(_errText)) {
         return _errText();
       }
-      Message.error(_errText || errmsg);
+      // Message.error(_errText || errmsg);
       Notify({ type: 'danger', message: _okText });
       // Message({ message: _errText || errmsg, duration: 60000 });
       return false;

+ 4 - 0
src/store/index.js

@@ -4,11 +4,13 @@ import leave from '@frame/store/leave';
 import attendance from '@frame/store/attendance';
 import group from '@frame/store/group';
 import question from '@frame/store/question';
+import questionAnswer from '@frame/store/question-answer';
 import questionnaire from '@frame/store/questionnaire';
 import student from '@frame/store/student';
 import lesson from '@frame/store/lesson';
 import teacher from '@frame/store/teacher';
 import score from '@frame/store/score';
+import termquest from '@frame/store/termquest';
 import login from '@frame/store/login';
 import * as ustate from '@frame/store/user/state';
 import * as umutations from '@frame/store/user/mutations';
@@ -24,11 +26,13 @@ export default new Vuex.Store({
     attendance,
     group,
     question,
+    questionAnswer,
     questionnaire,
     student,
     lesson,
     teacher,
     score,
+    termquest,
     login,
   },
 });

+ 12 - 6
src/views/question/index.vue

@@ -22,7 +22,8 @@ import NavBar from '@/layout/common/topInfo.vue';
 import footInfo from '@/layout/common/footInfo.vue';
 import questionnaireInfo from '@question/src/views/index.vue';
 import { mapState, createNamespacedHelpers } from 'vuex';
-const { mapActions: mapQuestion } = createNamespacedHelpers('questionnaire');
+const { mapActions: termquest } = createNamespacedHelpers('termquest');
+const { mapActions: questionnaire } = createNamespacedHelpers('questionnaire');
 export default {
   name: 'index',
   props: {},
@@ -39,7 +40,7 @@ export default {
     navShow: true,
   }),
   created() {
-    this.searchNaireList();
+    this.search();
   },
   computed: {
     ...mapState(['user']),
@@ -55,10 +56,15 @@ export default {
     },
   },
   methods: {
-    ...mapQuestion(['query', 'fetch', 'update']),
-    async searchNaireList({ ...info } = {}) {
-      const res = await this.query({ ...info });
-      this.$set(this, `questionnaireList`, res.data);
+    ...termquest(['query']),
+    ...questionnaire({ getQuestList: 'query', getQuestMerge: 'mergeRequest' }),
+    async search() {
+      let res = await this.query({ termid: this.user.termid });
+      if (this.$checkRes(res)) {
+        let ids = _.flattenDeep(res.data.map(i => i.questionnaireid));
+        let quest = await this.getQuestMerge({ method: 'fetch', data: ids });
+        this.$set(this, `questionnaireList`, quest);
+      }
     },
   },
 };

+ 35 - 4
src/views/question/question.vue

@@ -21,8 +21,9 @@
 import NavBar from '@/layout/common/topInfo.vue';
 import footInfo from '@/layout/common/footInfo.vue';
 import questionInfo from '@question/src/views/question.vue';
-import { createNamespacedHelpers, mapGetters } from 'vuex';
+import { mapState, createNamespacedHelpers } from 'vuex';
 const { mapActions: mapQuestion } = createNamespacedHelpers('questionnaire');
+const { mapActions: questionAnswer } = createNamespacedHelpers('questionAnswer');
 export default {
   name: 'question',
   props: {},
@@ -45,6 +46,7 @@ export default {
     this.searchInfo();
   },
   computed: {
+    ...mapState(['user']),
     id() {
       return this.$route.query.id;
     },
@@ -61,16 +63,45 @@ export default {
   },
   methods: {
     ...mapQuestion(['query', 'fetch', 'update']),
+    ...questionAnswer({ getAnswer: 'query', sendAnswer: 'create', updateAnswer: 'update' }),
     async searchInfo({ ...info } = {}) {
+      let answer = await this.getAnswer({ questionnaireid: this.id, studentid: this.user.userid });
+      let asArr = [];
+      let asObject = {};
+      if (this.$checkRes(answer)) {
+        let tAnswer = answer.data[0];
+        asArr = _.get(tAnswer, `answers`, []);
+        asObject.answerid = _.get(tAnswer, `_id`);
+      }
       let result = await this.fetch(this.id);
+      // 将答案塞进去
       result.data.question.map(i => {
         if (!i.answer) i.type === '1' ? (i.answer = []) : '';
+        let mid = asArr.find(f => f.questionid === i.id);
+        if (_.get(mid, `answer`)) {
+          i.answer = JSON.parse(mid.answer);
+        }
         return i;
       });
-      this.$set(this, `info`, result.data);
+      this.$set(this, `info`, { ...result.data, ...asObject });
     },
-    submit(task) {
-      console.log(task);
+    async submit(task) {
+      let { answerid } = task;
+      let { termid, batchid, classid, userid: studentid } = this.user;
+      let answers = task.question.map(i => {
+        let { answer, _id: questionid } = i;
+        answer ? (answer = JSON.stringify(answer)) : '';
+        return { answer, questionid };
+      });
+      let object = { termid, batchid, classid, studentid, questionnaireid: this.id, answers };
+      let res;
+      if (!answerid) {
+        res = await this.sendAnswer(object);
+      } else {
+        object.id = answerid;
+        res = await this.updateAnswer(object);
+      }
+      this.$checkRes(res, '提交成功', res.errmsg);
     },
   },
 };