1
闫增涛
2024-03-27 b2a6887c94cc7887b03ba1cce578e3fc59a62259
packageBookService/pages/bookServices/examination/examination.js
@@ -41,13 +41,23 @@
      answer: []
    },
    saveTime: 20,
    isNight: false
    isNight: false,
    sliderValue: 0, // 字体滑块
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // wx.enableAlertBeforeUnload({
    //   message: "未提交,是否退出答题?",
    //   success: function (res) {
    //     console.log('确定', res);
    //   },
    //   fail: function (err) {
    //     console.log("失败:", err);
    //   },
    // });
    const systInfo = wx.getSystemInfoSync();
    const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息
    const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 导航栏高度
@@ -75,13 +85,17 @@
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.answerType != 'mock') {
      if (wx.timer) {
        clearInterval(wx.timer)
      }
    }
    if (this.data.answerType == 'mock') {
      if (wx.timer) {
        clearInterval(wx.timer)
@@ -101,13 +115,18 @@
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if (wx.timer) {
      clearInterval(wx.timer)
    }
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  onUnload(e) {
    if (wx.timer) {
      clearInterval(wx.timer)
    }
    if (this.data.countdownInterval !== null) {
      clearInterval(this.data.countdownInterval)
    }
@@ -132,8 +151,34 @@
   */
  onShareAppMessage() {
  },
  // 监听watch
  watch(context, variableName, callback) {
    let value = context.data[variableName]; // 获取被监听属性的当前值
    // 使用 Object.defineProperty 方法在数据对象上定义属性的 getter 和 setter
    Object.defineProperty(context.data, variableName, {
      configurable: true, // 可配置
      enumerable: true, // 可枚举
      get: function () {
        return value; // 返回属性的当前值
      },
      set: function (newVal) {
        const oldVal = value; // 记录属性的旧值
        value = newVal; // 更新属性的值
        callback.call(context, newVal, oldVal); // 调用回调函数,传递新值和旧值
      }
    });
  },
  onChangeSlider(e) {
    this.setData({
      sliderValue: e.detail.value
    })
    console.log(e.detail.value);
  },
  // 返回
  goBack() {
    console.log(this.data.submitStatus);
    wx.navigateBack();
  },
  //设置背景色
@@ -202,9 +247,11 @@
      currentIndex: e.detail.index
    })
    let index = e.detail.index - 1 >= 0 ? e.detail.index - 1 : 0
    let flag = this.isHaveAnswer(this.data.questionDataList[index].userAnswer)
    if (flag) this.handleQuestion(e.detail.index)
    const item = this.data.questionDataList[index]
    if ((this.data.answerType == 'collectQuestion' || this.data.answerType == 'errorQuestion') && !item.isComplete) {
      let flag = this.isHaveAnswer(item.userAnswer)
      if (flag) this.handleQuestion(index)
    }
  },
  // 点击答题卡跳转题目
  goQuestion(e) {
@@ -223,14 +270,50 @@
    const id = e.detail.value.currentTarget.dataset.id
    const radioChecked = e.detail.value.detail.value
    const questionList = this.data.questionDataList
    questionList.forEach(item => {
      if (item.id == id) {
    // 我的错题和我的收藏模式下,单选题选择了直接批改
    for (let index = 0; index < questionList.length; index++) {
      const item = questionList[index];
      if (item.id == id && !item.isComplete) {
        item.userAnswer = radioChecked
        item.isUserAnswer = this.isHaveAnswer(radioChecked)
        if ((item.questionType == 'singleChoice' || item.questionType == 'judge') && (this.data.answerType == 'collectQuestion' || this.data.answerType == 'errorQuestion') && !item.isComplete) {
          debugger
          let index = this.data.currentIndex - 1 >= 0 ? this.data.currentIndex - 1 : 0
          this.handleQuestion(index)
        }
      }
    })
    }
    // questionList.forEach(item => {
    //   if (item.id == id && !item.isComplete) {
    //     item.userAnswer = radioChecked
    //     item.isUserAnswer = this.isHaveAnswer(radioChecked)
    //     if ((item.questionType == 'singleChoice' || item.questionType == 'judge') && (this.data.answerType == 'collectQuestion' || this.data.answerType == 'errorQuestion') && !item.isComplete) {
    //       this.handleQuestion(this.data.currentIndex)
    //     }
    //   }
    // })
    const cardListUpdata = this.data.cardList
    for (let index = 0; index < cardListUpdata.length; index++) {
      const item = cardListUpdata[index];
      for (let cindex = 0; cindex < item.infoList.length; cindex++) {
        const citem = item.infoList[cindex];
        if (citem.id == id && !citem.isComplete) {
          citem.userAnswer = radioChecked
          citem.isUserAnswer = this.isHaveAnswer(radioChecked)
        }
      }
    }
    // cardListUpdata.forEach(item => {
    //   item.infoList.forEach(citem => {
    //     if (citem.id == id && !citem.isComplete) {
    //       citem.userAnswer = radioChecked
    //       citem.isUserAnswer = this.isHaveAnswer(radioChecked)
    //     }
    //   })
    // })
    this.setData({
      questionDataList: questionList
      questionDataList: questionList,
      cardList: cardListUpdata
    })
    if (this.data.answerType == 'mock') {
      this.setData({
@@ -242,16 +325,27 @@
  onChangeInput(e) {
    const inputData = e.detail.value.detail.value
    const id = e.detail.value.currentTarget.dataset.id
    const index = e.detail.value.currentTarget.dataset.index
    const index = Number(e.detail.value.currentTarget.dataset.index)
    console.log(index);
    const questionList = this.data.questionDataList
    questionList.forEach(item => {
      if (item.id == id) {
        item.userAnswer[index] = inputData,
          item.isUserAnswer = this.isHaveAnswer(item.userAnswer)
        item.userAnswer[index] = inputData
        return item.isUserAnswer = this.isHaveAnswer(inputData)
      }
    })
    const cardListUpdata = this.data.cardList
    cardListUpdata.forEach(item => {
      item.infoList.forEach(citem => {
        if (citem.id == id) {
          citem.userAnswer[index] = inputData
          return citem.isUserAnswer = this.isHaveAnswer(inputData)
        }
      })
    })
    this.setData({
      questionDataList: questionList
      questionDataList: questionList,
      cardList: cardListUpdata,
    })
    if (this.data.answerType == 'mock') {
      this.setData({
@@ -282,19 +376,25 @@
      } else {
        return false
      }
    } else {
    } else if (typeof data == 'object') {
      const answer = data.find((item) => item.length > 0)
      if (answer) {
        return true
      } else {
        return false
      }
    } else {
      return false
    }
  },
  // 提交逻辑
  submitPaper() {
    // 关闭退出页面监听
    wx.disableAlertBeforeUnload()
    this.setData({
      submitStatus: true
      submitStatus: true,
      loading: true
    })
    const child = this.selectComponent('#question-options')
    if (this.data.answerType == 'option' || this.data.answerType == 'errorQuestion' || this.data.answerType == 'mock') {
@@ -302,7 +402,7 @@
      const qustionList = this.data.questionDataList
      for (let index = 0; index < qustionList.length; index++) {
        const item = qustionList[index];
        if (!item.isComplete) this.handleQuestion(index + 1)
        if (!item.isComplete) this.handleQuestion(index)
      }
    }
    if (this.data.answerType == 'option') {
@@ -352,7 +452,9 @@
      })
      child.openTestReportDialog()
    }
    this.setData({
      loading: false
    })
  },
  // 初始化函数
  async init() {
@@ -364,17 +466,23 @@
    })
    if (this.data.answerType == 'option') {
      if (this.data)
        this.startCountdown()
      this.setData({
        countdownTime: 2 * 60 * 60 * 1000
      })
        // this.startCountdown()
        this.setData({
          countdownTime: 2 * 60 * 60 * 1000
        })
      // 测试答题
      await this.getCollectIdList() // 获取收藏id列表
      await this.getErrorList()   // 获取错题id列表
    } else if (this.data.answerType == 'collectQuestion') {
      this.setData({
        submitStatus: true
      })
      // 我的收藏
      await this.getcollectId() // 获取收藏题目
    } else if (this.data.answerType == 'errorQuestion') {
      this.setData({
        submitStatus: true
      })
      // 我的错题
      // loadings.value = true
      await this.getErrorIdList()
@@ -404,18 +512,19 @@
            let oldQuestionList = []
            oldQuestionList = await this.getMockQuestionList()
            const userAnswerList = await this.getMockAnswer()
            await this.getMockDataList(oldQuestionList, userAnswerList)
            this.setData({
              'mockData.sumTime': oldMockData.time,
              cardList: oldQuestionList
            })
            await this.getMockDataList(oldQuestionList, userAnswerList)
          }
          if (oldMockData.state == '0' || oldMockData.state == '1' || oldMockData.state == '2') {
            this.startCountdown()
          }
          if (oldMockData.state == '3') {
            this.setData({
              submitStatus: true
              submitStatus: true,
              subjectiveNum: oldMockData.report.userScore
            })
          }
        }
@@ -493,6 +602,7 @@
              let value = JSON.parse(res[0].value)
              // 有答题记录,得分赋值
              if (value) {
                wx.disableAlertBeforeUnload()
                this.setData({
                  submitStatus: true
                })
@@ -506,6 +616,7 @@
              this.setData({
                currentIndex: value.currentIndex
              })
              console.log(this.data.submitStatus);
              // 携带答题记录 获取题目
              await this.getQuestionList(value.dataList)
            } else {
@@ -528,19 +639,19 @@
            errorList: JSON.parse(res[0].value)
          })
        } catch (error) {
        }
      })
  },
  // 获取题库题目
  getQuestionList(oldData) {
  async getQuestionList(oldData) {
    // 清空正确题数记录
    this.setData({
      cardList: [],
      correctNum: 0,
    })
    let flag = 0
    this.data.idPathList.forEach((pathitem) => {
    for (let index = 0; index < this.data.idPathList.length; index++) {
      const pathitem = this.data.idPathList[index];
      const pathList = this.data.cardList
      pathList.push({
        path: pathitem.productLinkPath,
@@ -573,7 +684,7 @@
          size: 999
        }
      }
      app.MG.store.getProductDetail(query).then((res) => {
      await app.MG.store.getProductDetail(query).then((res) => {
        this.setData({
          total: res.datas.cmsDatas[0].datas.length
        })
@@ -703,19 +814,21 @@
          })
          this.setData({
            questionDataList: questionList,
            cardList: cardUpdatedList
            cardList: cardUpdatedList,
          })
        })
      })
    })
    }
    // 有题目再开始倒计时
    if (this.data.questionDataList.length) this.startCountdown()
    this.setData({
      loading: false,
      loading: false
    })
  },
  // 批改题目 (练习,我的错题,我的收藏)
  // 批改题目 (练习,我的错题,我的收藏,,组卷)
  handleQuestion(num) {
    const questionList = this.data.questionDataList
    const index = num - 1 >= 0 ? num - 1 : 0
    const index = num
    if (questionList[index].isComplete) {
      // 题目已完成,跳过
      return true
@@ -1332,9 +1445,9 @@
  },
  // 获取组卷题目列表
  async getMockDataList(questionList, oldList) {
    console.log(questionList, oldList);
    const questionDataList = this.data.questionDataList
    questionList.forEach(async (pathitem, pathindex) => {
    for (let pathindex = 0; pathindex < questionList.length; pathindex++) {
      const pathitem = questionList[pathindex];
      let itemIds = []
      pathitem.infoList.forEach(item => {
        itemIds.push(item.id + '')
@@ -1393,7 +1506,7 @@
            isUserAnswer: oldObj ? this.isHaveAnswer(oldObj.userAnswer) : false,
            isRight: oldObj ? oldObj.isRight : null,
            // isComplete: oldObj ? oldObj.isComplete : false,
            isComplete: this.data.mockData.state == '3' ? true : oldObj ? this.isHaveAnswer(oldObj.answer) : false,
            isComplete: this.data.mockData.state == '3' ? true : false,
            isCollect: this.data.collectList.indexOf(item.id) > -1 ? true : false,
            isUnfold: '' // 控制解析的折叠面板是否展开
          }
@@ -1451,7 +1564,6 @@
          })
          let questionList = []
          const cardUpdatedList = this.data.cardList
          // if (flag == this.data.idPathList.length) {
          cardUpdatedList.forEach(aitem => {
            aitem.infoList.forEach((bitem, bindex) => {
              questionList.push(bitem)
@@ -1464,9 +1576,8 @@
          })
        })
      })
    })
    }
    this.setData({
      // questionDataList: questionDataList,
      loading: false
    })
  },