209 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			209 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
// pages/lianxi/main.js
 | 
						|
const api = require("../../utils/request.js");
 | 
						|
var util = require('../../utils/util.js')
 | 
						|
Page({
 | 
						|
 | 
						|
  /**
 | 
						|
   * 页面的初始数据
 | 
						|
   */
 | 
						|
  data: {
 | 
						|
    ctms: [],
 | 
						|
    tm_index: 0,
 | 
						|
    isright:false,
 | 
						|
    answerP:false,
 | 
						|
    tmtotal:0
 | 
						|
  },
 | 
						|
  radioChange: function (e) {
 | 
						|
    var that = this
 | 
						|
    that.data.currentTm['user_answer'] = e.detail.value
 | 
						|
    that.data.ctms[that.data.tm_index] = that.data.currentTm
 | 
						|
    that.showAnswer()
 | 
						|
  },
 | 
						|
  checkboxChange: function (e) {
 | 
						|
    var that = this
 | 
						|
    that.data.currentTm['user_answer'] = e.detail.value
 | 
						|
    that.data.ctms[that.data.tm_index] = that.data.currentTm
 | 
						|
  },
 | 
						|
  /**
 | 
						|
   * 生命周期函数--监听页面加载
 | 
						|
   */
 | 
						|
  onLoad: function () {
 | 
						|
    var that = this
 | 
						|
    try {
 | 
						|
      var value = wx.getStorageSync('ctms')
 | 
						|
      if (value) {
 | 
						|
        that.data.ctms = value
 | 
						|
        that.setData({
 | 
						|
          tmtotal: value.length
 | 
						|
        })
 | 
						|
      }
 | 
						|
    } catch (e) { }
 | 
						|
    try {
 | 
						|
      const res = wx.getSystemInfoSync()
 | 
						|
      that.setData({
 | 
						|
        scrollHeight: res.windowHeight - 70
 | 
						|
      })
 | 
						|
    } catch (e) {
 | 
						|
    }
 | 
						|
    if (that.data.tmtotal== 0) {
 | 
						|
      wx.showModal({
 | 
						|
        title: '提示',
 | 
						|
        content: '无错题记录!',
 | 
						|
        confirmText:'返回',
 | 
						|
        cancelText:'前去练习',
 | 
						|
        success: function (res) {
 | 
						|
          if (res.confirm) {
 | 
						|
            wx.navigateBack({
 | 
						|
            })
 | 
						|
          }else{
 | 
						|
            wx.redirectTo({
 | 
						|
              url: '/pages/lianxi/index',
 | 
						|
            })
 | 
						|
          }
 | 
						|
        }
 | 
						|
      })
 | 
						|
    }else{
 | 
						|
      that.showTm(that.data.tm_index)
 | 
						|
    }
 | 
						|
    
 | 
						|
  },
 | 
						|
  /**
 | 
						|
   * 生命周期函数--监听页面初次渲染完成
 | 
						|
   */
 | 
						|
  onReady: function () {
 | 
						|
 | 
						|
  },
 | 
						|
 | 
						|
  /**
 | 
						|
   * 生命周期函数--监听页面显示
 | 
						|
   */
 | 
						|
  onShow: function () {
 | 
						|
 | 
						|
  },
 | 
						|
 | 
						|
  /**
 | 
						|
   * 生命周期函数--监听页面隐藏
 | 
						|
   */
 | 
						|
  onHide: function () {
 | 
						|
 | 
						|
  },
 | 
						|
 | 
						|
  /**
 | 
						|
   * 生命周期函数--监听页面卸载
 | 
						|
   */
 | 
						|
  onUnload: function () {
 | 
						|
    try {
 | 
						|
      wx.setStorageSync('ctms', this.data.ctms)
 | 
						|
    } catch (e) { }
 | 
						|
  },
 | 
						|
 | 
						|
  /**
 | 
						|
   * 页面相关事件处理函数--监听用户下拉动作
 | 
						|
   */
 | 
						|
  onPullDownRefresh: function () {
 | 
						|
 | 
						|
  },
 | 
						|
 | 
						|
  /**
 | 
						|
   * 页面上拉触底事件的处理函数
 | 
						|
   */
 | 
						|
  onReachBottom: function () {
 | 
						|
 | 
						|
  },
 | 
						|
 | 
						|
  /**
 | 
						|
   * 用户点击右上角分享
 | 
						|
   */
 | 
						|
  onShareAppMessage: function () {
 | 
						|
 | 
						|
  },
 | 
						|
 | 
						|
  showTm: function (index) {
 | 
						|
    var that = this
 | 
						|
    var currentTm = that.data.ctms[index]
 | 
						|
    that.setData({
 | 
						|
      'tm_index': index,
 | 
						|
      'tmtotal':that.data.ctms.length,
 | 
						|
      'currentTm': currentTm
 | 
						|
    })
 | 
						|
    that.showOptions()
 | 
						|
    if (currentTm.user_answer) {
 | 
						|
      that.showAnswer()
 | 
						|
    }
 | 
						|
  },
 | 
						|
  panTi: function () {
 | 
						|
    var that = this
 | 
						|
    let currentTm = that.data.currentTm
 | 
						|
    let isright = false
 | 
						|
    if (currentTm.type == '多选') {
 | 
						|
      if (currentTm.user_answer) {
 | 
						|
        if (currentTm.user_answer.sort().toString() == currentTm.right.sort().toString()) {
 | 
						|
          isright = true
 | 
						|
        }
 | 
						|
      }
 | 
						|
 | 
						|
    } else {
 | 
						|
      isright = currentTm.right == currentTm.user_answer
 | 
						|
    }
 | 
						|
    // if (isright == false && currentTm.user_answer != undefined) {
 | 
						|
    //   currentTm.dtime = util.formatTime(new Date())
 | 
						|
    //   that.data.ctms.unshift(currentTm)
 | 
						|
    //   if (that.data.ctms.length > 40) {
 | 
						|
    //     that.data.ctms.length = 40
 | 
						|
    //   }
 | 
						|
    // }
 | 
						|
    return isright
 | 
						|
  },
 | 
						|
  showAnswer: function () {
 | 
						|
    let isright = this.panTi()
 | 
						|
    this.setData({
 | 
						|
      isright: isright,
 | 
						|
      answerP: true,
 | 
						|
      currentTm: this.data.currentTm
 | 
						|
    })
 | 
						|
  },
 | 
						|
  next: function () {
 | 
						|
    var that = this
 | 
						|
    var tm_index = that.data.tm_index + 1
 | 
						|
    that.showTm(tm_index)
 | 
						|
  },
 | 
						|
  previous: function () {
 | 
						|
    var that = this
 | 
						|
    var tm_index = that.data.tm_index - 1
 | 
						|
    that.showTm(tm_index)
 | 
						|
  },
 | 
						|
  showOptions: function () {
 | 
						|
    let currentTm = this.data.currentTm
 | 
						|
    let options = []
 | 
						|
    for (let key in currentTm.options) {
 | 
						|
      let option = {}
 | 
						|
      option.key = key
 | 
						|
      option.value = key + ':' + currentTm.options[key]
 | 
						|
      if (currentTm.user_answer) {
 | 
						|
        if (key == currentTm.user_answer || currentTm.user_answer.indexOf(key) != -1) {
 | 
						|
          option.checked = true
 | 
						|
        }
 | 
						|
      } else {
 | 
						|
        option.checked = false
 | 
						|
      }
 | 
						|
      options.push(option)
 | 
						|
    }
 | 
						|
    this.setData({
 | 
						|
      options: options
 | 
						|
    })
 | 
						|
  },
 | 
						|
  remove: function () {
 | 
						|
    var that = this
 | 
						|
    var index = that.data.tm_index
 | 
						|
    that.data.ctms.splice(index,1)
 | 
						|
    if(that.data.ctms.length>index){
 | 
						|
      that.showTm(index)
 | 
						|
    } else if (that.data.ctms.length > 0 && index > 1){
 | 
						|
      that.showTm(index-1)
 | 
						|
    }
 | 
						|
    else{
 | 
						|
      wx.navigateBack({
 | 
						|
      })
 | 
						|
    }
 | 
						|
  }
 | 
						|
}) |