闫增涛
2024-05-28 cb636fc0da716e6719ebaf3a848cdab47b130d96
src/components/examinations/index.vue
@@ -1,7 +1,12 @@
<template>
  <div class="examination" v-loading="loading">
    <div v-for="(item, nindex) in cardList" :key="nindex" class="border-box">
      <p class="catalogName">
  <div class="examination" v-loading="!cardData.length">
    <div
      v-for="(item, nindex) in cardData"
      :key="nindex"
      class="border-box"
      v-show="item.infoList.length"
    >
      <p class="catalogName" :style="{ color: primaryColor }">
        <span v-if="nindex == 0">一、</span>
        <span v-if="nindex == 1">二、</span>
        <span v-if="nindex == 2">三、</span>
@@ -23,8 +28,8 @@
                :style="{
                  marginTop: value.questionType == 'completion' ? '12px' : '0',
                }"
                >{{ index + 1 }}. </span
              >
                >{{ index + 1 }}.
              </span>
              <!-- 听力进度条 -->
              <!-- <answerAudioPlayer :audioUrl="value.src" v-show="item.name == '听力题'" /> -->
              <!-- 题干 -->
@@ -63,7 +68,7 @@
                  v-for="(itemText, indexText) in value.stem"
                  :key="indexText"
                >
                  <span v-if="typeof itemText == 'string'" >{{ itemText }}</span>
                  <span v-if="typeof itemText == 'string'">{{ itemText }}</span>
                  <!-- &nbsp; -->
                  <el-input
                    v-else
@@ -206,7 +211,8 @@
            "
          ></TEditorVue> -->
            <el-input
              style="width: 90%; margin: 0 auto"
              :disabled="value.isComplete"
              style="width: 94%; margin: 0 auto"
              v-else-if="value.questionType == 'shortAnswer'"
              type="textarea"
              :rows="6"
@@ -274,12 +280,12 @@
                      </p>
                      <div class="headerConent sitgBox">
                        <p v-if="!value.isUnfold">
                          <span class="analysisColor">查看解析</span
                          ><el-image  />
                          <span class="analysisColor">展开解析</span
                          ><el-image />
                        </p>
                        <p v-else>
                          <span class="analysisColor">收起解析</span
                          ><el-image  />
                          ><el-image />
                        </p>
                      </div>
                    </div>
@@ -293,12 +299,53 @@
                </el-collapse-item>
                <!-- 主观题 -->
              </el-collapse>
              <div
              <el-collapse v-else v-model="value.isUnfold" accordion>
                <el-collapse-item :name="value.id" class="objective">
                  <template #title>
                    <div class="headerBox">
                      <p v-if="!value.isUnfold">
                        <span class="analysisColor">展开答案与解析</span>
                      </p>
                      <p v-else>
                        <span class="analysisColor">收起答案与解析</span>
                      </p>
                    </div>
                  </template>
                  <div class="referBox">
                    <span>参考答案:</span>
                    <p
                      v-html="arrayToString(value.answer)"
                      class="ti-2"
                      :class="[
                        'subjectiveBox',
                        value.isUnfold ? 'showText' : 'hideText',
                      ]"
                    ></p>
                  </div>
                  <!-- 解析 -->
                  <div class="referBox">
                    <span>解析:</span>
                    <p
                      v-if="value.analysisCon"
                      v-html="value.analysisCon"
                      :class="
                        [
                          'subjectiveBox',
                          'ti-2',
                          value.isUnfold ? 'yincang' : '',
                        ].join(' ')
                      "
                    ></p>
                    <p v-else class="ti-2">暂无数据</p>
                  </div>
                </el-collapse-item>
              </el-collapse>
              <!-- <div
                v-if="value.questionType == 'shortAnswer'"
                class="subjective subjectiveItem"
              >
                <div class="questionAnalysis">
                  <p class="difficulty">
                  <p class="difficulty" v-if="value.difficulty">
                    <span style="color: #333">难度等级:</span>
                    <el-rate
                      v-model="value.difficulty"
