litian
2024-09-12 bbba5d26e9e26c910ed337dcb65f462752ee6dce
packageBookService/pages/bookServices/examination/examination.js
@@ -1,6 +1,9 @@
import {
  getPublicImage
} from "../../../../assets/js/middleGround/tool";
import {
  loginInfo
} from '../../../../assets/js/login';
const app = getApp();
Page({
  /**
@@ -14,6 +17,8 @@
    countdownInterval: null, // 计时器
    isCountdownRunning: true, // 是否倒计时
    countdownTime: 0, // 倒计时时间
    storeInfo: '',
    jslx: '',
    bookId: "",
    productLinkPath: "",
    rootCmsItemId: "",
@@ -30,6 +35,7 @@
    total: 0, // 题目总数
    cardList: [], // 提交项,
    questionDataList: [], // 显示题目列表
    noData: false,
    mockid: 0,
    uuid: 0,
    mockData: {
@@ -46,6 +52,9 @@
    sliderValue: 0, // 字体滑块
    startTime: "", //进入页面当前时间
    pauseTime: 0, //暂停时间
    showDialog: false, // 未提交退出拦截弹窗
    showId: '',
    isShowDialog: false, // 测试报告弹窗是否显示
  },
  /**
@@ -65,15 +74,33 @@
      rootCmsItemId: options.rootCmsItemId,
      idPathList: options.idPathList ? JSON.parse(options.idPathList) : [],
      answerType: options.answerType,
      storeInfo: options.storeInfo,
      jslx: options.jslx,
    });
    wx.setNavigationBarTitle({
      title: options.answerTitle,
    })
    if (this.data.answerType == "mock") {
      this.setData({
        uuid: options.uuid,
        mockid: options.mockid,
      });
    }
    this.init();
    console.log("传参", options);
    const token = wx.getStorageSync(app.config.tokenKey)
    if (!token) {
      loginInfo(app, (data) => {
        if (data) {
          this.init();
        } else {
          this.init();
        }
      })
    } else {
      this.init()
    }
  },
  /**
@@ -176,11 +203,11 @@
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  onShareTimeline() {},
  // 监听watch
  watch(context, variableName, callback) {
    let value = context.data[variableName]; // 获取被监听属性的当前值
    // 使用 Object.defineProperty 方法在数据对象上定义属性的 getter 和 setter
    Object.defineProperty(context.data, variableName, {
      configurable: true, // 可配置
@@ -195,6 +222,48 @@
      },
    });
  },
  // 修改nav颜色
  changeNavBarColor(e) {
    if (type == 'night') {
      wx.setNavigationBarColor({
        backgroundColor: '#000000',
        frontColor: '#ffffff',
      })
    } else {
      wx.setNavigationBarColor({
        backgroundColor: '#ffffff',
        frontColor: '#000000',
      })
    }
  },
  // 正则找出听力src
  extractSourceSrc(htmlString) {
    // 正则表达式匹配<source>标签中的src属性值
    var srcRegex = /<source\s+src="([^"]+)"/i;
    var srcTwo = /<audio\s+src="([^"]+)"/i;
    // 执行正则匹配
    var match = srcRegex.exec(htmlString);
    const local = srcTwo.exec(htmlString)
    // 如果匹配成功,返回第一个捕获组的内容(src属性的值)
    if (match && match[1]) {
      return match[1].replace('../file', app.config.requestCtx + '/file');
    } else if (local && local[1]) {
      return local[1].replace('../file', app.config.requestCtx + '/file')
    } else {
      // 如果没有匹配到,返回null
      return null;
    }
  },
  // 拿到听力题除audio标签外其他内容
  removeVideoAndAudioTags(htmlString) {
    // 使用正则表达式匹配并移除所有的 <video> 和 <audio> 标签
    var cleanedHtml = htmlString.replace(/<video[^>]*>[\s\S]*?<\/video>|<audio[^>]*>[\s\S]*?<\/audio>/gi, '');
    return cleanedHtml;
  },
  // 改变loading状态
  changeLoadingState() {
    this.setData({
@@ -203,21 +272,28 @@
  },
  // 返回拦截
  beforeleave() {
    wx.showModal({
      title: "提示",
      content: "未提交,是否退出答题",
      confirmColor: "#ff6c00",
      cancelColor: "#949494",
      complete: (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          this.setData({
            submitStatus: true,
          });
          wx.navigateBack();
        }
      },
    });
    if ((this.data.answerType == 'option' || this.data.answerType == 'mock') && !this.data.submitStatus) {
      wx.showModal({
        title: "提示",
        content: "未提交,是否退出答题",
        confirmColor: "#ff6c00",
        cancelColor: "#949494",
        complete: (res) => {
          if (res.cancel) {
            this.setData({
              showDialog: true
            })
          }
          if (res.confirm) {
            this.setData({
              submitStatus: true,
              showDialog: false
            });
            wx.navigateBack();
          }
        },
      });
    }
  },
  onChangeSlider(e) {
    this.setData({
@@ -232,9 +308,21 @@
  },
  //设置背景色
  changeBGColor(e) {
    const flag = e.detail.value
    this.setData({
      isNight: e.detail.value,
      isNight: flag,
    });
    if (flag) {
      wx.setNavigationBarColor({
        backgroundColor: '#000000',
        frontColor: '#ffffff',
      })
    } else {
      wx.setNavigationBarColor({
        backgroundColor: '#ffffff',
        frontColor: '#000000',
      })
    }
  },
  // 获取保存的倒计时时间
  getSavedTime() {
@@ -292,11 +380,13 @@
  },
  // 切换题目
  changeSwiper(e) {
    this.setData({
      currentIndex: e.detail.index,
    });
    let index = e.detail.index - 1 >= 0 ? e.detail.index - 1 : 0;
    const item = this.data.questionDataList[index];
    const lastItem = this.data.questionDataList[index + 1];
    this.setData({
      currentIndex: e.detail.index,
      showId: lastItem.id
    });
    if (
      (this.data.answerType == "collectQuestion" ||
        this.data.answerType == "errorQuestion") &&
@@ -395,7 +485,6 @@
    const inputData = e.detail.value.detail.value;
    const id = e.detail.value.currentTarget.dataset.id;
    const index = Number(e.detail.value.currentTarget.dataset.index);
    console.log(index);
    const questionList = this.data.questionDataList;
    questionList.forEach((item) => {
      if (item.id == id) {
@@ -462,8 +551,8 @@
      submitStatus: true,
      loading: true,
    });
    const child = this.selectComponent("#question-options");
    debugger
    const child = this.selectComponent('#question-options');
    if (
      this.data.answerType == "option" ||
      this.data.answerType == "errorQuestion" ||
@@ -479,7 +568,11 @@
    if (this.data.answerType == "option") {
      this.toggleCountdown();
      this.recordAnswerData();
      child.openTestReportDialog();
      // this.selectComponent 拿不到 组件了, 改为监听组件传值 打开弹窗
      // child.openTestReportDialog();
      this.setData({
        isShowDialog: true
      })
    } else if (
      this.data.answerType == "collectQuestion" ||
      this.data.answerType == "errorQuestion"
@@ -522,10 +615,14 @@
          }),
        }, ],
      });
      child.openTestReportDialog();
      // child.openTestReportDialog();
      this.setData({
        isShowDialog: true
      })
    }
    this.setData({
      loading: false,
      showDialog: false,
    });
  },
  // 初始化函数
@@ -538,7 +635,6 @@
    });
    if (this.data.answerType == "option") {
      if (this.data)
        // this.startCountdown()
        this.setData({
          countdownTime: 2 * 60 * 60 * 1000,
        });
@@ -602,6 +698,9 @@
            oldMockData.state == "2"
          ) {
            this.startCountdown();
            this.setData({
              showDialog: true
            })
          }
          if (oldMockData.state == "3") {
            this.setData({
@@ -623,6 +722,7 @@
      subjectiveNum: 0,
      currentIndex: 0,
      submitStatus: false,
      showDialog: true
    });
    if (this.data.answerType == "option") {
      this.setData({
@@ -697,11 +797,13 @@
              this.setData({
                currentIndex: value.currentIndex,
              });
              console.log(this.data.submitStatus);
              // 携带答题记录 获取题目
              await this.getQuestionList(value.dataList);
            } else {
              await this.getQuestionList(); // 获取题库题目
              this.setData({
                showDialog: true
              })
            }
          });
        }
@@ -726,6 +828,7 @@
  async getQuestionList(oldData) {
    // 清空正确题数记录
    this.setData({
      noData: false,
      cardList: [],
      correctNum: 0,
    });
@@ -745,6 +848,7 @@
      let query = {
        path: "*",
        queryType: "*",
        productId: this.data.bookId,
        cmsPath: pathitem.productLinkPath,
        itemFields: {
@@ -764,6 +868,9 @@
          size: 999,
        },
      };
      if (this.data.storeInfo) {
        query.storeInfo = this.data.storeInfo
      }
      await app.MG.store.getProductDetail(query).then((res) => {
        this.setData({
          total: res.datas.cmsDatas[0].datas.length,
@@ -780,6 +887,19 @@
          });
        }
        res.datas.cmsDatas[0].datas.forEach((item, index) => {
          if (this.data.storeInfo || this.data.jslx) {
            if (item.Embedded_QuestionBank_QuestionType == 'singleChoice') {
              pathitem.name = "单选题"
            } else if (item.Embedded_QuestionBank_QuestionType == 'shortAnswer') {
              pathitem.name = "简答题"
            } else if (item.Embedded_QuestionBank_QuestionType == "completion") {
              pathitem.name = "填空题";
            } else if (item.Embedded_QuestionBank_QuestionType == "multipleChoice") {
              pathitem.name = "多选题";
            } else if (item.Embedded_QuestionBank_QuestionType == "judge") {
              pathitem.name = "判断题";
            }
          }
          let oldObj = "";
          if (oldList)
            oldObj = oldList.find((oldItem) => oldItem.id == item.id);
@@ -843,7 +963,7 @@
                /\<img/gi,
                '<img style="max-width: 300rpx !important;object-fit: contain;" class="stem-rich-img" '
              )
              .replace(/\<p/gi, '<p class="stem-rich-p"');
              .replace(/\<p/gi, '<p class="stem-rich-p"').replace('../file', app.config.requestCtx + '/file');
          }
          // 选项富文本处理
          if (
@@ -856,7 +976,7 @@
              if (item.txt)
                item.txt = item.txt
                .replace(/\<img/gi, '<img class="option-rich-img"')
                .replace(/\<p/gi, '<p class="stem-rich-p"');
                .replace(/\<p/gi, '<p class="stem-rich-p"').replace('../file', app.config.requestCtx + '/file');
            });
          }
          // 解析富文本处理
@@ -868,6 +988,14 @@
              /\<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 (
@@ -943,13 +1071,24 @@
            questionDataList: questionList,
            cardList: cardUpdatedList,
          });
          console.log(
            '题目', this.data.questionDataList
          );
        });
      });
    }
    // 有题目再开始倒计时
    if (this.data.questionDataList.length) this.startCountdown();
    if (this.data.questionDataList.length) {
      console.log(this.data.questionDataList);
      this.startCountdown();
    } else {
      this.setData({
        noData: true
      })
    }
    this.setData({
      loading: false,
      showId: this.data.questionDataList[0].id
    });
  },
  // 批改题目 (练习,我的错题,我的收藏,,组卷)
@@ -983,7 +1122,7 @@
      // 简答 翻译
      questionList[index].isRight = null;
    } else if (item.questionType == "completion") {
      if (item.answer == "string") {
      if (typeof item.answer == "string") {
        item.isRight = item.answer == item.userAnswer[0];
      } else {
        if (item.answer.length != item.userAnswer.length) {
@@ -1141,7 +1280,6 @@
        }
      }
    }
    this.setAnswerInfo(setInfoData);
  },
  // 提交答题数据
@@ -1196,6 +1334,7 @@
        } else {
          this.setData({
            loading: false,
            noData: true
          });
          wx.showModal({
            title: "提示",
@@ -1268,6 +1407,9 @@
          Embedded_QuestionBank_Difficulty: [],
        },
      };
      if (this.data.storeInfo) {
        query.storeInfo = this.data.storeInfo
      }
      await app.MG.store.getProductDetail(query).then((res) => {
        let questionArr = [];
        res.datas.cmsDatas[0].datas.forEach((item, index) => {
@@ -1347,7 +1489,7 @@
                /\<img/gi,
                '<img style="max-width: 300rpx !important;object-fit: contain;" class="stem-rich-img" '
              )
              .replace(/\<p/gi, '<p class="stem-rich-p"');
              .replace(/\<p/gi, '<p class="stem-rich-p"').replace('../file', app.config.requestCtx + '/file');
          }
          // 选项富文本处理
          if (
@@ -1360,7 +1502,7 @@
              if (item.txt)
                item.txt = item.txt
                .replace(/\<img/gi, '<img class="option-rich-img"')
                .replace(/\<p/gi, '<p class="stem-rich-p"');
                .replace(/\<p/gi, '<p class="stem-rich-p"').replace('../file', app.config.requestCtx + '/file');
            });
          }
          // 解析富文本处理
@@ -1372,6 +1514,14 @@
              /\<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 (item.Embedded_QuestionBank_QuestionType == "judge") {
            questionObj.type = "判断题";
@@ -1414,6 +1564,7 @@
    }
    this.setData({
      questionDataList: questionArr,
      showId: questionArr[0].id,
      cardList: cardList,
      loading: false,
    });
@@ -1435,7 +1586,8 @@
          this.getErrorDataList();
        } else {
          this.setData({
            loading: true,
            loading: false,
            noData: true
          });
          wx.showModal({
            title: "提示",
@@ -1505,6 +1657,9 @@
        Embedded_QuestionBank_Difficulty: [],
      },
    };
    if (this.data.storeInfo) {
      query.storeInfo = this.data.storeInfo
    }
    await app.MG.store.getProductDetail(query).then((res) => {
      let questionArr = [];
      res.datas.cmsDatas[0].datas.forEach((item, index) => {
@@ -1586,7 +1741,7 @@
              /\<img/gi,
              '<img style="max-width: 300rpx !important;object-fit: contain;" class="stem-rich-img" '
            )
            .replace(/\<p/gi, '<p class="stem-rich-p"');
            .replace(/\<p/gi, '<p class="stem-rich-p"').replace('../file', app.config.requestCtx + '/file');
        }
        // 选项富文本处理
        if (
@@ -1599,7 +1754,7 @@
            if (item.txt)
              item.txt = item.txt
              .replace(/\<img/gi, '<img class="option-rich-img"')
              .replace(/\<p/gi, '<p class="stem-rich-p"');
              .replace(/\<p/gi, '<p class="stem-rich-p"').replace('../file', app.config.requestCtx + '/file');
          });
        }
        // 解析富文本处理
@@ -1611,6 +1766,15 @@
            /\<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)
            console.log('题干', questionObj.stem.stemTxt);
          }
        }
        if (item.Embedded_QuestionBank_QuestionType == "judge") {
          questionObj.type = "判断题";
@@ -1652,11 +1816,11 @@
      }
      this.setData({
        questionDataList: questionArr,
        showId: questionArr[0].id,
        cardList: cardList,
        loading: false,
      });
    });
    // loadings.value = false
  },
  // 获取组卷结果
  async getEduQuizConfig() {
@@ -1781,6 +1945,9 @@
          Embedded_QuestionBank_Difficulty: [],
        },
      };
      if (this.data.storeInfo) {
        query.storeInfo = this.data.storeInfo
      }
      await app.MG.store.getProductDetail(query).then((res) => {
        res.datas.cmsDatas[0].datas.forEach((item, index) => {
          // 循环questionList,给每题赋值分数
@@ -1847,7 +2014,7 @@
            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 (
@@ -1861,8 +2028,16 @@
                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.questionType == 'singleChoice') {
            const src = this.extractSourceSrc(questionObj.stem.stemTxt)
            if (src) {
              questionObj.src = src
              questionObj.stem.stemTxt = this.removeVideoAndAudioTags(questionObj.stem.stemTxt)
            }
          }
          // 获取图片
          if (
@@ -1927,6 +2102,7 @@
    }
    this.setData({
      loading: false,
      showId: this.data.questionDataList[0].id
    });
  },
  // 获取组卷数据
@@ -1987,11 +2163,12 @@
      })
      .then((res) => {
        if (res[0]) {
          // 这里的 countdownTime 赋值:如果是时间到了提交,记录的剩余时间就是0,所以:如果是0的话赋值1秒,防止监听到时间为0再次提交
          this.setData({
            currentIndex: JSON.parse(res[0].value).currentIndex,
            "mockData.time": JSON.parse(res[0].value).time,
            countdownTime: JSON.parse(res[0].value).time ?
              JSON.parse(res[0].value).time : this.data.mockData.sumTime,
            countdownTime: JSON.parse(res[0].value).time > 0 ?
              JSON.parse(res[0].value).time : 1000,
            "mockData.answer": JSON.parse(res[0].value).answerData,
          });
          console.log(JSON.parse(res[0].value));