let app = getApp() const util = require("../../../utils/util") Page({ data: { preList:["A","B","C", "D", "E", "F"], info:{}, total:0, unfinish:0, answerId:0, index:0, type:1, answerMap:{}, showResult: false, types:{ 1:'判断题', 2:'单选题', 3:'多选题', 4:'案例题' }, groupId:0 }, onLoad: function(options) { let groupId = +options.groupId||1; let type = +options.type||1; let total = +options.count ||0; this.setData({ groupId, type, total }); }, onChange(event){ const showResult = event.detail.value; let {info} = this.data; if( showResult ) { info.select = info.result; }else{ info.select = 0 } this.setData({showResult, info}) }, onShow: function(){ app.checkLogin( userInfo =>{ this.setData({userInfo, from:0}) this.loadAnswer() }) }, loadAnswer( ){ let {groupId, type, showResult} = this.data app.formPost('Exam.LoadAnswerNew', {groupId, type}).then(res => { if (res.code ==200) { let {unfinish, info, total} = res.data; let index = total- unfinish; if(showResult) info.select = info.result; this.setData({unfinish, total, info, index}) } }) }, checkAnswer( e ){ let {showResult, info, answerMap, groupId} = this.data; let answerId= info.answerId||0; if( showResult ) return; if(!answerId) return; let param = {answerId, groupId} // 多选 if( info.type == 3 ){ info.correct = info.select == info.result; }else{ info.correct = info.select == info.result; } param.correct = info.correct?1:0; let result = answerMap[answerId]; if( result === param.correct) return; answerMap[answerId] = param.correct; // 打开下一题 app.formPost('Exam.EditErrorAnswer', param).then(res => { this.setData({info, next:true, answerMap}) }) }, radioChange( e ){ let info = this.data.info info.select = +e.detail.value this.setData( {info} ); }, checkboxChange( e ){ let info = this.data.info info.select = +e.detail.value.sort().join("") let result = ""+info.result if (info.select == result ){ info._select = true }else{ for( let i in e.detail.value){ if(result.indexOf( e.detail.value[i] )==-1 ){ info._select = true } } } this.setData( {info} ); }, restartAnswer(){ let {groupId, type, showResult} = this.data app.formPost('Exam.resetAnswerNew', {groupId, type}).then(res => { if (res.code ==200) { let {total, unfinish,info} = res.data let index = total - unfinish; if(showResult) info.select = info.result; this.setData({info, index, total, unfinish}) } }) }, finishAnswer( e ){ this.checkAnswer() let action = e.currentTarget.dataset.action; let {groupId, unfinish, type, showResult, total, index} = this.data let correct = this.data.info.select == this.data.info.result?1:0; if( action == "prev"){ if( total <= unfinish ) { util.showMsg("已经在第一题") return } unfinish+=1 }else{ if( unfinish < 1 ) { util.showMsg("已经最后一题") return; } unfinish-=1 } app.formPost('Exam.finishAnswerNew', {groupId, index, type, correct, action}).then(res => { if (res.code ==200) { let info = res.data let index = total-unfinish; if( showResult) info.select = info.result; this.setData({info, unfinish, index}) } }) } })