123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- 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();
- }),
- });
|