answerAfterclass.js 9.7 KB

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