index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. let app = getApp()
  2. const util = require("../../../utils/util")
  3. Page({
  4. data: {
  5. preList:["A","B","C", "D", "E", "F"],
  6. info:{},
  7. total:0,
  8. unfinish:0,
  9. answerId:0,
  10. index:0,
  11. type:1,
  12. answerMap:{},
  13. showResult: false,
  14. types:{
  15. 1:'判断题',
  16. 2:'单选题',
  17. 3:'多选题',
  18. 4:'案例题'
  19. },
  20. groupId:0
  21. },
  22. onLoad: function(options) {
  23. let groupId = +options.groupId||1;
  24. let type = +options.type||1;
  25. let total = +options.count ||0;
  26. this.setData({ groupId, type, total });
  27. },
  28. onChange(event){
  29. const showResult = event.detail.value;
  30. let {info} = this.data;
  31. if( showResult ) {
  32. info.select = info.result;
  33. }else{
  34. info.select = 0
  35. }
  36. this.setData({showResult, info})
  37. },
  38. onShow: function(){
  39. app.checkLogin( userInfo =>{
  40. this.setData({userInfo, from:0})
  41. this.loadAnswer()
  42. })
  43. },
  44. loadAnswer( ){
  45. let {groupId, type, showResult} = this.data
  46. app.formPost('Exam.LoadAnswerNew', {groupId, type}).then(res => {
  47. if (res.code ==200) {
  48. let {unfinish, info, total} = res.data;
  49. let index = total- unfinish;
  50. if(showResult) info.select = info.result;
  51. this.setData({unfinish, total, info, index})
  52. }
  53. })
  54. },
  55. checkAnswer( e ){
  56. let {showResult, info, answerMap, groupId} = this.data;
  57. let answerId= info.answerId||0;
  58. if( showResult ) return;
  59. if(!answerId) return;
  60. let param = {answerId, groupId}
  61. // 多选
  62. if( info.type == 3 ){
  63. info.correct = info.select == info.result;
  64. }else{
  65. info.correct = info.select == info.result;
  66. }
  67. param.correct = info.correct?1:0;
  68. let result = answerMap[answerId];
  69. if( result === param.correct) return;
  70. answerMap[answerId] = param.correct;
  71. // 打开下一题
  72. app.formPost('Exam.EditErrorAnswer', param).then(res => {
  73. this.setData({info, next:true, answerMap})
  74. })
  75. },
  76. radioChange( e ){
  77. let info = this.data.info
  78. info.select = +e.detail.value
  79. this.setData( {info} );
  80. },
  81. checkboxChange( e ){
  82. let info = this.data.info
  83. info.select = +e.detail.value.sort().join("")
  84. let result = ""+info.result
  85. if (info.select == result ){
  86. info._select = true
  87. }else{
  88. for( let i in e.detail.value){
  89. if(result.indexOf( e.detail.value[i] )==-1 ){
  90. info._select = true
  91. }
  92. }
  93. }
  94. this.setData( {info} );
  95. },
  96. restartAnswer(){
  97. let {groupId, type, showResult} = this.data
  98. app.formPost('Exam.resetAnswerNew', {groupId, type}).then(res => {
  99. if (res.code ==200) {
  100. let {total, unfinish,info} = res.data
  101. let index = total - unfinish;
  102. if(showResult) info.select = info.result;
  103. this.setData({info, index, total, unfinish})
  104. }
  105. })
  106. },
  107. finishAnswer( e ){
  108. this.checkAnswer()
  109. let action = e.currentTarget.dataset.action;
  110. let {groupId, unfinish, type, showResult, total, index} = this.data
  111. let correct = this.data.info.select == this.data.info.result?1:0;
  112. if( action == "prev"){
  113. if( total <= unfinish ) {
  114. util.showMsg("已经在第一题")
  115. return
  116. }
  117. unfinish+=1
  118. }else{
  119. if( unfinish < 1 ) {
  120. util.showMsg("已经最后一题")
  121. return;
  122. }
  123. unfinish-=1
  124. }
  125. app.formPost('Exam.finishAnswerNew', {groupId, index, type, correct, action}).then(res => {
  126. if (res.code ==200) {
  127. let info = res.data
  128. let index = total-unfinish;
  129. if( showResult) info.select = info.result;
  130. this.setData({info, unfinish, index})
  131. }
  132. })
  133. }
  134. })