question.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div id="question">
  3. <el-row>
  4. <el-col :span="24" class="style">
  5. <el-col :span="24" class="top">
  6. <nav-bar v-show="navShow" :title="title" :isleftarrow="isleftarrow"> </nav-bar>
  7. </el-col>
  8. <el-col :span="24" class="main" v-if="info">
  9. <question-info :info="info" :form="form" @submit="submit"></question-info>
  10. </el-col>
  11. <el-col :span="24" class="foot">
  12. <foot-info></foot-info>
  13. </el-col>
  14. </el-col>
  15. </el-row>
  16. </div>
  17. </template>
  18. <script>
  19. import NavBar from '@/layout/common/topInfo.vue';
  20. import footInfo from '@/layout/common/footInfo.vue';
  21. import questionInfo from '@question/src/views/question.vue';
  22. import { mapState, createNamespacedHelpers } from 'vuex';
  23. const { mapActions: mapQuestion } = createNamespacedHelpers('questionnaire');
  24. const { mapActions: questionAnswer } = createNamespacedHelpers('questionAnswer');
  25. export default {
  26. name: 'question',
  27. props: {},
  28. components: {
  29. NavBar, //头部导航
  30. footInfo, //底部导航
  31. questionInfo, //问卷调查
  32. },
  33. data: () => ({
  34. info: {},
  35. form: {
  36. opname: [],
  37. },
  38. title: '',
  39. isleftarrow: '',
  40. transitionName: 'fade',
  41. navShow: true,
  42. }),
  43. created() {
  44. this.searchInfo();
  45. },
  46. computed: {
  47. ...mapState(['user']),
  48. id() {
  49. return this.$route.query.id;
  50. },
  51. },
  52. mounted() {
  53. this.title = this.$route.meta.title;
  54. this.isleftarrow = this.$route.meta.isleftarrow;
  55. },
  56. watch: {
  57. $route(to, from) {
  58. this.title = to.meta.title;
  59. this.isleftarrow = to.meta.isleftarrow;
  60. },
  61. },
  62. methods: {
  63. ...mapQuestion(['query', 'fetch', 'update']),
  64. ...questionAnswer({ getAnswer: 'query', sendAnswer: 'create', updateAnswer: 'update' }),
  65. // 查询问卷题
  66. async searchInfo({ ...info } = {}) {
  67. let answer = await this.getAnswer({ questionnaireid: this.id, studentid: this.user.userid });
  68. let asArr = [];
  69. let asObject = {};
  70. if (this.$checkRes(answer)) {
  71. let tAnswer = answer.data[0];
  72. asArr = _.get(tAnswer, `answers`, []);
  73. asObject.answerid = _.get(tAnswer, `_id`);
  74. }
  75. let result = await this.fetch(this.id);
  76. // 将答案塞进去
  77. result.data.question.map(i => {
  78. if (!i.answer) i.type === '1' ? (i.answer = []) : '';
  79. let mid = asArr.find(f => f.questionid === i._id);
  80. if (_.get(mid, `answer`)) {
  81. i.answer = JSON.parse(mid.answer);
  82. }
  83. return i;
  84. });
  85. this.$set(this, `info`, { ...result.data, ...asObject });
  86. },
  87. // 提交答案
  88. async submit(task) {
  89. let { answerid } = task;
  90. let { termid, batchid, classid, userid: studentid, planid } = this.user;
  91. let answers = task.question.map(i => {
  92. let { answer, _id: questionid } = i;
  93. answer ? (answer = JSON.stringify(answer)) : '';
  94. return { answer, questionid };
  95. });
  96. let object = { termid, batchid, classid, studentid, planid, questionnaireid: this.id, answers };
  97. let res;
  98. if (!answerid) {
  99. res = await this.sendAnswer(object);
  100. } else {
  101. object.id = answerid;
  102. res = await this.updateAnswer(object);
  103. }
  104. this.$checkRes(res, '提交成功', res.errmsg);
  105. },
  106. },
  107. };
  108. </script>
  109. <style lang="less" scoped>
  110. .style {
  111. width: 100%;
  112. min-height: 667px;
  113. position: relative;
  114. background-color: #f9fafc;
  115. }
  116. .top {
  117. height: 46px;
  118. overflow: hidden;
  119. position: relative;
  120. z-index: 999;
  121. }
  122. .main {
  123. min-height: 570px;
  124. margin: 0 0 50px 0;
  125. }
  126. </style>