questionDetail.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import Config from "../../model/config";
  2. import {formatTime, getDataSet, getEventParam, throttle, toast} from "../../utils/utils";
  3. import Api from "../../model/api";
  4. import {questionTypes, questionTypeTexts} from "../../model/enum";
  5. Page({
  6. data: {
  7. exams: [],
  8. answers: {},
  9. answerSize: 0,
  10. qType: questionTypeTexts,
  11. questionTypesEnum: questionTypes,
  12. id: '',
  13. teamId: '',
  14. courseId: '',
  15. eduStuId: '',
  16. scheduleId: '',
  17. answerBegin: ''
  18. },
  19. async onLoad(options) {
  20. const {id, teamId, courseId, eduStuId, scheduleId} = options;
  21. let res = await Api.getQuestion(id, true);
  22. let testQuestionsList = res.data.questionnaireList;
  23. let {singleChoice = [], multipleChoice = [], qaq = []} = testQuestionsList;
  24. [singleChoice, multipleChoice].forEach(questions => {
  25. questions.forEach(ques => {
  26. if (ques.otherAnswerType) {
  27. ques.questionnaireAnswerList.push({
  28. answer: '其他',
  29. answerType: 0,
  30. questionnaireId: ques.id,
  31. id: 'otherAnswer'
  32. });
  33. }
  34. });
  35. });
  36. let exams = [
  37. ...singleChoice,
  38. ...multipleChoice,
  39. ...qaq,
  40. ];
  41. exams.forEach(item => {
  42. if (item && item.testAnswerList) {
  43. item.testAnswerList.forEach((test, index) => {
  44. test.opt = Config.OPT_TIP[index];
  45. })
  46. }
  47. })
  48. this.setData({
  49. answerBegin: formatTime(),
  50. exams,
  51. id, teamId, courseId, eduStuId, scheduleId
  52. })
  53. },
  54. slectAnswer(e) {
  55. let question = getDataSet(e, "q");
  56. let opt = getDataSet(e, "opt");
  57. let id = opt.id;
  58. let qId = question.id;
  59. let obj = this.data.answers[qId] || {};
  60. if (question.questionType == questionTypes.SINGLE) {
  61. Object.keys(obj).forEach(key => {
  62. if (key != id) {
  63. obj[key] = false
  64. }
  65. })
  66. }
  67. obj[id] = !obj[id];
  68. this.saveAnswer(qId, obj);
  69. },
  70. saveAnswer(qId, obj) {
  71. let newArr = {
  72. ...this.data.answers,
  73. [qId]: obj
  74. };
  75. let answerSize = 0;
  76. this.data.exams.forEach(preQ => {
  77. let answer = newArr[preQ.id];
  78. let answer2 = newArr[preQ.id + "*"];
  79. if (this.checkAnswers(preQ, answer, answer2)) {
  80. answerSize++;
  81. }
  82. })
  83. this.setData({
  84. answers: {
  85. ...this.data.answers,
  86. [qId]: obj
  87. },
  88. answerSize
  89. })
  90. },
  91. onChange(e) {
  92. let qId = getDataSet(e, "id");
  93. let detail = getEventParam(e);
  94. this.saveAnswer(qId, detail);
  95. },
  96. checkAnswers(preQ, answer, answer2 = '') {
  97. let flag = false;
  98. if (answer && (preQ.questionType == questionTypes.SINGLE || preQ.questionType == questionTypes.MULTIPLE)) {
  99. if (answer.otherAnswer && !answer2.trim()) {
  100. //如果选择了其他答案 但是 没有输入内容
  101. flag = false;
  102. return flag;
  103. }
  104. flag = Object.keys(answer).some(key => {
  105. return answer[key];
  106. })
  107. } else {
  108. if (answer) {
  109. flag = !!answer.trim();
  110. } else {
  111. flag = answer2 ? !!answer2.trim() : false;
  112. }
  113. }
  114. return flag;
  115. },
  116. geneContent() {
  117. let arr = [];
  118. this.data.exams.forEach(question => {
  119. if (question.questionType == questionTypes.SINGLE || question.questionType == questionTypes.MULTIPLE) {
  120. let answer = this.data.answers[question.id];
  121. delete answer.otherAnswer; //删除其他答案 非文字部分
  122. let opts = [];
  123. if (answer) {
  124. opts = Object.keys(answer).filter(a => {
  125. return answer[a];
  126. })
  127. }
  128. let otherAnswer = {};
  129. if (this.data.answers[question.id + "*"]) {
  130. otherAnswer.otherAnswer = this.data.answers[question.id + "*"]
  131. }
  132. arr.push({
  133. id: question.id,
  134. answer: opts.join(","),
  135. ...otherAnswer
  136. })
  137. } else {
  138. arr.push({
  139. id: question.id,
  140. answer: this.data.answers[question.id]
  141. })
  142. }
  143. });
  144. return JSON.stringify(arr);
  145. },
  146. toPut:throttle(async function (e){
  147. let flag = true;
  148. this.data.exams.some(preQ => {
  149. let answer = this.data.answers[preQ.id];
  150. let answer2 = this.data.answers[preQ.id + "*"];
  151. if (!this.checkAnswers(preQ, answer, answer2)) {
  152. flag = false;
  153. }
  154. return !flag;
  155. })
  156. if (!flag) {
  157. toast("存在未答的题目,请将答案填写完整后重试");
  158. return;
  159. }
  160. let content = this.geneContent();
  161. // console.log('answer is : ', content);
  162. // return;
  163. await Api.putQuestion({
  164. teamId: this.data.teamId,
  165. courseId: this.data.courseId,
  166. studentId: this.data.eduStuId,
  167. scheduleId: this.data.scheduleId,
  168. questionnaireId: this.data.id,
  169. answerBegin: this.data.answerBegin,
  170. answers: content,
  171. }, true);
  172. const eventChannel = this.getOpenerEventChannel();
  173. eventChannel.emit('answer');
  174. wx.navigateBack();
  175. }),
  176. });