@@ -312,12 +359,10 @@
                    <span class="analysisColor"
                      >{{
                        value.isUnfold ? "收起答案和解析" : "查看答案与解析"
                      }} </span
                    ><el-image :src="!value.isUnfold ? chakanIcon : packIcon" />
                      }}
                    </span>
                  </p>
                </div>
                <!-- :class="['analysisDetailBox', value.isUnfold ? 'open' : 'close'].join(' ') -->
                <!-- :class="['analysisDetailBox', value.isUnfold ? 'cartoon' : 'downCartoon'].join(' ') -->
                <div
                  :class="
                    [
@@ -328,7 +373,6 @@
                >
                  <div class="referBox">
                    <span>参考答案:</span>
                    <!-- <p v-html="arrayToString(value.answer)" class="subjectiveBox"></p> -->
                    <p
                      v-html="arrayToString(value.answer)"
                      :class="[
@@ -351,26 +395,25 @@
                    <p v-else>暂无数据</p>
                  </div>
                </div>
              </div>
              </div> -->
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="bottom-btn" v-if="!loading">
      <el-button style="height: 34px; padding: 4px 10px">保存</el-button>
      <el-button class="btn-box" @click="saveAnswer">保存</el-button>
      <el-button
        @click="handleQuestion"
        style="border-color: #1eb9ee; height: 34px; padding: 4px 10px"
        class="btn-box"
        :style="{ borderColor: primaryColor }"
        >提交</el-button
      >
      <el-button @click="redo" style="height: 34px; padding: 4px 10px"
        >重做</el-button
      >
      <el-button @click="redo" class="btn-box">重做</el-button>
      <el-button
        @click="openAnswers"
        style="border-color: #1eb9ee; height: 34px; padding: 4px 10px"
        class="btn-box"
        :style="{ borderColor: primaryColor }"
        >查看答案</el-button
      >
    </div>
@@ -378,19 +421,36 @@
</template>
<script>
import { getPublicImage } from "@/assets/js/middleGround/tool";
export default {
  name: "examination-option",
  props: ["productLinkPath"],
  props: {
    cardList: {
      type: Array,
      default: [],
    },
    primaryColor: {
      type: String,
      default: "#00aeef",
    },
    chapter: {
      type: Number,
    },
    page: {
      type: Number,
    },
  },
  data() {
    return {
      type: "option",
      cardList: [],
      loading: false,
      cardData:[]
    };
  },
  inject:['changeQuestionData'],
  mounted() {
    this.getIdPathList();
    this.cardData = this.cardList
    console.log('this.cardList',this.cardList);
    console.log('this.cardData',this.cardData);
  },
  methods: {
    // 数组转为字符串方法
@@ -425,24 +485,19 @@
      }
    },
    redo() {
      for (let index = 0; index < this.cardList.length; index++) {
        const item = this.cardList[index];
      for (let index = 0; index < this.cardData.length; index++) {
        const item = this.cardData[index];
        for (let cindex = 0; cindex < item.infoList.length; cindex++) {
          const citem = item.infoList[cindex];
          citem.userAnswer = "";
          citem.isComplete = false;
        }
      }
      // for (let index = 0; index < this.questionList.length; index++) {
      //   const item = this.questionList[index];
      //   item.userAnswer = "";
      //   item.isComplete = false
      // }
    },
    openAnswers() {
      this.$data.loading = true;
      for (let index = 0; index < this.cardList.length; index++) {
        const item = this.cardList[index];
      for (let index = 0; index < this.cardData.length; index++) {
        const item = this.cardData[index];
        for (let cindex = 0; cindex < item.infoList.length; cindex++) {
          const citem = item.infoList[cindex];
          citem.isComplete = true;
@@ -453,299 +508,105 @@
    watchParsing(data) {
      data.isUnfold = !data.isUnfold;
    },
    // 获取试卷下题目类型
    getIdPathList() {
      this.$data.loading = true;
      let query = {
        path: "*",
        queryType: "*",
        productId: this.config.bookId,
        cmsPath: "24080\\63791\\63792\\63807",
        pading: {
          start: 0,
          size: 999,
        },
      };
      this.MG.store.getProductDetail(query).then(async (res) => {
        console.log(this.$props.productLinkPath, res.datas.cmsDatas[0].datas);
        const idPathList = res.datas.cmsDatas[0].datas;
        await this.getQuestionList(idPathList);
      });
    },
    // 获取题库题目
    async getQuestionList(idPathList, oldData) {
      let cardList = [];
      // 清空正确题数记录
      for (let index = 0; index < idPathList.length; index++) {
        const pathitem = idPathList[index];
        const pathList = cardList;
        pathList.push({
          path: pathitem.productLinkPath,
          catalogName: pathitem.name,
          infoList: [],
        });
        cardList = pathList;
        // 获取题目
        let query = {
          path: "*",
          queryType: "*",
          productId: this.config.bookId,
          cmsPath: pathitem.productLinkPath,
          itemFields: {
            Embedded_QuestionBank_Stem: [],
            Embedded_QuestionBank_AnalysisCon: [],
            Embedded_QuestionBank_Answer: [],
            Embedded_QuestionBank_Option: [],
            Embedded_QuestionBank_QuestionType: [],
            Embedded_QuestionBank_StemStyle: [],
            Embedded_QuestionBank_OptionStyle: [],
            Embedded_QuestionBank_KnowledgePoint: [],
            Embedded_QuestionBank_Difficulty: [],
          },
          pading: {
            start: 0,
            size: 999,
          },
        };
        await this.MG.store.getProductDetail(query).then((res) => {
          let oldList;
          if (oldData) {
            // 提交过,存在答题记录
            oldList = oldData.find(
              (item) => item.path == pathitem.productLinkPath
            ).infoList;
            this.setData({
              submitStatus: true,
            });
          }
          res.datas.cmsDatas[0].datas.forEach((item, index) => {
            let oldObj = "";
            if (oldList)
              oldObj = oldList.find((oldItem) => oldItem.id == item.id);
            let questionObj = {
              // num: index, // 题号
              id: item.id,
              type: pathitem.name,
              stem:
                item.Embedded_QuestionBank_QuestionType == "completion"
                  ? JSON.parse(item.Embedded_QuestionBank_Stem)
                      .stemTxt.replaceAll("<vacancy>", ",input,")
                      .split(",")
                  : JSON.parse(item.Embedded_QuestionBank_Stem), // 题干
              answer: item.Embedded_QuestionBank_Answer, // 答案
              option: item.Embedded_QuestionBank_Option
                ? JSON.parse(item.Embedded_QuestionBank_Option)
                : "", // 选择题选项
              analysisCon: item.Embedded_QuestionBank_AnalysisCon, // 解析
              questionType: item.Embedded_QuestionBank_QuestionType, // 题型
              optionStyle: item.Embedded_QuestionBank_OptionStyle, // 选项显示类型
              stemStyle: item.Embedded_QuestionBank_StemStyle, // 题干显示类型
              difficulty: item.Embedded_QuestionBank_Difficulty
                ? 4 - item.Embedded_QuestionBank_Difficulty
                : 0, // 难度等级
              userAnswer: oldObj
                ? oldObj.userAnswer
                : item.Embedded_QuestionBank_QuestionType == "completion" ||
                  item.Embedded_QuestionBank_QuestionType == "multipleChoice"
                ? []
                : "",
              isUserAnswer: oldObj
                ? this.isHaveAnswer(oldObj.userAnswer)
                : false,
              isRight: oldObj ? oldObj.isRight : null,
              isComplete: oldObj ? oldObj.isComplete : false,
              isCollect: false,
              isUnfold: "", // 控制解析的折叠面板是否展开
            };
            // 多选和填空答案肯为数组,要转换JSON格式
            if (
              questionObj.questionType == "completion" ||
              questionObj.questionType == "multipleChoice"
            ) {
              try {
                questionObj.answer = JSON.parse(questionObj.answer).toString();
              } catch (error) {
                questionObj.answer = item.Embedded_QuestionBank_Answer;
              }
            }
            // 填空题改造
            if (questionObj.questionType == "completion") {
              let index = 0;
              for (let i = 0; i < questionObj.stem.length; i++) {
                const item = questionObj.stem[i];
                if (item == "input") {
                  questionObj.stem[i] = {
                    num: index,
                    data: "input",
                  };
                  if (!oldObj) questionObj.userAnswer[index] = "";
                  index++;
                }
              }
            }
            // 题干富文本处理
            if (questionObj.stemStyle == "RichText") {
              // questionObj.option.txt = ''
              questionObj.stem.stemTxt = questionObj.stem.stemTxt
                .replace(
                  /\<img/gi,
                  '<img style="max-width: 300rpx !important;object-fit: contain;" class="stem-rich-img" '
                )
                .replace(/\<p/gi, '<p class="stem-rich-p"')
                .replace("../file", app.config.requestCtx + "/file");
            }
            // 选项富文本处理
            if (
              questionObj.optionStyle == "RichText" &&
              (questionObj.questionType == "singleChoice" ||
                questionObj.questionType == "judge" ||
                questionObj.questionType == "multipleChoice")
            ) {
              questionObj.option.forEach((item) => {
                if (item.txt)
                  item.txt = item.txt
                    .replace(/\<img/gi, '<img class="option-rich-img"')
                    .replace(/\<p/gi, '<p class="stem-rich-p"')
                    .replace("../file", app.config.requestCtx + "/file");
              });
            }
            // 解析富文本处理
            if (
              questionObj.analysisCon &&
              typeof questionObj.analysisCon == "string"
            ) {
              questionObj.analysisCon = questionObj.analysisCon.replace(
                /\<img/gi,
                '<img style="max-width: 300rpx !important;object-fit: contain;" class="stem-rich-img" '
              );
            }
            // 听力题修改
            // if (questionObj.questionType == 'singleChoice') {
            //   const src = this.extractSourceSrc(questionObj.stem.stemTxt)
            //   if (src) {
            //     questionObj.src = src
            //     questionObj.stem.stemTxt = this.removeVideoAndAudioTags(questionObj.stem.stemTxt)
            //   }
            // }
            // 获取图片
            if (
              questionObj.stemStyle == "Image" ||
              questionObj.stemStyle == "TxtAndImage"
            ) {
              questionObj.stem.stemImage = getPublicImage(
                questionObj.stem.stemImage,
                150
              );
            }
            if (
              questionObj.optionStyle == "Image" ||
              questionObj.optionStyle == "TxtAndImage"
            ) {
              questionObj.option.forEach((optionItem) => {
                if (optionItem.img)
                  optionItem.img = getPublicImage(optionItem.img, 150);
              });
            }
            // 客观题总分赋值
            if (questionObj.questionType !== "shortAnswer") {
              // this.setData({
              //   subjectiveGrade: this.data.subjectiveGrade + 2,
              // });
            }
            // 旧数据里 题目正确 记录正确数量
            if (questionObj.isRight) {
              // this.setData({
              //   correctNum: this.data.correctNum + 1,
              // });
            }
            if (
              pathitem.name == "判断题" ||
              pathitem.name == "填空题" ||
              pathitem.name == "多选题" ||
              pathitem.name == "单选题" ||
              pathitem.name == "听力题"
            ) {
              if (oldObj) {
                // this.setData({
                //   subjectiveTotal: this.data.subjectiveTotal + 1,
                // });
              }
            }
            // cardList赋值
            let cardIndex = cardList.findIndex(
              (item) => item.path == pathitem.productLinkPath
            );
            let infoList = cardList[cardIndex].infoList;
            infoList.push(questionObj);
            cardList[cardIndex].infoList = infoList;
            let questionList = [];
            const cardUpdatedList = cardList;
            cardUpdatedList.forEach((aitem) => {
              aitem.infoList.forEach((bitem, bindex) => {
                questionList.push(bitem);
                bitem.number = bindex + 1;
                bitem.score = 2;
              });
            });
            cardList = cardUpdatedList;
            this.$data.cardList = cardList;
            this.$data.loading = false;
            // console.log("题目呢", cardList);
            // return cardList
          });
        });
      }
    },
    // 批改题目  (练习,我的做题,我的收藏模式下)
    handleQuestion() {
      const list = this.$data.cardList
      const list = this.cardData;
      for (let index = 0; index < list.length; index++) {
        const item = list[index];
        for (let cindex = 0; cindex < item.infoList.length; cindex++) {
          const citem = item.infoList[cindex];
          citem.isComplete = true;
           // 修改题目状态为完成
           citem.isComplete = true;
            // 批改题目
            if (citem.questionType == "multipleChoice") {
              // 多选题
              if (citem.answer.length == citem.userAnswer.length) {
                const sortedArr1 = citem.answer.slice().sort();
                const sortedArr2 = citem.userAnswer.slice().sort();
                citem.isRight = sortedArr1.every(
                  (value, index) => value === sortedArr2[index]
                );
              } else {
          // 修改题目状态为完成
          citem.isComplete = true;
          // 批改题目
          if (citem.questionType == "multipleChoice") {
            // 多选题
            if (citem.answer.length == citem.userAnswer.length) {
              const sortedArr1 = citem.answer.slice().sort();
              const sortedArr2 = citem.userAnswer.slice().sort();
              citem.isRight = sortedArr1.every(
                (value, index) => value === sortedArr2[index]
              );
            } else {
              citem.isRight = false;
            }
          } else if (
            citem.questionType == "singleChoice" ||
            citem.questionType == "judge"
          ) {
            if (citem.id == "63825") console.log("i", item);
            citem.isRight = citem.answer == citem.userAnswer;
          } else if (citem.questionType == "shortAnswer") {
            // 简答 翻译
            citem.isRight = null;
          } else if (citem.questionType == "completion") {
            // 填空
            if (typeof citem.answer == "string") {
              citem.isRight = citem.answer == citem.userAnswer[0];
            } else {
              if (citem.answer.length != citem.userAnswer.length) {
                citem.isRight = false;
              }
            } else if (
              citem.questionType == "singleChoice" ||
              citem.questionType == "judge"
            ) {
              if(citem.id == '63825') console.log('i',item);
              citem.isRight = citem.answer == citem.userAnswer;
            } else if (citem.questionType == "shortAnswer") {
              // 简答 翻译
              citem.isRight = null;
            } else if (citem.questionType == "completion") {
              // 填空
              if (typeof citem.answer == "string") {
                citem.isRight = citem.answer == citem.userAnswer[0];
              } else {
                if (citem.answer.length != citem.userAnswer.length) {
                  citem.isRight = false;
                } else {
                  citem.isRight = citem.answer.every(
                    (value, index) =>
                      value === citem.userAnswer[index]
                  );
                }
                citem.isRight = citem.answer.every(
                  (value, index) => value === citem.userAnswer[index]
                );
              }
            }
          }
        }
      }
      this.$data.cardList = list
      this.cardData = list;
      console.log(this.cardData);
    },
    getParentWithClass(element, className) {
      while (element.parentElement) {
        element = element.parentElement;
        if (element.classList.contains(className)) {
          return element;
        }
      }
    },
    // 保存按钮
    saveAnswer() {
      const element = document.getElementsByClassName("examination")[0];
      if (element) {
        const pageNum = this.getParentWithClass(
          element,
          "page-box"
        ).getAttribute("page");
        console.log(this.config.activeBook.name, pageNum);
        const oldAnswerData = localStorage.getItem(
          this.config.activeBook.name + "oldAnswerData"
        );
        let oldData = oldAnswerData ? JSON.parse(oldAnswerData) : {};
        const asnwerList = [];
        for (let index = 0; index < this.cardData.length; index++) {
          const item = this.cardData[index];
          for (let cindex = 0; cindex < item.infoList.length; cindex++) {
            const citem = item.infoList[cindex];
            asnwerList.push({
              id: citem.id,
              userAnswer: citem.userAnswer,
            });
          }
        }
        if(oldData[pageNum]) {
          oldData[pageNum] = oldData[pageNum].filter(citem => asnwerList.findIndex(ditem => ditem.id == citem.id) == -1 )
          oldData[pageNum].push(asnwerList)
        } else {
          oldData[pageNum] = asnwerList
        }
        localStorage.setItem(this.config.activeBook.name + "oldAnswerData", JSON.stringify(oldData));
      }
      // if(oldData[this.chapter]) {
      //   oldData[this.chapter][this.page] = asnwerList
      // } else {
      //   oldData[this.chapter] = {}
      //   oldData[this.chapter][this.page] = asnwerList
      // }
      // console.log(oldData);
    },
  },
};
@@ -753,27 +614,38 @@
<style lang="less" scoped>
.catalogName {
  color: #00aeef;
  // color: #00aeef;
  // color: #ff6c00;
  text-indent: 1em !important;
  font-size: 16px;
}
.examination {
  margin-top: 20px;
  min-height: 100px;
  ul {
    list-style: none;
  }
}
.border-box {
  border: 2px solid #e9e9e9;
  border-top: 0;
.btn-box {
  height: 30px;
  width: 78px;
  padding: 4px 10px;
}
.border-box:first-child {
  border-top:2px solid #e9e9e9 ;
.border-box {
  padding-top: 20px;
  // border: 2px solid #e9e9e9;
}
.ti-2 {
  text-indent: 2em !important;
}
.questionContent {
  // display: flex;
  // justify-content: flex-start;
  margin-left: -36px;
  // justify-content: center;
  text-indent: 0em !important;
  margin-left: 20px;
  /deep/ .el-textarea__inner:focus {
    border-color: #15c0f2;
  }
}
.input {
  /deep/ .el-input__inner {
@@ -843,12 +715,14 @@
/** 题干 */
.questionTitle {
  display: flex;
  justify-content: space-between;
  justify-content: center;
  .titleContent {
    width: 95%;
    display: flex;
    align-items: flex-start;
    .questionNum {
      text-indent: 0 !important;
      margin-right: 5px;
      height: 32px;
      line-height: 32px;
      text-align: center;
@@ -858,7 +732,7 @@
    }
    .titleText {
      text-align: left;
      padding: 0 10px 10px 0px;
      // padding: 0 10px 10px 0px;
      min-width: 90%;
      font-size: 16px;
      line-height: 32px;
@@ -875,6 +749,9 @@
        width: 140px;
        height: 24px;
        border-top: 0;
      }
      /deep/ .el-textarea.is-disabled .el-textarea__inner {
        background-color: #fff;
      }
      /deep/.el-input__wrapper {
        border-top: 0 !important;
@@ -936,8 +813,8 @@
}
.analysis {
  margin: 20px 0;
  width:550px;
  margin-left: 48px;
  width: 94%;
  // margin-left: 12px;
}
.el-collapse {
  width: 100%;