answerAfterclasses.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // onShow: function () {
  120. // if (wx.getStorageSync('questionList' + wx.getStorageSync('answersId'))) {
  121. // const questionList = wx.getStorageSync('questionList' + wx.getStorageSync('answersId'));
  122. // this.setData({
  123. // questionList: questionList,
  124. // index: wx.getStorageSync('titleINDEX' + wx.getStorageSync('answersId')),
  125. // answersId: wx.getStorageSync('answersId' + wx.getStorageSync('answersId')),
  126. // chooseValue: wx.getStorageSync('chooseValue' + wx.getStorageSync('answersId')),
  127. // })
  128. // } else {
  129. // this.allShi(this.data.id, this.data.paperId, this.data.sessionKey);
  130. // }
  131. // },
  132. async onLoad(options) {
  133. const sessionKey = await tools.checkSessionAndLogin();
  134. wx.setStorageSync('answersId', options.answersId);
  135. this.setData({
  136. sessionKey: sessionKey,
  137. paperId: options.paperId,
  138. answersId: options.answersId,
  139. id: options.id,
  140. })
  141. if (wx.getStorageSync('questionList' + wx.getStorageSync('answersId'))) {
  142. const questionList = wx.getStorageSync('questionList' + wx.getStorageSync('answersId'));
  143. const titleINDEX = wx.getStorageSync('titleINDEX' + wx.getStorageSync('answersId'));
  144. const chooseValue = wx.getStorageSync('chooseValue' + wx.getStorageSync('answersId'));
  145. this.setData({
  146. questionList: questionList,
  147. answersId: wx.getStorageSync('answersId'),
  148. chooseValue: chooseValue,
  149. index: titleINDEX
  150. })
  151. } else {
  152. this.allShi(options.id, options.paperId, sessionKey);
  153. }
  154. },
  155. allShi(id, paperId, sessionKey) {
  156. wx.request({
  157. url: app.globalData.publicUrl + '/wx/paper/detail',
  158. method: "post",
  159. data: {
  160. sessionKey: sessionKey,
  161. id: id,
  162. paperId: paperId
  163. },
  164. success: (res) => {
  165. let jsonArr = JSON.parse(res.data.paperContent).map(item => {
  166. let json = {};
  167. json.QuestType = item.QuestType;
  168. json.id = item.Id;
  169. json.QuestAnswer = item.QuestAnswer;
  170. json.QuestStem = item.QuestStem;
  171. if (item.QuestChoice) {
  172. let arr = item.QuestChoice.split('||');
  173. json.QuestChoice = arr;
  174. }
  175. if (item.QuestType == '1') {
  176. let arr = item.QuestChoice.split('||');
  177. let arrJson = arr.map(item => {
  178. let json1 = {};
  179. json1.name = item;
  180. json1.value = item;
  181. json1.checked = false;
  182. return json1
  183. });
  184. json.QuestChoice = arrJson;
  185. }
  186. if (item.QuestType == '0') {
  187. let arr = item.QuestChoice.split('||');
  188. let arrJson = arr.map(item => {
  189. let json1 = {};
  190. json1.name = item;
  191. json1.value = item;
  192. json1.checked = false;
  193. return json1
  194. });
  195. json.QuestChoice = arrJson;
  196. }
  197. if (item.QuestType == '2') {
  198. let arr = item.QuestChoice.split('||');
  199. let arrJson = arr.map(item => {
  200. let json1 = {};
  201. json1.name = item;
  202. json1.value = item;
  203. json1.checked = false;
  204. return json1
  205. });
  206. json.QuestChoice = arrJson;
  207. }
  208. return json;
  209. })
  210. this.setData({
  211. questionList: jsonArr
  212. })
  213. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), 0);
  214. wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), []),
  215. wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  216. }
  217. })
  218. },
  219. submitAll(currentAnswerTemp) {
  220. wx.request({
  221. url: app.globalData.publicUrl + '/wx/paper/commit',
  222. method: "post",
  223. data: {
  224. sessionKey: this.data.sessionKey,
  225. currentAnswerTemp: currentAnswerTemp,
  226. answersId: wx.getStorageSync('answersId')
  227. },
  228. success: (res) => {
  229. if (res.data.code == 0) {
  230. wx.showModal({
  231. showCancel: false,
  232. content: '恭喜您,提交答案成功,请耐心等待老师判卷',
  233. success() {
  234. wx.navigateBack({
  235. delta: 1
  236. })
  237. }
  238. })
  239. }
  240. else {
  241. wx.showModal({
  242. showCancel: false,
  243. content: res.data.msg,
  244. success() {
  245. wx.navigateBack({
  246. delta: 1
  247. })
  248. }
  249. })
  250. }
  251. }
  252. })
  253. }
  254. })