answerAfterclass.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. console.log(json, "josnsosjosjdosdjsodjsod");
  23. this.data.chooseValue[this.data.index] = json;
  24. const items = this.data.questionList[this.data.index].QuestChoice
  25. for (let i = 0, len = items.length; i < len; ++i) {
  26. items[i].checked = items[i].value === e.detail.value
  27. }
  28. this.setData({
  29. questionList: this.data.questionList
  30. })
  31. wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  32. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  33. wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);
  34. },
  35. /*
  36. * 多选事件
  37. */
  38. checkboxChange: function (e) {
  39. console.log(e)
  40. const questionList = wx.getStorageSync('questionList' + wx.getStorageSync('answersId'));
  41. console.log(questionList, '我是选项');
  42. console.log(this.data.index)
  43. let data = questionList[this.data.index].QuestChoice;
  44. let json = {};
  45. let list = e.detail.value;
  46. let valueData = [];
  47. data.map(ite => {
  48. list.map(items => {
  49. if (ite.name == items) {
  50. valueData.push(ite.name)
  51. }
  52. })
  53. })
  54. let aa = valueData.join("||")
  55. json.currentAnswer = aa;
  56. json.questType = e.currentTarget.dataset.questtypes;
  57. json.id = e.currentTarget.dataset.idx;
  58. console.log(json, "chexboxsssssssssssssssssss");
  59. this.data.chooseValue[this.data.index] = json;
  60. const items = this.data.questionList[this.data.index].QuestChoice
  61. const values = e.detail.value
  62. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  63. items[i].checked = false
  64. for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
  65. if (items[i].value === values[j]) {
  66. items[i].checked = true
  67. break
  68. }
  69. }
  70. }
  71. this.setData({
  72. questionList: this.data.questionList
  73. })
  74. wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  75. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  76. wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);
  77. },
  78. /*
  79. * 解答事件
  80. */
  81. bindTextAreaBlur(e) {
  82. let json = {};
  83. json.currentAnswer = e.detail.value;
  84. json.id = e.currentTarget.dataset.idx;
  85. json.questType = e.currentTarget.dataset.questtypes;
  86. this.data.chooseValue[this.data.index] = json;
  87. this.data.questionList[this.data.index].currentAnswer = e.detail.value;
  88. this.setData({
  89. questionList: this.data.questionList
  90. })
  91. wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  92. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  93. wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);
  94. },
  95. /*
  96. * 下一题/提交 按钮
  97. */
  98. nextSubmit: function () {
  99. // 判断是不是最后一题
  100. if (this.data.index < this.data.questionList.length - 1) {
  101. // 渲染下一题
  102. this.setData({
  103. index: this.data.index + 1
  104. })
  105. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  106. } else {
  107. if (wx.getStorageSync('chooseValue' + this.data.answersId)) {
  108. this.setData({
  109. chooseValue: wx.getStorageSync('chooseValue' + this.data.answersId)
  110. })
  111. }
  112. console.log(this.data.chooseValue, "000000")
  113. this.submitAll(this.data.chooseValue);
  114. }
  115. },
  116. upSubmit() {
  117. this.setData({
  118. index: this.data.index - 1
  119. })
  120. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  121. },
  122. async onLoad(options) {
  123. wx.showLoading({
  124. title: '加载中',
  125. task: true
  126. })
  127. const sessionKey = await tools.checkSessionAndLogin();
  128. wx.setStorageSync('answersId', options.answersId);
  129. this.setData({
  130. sessionKey: sessionKey,
  131. paperId: options.paperId,
  132. answersId: options.answersId,
  133. id: options.id,
  134. })
  135. if (wx.getStorageSync('questionList' + wx.getStorageSync('answersId'))) {
  136. const questionList = wx.getStorageSync('questionList' + wx.getStorageSync('answersId'));
  137. console.log(questionList, "大阪烧");
  138. const titleINDEX = wx.getStorageSync('titleINDEX' + wx.getStorageSync('answersId'));
  139. const chooseValue = wx.getStorageSync('chooseValue' + wx.getStorageSync('answersId'));
  140. console.log(wx.getStorageSync('questionList' + wx.getStorageSync('answersId')))
  141. this.setData({
  142. questionList: questionList,
  143. answersId: wx.getStorageSync('answersId'),
  144. chooseValue: chooseValue,
  145. index: titleINDEX
  146. })
  147. wx.hideLoading();
  148. } else {
  149. this.allShi(options.id, options.paperId, sessionKey);
  150. }
  151. },
  152. allShi(id, paperId, sessionKey) {
  153. wx.request({
  154. url: app.globalData.publicUrl + '/wx/paper/detail',
  155. method: "post",
  156. data: {
  157. sessionKey: sessionKey,
  158. id: id,
  159. paperId: paperId
  160. },
  161. success: (res) => {
  162. console.log(res)
  163. let jsonArr = JSON.parse(res.data.paperContent).map(item => {
  164. let json = {};
  165. json.QuestType = item.QuestType;
  166. json.id = item.Id;
  167. json.QuestAnswer = item.QuestAnswer;
  168. json.QuestStem = item.QuestStem;
  169. if (item.QuestChoice) {
  170. let arr = item.QuestChoice.split('||');
  171. json.QuestChoice = arr;
  172. }
  173. if (item.QuestType == '1') {
  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 == '0') {
  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. if (item.QuestType == '2') {
  196. let arr = item.QuestChoice.split('||');
  197. let arrJson = arr.map(item => {
  198. let json1 = {};
  199. json1.name = item;
  200. json1.value = item;
  201. json1.checked = false;
  202. return json1
  203. });
  204. json.QuestChoice = arrJson;
  205. }
  206. return json;
  207. })
  208. this.setData({
  209. questionList: jsonArr
  210. })
  211. wx.hideLoading();
  212. console.log(this.data.questionList)
  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. fail: () => {
  218. wx.hideLoading()
  219. }
  220. })
  221. },
  222. submitAll(currentAnswerTemp) {
  223. wx.showLoading({
  224. mask: true,
  225. title: '加载中',
  226. })
  227. console.log(currentAnswerTemp, "传值");
  228. let indexs = currentAnswerTemp.findIndex(item => {
  229. return !item
  230. })
  231. console.log(indexs, "sssssss");
  232. if (indexs == -1) {
  233. if (currentAnswerTemp.length == 0) {
  234. let arr = this.data.questionList.map(item => {
  235. let json = {};
  236. json.currentAnswer = '';
  237. json.questType = item.QuestType;
  238. json.id = item.id;
  239. return json
  240. });
  241. wx.request({
  242. url: app.globalData.publicUrl + '/wx/paper/commit',
  243. method: "post",
  244. data: {
  245. sessionKey: this.data.sessionKey,
  246. currentAnswerTemp: arr,
  247. answersId: wx.getStorageSync('answersId')
  248. },
  249. success: (res) => {
  250. wx.hideLoading();
  251. if (res.data.code == 0) {
  252. wx.showModal({
  253. showCancel: false,
  254. content: '恭喜您,提交答案成功,请耐心等待老师判卷',
  255. success() {
  256. wx.navigateBack({
  257. delta: 1
  258. })
  259. }
  260. })
  261. } else {
  262. wx.showModal({
  263. showCancel: false,
  264. content: res.data.msg,
  265. success() {
  266. wx.navigateBack({
  267. delta: 1
  268. })
  269. }
  270. })
  271. }
  272. },
  273. fail: () => {
  274. wx.hideLoading()
  275. }
  276. })
  277. } else {
  278. wx.request({
  279. url: app.globalData.publicUrl + '/wx/paper/commit',
  280. method: "post",
  281. data: {
  282. sessionKey: this.data.sessionKey,
  283. currentAnswerTemp: currentAnswerTemp,
  284. answersId: wx.getStorageSync('answersId')
  285. },
  286. success: (res) => {
  287. wx.hideLoading();
  288. if (res.data.code == 0) {
  289. wx.showModal({
  290. showCancel: false,
  291. content: '恭喜您,提交答案成功,请耐心等待老师判卷',
  292. success() {
  293. wx.navigateBack({
  294. delta: 1
  295. })
  296. }
  297. })
  298. } else {
  299. wx.showModal({
  300. showCancel: false,
  301. content: res.data.msg,
  302. success() {
  303. wx.navigateBack({
  304. delta: 1
  305. })
  306. }
  307. })
  308. }
  309. },
  310. fail: () => {
  311. wx.hideLoading()
  312. }
  313. })
  314. }
  315. } else {
  316. wx.hideLoading();
  317. wx.showModal({
  318. showCancel: false,
  319. content: '当前第' + Number(indexs+1) + '没答,请答题',
  320. })
  321. }
  322. }
  323. })