import Config from "../../model/config"; import {formatTime, getDataSet, getEventParam, throttle, 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: '', answerBegin: '' }, async onLoad(options) { const {id, teamId, courseId, eduStuId, scheduleId} = options; let res = await Api.getQuestion(id, true); let testQuestionsList = res.data.questionnaireList; let {singleChoice = [], multipleChoice = [], qaq = []} = testQuestionsList; [singleChoice, multipleChoice].forEach(questions => { questions.forEach(ques => { if (ques.otherAnswerType) { ques.questionnaireAnswerList.push({ answer: '其他', answerType: 0, questionnaireId: ques.id, id: 'otherAnswer' }); } }); }); let exams = [ ...singleChoice, ...multipleChoice, ...qaq, ]; exams.forEach(item => { if (item && item.testAnswerList) { item.testAnswerList.forEach((test, index) => { test.opt = Config.OPT_TIP[index]; }) } }) this.setData({ answerBegin: formatTime(), 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)) { if (answer.otherAnswer && !answer2.trim()) { //如果选择了其他答案 但是 没有输入内容 flag = false; return flag; } 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]; delete answer.otherAnswer; //删除其他答案 非文字部分 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); }, toPut:throttle(async function (e){ let flag = true; this.data.exams.some(preQ => { let answer = this.data.answers[preQ.id]; let answer2 = this.data.answers[preQ.id + "*"]; if (!this.checkAnswers(preQ, answer, answer2)) { flag = false; } return !flag; }) if (!flag) { toast("存在未答的题目,请将答案填写完整后重试"); return; } let content = this.geneContent(); // console.log('answer is : ', content); // return; await Api.putQuestion({ teamId: this.data.teamId, courseId: this.data.courseId, studentId: this.data.eduStuId, scheduleId: this.data.scheduleId, questionnaireId: this.data.id, answerBegin: this.data.answerBegin, answers: content, }, true); const eventChannel = this.getOpenerEventChannel(); eventChannel.emit('answer'); wx.navigateBack(); }), });