index.js 3.4 KB

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