answerAfterclasses.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. checkboxChange: function (e) {
  77. const questionList = wx.getStorageSync('questionList' + wx.getStorageSync('answersId'));
  78. console.log(questionList,'我是选项es');
  79. console.log(this.data.index)
  80. let data=questionList[this.data.index].QuestChoice;
  81. let json = {};
  82. let list=e.detail.value;
  83. let valueData=[];
  84. data.map(ite=>{
  85. list.map(items=>{
  86. if(ite.name==items)
  87. {
  88. valueData.push(ite.name)
  89. }
  90. })
  91. })
  92. let aa = valueData.join("||")
  93. json.currentAnswer = aa;
  94. json.questType = e.currentTarget.dataset.questtypes;
  95. json.id = e.currentTarget.dataset.idx;
  96. this.data.chooseValue[this.data.index] = json;
  97. const items = this.data.questionList[this.data.index].QuestChoice
  98. const values = e.detail.value
  99. for (let i = 0, lenI = items.length; i < lenI; ++i) {
  100. items[i].checked = false
  101. for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
  102. if (items[i].value === values[j]) {
  103. items[i].checked = true
  104. break
  105. }
  106. }
  107. }
  108. this.setData({
  109. questionList: this.data.questionList
  110. })
  111. wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  112. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  113. wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);
  114. },
  115. /*
  116. * 解答事件
  117. */
  118. bindTextAreaBlur(e) {
  119. let json = {};
  120. json.currentAnswer = e.detail.value;
  121. json.id = e.currentTarget.dataset.idx;
  122. json.questType = e.currentTarget.dataset.questtypes;
  123. this.data.chooseValue[this.data.index] = json;
  124. this.data.questionList[this.data.index].currentAnswer = e.detail.value;
  125. this.setData({
  126. questionList: this.data.questionList
  127. })
  128. wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  129. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  130. wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), this.data.chooseValue);
  131. },
  132. /*
  133. * 下一题/提交 按钮
  134. */
  135. nextSubmit: function () {
  136. // console.log("0000")
  137. // 判断是不是最后一题
  138. if (this.data.index < this.data.questionList.length - 1) {
  139. console.log(this.data.chooseValue,"0000000000000000")
  140. // 渲染下一题
  141. this.setData({
  142. index: this.data.index + 1
  143. })
  144. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  145. } else {
  146. console.log(this.data.chooseValue,"1111111111")
  147. if (wx.getStorageSync('chooseValue' + this.data.answersId)) {
  148. this.setData({
  149. chooseValue: wx.getStorageSync('chooseValue' + this.data.answersId)
  150. })
  151. }
  152. this.submitAll(this.data.chooseValue);
  153. }
  154. },
  155. upSubmit() {
  156. console.log(this.data.chooseValue,"我点击上一题了")
  157. this.setData({
  158. index: this.data.index - 1
  159. })
  160. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), this.data.index);
  161. },
  162. async onLoad(options) {
  163. console.log("我是答题页","00000000000")
  164. const sessionKey = await tools.checkSessionAndLogin();
  165. wx.setStorageSync('answersId', options.answersId);
  166. this.setData({
  167. sessionKey: sessionKey,
  168. paperId: options.paperId,
  169. answersId: options.answersId,
  170. id: options.id,
  171. })
  172. if (wx.getStorageSync('questionList' + wx.getStorageSync('answersId'))) {
  173. const questionList = wx.getStorageSync('questionList' + wx.getStorageSync('answersId'));
  174. const titleINDEX = wx.getStorageSync('titleINDEX' + wx.getStorageSync('answersId'));
  175. const chooseValue = wx.getStorageSync('chooseValue' + wx.getStorageSync('answersId'));
  176. this.setData({
  177. questionList: questionList,
  178. answersId: wx.getStorageSync('answersId'),
  179. chooseValue: chooseValue,
  180. index: titleINDEX
  181. })
  182. } else {
  183. this.allShi(options.id, options.paperId, sessionKey);
  184. }
  185. },
  186. allShi(id, paperId, sessionKey) {
  187. wx.request({
  188. url: app.globalData.publicUrl + '/wx/paper/detail',
  189. method: "post",
  190. data: {
  191. sessionKey: sessionKey,
  192. id: id,
  193. paperId: paperId
  194. },
  195. success: (res) => {
  196. let jsonArr = JSON.parse(res.data.paperContent).map(item => {
  197. let json = {};
  198. json.QuestType = item.QuestType;
  199. json.id = item.Id;
  200. json.QuestAnswer = item.QuestAnswer;
  201. json.QuestStem = item.QuestStem;
  202. if (item.QuestChoice) {
  203. let arr = item.QuestChoice.split('||');
  204. json.QuestChoice = arr;
  205. }
  206. if (item.QuestType == '1') {
  207. let arr = item.QuestChoice.split('||');
  208. let arrJson = arr.map(item => {
  209. let json1 = {};
  210. json1.name = item;
  211. json1.value = item;
  212. json1.checked = false;
  213. return json1
  214. });
  215. json.QuestChoice = arrJson;
  216. }
  217. if (item.QuestType == '0') {
  218. let arr = item.QuestChoice.split('||');
  219. let arrJson = arr.map(item => {
  220. let json1 = {};
  221. json1.name = item;
  222. json1.value = item;
  223. json1.checked = false;
  224. return json1
  225. });
  226. json.QuestChoice = arrJson;
  227. }
  228. if (item.QuestType == '2') {
  229. let arr = item.QuestChoice.split('||');
  230. let arrJson = arr.map(item => {
  231. let json1 = {};
  232. json1.name = item;
  233. json1.value = item;
  234. json1.checked = false;
  235. return json1
  236. });
  237. json.QuestChoice = arrJson;
  238. }
  239. return json;
  240. })
  241. this.setData({
  242. questionList: jsonArr
  243. })
  244. wx.setStorageSync('titleINDEX' + wx.getStorageSync('answersId'), 0);
  245. wx.setStorageSync('chooseValue' + wx.getStorageSync('answersId'), []),
  246. wx.setStorageSync('questionList' + wx.getStorageSync('answersId'), this.data.questionList);
  247. },
  248. fail:()=>{
  249. wx.hideLoading();
  250. }
  251. })
  252. },
  253. submitAll(currentAnswerTemp) {
  254. console.log(currentAnswerTemp,"我是带过来的答案")
  255. let indexs = currentAnswerTemp.findIndex(item => {
  256. return !item
  257. })
  258. if (indexs == -1) {
  259. if (currentAnswerTemp.length == 0) {
  260. let arr = this.data.questionList.map(item => {
  261. let json = {};
  262. json.currentAnswer = '';
  263. json.questType = item.QuestType;
  264. json.id = item.id;
  265. return json
  266. });
  267. wx.request({
  268. url: app.globalData.publicUrl + '/wx/paper/commit',
  269. method: "post",
  270. data: {
  271. sessionKey: this.data.sessionKey,
  272. currentAnswerTemp: arr,
  273. answersId: wx.getStorageSync('answersId')
  274. },
  275. success: (res) => {
  276. wx.hideLoading();
  277. if (res.data.code == 0) {
  278. wx.showModal({
  279. showCancel: false,
  280. content: '恭喜您,提交答案成功,请耐心等待老师判卷',
  281. success() {
  282. wx.navigateBack({
  283. delta: 1
  284. })
  285. }
  286. })
  287. } else {
  288. wx.showModal({
  289. showCancel: false,
  290. content: res.data.msg,
  291. success() {
  292. wx.navigateBack({
  293. delta: 1
  294. })
  295. }
  296. })
  297. }
  298. }
  299. })
  300. } else {
  301. wx.request({
  302. url: app.globalData.publicUrl + '/wx/paper/commit',
  303. method: "post",
  304. data: {
  305. sessionKey: this.data.sessionKey,
  306. currentAnswerTemp: currentAnswerTemp,
  307. answersId: wx.getStorageSync('answersId')
  308. },
  309. success: (res) => {
  310. wx.hideLoading();
  311. if (res.data.code == 0) {
  312. wx.showModal({
  313. showCancel: false,
  314. content: '恭喜您,提交答案成功,请耐心等待老师判卷',
  315. success() {
  316. wx.navigateBack({
  317. delta: 1
  318. })
  319. }
  320. })
  321. } else {
  322. wx.showModal({
  323. showCancel: false,
  324. content: res.data.msg,
  325. success() {
  326. wx.navigateBack({
  327. delta: 1
  328. })
  329. }
  330. })
  331. }
  332. }
  333. })
  334. }
  335. }else {
  336. wx.hideLoading();
  337. wx.showModal({
  338. showCancel: false,
  339. content: '当前第' + Number(indexs+1) + '没答,请答题',
  340. })
  341. }
  342. // wx.request({
  343. // url: app.globalData.publicUrl + '/wx/paper/commit',
  344. // method: "post",
  345. // data: {
  346. // sessionKey: this.data.sessionKey,
  347. // currentAnswerTemp: currentAnswerTemp,
  348. // answersId: wx.getStorageSync('answersId')
  349. // },
  350. // success: (res) => {
  351. // if (res.data.code == 0) {
  352. // wx.showModal({
  353. // showCancel: false,
  354. // content: '恭喜您,提交答案成功,请耐心等待老师判卷',
  355. // success() {
  356. // wx.navigateBack({
  357. // delta: 1
  358. // })
  359. // }
  360. // })
  361. // }
  362. // else {
  363. // wx.showModal({
  364. // showCancel: false,
  365. // content: res.data.msg,
  366. // success() {
  367. // wx.navigateBack({
  368. // delta: 1
  369. // })
  370. // }
  371. // })
  372. // }
  373. // }
  374. // })
  375. }
  376. })