import {formatSeconds} from '../../../utils/util.js' const util = require("../../../utils/util") let app = getApp() Page({ data: { preList:["A","B","C", "D", "E", "F"], userInfo: {}, modalShow: false, timeOutShow: false, remainTime: 0, remainTimeStr: '', index:0, score:0, useTime:0, groupId:0, item: {index:0}, cacheKey: "", ansewersMap:{ }, info: {}, list: [], }, onLoad: function(options) { let groupId = +options.id||1 let cacheKey = "@mockExam_"+groupId; app.checkLogin( userInfo =>{ this.setData({userInfo, cacheKey}) this.loadData( groupId ) }) }, loadData( groupId ){ let info = wx.getStorageSync(this.data.cacheKey)||{}; if( info && info.groupId == groupId){ let remainTime = info.duration + info.startTime - parseInt(Date.now()/1000) if( remainTime >0){ let index = info.index||0; this.setData({info}) this.loadQuestion( index ) this.timeReduce() return; } } app.formPost('Exam.StartMockExamV1', {groupId}).then(res => { if (res.code ==200) { this.setData({info:res.data }) this.loadQuestion( 0 ) this.timeReduce() } }) }, selectquestion( e ){ let index = e.currentTarget.dataset.index this.loadQuestion( index ) }, checkAnswer( goNext ){ let item = this.data.item; let examId = this.data.info.examId; let param = {id: item.id, examId} let ansewersMap = this.data.ansewersMap let index = item.index||0; if( !item.select ){ return; } if( item.type == 4){ let answers = {}; for( let i in item.child){ let select = item.child[i].select||0; if( item.child[i].type == 3 && !Number(select)) { console.log("select", select) select = +select.join("") }; answers[item.child[i].answerId] = select } param.answers = answers let prevSubMit = ansewersMap[index]; let answerStr = JSON.stringify(answers) if( prevSubMit == answerStr ) { console.log("submit exit", index, prevSubMit) return; } ansewersMap[index] = answerStr }else{ let select = item.select||0; if( item.type == 3 && !Number(select)) select = +select.join(""); let prevSubMit = ansewersMap[index]; if( prevSubMit == select ) { console.log("submit exit", index, prevSubMit) return; } ansewersMap[index] = select param.select = select } this.setData({ansewersMap}); app.formPost('Exam.SubmitMockAnswerV1', param).then(res => { goNext && this.nextAnswer( ) }) }, radioChange( e ){ let cid=e.currentTarget.dataset.cid; let item = this.data.item item.select = +e.detail.value; this.setData({item}) if( !item.child ){ this.checkAnswer( true ) }else{ item.child[cid].select = item.select; this.setData({item}) } }, loadQuestion( index ) { this.checkAnswer( false ) let item = this.data.info.answers[index]; if( !item ) return; item.index = index; if( !item.title ){ app.formPost("Exam.loadMockAnswerV1", {id:item.id} ).then(res => { if( res.code != 200) return; Object.assign( item, res.data) this.saveItem( item ) }) }else{ this.saveItem( item ) } wx.pageScrollTo({ scrollTop: 0 }) }, saveItem( item ){ let info = this.data.info; info.index = item.index; info.answers[ item.index] = item; wx.setStorageSync(this.data.cacheKey, info) this.setData({item, info}) }, checkboxChange( e ){ let cid=e.currentTarget.dataset.cid; let item = this.data.item item.select = parseInt(e.detail.value.sort().join('')) if( item.child ){ item.child[cid].select = item.select } console.log("item", cid, item.select ) this.setData({item}) }, nextAnswer( ){ let nextId = (this.data.item.index||0)+1 if( nextId >= this.data.info.answers.length){ util.showModel("错误提示","已经最后一题" ) return } this.loadQuestion( nextId ) }, returnRecord(){ wx.navigateBack({ delta: 1, }) }, dosubmit(e){ this.checkAnswer( false ); let examId = this.data.info.examId; app.formPost("Exam.FinishMockExam", {examId}).then(res => { if (res.code == 200) { let { score, useTime } = res.data; this.setData({score, useTime, modalShow:true}) wx.setStorageSync(this.data.cacheKey, {}) if( this.data.timer) clearInterval(this.data.timer) } }); }, timeOut() { clearInterval(this.data.timer) this.setData({ timeOutShow: true }); }, timeReduce() { let _this = this let remainTime = this.data.info.duration + this.data.info.startTime - parseInt(Date.now()/1000) console.log("remainTime", remainTime); this.setData({remainTime}) let timer = setInterval(function() { let remainTime = _this.data.remainTime if (remainTime <= 0) { _this.timeOut() } else { _this.setData({ remainTime: remainTime - 1, remainTimeStr: formatSeconds(remainTime), doTime: _this.data.doTime + 1 }); } }, 1000) _this.setData({ timer: timer }); } })