import Config from "../../model/config"; import {formatTime, getDataSet, getEventParam, toast} from "../../utils/utils"; import Api from "../../model/api"; import {questionTypes, questionTypeTexts} from "../../model/enum"; Page({ data: { exams: [], answers: {}, answerSize: 0, qType: questionTypeTexts, questionTypesEnum: questionTypes, id: '', teamId: '', courseId: '', eduStuId: '', scheduleId: '', }, async onLoad(options) { let id = options.id; let teamId = options.teamId; let courseId = options.courseId; let eduStuId = options.eduStuId; let scheduleId = options.scheduleId; let res = await Api.getQuestion(id, true); let testQuestionsList = res.data.questionnaireList; let exams = [ ...(testQuestionsList.singleChoice || []), ...(testQuestionsList.multipleChoice || []), ...(testQuestionsList.qaq || []), ]; exams.forEach(item => { if (item && item.testAnswerList) { item.testAnswerList.forEach((test, index) => { test.opt = Config.OPT_TIP[index]; }) } }) this.setData({ exams, id, teamId, courseId, eduStuId, scheduleId }) }, slectAnswer(e) { let question = getDataSet(e, "q"); let opt = getDataSet(e, "opt"); let id = opt.id; let qId = question.id; let obj = this.data.answers[qId] || {}; if (question.questionType == questionTypes.SINGLE) { Object.keys(obj).forEach(key => { if (key != id) { obj[key] = false } }) } obj[id] = !obj[id]; this.saveAnswer(qId, obj); }, saveAnswer(qId, obj) { let newArr = { ...this.data.answers, [qId]: obj }; let answerSize = 0; this.data.exams.forEach(preQ => { let answer = newArr[preQ.id]; let answer2 = newArr[preQ.id + "*"]; if (this.checkAnswers(preQ, answer, answer2)) { answerSize++; } }) this.setData({ answers: { ...this.data.answers, [qId]: obj }, answerSize }) }, onChange(e) { let qId = getDataSet(e, "id"); let detail = getEventParam(e); this.saveAnswer(qId, detail); }, checkAnswers(preQ, answer, answer2) { let flag = false; if (answer && (preQ.questionType == questionTypes.SINGLE || preQ.questionType == questionTypes.MULTIPLE)) { flag = Object.keys(answer).some(key => { return answer[key]; }) } else { if (answer) { flag = !!answer.trim(); } else { flag = answer2 ? !!answer2.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 = []; if (answer) { opts = Object.keys(answer).filter(a => { return answer[a]; }) } let otherAnswer = {}; if (this.data.answers[question.id + "*"]) { otherAnswer.otherAnswer = this.data.answers[question.id + "*"] } arr.push({ id: question.id, answer: opts.join(","), ...otherAnswer }) } else { arr.push({ id: question.id, answer: this.data.answers[question.id] }) } }); return JSON.stringify(arr); }, async toPut(e) { let flag = true; this.data.exams.forEach(preQ => { let answer = this.data.answers[preQ.id]; let answer2 = this.data.answers[preQ.id + "*"]; if (!this.checkAnswers(preQ, answer, answer2)) { flag = false; } }) if (!flag) { toast("存在未答的题目") return; } let content = this.geneContent(); await Api.putQuestion({ teamId: this.data.teamId, courseId: this.data.courseId, studentId: this.data.eduStuId, scheduleId: this.data.scheduleId, questionnaireId: this.data.id, answerBegin: formatTime(), answers: content, }, true); const eventChannel = this.getOpenerEventChannel() eventChannel.emit('answer'); wx.navigateBack(); }, });