answerAfterclass.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. wx.hideLoading();
  125. this.setData({
  126. questionList: questionList,
  127. answersId: wx.getStorageSync('answersId'),
  128. chooseValue: chooseValue,
  129. index: titleINDEX
  130. })
  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. wx.hideLoading();
  192. this.setData({
  193. questionList: jsonArr
  194. })
  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. // if(currentAnswerTemp.length==0){
  207. // currentAnswerTemp
  208. // }
  209. wx.showLoading({
  210. mask: true,
  211. title: '加载中',
  212. })
  213. if (currentAnswerTemp.length == 0) {
  214. let arr = this.data.questionList.map(item => {
  215. let json = {};
  216. json.currentAnswer = '';
  217. json.questType = item.QuestType;
  218. json.id = item.id;
  219. return json
  220. });
  221. wx.request({
  222. url: app.globalData.publicUrl + '/wx/paper/commit',
  223. method: "post",
  224. data: {
  225. sessionKey: this.data.sessionKey,
  226. currentAnswerTemp: arr,
  227. answersId: wx.getStorageSync('answersId')
  228. },
  229. success: (res) => {
  230. wx.hideLoading();
  231. if (res.data.code == 0) {
  232. wx.showModal({
  233. showCancel: false,
  234. content: '恭喜您,提交答案成功,请耐心等待老师判卷',
  235. success() {
  236. wx.navigateBack({
  237. delta: 1
  238. })
  239. }
  240. })
  241. } else {
  242. wx.showModal({
  243. showCancel: false,
  244. content: res.data.msg,
  245. success() {
  246. wx.navigateBack({
  247. delta: 1
  248. })
  249. }
  250. })
  251. }
  252. },
  253. fail: () => {
  254. wx.hideLoading()
  255. }
  256. })
  257. } else {
  258. wx.request({
  259. url: app.globalData.publicUrl + '/wx/paper/commit',
  260. method: "post",
  261. data: {
  262. sessionKey: this.data.sessionKey,
  263. currentAnswerTemp: currentAnswerTemp,
  264. answersId: wx.getStorageSync('answersId')
  265. },
  266. success: (res) => {
  267. wx.hideLoading();
  268. if (res.data.code == 0) {
  269. wx.showModal({
  270. showCancel: false,
  271. content: '恭喜您,提交答案成功,请耐心等待老师判卷',
  272. success() {
  273. wx.navigateBack({
  274. delta: 1
  275. })
  276. }
  277. })
  278. } else {
  279. wx.showModal({
  280. showCancel: false,
  281. content: res.data.msg,
  282. success() {
  283. wx.navigateBack({
  284. delta: 1
  285. })
  286. }
  287. })
  288. }
  289. },
  290. fail: () => {
  291. wx.hideLoading()
  292. }
  293. })
  294. }
  295. }
  296. })