index.vue 8.64 KB
<template>
  <div class="answer" v-if="answerData.body">
    <template v-if="answerData.body.answerType == 0">
      <div class="select">
        <ul>
          <li
            :key="index"
            v-for="(item, index) in answerData.body.choice"
            :class="
              selectResult == answerData.body.answer && selectIndex == index
                ? 'select-correct'
                : '' + selectResult != answerData.body.answer &&
                  selectIndex == index
                ? 'select-error'
                : answerData.body.answer == item && selectResult
                ? 'select-correct'
                : ''
            "
            @click="optionsHandle(item, index)"
          >
            <div class="options-box">{{ spiltString(item) }}</div>
          </li>
        </ul>
      </div>
    </template>
    <template v-if="answerData.body.answerType == 1">
      <div class="fill">
        <div class="fill-answer">
          <div
            :key="index"
            @click="cursorEven(index)"
            :class="[
              'answer-item',
              selectResult[index] ? 'answer-have' : '',
              isError ? 'answer-err' : '',
              isSuccess ? 'answer-success' : '',
              isSelectSeat == index ? 'answer-select' : '',
            ]"
            v-for="(item, index) in answerData.body.answerLength"
          >
            {{ selectResult[index] ? selectResult[index] : "" }}
          </div>
        </div>
        <div class="fill-options">
          <ul>
            <li
              :key="index"
              @click="optionsHandle(item, index)"
              :class="[item.isSelect ? 'empty' : '']"
              v-for="(item, index) in answerOption"
            >
              <div class="options-box">{{ item.val }}</div>
            </li>
          </ul>
        </div>
      </div>
    </template>
    <!-- <template v-if="answerData.body.currentSize != answerData.body.size"> -->
    <template v-if="answerData.body.answerType == 0">
      <div class="next-btn next-btn-a" v-if="isMade">
        <div class="next-end">下一题</div>
        <div class="next-time">{{ timeLimit }}s后自动跳转</div>
      </div>
    </template>
    <template v-else>
      <div class="next-btn" @click="handleNext">
        <div
          :class="{
            'next-text': !timeLimit,
            'next-end': true,
          }"
        >
          下一题
        </div>
        <div class="next-time" v-if="timeLimit">{{ timeLimit }}s后自动跳转</div>
      </div>
    </template>
    <!-- </template> -->
  </div>
</template>

<script>
export default {
  props: {
    isError: {
      type: Boolean,
      default: false,
    },
    isSuccess: {
      type: Boolean,
      default: false,
    },
    isSelectSeat: Number,
    answerOption: {
      type: Array,
      default: () => {
        return [];
      },
    },
    answerData: {
      type: Object,
      default: () => {
        return {
          body: {
            size: null,
            currentSize: null,
            timeLimit: null,
            rightNum: null,
            question: null,
            choice: [],
            answerLength: 0,
          },
        };
      },
    },
    selectResult: {
      type: [String, Array],
      default: () => {
        return [];
      },
    },
    selectIndex: {
      type: Number,
    },
  },
  watch: {
    "answerData.body.timeLimit"(val) {
      if (val == 0) this.answerData.body.answerType == 1 && this.valideAnswer();
    },
  },
  inject: ["countdown"],
  data() {
    return {
      timeLimit: 0,
      downTimer: "",
      isMade: false,
      isSelect: false,
      selectItem: "",
      selectNum: "",
    };
  },
  created() {},
  methods: {
    spiltString(val) {
      return val.replace(/^[ABCD]、{0,1}/, "");
    },
    valideAnswer() {
      if (
        this.selectResult &&
        this.selectResult.join("") === this.answerData.body.answer
      ) {
        let correctNum = this.$store.state.correctNum;
        correctNum++;
        this.$store.commit("setCorrectNum", correctNum);
      }
      // console.log(this.$parent);
      this.$parent.$parent.backGame();
    },
    cursorEven(index) {
      this.$emit("cursorEven", index);
    },
    handleCountdown() {
      clearInterval(this.downTimer);
      this.timeLimit = this.countdown;
      this.downTimer = setInterval(() => {
        this.timeLimit--;
        if (this.timeLimit === 0) {
          clearInterval(this.downTimer);
        }
      }, 1000);
    },
    handleNext() {
      if (this.isSuccess || this.isError) return;
      if (
        !(this.isSuccess || this.isError) &&
        this.answerData.body.timeLimit >= 1
      ) {
        this.$emit("answerGame");
      }
      if (this.answerData.body.timeLimit >= 2) this.handleCountdown();
    },
    optionsHandle(item, index) {
      this.selectItem = item;
      this.selectNum = index;
      this.$emit("optionsHandle", item, index, this.countdown);
      if (
        this.answerData.body.answerType != 0 ||
        this.answerData.body.timeLimit < 2
      )
        return;
      this.isMade = true;
      this.timeLimit = this.countdown;
      this.downTimer = setInterval(() => {
        this.timeLimit--;
        if (this.timeLimit === 0) {
          this.isMade = false;
          this.timeLimit = this.countdown;
          clearInterval(this.downTimer);
        }
      }, 1000);
    },
  },
};
</script>

