import {formatTime, getDataSet, getEventParam, throttle, toast} from "../../utils/utils"; import Api from "../../model/api"; import Config from "../../model/config"; import storage from "../../utils/storage"; import {questionTypes, questionTypeTexts} from "../../model/enum"; Page({ data: { id: '', examId: '', stuId: '', exams: [], answers: {}, current: 0, question: {}, qType: questionTypeTexts, questionTypesEnum: questionTypes, answerBegin: '' }, async onLoad(options) { const {id, examId, stuId} = options; //获取试卷详情 let res = await Api.getPaper(id, true); let testQuestionsList = res.data.testQuestionsList; let exams = [ ...(testQuestionsList.singleChoice || []), ...(testQuestionsList.multipleChoice || []), ...(testQuestionsList.qaq || []), ]; exams.forEach(item => { item.testAnswerList.forEach((test, index) => { test.opt = Config.OPT_TIP[index]; }) }) const answers = storage.getItem(`answer${examId}`) || '{}' this.setData({ exams, answers, question: exams[0], examId, id, stuId, answerBegin: formatTime() }); }, toNext(e) { //检查当前题是否答了 let preQ = this.data.question; let answer = this.data.answers[preQ.id]; let flag = this.checkAnswers(preQ, answer) if (!flag) { toast("请答题") return; } let current = this.data.current + 1; let question = this.data.exams[current]; this.setData({ current, question }) }, toPut:throttle(async function (e) { let flag = true; this.data.exams.forEach(preQ => { let answer = this.data.answers[preQ.id]; if (!this.checkAnswers(preQ, answer)) { flag = false; } }) if (!flag) { toast("存在未答的题目") return; } let content = this.geneContent(); await Api.putPaper({ examId: this.data.examId, paperId: this.data.id, answerBegin: this.data.answerBegin, answers: content, studentId: this.data.stuId }, true); storage.clearItem(`answer${this.data.examId}`) // const eventChannel = this.getOpenerEventChannel() // eventChannel.emit('answer'); wx.navigateBack({ complete: () => { const eventChannel = this.getOpenerEventChannel() eventChannel.emit('answer'); } }); }), toPre(e) { let current = this.data.current - 1; let question = this.data.exams[current]; this.setData({ current, question }) }, slectAnswer(e) { let id = getDataSet(e, "id") + ""; let qId = this.data.question.id; // this.data.answers[qId] = id; let obj = this.data.answers[qId] || {}; if (this.data.question.questionType == questionTypes.SINGLE) { Object.keys(obj).forEach(key => { if (key != id) { obj[key] = false } }) } obj[id] = !obj[id]; this.saveAnswer(qId, obj); }, onChange(e) { let detail = getEventParam(e); let qId = this.data.question.id; this.saveAnswer(qId, detail); }, saveAnswer(qId, obj) { this.setData({ answers: { ...this.data.answers, [qId]: obj } }) storage.setItem(`answer${this.data.examId}`, { ...this.data.answers, [qId]: obj }) }, checkAnswers(preQ, answer) { let flag = false; if (answer && (preQ.questionType == questionTypes.SINGLE || preQ.questionType == questionTypes.MULTIPLE)) { flag = Object.keys(answer).some(key => { return answer[key]; }) } else { flag = answer ? !!answer.trim() : false; } return flag; }, geneContent() { let arr = []; this.data.exams.forEach(question => { if (question.questionType == questionTypes.SINGLE || question.questionType == questionTypes.MULTIPLE) { let answer = this.data.answers[question.id]; let opts = Object.keys(answer).filter(a => { return answer[a]; }) arr.push({ id: question.id, answer: opts.join(",") }) } else { arr.push({ id: question.id, answer: this.data.answers[question.id] }) } }); return JSON.stringify(arr); } });