AnswerLesson.vue 9.27 KB
Newer Older
耿迪迪's avatar
耿迪迪 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<template>
  <el-dialog
    class="answerLesson"
    title="开始答题"
    :visible.sync="visible"
    width="57.5%"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="dialogCancel"
    @closed="closeFinished"
    destroy-on-close
  >
    <div ref="myBody" class="body" v-loading="loading">
      <div class="text">
15
        <div class="float">{{courseName}}</div>
耿迪迪's avatar
耿迪迪 committed
16 17 18 19 20 21 22
      </div>
      <transition name="fade" mode="out-in">
        <div :key="goodJobShow">
          <template v-if="!goodJobShow">
            <transition name="fade" mode="out-in">
              <div class="question-wrapper" v-if="visible" :key="nowQuestion">
                <div v-for="(item, index) in list" :key="item.id">
纪泽龙's avatar
纪泽龙 committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
                  <template v-if="item.topicType === 1 || item.topicType === 3">
                    <Question
                      v-if="index === nowQuestion"
                      :questionObj="item"
                      :index="index"
                      :nowQuestion="nowQuestion"
                      :selectLetter="selectLetter"
                      @changeLetter="changeLetter"
                    />
                  </template>
                  <template v-else>
                    <QuestionChoice
                      v-if="index === nowQuestion"
                      :questionObj="item"
                      :index="index"
                      :nowQuestion="nowQuestion"
                      :selectLetter="selectLetter"
                      @changeLetter="changeLetter"
                    />
                  </template>
耿迪迪's avatar
耿迪迪 committed
43 44 45 46 47 48 49 50 51 52 53 54
                </div>
              </div>
            </transition>

            <div class="select flex">
              <div class="select-item flex">
                <div
                  class="item"
                  :class="{
                    active:
                      answerArr.findIndex(
                        (item) => item.questionNum === index + 1
纪泽龙's avatar
纪泽龙 committed
55 56 57 58 59
                      ) >= 0 && activeBool(index),
                    activered:
                      answerArr.findIndex(
                        (item) => item.questionNum === index + 1
                      ) >= 0 && !activeBool(index),
耿迪迪's avatar
耿迪迪 committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
                    now: index === nowQuestion,
                  }"
                  v-for="(item, index) in list"
                  :key="item.id + 'a' + index"
                  @click="questionNumClick(index)"
                >
                  {{ index + 1 }}
                </div>
                <div @click="nextBtnClick" class="btn">下一题</div>
              </div>
            </div>
          </template>

          <template v-else>
            <div :style="{ height: startHeight }">
              <GoodJob :goodJobData="goodJobData" />
            </div>
          </template>
        </div>
      </transition>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="closeFinished" v-if="goodJobShow"
        >重新考试</el-button
      >
      <el-button type="primary" @click="dialogSubmitForm" v-else
        >交卷</el-button
      >
      <el-button @click="dialogCancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Question from "./Question";
纪泽龙's avatar
纪泽龙 committed
96
import QuestionChoice from "./QuestionChoice";
耿迪迪's avatar
耿迪迪 committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
import GoodJob from "./GoodJob.vue";
import {
  userQuestionList,
  setAnswer,
} from "@/api/educationPlanExam/lessonsProgram";
export default {
  name: "AnswerLesson",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    userCourseId: {
      type: [Number, String],
    },
112 113 114
    courseName:{
      type: [String,String]
    },
耿迪迪's avatar
耿迪迪 committed
115 116 117 118 119 120
    courseId: {
      type: [Number, String],
    },
  },
  components: {
    Question,
纪泽龙's avatar
纪泽龙 committed
121
    QuestionChoice,
耿迪迪's avatar
耿迪迪 committed
122 123
    GoodJob,
  },
纪泽龙's avatar
纪泽龙 committed
124

耿迪迪's avatar
耿迪迪 committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  data() {
    return {
      nowQuestion: 0,
      startHeight: "0px",
      goodJobShow: false,
      goodJobData: {},
      loading: false,
      list: [
        {
          id: 19,
          text: "j9",
          question: ["adsf", "dfgsdfg", "adsfadsf", "dfasdfadsf"],
        },
      ],
      answerArr: [],
      // 题目是否被答过,如果答过,就把值传回去,如果没有答过,就是空
纪泽龙's avatar
纪泽龙 committed
141
      selectLetter: [],
耿迪迪's avatar
耿迪迪 committed
142 143
    };
  },
纪泽龙's avatar
纪泽龙 committed
144

