闫增涛
2024-03-15 cd98c69ff835643f518337ee9ab532885c647efb
pages/bookServices/examination/questionSchedule/index.js
@@ -5,45 +5,87 @@
   * 组件的属性列表
   */
  properties: {
    questionList: {
      type: Array,
      value: []
    },
    submitStatus: {
      type: Boolean,
      value: false
    },
    currentIndex: {
      type: Number,
      value: 1
    },
    countdownTime: {
      type: Number,
      value: 0
    },
    answerType: {
      type: String,
      value: ''
    }
  },
  created() {
    // if (props.answerType == 'option') {
    // this.startCountdown()
    this.setData({
      countdownTime: 2 * 60 * 60 * 1000
    })
    // }
  },
  detached() {
    if (this.data.countdownInterval !== null) {
      clearInterval(this.data.countdownInterval)
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    countdownInterval: null,   // 计时器
    isCountdownRunning: true, // 是否倒计时
    countdownTime: "",  // 时间
    showTime: '',
    showTime: '', //
    percentage: 0,
    ready: 0
  },
  observers: {
    'countdownTime': function (newValue, oldValue) {
      const showTime = this.formatTime(this.data.countdownTime)
    'countdownTime': function (newValue) {
      const showTime = this.formatTime(this.properties.countdownTime)
      this.setData({
        showTime: showTime
      })
      console.log(this.data.showTime);
    },
    'questionList': function (newValue) {
      this.setData({
        ready: 0
      })
      for (let index = 0; index < newValue.length; index++) {
        const item = newValue[index];
        if (this, this.isHaveAnswer(item.userAnswer)) {
          this.setData({
            ready: this.data.ready + 1
          })
        }
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 格式化时间
    // 判断是否有用户答案
    isHaveAnswer(data) {
      if (typeof data == 'string') {
        data = data
          .replace(/<[^>]*>/g, '')
          .replace(/&nbsp;/g, '')
          .trim()
        if (data.length) {
          return true
        } else {
          return false
        }
      } else {
        const answer = data.find((item) => item.length > 0)
        if (answer) {
          return true
        } else {
          return false
        }
      }
    },
    // // 格式化时间
    formatTime(ms) {
      const hours = Math.floor((ms / (1000 * 60 * 60)) % 24)
        .toString()
@@ -56,60 +98,60 @@
        .padStart(2, '0')
      return `${hours}:${minutes}:${seconds}`
    },
    // 获取保存的倒计时时间
    getSavedTime() {
      const savedTime = wx.getStorageSync('countdownTime')
      return savedTime ? parseInt(savedTime) : null
    },
    // 保存倒计时时间到本地存储
    saveTime() {
      wx.setStorageSync('countdownTime', this.data.countdownTime.toString())
    },
    clearTime() {
      this.setData({
        countdownTime: 2 * 60 * 60 * 1000
      })
    },
    // 暂停或继续倒计时
    toggleCountdown() {
      if (countdownInterval) {
        clearInterval(this.data.countdownInterval)
        this.setData({
          countdownInterval: null,
          isCountdownRunning: false
        })
      } else {
        this.startCountdown()
        this.setData({
          isCountdownRunning: true
        })
      }
    },
    // 开始倒计时
    startCountdown() {
      // 如果计时器已经存在,先清除之前的计时器
      if (this.data.countdownInterval) {
        clearInterval(this.data.countdownInterval)
        this.setData({
          countdownInterval: null
        })
      }
      this.setData({
        countdownInterval: setInterval(() => {
          this.setData({
            countdownTime: this.data.countdownTime - 1000
          })
          if (this.data.countdownTime <= 0) {
            clearInterval(this.data.countdownInterval)
            this.setData({
              countdownTime: 0,
              isCountdownRunning: false
            })
          }
          this.saveTime()
        }, 1000)
      })
    // // 获取保存的倒计时时间
    // getSavedTime() {
    //   const savedTime = wx.getStorageSync('countdownTime')
    //   return savedTime ? parseInt(savedTime) : null
    // },
    // // 保存倒计时时间到本地存储
    // saveTime() {
    //   wx.setStorageSync('countdownTime', this.data.countdownTime.toString())
    // },
    // clearTime() {
    //   this.setData({
    //     countdownTime: 2 * 60 * 60 * 1000
    //   })
    // },
    // // 暂停或继续倒计时
    // toggleCountdown() {
    //   if (countdownInterval) {
    //     clearInterval(this.data.countdownInterval)
    //     this.setData({
    //       countdownInterval: null,
    //       isCountdownRunning: false
    //     })
    //   } else {
    //     this.startCountdown()
    //     this.setData({
    //       isCountdownRunning: true
    //     })
    //   }
    // },
    // // 开始倒计时
    // startCountdown() {
    //   // 如果计时器已经存在,先清除之前的计时器
    //   if (this.data.countdownInterval) {
    //     clearInterval(this.data.countdownInterval)
    //     this.setData({
    //       countdownInterval: null
    //     })
    //   }
    //   this.setData({
    //     countdownInterval: setInterval(() => {
    //       this.setData({
    //         countdownTime: this.data.countdownTime - 1000
    //       })
    //       if (this.data.countdownTime <= 0) {
    //         clearInterval(this.data.countdownInterval)
    //         this.setData({
    //           countdownTime: 0,
    //           isCountdownRunning: false
    //         })
    //       }
    //       this.saveTime()
    //     }, 1000)
    //   })
    }
    // }
  }
})