answerAfterclasses.js 8.1 KB

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