answerAfterclass.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. const app = require('../../utils/util.js');
  2. const tools = require('../../utils/tools.js');
  3. Page({
  4. data: {
  5. index: 0, // 题目序列
  6. chooseValue: [], // 选择的答案序列
  7. questionList: [],
  8. totalScore: 0,
  9. id: '', //考试主键id(可取list接口中id)
  10. paperId: '', //试卷id(可取list接口中paperId)
  11. answersId: '', // 提交答案
  12. sessionKey: '',
  13. },
  14. /*
  15. * 单选事件
  16. */
  17. radioChange: function (e) {
  18. let json = {};
  19. json.currentAnswer = e.detail.value;
  20. json.id = e.currentTarget.dataset.idx;
  21. json.questType = e.currentTarget.dataset.questtypes;
  22. this.data.chooseValue[this.data.index] = json;
  23. const items = this.data.questionList[this.data.index].QuestChoice
  24. for (let i = 0, len = items.length; i < len; ++i) {
  25. items[i].checked = items[i].value === e.detail.value
  26. }
  27. this.setData({
  28. questionList: this.data.questionList
  29. })
  30. wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  31. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  32. wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);
  33. },
  34. /*
  35. * 多选事件
  36. */
  37. checkboxChange: function (e) {
  38. let json = {};
  39. let aa = e.detail.value.join("||")
  40. json.currentAnswer = aa;
  41. json.questType = e.currentTarget.dataset.questtypes;
  42. json.id = e.currentTarget.dataset.idx;
  43. this.data.chooseValue[this.data.index] = json;
  44. const items = this.data.questionList[this.data.index].QuestChoice
  45. const values = e.detail.value
  46. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  47. items[i].checked = false
  48. for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
  49. if (items[i].value === values[j]) {
  50. items[i].checked = true
  51. break
  52. }
  53. }
  54. }
  55. this.setData({
  56. questionList: this.data.questionList
  57. })
  58. wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  59. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  60. wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);
  61. },
  62. /*
  63. * 解答事件
  64. */
  65. bindTextAreaBlur(e) {
  66. let json = {};
  67. json.currentAnswer = e.detail.value;
  68. json.id = e.currentTarget.dataset.idx;
  69. json.questType = e.currentTarget.dataset.questtypes;
  70. this.data.chooseValue[this.data.index] = json;
  71. this.data.questionList[this.data.index].currentAnswer = e.detail.value;
  72. this.setData({
  73. questionList: this.data.questionList
  74. })
  75. wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  76. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  77. wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);
  78. },
  79. /*
  80. * 下一题/提交 按钮
  81. */
  82. nextSubmit: function () {
  83. // 判断是不是最后一题
  84. if (this.data.index < this.data.questionList.length - 1) {
  85. // 渲染下一题
  86. this.setData({
  87. index: this.data.index + 1
  88. })
  89. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  90. } else {
  91. if (wx.getStorageSync('chooseValue' + this.data.answersId)) {
  92. this.setData({
  93. chooseValue: wx.getStorageSync('chooseValue' + this.data.answersId)
  94. })
  95. }
  96. this.submitAll(this.data.chooseValue);
  97. }
  98. },
  99. upSubmit() {
  100. this.setData({
  101. index: this.data.index - 1
  102. })
  103. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  104. },
  105. async onLoad(options) {
  106. const sessionKey = await tools.checkSessionAndLogin();
  107. wx.setStorageSync('answersId', options.answersId);
  108. this.setData({
  109. sessionKey: sessionKey,
  110. paperId: options.paperId,
  111. answersId: options.answersId,
  112. id: options.id,
  113. })
  114. if (wx.getStorageSync('questionList' + wx.getStorageSync('answersId'))) {
  115. const questionList = wx.getStorageSync('questionList' + wx.getStorageSync('answersId'));
  116. const titleINDEX = wx.getStorageSync('titleINDEX' + wx.getStorageSync('answersId'));
  117. const chooseValue = wx.getStorageSync('chooseValue' + wx.getStorageSync('answersId'));
  118. this.setData({
  119. questionList: questionList,
  120. answersId: wx.getStorageSync('answersId'),
  121. chooseValue: chooseValue,
  122. index: titleINDEX
  123. })
  124. } else {
  125. this.allShi(options.id, options.paperId, sessionKey);
  126. }
  127. },
  128. allShi(id, paperId, sessionKey) {
  129. wx.request({
  130. url: app.globalData.publicUrl + '/wx/paper/detail',
  131. method: "post",
  132. data: {
  133. sessionKey: sessionKey,
  134. id: id,
  135. paperId: paperId
  136. },
  137. success: (res) => {
  138. let jsonArr = JSON.parse(res.data.paperContent).map(item => {
  139. let json = {};
  140. json.QuestType = item.QuestType;
  141. json.id = item.Id;
  142. json.QuestAnswer = item.QuestAnswer;
  143. json.QuestStem = item.QuestStem;
  144. if (item.QuestChoice) {
  145. let arr = item.QuestChoice.split('||');
  146. json.QuestChoice = arr;
  147. }
  148. if (item.QuestType == '1') {
  149. let arr = item.QuestChoice.split('||');
  150. let arrJson = arr.map(item => {
  151. let json1 = {};
  152. json1.name = item;
  153. json1.value = item;
  154. json1.checked = false;
  155. return json1
  156. });
  157. json.QuestChoice = arrJson;
  158. }
  159. if (item.QuestType == '0') {
  160. let arr = item.QuestChoice.split('||');
  161. let arrJson = arr.map(item => {
  162. let json1 = {};
  163. json1.name = item;
  164. json1.value = item;
  165. json1.checked = false;
  166. return json1
  167. });
  168. json.QuestChoice = arrJson;
  169. }
  170. if (item.QuestType == '2') {
  171. let arr = item.QuestChoice.split('||');
  172. let arrJson = arr.map(item => {
  173. let json1 = {};
  174. json1.name = item;
  175. json1.value = item;
  176. json1.checked = false;
  177. return json1
  178. });
  179. json.QuestChoice = arrJson;
  180. }
  181. return json;
  182. })
  183. this.setData({
  184. questionList: jsonArr
  185. })
  186. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), 0);
  187. wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), []),
  188. wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  189. }
  190. })
  191. },
  192. submitAll(currentAnswerTemp) {
  193. wx.showLoading({
  194. mask: true,
  195. title: '加载中',
  196. })
  197. wx.request({
  198. url: app.globalData.publicUrl + '/wx/paper/commit',
  199. method: "post",
  200. data: {
  201. sessionKey: this.data.sessionKey,
  202. currentAnswerTemp: currentAnswerTemp,
  203. answersId: wx.getStorageSync('answersId')
  204. },
  205. success: (res) => {
  206. wx.hideLoading();
  207. if (res.data.code == 0) {
  208. wx.showModal({
  209. showCancel: false,
  210. content: '恭喜您,提交答案成功,请耐心等待老师判卷',
  211. success() {
  212. wx.navigateBack({
  213. delta: 1
  214. })
  215. }
  216. })
  217. } else {
  218. wx.showModal({
  219. showCancel: false,
  220. content: res.data.msg,
  221. success() {
  222. wx.navigateBack({
  223. delta: 1
  224. })
  225. }
  226. })
  227. }
  228. }
  229. })
  230. }
  231. })