examDetail.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {formatTime, getDataSet, getEventParam, throttle, toast} from "../../utils/utils";
  2. import Api from "../../model/api";
  3. import Config from "../../model/config";
  4. import storage from "../../utils/storage";
  5. import {questionTypes, questionTypeTexts} from "../../model/enum";
  6. Page({
  7. data: {
  8. id: '',
  9. examId: '',
  10. stuId: '',
  11. exams: [],
  12. answers: {},
  13. current: 0,
  14. question: {},
  15. qType: questionTypeTexts,
  16. questionTypesEnum: questionTypes,
  17. answerBegin: ''
  18. },
  19. async onLoad(options) {
  20. const {id, examId, stuId} = options;
  21. //获取试卷详情
  22. let res = await Api.getPaper(id, true);
  23. let testQuestionsList = res.data.testQuestionsList;
  24. let exams = [
  25. ...(testQuestionsList.singleChoice || []),
  26. ...(testQuestionsList.multipleChoice || []),
  27. ...(testQuestionsList.qaq || []),
  28. ];
  29. exams.forEach(item => {
  30. item.testAnswerList.forEach((test, index) => {
  31. test.opt = Config.OPT_TIP[index];
  32. })
  33. })
  34. const answers = storage.getItem(`${stuId}answer${examId}`) || '{}'
  35. this.setData({
  36. exams, answers,
  37. question: exams[0],
  38. examId,
  39. id,
  40. stuId,
  41. answerBegin: formatTime()
  42. });
  43. },
  44. toNext(e) {
  45. //检查当前题是否答了
  46. let preQ = this.data.question;
  47. let answer = this.data.answers[preQ.id];
  48. let flag = this.checkAnswers(preQ, answer)
  49. if (!flag) {
  50. toast("请答题")
  51. return;
  52. }
  53. let current = this.data.current + 1;
  54. let question = this.data.exams[current];
  55. this.setData({
  56. current, question
  57. })
  58. },
  59. toPut:throttle(async function (e) {
  60. let flag = true;
  61. this.data.exams.forEach(preQ => {
  62. let answer = this.data.answers[preQ.id];
  63. if (!this.checkAnswers(preQ, answer)) {
  64. flag = false;
  65. }
  66. })
  67. if (!flag) {
  68. toast("存在未答的题目")
  69. return;
  70. }
  71. let content = this.geneContent();
  72. await Api.putPaper({
  73. examId: this.data.examId,
  74. paperId: this.data.id,
  75. answerBegin: this.data.answerBegin,
  76. answers: content,
  77. studentId: this.data.stuId
  78. }, true);
  79. storage.clearItem(`${this.data.stuId}answer${this.data.examId}`)
  80. const eventChannel = this.getOpenerEventChannel()
  81. eventChannel.emit('answer');
  82. wx.navigateBack();
  83. }),
  84. toPre(e) {
  85. let current = this.data.current - 1;
  86. let question = this.data.exams[current];
  87. this.setData({
  88. current, question
  89. })
  90. },
  91. slectAnswer(e) {
  92. let id = getDataSet(e, "id") + "";
  93. let qId = this.data.question.id;
  94. // this.data.answers[qId] = id;
  95. let obj = this.data.answers[qId] || {};
  96. if (this.data.question.questionType == questionTypes.SINGLE) {
  97. Object.keys(obj).forEach(key => {
  98. if (key != id) {
  99. obj[key] = false
  100. }
  101. })
  102. }
  103. obj[id] = !obj[id];
  104. this.saveAnswer(qId, obj);
  105. },
  106. onChange(e) {
  107. let detail = getEventParam(e);
  108. let qId = this.data.question.id;
  109. this.saveAnswer(qId, detail);
  110. },
  111. saveAnswer(qId, obj) {
  112. this.setData({
  113. answers: {
  114. ...this.data.answers,
  115. [qId]: obj
  116. }
  117. })
  118. storage.setItem(`${this.data.stuId}answer${this.data.examId}`, {
  119. ...this.data.answers,
  120. [qId]: obj
  121. })
  122. },
  123. checkAnswers(preQ, answer) {
  124. let flag = false;
  125. if (answer && (preQ.questionType == questionTypes.SINGLE || preQ.questionType == questionTypes.MULTIPLE)) {
  126. flag = Object.keys(answer).some(key => {
  127. return answer[key];
  128. })
  129. } else {
  130. flag = answer ? !!answer.trim() : false;
  131. }
  132. return flag;
  133. },
  134. geneContent() {
  135. let arr = [];
  136. this.data.exams.forEach(question => {
  137. if (question.questionType == questionTypes.SINGLE || question.questionType == questionTypes.MULTIPLE) {
  138. let answer = this.data.answers[question.id];
  139. let opts = Object.keys(answer).filter(a => {
  140. return answer[a];
  141. })
  142. arr.push({
  143. id: question.id,
  144. answer: opts.join(",")
  145. })
  146. } else {
  147. arr.push({
  148. id: question.id,
  149. answer: this.data.answers[question.id]
  150. })
  151. }
  152. });
  153. return JSON.stringify(arr);
  154. }
  155. });