123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- 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(`${stuId}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(`${this.data.stuId}answer${this.data.examId}`)
- const eventChannel = this.getOpenerEventChannel()
- eventChannel.emit('answer');
- wx.navigateBack();
- }),
- 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(`${this.data.stuId}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);
- }
- });
|