answerAfterclass.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. console.log(this.data.chooseValue, "000000")
  97. this.submitAll(this.data.chooseValue);
  98. }
  99. },
  100. upSubmit() {
  101. this.setData({
  102. index: this.data.index - 1
  103. })
  104. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  105. },
  106. async onLoad(options) {
  107. wx.showLoading({
  108. title: '加载中',
  109. task: true
  110. })
  111. const sessionKey = await tools.checkSessionAndLogin();
  112. wx.setStorageSync('answersId', options.answersId);
  113. this.setData({
  114. sessionKey: sessionKey,
  115. paperId: options.paperId,
  116. answersId: options.answersId,
  117. id: options.id,
  118. })
  119. if (wx.getStorageSync('questionList' + wx.getStorageSync('answersId'))) {
  120. const questionList = wx.getStorageSync('questionList' + wx.getStorageSync('answersId'));
  121. const titleINDEX = wx.getStorageSync('titleINDEX' + wx.getStorageSync('answersId'));
  122. const chooseValue = wx.getStorageSync('chooseValue' + wx.getStorageSync('answersId'));
  123. console.log(wx.getStorageSync('questionList' + wx.getStorageSync('answersId')))
  124. this.setData({
  125. questionList: questionList,
  126. answersId: wx.getStorageSync('answersId'),
  127. chooseValue: chooseValue,
  128. index: titleINDEX
  129. })
  130. wx.hideLoading();
  131. } else {
  132. this.allShi(options.id, options.paperId, sessionKey);
  133. }
  134. },
  135. allShi(id, paperId, sessionKey) {
  136. wx.request({
  137. url: app.globalData.publicUrl + '/wx/paper/detail',
  138. method: "post",
  139. data: {
  140. sessionKey: sessionKey,
  141. id: id,
  142. paperId: paperId
  143. },
  144. success: (res) => {
  145. console.log(res)
  146. let jsonArr = JSON.parse(res.data.paperContent).map(item => {
  147. let json = {};
  148. json.QuestType = item.QuestType;
  149. json.id = item.Id;
  150. json.QuestAnswer = item.QuestAnswer;
  151. json.QuestStem = item.QuestStem;
  152. if (item.QuestChoice) {
  153. let arr = item.QuestChoice.split('||');
  154. json.QuestChoice = arr;
  155. }
  156. if (item.QuestType == '1') {
  157. let arr = item.QuestChoice.split('||');
  158. let arrJson = arr.map(item => {
  159. let json1 = {};
  160. json1.name = item;
  161. json1.value = item;
  162. json1.checked = false;
  163. return json1
  164. });
  165. json.QuestChoice = arrJson;
  166. }
  167. if (item.QuestType == '0') {
  168. let arr = item.QuestChoice.split('||');
  169. let arrJson = arr.map(item => {
  170. let json1 = {};
  171. json1.name = item;
  172. json1.value = item;
  173. json1.checked = false;
  174. return json1
  175. });
  176. json.QuestChoice = arrJson;
  177. }
  178. if (item.QuestType == '2') {
  179. let arr = item.QuestChoice.split('||');
  180. let arrJson = arr.map(item => {
  181. let json1 = {};
  182. json1.name = item;
  183. json1.value = item;
  184. json1.checked = false;
  185. return json1
  186. });
  187. json.QuestChoice = arrJson;
  188. }
  189. return json;
  190. })
  191. this.setData({
  192. questionList: jsonArr
  193. })
  194. wx.hideLoading();
  195. console.log(this.data.questionList)
  196. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), 0);
  197. wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), []),
  198. wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  199. },
  200. fail: () => {
  201. wx.hideLoading()
  202. }
  203. })
  204. },
  205. submitAll(currentAnswerTemp) {
  206. wx.showLoading({
  207. mask: true,
  208. title: '加载中',
  209. })
  210. if (currentAnswerTemp.length == 0) {
  211. let arr = this.data.questionList.map(item => {
  212. let json = {};
  213. json.currentAnswer = '';
  214. json.questType = item.QuestType;
  215. json.id = item.id;
  216. return json
  217. });
  218. wx.request({
  219. url: app.globalData.publicUrl + '/wx/paper/commit',
  220. method: "post",
  221. data: {
  222. sessionKey: this.data.sessionKey,
  223. currentAnswerTemp: arr,
  224. answersId: wx.getStorageSync('answersId')
  225. },
  226. success: (res) => {
  227. wx.hideLoading();
  228. if (res.data.code == 0) {
  229. wx.showModal({
  230. showCancel: false,
  231. content: '恭喜您,提交答案成功,请耐心等待老师判卷',
  232. success() {
  233. wx.navigateBack({
  234. delta: 1
  235. })
  236. }
  237. })
  238. } else {
  239. wx.showModal({
  240. showCancel: false,
  241. content: res.data.msg,
  242. success() {
  243. wx.navigateBack({
  244. delta: 1
  245. })
  246. }
  247. })
  248. }
  249. },
  250. fail: () => {
  251. wx.hideLoading()
  252. }
  253. })
  254. } else {
  255. wx.request({
  256. url: app.globalData.publicUrl + '/wx/paper/commit',
  257. method: "post",
  258. data: {
  259. sessionKey: this.data.sessionKey,
  260. currentAnswerTemp: currentAnswerTemp,
  261. answersId: wx.getStorageSync('answersId')
  262. },
  263. success: (res) => {
  264. wx.hideLoading();
  265. if (res.data.code == 0) {
  266. wx.showModal({
  267. showCancel: false,
  268. content: '恭喜您,提交答案成功,请耐心等待老师判卷',
  269. success() {
  270. wx.navigateBack({
  271. delta: 1
  272. })
  273. }
  274. })
  275. } else {
  276. wx.showModal({
  277. showCancel: false,
  278. content: res.data.msg,
  279. success() {
  280. wx.navigateBack({
  281. delta: 1
  282. })
  283. }
  284. })
  285. }
  286. },
  287. fail: () => {
  288. wx.hideLoading()
  289. }
  290. })
  291. }
  292. }
  293. })