// pages/lianxi/main.js const api = require("../../utils/request.js"); var util = require('../../utils/util.js') Page({ /** * 页面的初始数据 */ data: { tms:[], tm_index: 0, ctms:[], }, radioChange: function (e) { var that = this that.data.tm_current['user_answer'] = e.detail.value that.data.tms[that.data.tm_index] = that.data.tm_current }, checkboxChange: function (e) { var that = this that.data.tm_current['user_answer'] = e.detail.value that.data.tms[that.data.tm_index] = that.data.tm_current }, /** * 生命周期函数--监听页面加载 */ onLoad: function () { var that = this try { var value = wx.getStorageSync('examtest') console.log(value) if (value) { that.data.examtest = value that.data.tms = value.questions that.setData({ tm_total:value.questions.length }) that.showTm(that.data.tm_index) } } catch (e) { wx.navigateBack({}) } try { const res = wx.getSystemInfoSync() that.setData({ scrollHeight: res.windowHeight - 70 }) } catch (e) { } try { var value = wx.getStorageSync('ctms') if (value) { that.data.ctms = value } } catch (e) { } let mil = that.data.examtest.limit * 60 * 1000 let starttimes = (new Date()).getTime() //时间戳 let endtimes = starttimes + mil that.data.starttimes = starttimes that.data.endtimes = endtimes getApp().globalData.timer = setInterval(function () { that.startPass() }, 1000) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { wx.hideHomeButton({}) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { clearInterval(getApp().globalData.timer) try { wx.setStorageSync('ctms', this.data.ctms) } catch (e) { } }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, startPass: function () { var mil = this.data.endtimes - (new Date()).getTime() if (mil < 2000) { clearInterval(getApp().globalData.timer) this.handTest() }; var temp = util.formatmil(mil); this.setData({ countdown: temp, }); }, showTm: function (index) { var that = this var tm_current = that.data.tms[index] that.setData({ 'tm_index': index, 'tm_current': tm_current }) that.showOptions() }, panTi: function (tm_current) { // 返回当前题目是否正确,得分多少 let is_right = false, score = 0 if (tm_current.type == '多选') { if (tm_current.user_answer) { if (tm_current.user_answer.sort().toString() == tm_current.right.sort().toString()) { is_right = true score = tm_current.total_score }else{ for(var i=0;i 40) { that.data.ctms.length = 40 } } that.data.tms[i].score = tm_result.score score = score + tm_result.score } that.data.examtest.score = score if (score >= that.data.examtest.pass_score){ that.data.examtest.is_pass = true }else{ that.data.examtest.is_pass = false } that.data.examtest.questions = that.data.tms that.data.examtest.start_time = util.formatTime(new Date(that.data.starttimes)) that.data.examtest.end_time = util.formatTime(new Date()) that.data.examtest.took = Math.floor(((new Date()).getTime() - this.data.starttimes) / 1000) api.request('/examtest/examtest/', 'POST', that.data.examtest).then(res => { getApp().globalData.testData = res.data try { wx.removeStorageSync('examtest') } catch (e) { } wx.redirectTo({ url: 'result', }) }) } })