// pages/lianxi/main.js const api = require("../../utils/request.js"); var util = require('../../utils/util.js') Page({ /** * 页面的初始数据 */ data: { questioncat:null, tms:[], ydtms:[], ctms:[], tmIndex: 0, answerP: false, isLoad:true, }, radioChange: function (e) { var that = this that.data.tm_current['user_answer'] = e.detail.value that.data.tms[that.data.tmIndex] = that.data.tm_current that.showAnswer() if (that.data.ydtms.indexOf(that.data.tm_current.id)==-1){ that.data.ydtms.push(that.data.tm_current.id) } }, checkboxChange: function (e) { var that = this that.data.tm_current['user_answer'] = e.detail.value that.data.tms[that.data.tmIndex] = that.data.tm_current if (that.data.ydtms.indexOf(that.data.tm_current.id) == -1) { that.data.ydtms.push(that.data.tm_current.id) } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var that = this that.data.questioncat = options.questioncat try { var value = wx.getStorageSync('cat' + that.data.questioncat.toString()) if (value) { that.data.ydtms = value } } catch (e) {} try { var value = wx.getStorageSync('ctms') if (value) { that.data.ctms = value } } catch (e) { } that.getTms() try{ const res = wx.getSystemInfoSync() that.setData({ scrollHeight:res.windowHeight-70 }) }catch(e){ } }, getTms: function(){ var that = this var query = { questioncat: that.data.questioncat, ydtms: that.data.ydtms } api.request('question/question/exercise/', 'POST', query).then(res => { if (res.code == 200) { if (res.data.results.length==0){ wx.showModal({ title: '提示', content: '无更多新题,返回重新开始', showCancel: false, success: function (res) { if (res.confirm) { wx.navigateBack({ }) } } }) }else{ that.data.tms = that.data.tms.concat(res.data.results) that.showTm(that.data.tmIndex) //展示题目和答案 if(that.data.isLoad){ that.setData({ tmtotal: res.data.total, }) that.data.isLoad=false } } } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { try { wx.setStorageSync('cat' + this.data.questioncat.toString(), this.data.ydtms) } catch (e) { } try { wx.setStorageSync('ctms', this.data.ctms) } catch (e) { } }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, showTm: function (index) { var that = this var tm_current = that.data.tms[index] that.setData({ 'tmIndex': index, 'tm_current': tm_current }) that.showOptions() if (tm_current.user_answer){ that.showAnswer() } }, panTi: function () { var that = this let tm_current = that.data.tm_current let isright = false if (tm_current.type == '多选') { if (tm_current.user_answer) { if (tm_current.user_answer.sort().toString() == tm_current.right.sort().toString()) { isright = true } } } else { isright = tm_current.right == tm_current.user_answer } if(isright == false && tm_current.user_answer != undefined){ tm_current.dtime = util.formatTime(new Date()) that.data.ctms.unshift(tm_current) 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, tm_current: this.data.tm_current }) }, next: function () { var that = this var tmIndex = that.data.tmIndex + 1 that.setData({ tmIndex: tmIndex, answerP: false }) if (tmIndex + 1 > that.data.tms.length) { that.getTms() } else { that.showTm(tmIndex) } }, previous: function () { var that = this var tmIndex = that.data.tmIndex - 1 that.setData({ answerP: false }) that.showTm(tmIndex) }, showOptions: function () { let tm_current = this.data.tm_current let options = [] for (let key in tm_current.options) { let option = {} option.key = key option.value = key + ':' + tm_current.options[key] if (tm_current.user_answer){ if (key == tm_current.user_answer || tm_current.user_answer.indexOf(key)!=-1){ option.checked = true } }else{ option.checked = false } options.push(option) } this.setData({ options:options }) }, })