闫增涛
2024-04-16 cf1fcff430fa1331c1d9b2b844fa9ad710fb7a26
packageBookService/pages/bookServices/examination/examination.js
@@ -47,7 +47,7 @@
    sliderValue: 0, // 字体滑块
    startTime: "", //进入页面当前时间
    pauseTime: 0, //暂停时间
    showDialog: false // 未提交退出拦截弹窗
  },
  /**
@@ -68,6 +68,9 @@
      idPathList: options.idPathList ? JSON.parse(options.idPathList) : [],
      answerType: options.answerType,
    });
    // wx.setNavigationBarTitle({
    //   title: options.answerTitle,
    // })
    if (this.data.answerType == "mock") {
      this.setData({
        uuid: options.uuid,
@@ -75,7 +78,6 @@
      });
    }
    this.init();
    console.log("传参", options);
  },
  /**
@@ -182,7 +184,6 @@
  // 监听watch
  watch(context, variableName, callback) {
    let value = context.data[variableName]; // 获取被监听属性的当前值
    // 使用 Object.defineProperty 方法在数据对象上定义属性的 getter 和 setter
    Object.defineProperty(context.data, variableName, {
      configurable: true, // 可配置
@@ -197,6 +198,35 @@
      },
    });
  },
  // 正则找出听力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({
@@ -211,10 +241,15 @@
      confirmColor: "#ff6c00",
      cancelColor: "#949494",
      complete: (res) => {
        if (res.cancel) {}
        if (res.cancel) {
          this.setData({
            showDialog: true
          })
        }
        if (res.confirm) {
          this.setData({
            submitStatus: true,
            showDialog: false
          });
          wx.navigateBack();
        }
@@ -461,6 +496,7 @@
  // 提交逻辑
  submitPaper() {
    this.setData({
      showDialog: false,
      submitStatus: true,
      loading: true,
    });
@@ -539,7 +575,6 @@
    });
    if (this.data.answerType == "option") {
      if (this.data)
        // this.startCountdown()
        this.setData({
          countdownTime: 2 * 60 * 60 * 1000,
        });
@@ -603,6 +638,9 @@
            oldMockData.state == "2"
          ) {
            this.startCountdown();
            this.setData({
              showDialog: true
            })
          }
          if (oldMockData.state == "3") {
            this.setData({
@@ -702,6 +740,9 @@
              await this.getQuestionList(value.dataList);
            } else {
              await this.getQuestionList(); // 获取题库题目
              this.setData({
                showDialog: true
              })
            }
          });
        }
@@ -766,9 +807,6 @@
        },
      };
      await app.MG.store.getProductDetail(query).then((res) => {
        if (!res.datas.cmsDatas[0].datas.length) return this.setData({
          noData: true
        })
        this.setData({
          total: res.datas.cmsDatas[0].datas.length,
        });
@@ -847,7 +885,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 (
@@ -860,7 +898,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');
            });
          }
          // 解析富文本处理
@@ -874,12 +912,12 @@
            );
          }
          // 听力题修改
          if (questionObj.questionType == 'singleChoiceArr') {
            questionObj.stem.stemTxt = questionObj.stem.stemTxt
              .replace(
                /\<audio/gi,
                '<audio @play="play" '
              )
          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 (
@@ -959,7 +997,14 @@
      });
    }
    // 有题目再开始倒计时
    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,
    });
@@ -1360,7 +1405,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 (
@@ -1373,7 +1418,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');
            });
          }
          // 解析富文本处理
@@ -1385,6 +1430,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 = "判断题";
@@ -1600,7 +1653,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 (
@@ -1613,7 +1666,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');
          });
        }
        // 解析富文本处理
@@ -1625,6 +1678,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 = "判断题";
@@ -1861,7 +1923,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 (
@@ -1875,8 +1937,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 (
@@ -2001,11 +2071,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));