<style lang="less" scoped>
.answer {
  width: 304px;
  margin: 0 auto;
}
.select {
  min-height: 240px;
  ul {
    padding: 0;
    li {
      width: 235px;
      height: 40px;
      background: #fff4ad;
      box-shadow: 0 2px 1px 0 rgba(230, 195, 156, 0.8);
      border-radius: 10px;
      margin: 0 auto 10px;
      word-break: break-all;
      box-sizing: border-box;
      position: relative;
      overflow: hidden;
      .options-box {
        position: absolute;
        top: 1px;
        left: 2px;
        height: 36px;
        width: 231px;
        background: #ffffff;
        line-height: 36px;
        text-align: center;
        font-size: 16px;
        color: #2a2c42;
      }
    }
    .select-correct {
      background: #8ff0cc;
      box-shadow: 0 2px 1px 0 rgba(230, 195, 156, 0.8);
      ::before {
        content: "";
        position: absolute;
        top: 6px;
        left: 8px;
        width: 24px;
        height: 24px;
        background: url("https://img02.glsx.com.cn/weapp/resource/miniprogram/game/icon-success.png");
        background-size: 100% 100%;
      }
      .options-box {
        background: #8ff0cc;
      }
    }
    .select-error {
      background: #fe908e;
      box-shadow: 0 2px 2px 0 rgba(230, 195, 156, 0.8);
      ::after {
        content: "";
        position: absolute;
        top: 6px;
        right: 8px;
        width: 24px;
        height: 24px;
        background: url("https://img02.glsx.com.cn/weapp/resource/miniprogram/game/icon-error.png");
        background-size: 100% 100%;
      }
      .options-box {
        background: #fe908e;
      }
    }
  }
}
.fill {
  min-height: 30vh;
  &-answer {
    margin-top: 24px;
    flex-wrap: wrap;
    .disflex(center,center);
    .answer {
      &-item {
        .disflex;
        width: 32px;
        height: 32px;
        font-size: 16px;
        color: #2a2c42;
        border-radius: 5px;
        margin: 0 3px 3px 0;
        box-sizing: border-box;
        background: #f7f7f2;
        border-radius: 7px;
      }
      &-select {
        border: 1px solid #f0980a;
        background: #ffffff;
      }
      &-have {
        background: #fff4ad;
        border: 1px solid #febc47;
      }
      &-err {
        background: #fe908e;
        border: 1px solid #e65553;
      }
      &-success {
        background: #8ff0cc;
        border: 1px solid #48c0a8;
      }
    }
  }
  &-options {
    margin-top: 20px;
    ul {
      padding: 0;
      flex-wrap: wrap;
      .disflex(center,center);
      li {
        .disflex;
        width: 34px;
        height: 34px;
        background: #fff4ad;
        box-shadow: 0 2px 1px 0 rgba(230, 195, 156, 0.8);
        border-radius: 10px;
        font-size: 16px;
        color: #2a2c42;
        cursor: pointer;
        list-style: none;
        margin: 0 4px 6px 0;
        font-family: PingFangSC-Regular;
      }
      .empty {
        opacity: 0;
      }
    }
  }
}
.next-btn {
  background: url("https://img02.glsx.com.cn/weapp/resource/miniprogram/game/next-button.png");
  width: 192px;
  height: 80px;
  background-size: 100% 100%;
  margin: 6vh auto 0px;
  font-size: 20px;
  color: #2155a3;
  text-align: center;
  &.next-btn-a {
    margin: 6vh auto 0px;
  }
  .next-end {
    padding-top: 10px;
    &.next-text {
      padding-top: 16px;
    }
  }
  .next-time {
    font-size: 10px;
    color: #2155a3;
  }
}
</style>