耿迪迪's avatar
耿迪迪 committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  // watch: {
  //   visible(newValue) {
  //     if (newValue) {
  //       this.$nextTick(() => {
  //         this.saveBody();
  //       });
  //     }
  //   },
  // },
  created() {
    userQuestionList({ courseId: this.courseId }).then((res) => {
      console.log(res.data);
      this.list = res.data.map((item) => {
        return {
          id: item.topicId,
          text: item.topicTitle,
纪泽龙's avatar
纪泽龙 committed
161
          topicType: item.topicType,
耿迪迪's avatar
耿迪迪 committed
162 163 164 165 166 167 168 169 170 171 172 173 174
          question: JSON.parse(item.topicOption).map((item) => item.value),
        };
      });
    });
  },
  methods: {
    saveBody() {
      this.startHeight = this.$refs.myBody.offsetHeight - 55 + "px";
    },
    dialogSubmitForm() {
      // this.answerClear();
      // this.$emit("update:visible", false);
      this.saveBody();
纪泽龙's avatar
纪泽龙 committed
175 176
      const json = JSON.stringify(this.answerArr.map((item) => item.answer));
      const answers = json.slice(1,json.length-1);
纪泽龙's avatar
纪泽龙 committed
177
      console.log(answers);
耿迪迪's avatar
耿迪迪 committed
178 179 180 181 182 183 184
      this.loading = true;
      setAnswer({
        userCourseId: this.userCourseId,
        answers,
      })
        .then((res) => {
          if (res.code == 200) {
185
            console.log(res.data)
耿迪迪's avatar
耿迪迪 committed
186 187 188 189 190 191 192
            this.goodJobData = res.data;
            this.goodJobShow = true;
          }
        })
        .finally(() => {
          this.loading = false;
          // 是否作对
纪泽龙's avatar
纪泽龙 committed
193
          this.$emit("jj", this.goodJobData);
耿迪迪's avatar
耿迪迪 committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        });
    },
    dialogCancel() {
      this.$emit("update:visible", false);
    },
    // 关闭之后
    closeFinished() {
      this.answerClear();
      this.goodJobShow = false;
    },
    answerClear() {
      this.answerArr = [];
      this.nowQuestion = 0;
    },
    // 点题目时
    questionNumClick(index) {
      // 是否是回答过的,数组中存在它,那它就是回答过的
      const bool =
        this.answerArr.findIndex((item) => item.questionNum === index + 1) >= 0;
      // 或者下一题与当前题目是紧挨着的并且当前题目是答完的,相差为1就算是紧挨着的
      const nowQuestionAnswerBool = this.nextQuestion(index);
      if (bool || nowQuestionAnswerBool) {
        this.nowQuestion = index;
      }
      // 赋值,如果答过的,就传回去,如果没答过,就是空 变成字符串是因为0位false
纪泽龙's avatar
纪泽龙 committed
219
      this.selectLetter = this.answerArr[this.nowQuestion]?.answer || [];
耿迪迪's avatar
耿迪迪 committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    },
    nextBtnClick() {
      // 到头了,打完了
      if (this.nowQuestion + 1 == this.list.length) return;
      this.questionNumClick(this.nowQuestion + 1);
    },
    // 紧挨着且当前题目是打完的
    nextQuestion(index) {
      // 下一题相差1
      // const nextIndexBool = index - this.nowQuestion == 1;
      // 答案数组的长度,就是档当前达到了第几题,长度-1是因为题目是从0开始记录
      const nextIndexBool = index - (this.answerArr.length - 1) == 1;
      // 当前题已经回答过
      const nowQuestionAnswerBool =
        this.answerArr.findIndex(
          (item) => item.questionNum === this.nowQuestion + 1
        ) >= 0;
      return nextIndexBool && nowQuestionAnswerBool;
    },
    changeLetter(letter) {
      console.log(letter);
      const obj = {};
      obj.questionNum = this.nowQuestion + 1;
      obj.answer = letter;
      // 数组中是否存在这个题目
      const index = this.answerArr.findIndex(
        (item) => item.questionNum === this.nowQuestion + 1
      );
      if (index < 0) {
        // 如果不存在
        // 推入
        this.answerArr.push(obj);
      } else {
        // 如果存在
        // 替换
        this.answerArr.splice(index, 1, obj);
      }
纪泽龙's avatar
纪泽龙 committed
257 258 259 260
      console.log(this.answerArr);
    },
    activeBool(index) {
      return this.answerArr[index]?.answer.length > 0;
耿迪迪's avatar
耿迪迪 committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    },
  },
};
</script>
<style lang="scss" scoped>
.body {
  width: 100%;
  height: 100%;
  padding-right: 50px;
  padding-left: 60px;
  .question-wrapper {
  }
  .text {
    margin-bottom: 27px;
    .float {
      padding-right: 7px;
      width: 411px;
      height: 28px;
      background: #1d84ff;
      line-height: 28px;
      color: #ffffff;
      font-size: 14px;
      text-align: right;
      float: right;
    }
    &:after {
      content: "";
      display: block;
      clear: both;
    }
  }
  .select {
    .select-item {
      padding-top: 20px;
      flex-wrap: wrap;
      > div {
        margin-bottom: 10px;
      }
      .item {
        width: 28px;
        height: 28px;
        border: 1px solid #bbbbbb;
        line-height: 28px;
        font-size: 14px;
        text-align: center;
        margin-right: 18px;
纪泽龙's avatar
纪泽龙 committed
307
        box-sizing: border-box;
耿迪迪's avatar
耿迪迪 committed
308 309
        cursor: pointer;
        &.active {
纪泽龙's avatar
纪泽龙 committed
310 311
          background: #afe8ab87;
          border:none;
耿迪迪's avatar
耿迪迪 committed
312
        }
纪泽龙's avatar
纪泽龙 committed
313
        &.activered {
纪泽龙's avatar
纪泽龙 committed
314 315 316
          background: #b9112633;
          border:none;

纪泽龙's avatar
纪泽龙 committed
317
        }
耿迪迪's avatar
耿迪迪 committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
        &.now {
          background: #a3d3ff;
          border: none;
        }
      }
      .btn {
        width: 84px;
        height: 28px;
        background: #e8f4ff;
        border: 1px solid #a3d3ff;
        color: #1d84ff;
        text-align: center;
        line-height: 28px;
        font-size: 14px;
        cursor: pointer;
        &:hover {
          background: rgba(29, 132, 255, 0.5);
          color: #ffffff;
        }
      }
    }
  }
}
</style>