index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import {formatSeconds} from '../../../utils/util.js'
  2. let app = getApp()
  3. Page({
  4. data: {
  5. preList:["A","B","C", "D", "E", "F"],
  6. userInfo: {},
  7. modalShow: false,
  8. timeOutShow: false,
  9. remainTime: 0,
  10. remainTimeStr: '',
  11. index:0,
  12. score:0,
  13. useTime:0,
  14. item: {index:0},
  15. info: {},
  16. list: [],
  17. },
  18. onLoad: function(options) {
  19. let courseId = +options.courseId||36
  20. app.checkLogin( userInfo =>{
  21. this.setData({userInfo})
  22. this.loadData( courseId )
  23. })
  24. },
  25. loadData( courseId ){
  26. let info = wx.getStorageSync('@examinfo')||{};
  27. if( info && info.courseId == courseId){
  28. let index = info.index||0;
  29. this.setData({info})
  30. this.loadQuestion( index )
  31. this.timeReduce()
  32. return;
  33. }
  34. app.formPost('Course.startExam', {courseId}).then(res => {
  35. if (res.code ==200) {
  36. this.setData({info:res.data })
  37. this.loadQuestion( 0 )
  38. this.timeReduce()
  39. }
  40. })
  41. },
  42. selectquestion( e ){
  43. let index = e.currentTarget.dataset.index
  44. this.loadQuestion( index )
  45. },
  46. checkAnswer( e ){
  47. let item = this.data.item;
  48. let examId = this.data.index.examId;
  49. if( !item.select ){
  50. util.message("还未作答", 'error')
  51. return;
  52. }
  53. let select = item.select;
  54. if( item.type == 3) select = +select.join("");
  55. let param = {id: item.id, examId, select}
  56. app.formPost('Course.SubmitAnswer', param).then(res => {
  57. this.nextAnswer( )
  58. })
  59. },
  60. radioChange( e ){
  61. let item = this.data.item
  62. item.select = +e.detail.value;
  63. console.log( "radioChange", item)
  64. this.setData({item})
  65. this.checkAnswer()
  66. },
  67. loadQuestion( index ) {
  68. let item = this.data.info.answers[index];
  69. item.index = index;
  70. if( !item.title ){
  71. app.formPost("course.loadAnswer", {id:item.id} ).then(res => {
  72. if( res.code != 200) return;
  73. Object.assign( item, res.data)
  74. this.saveItem( item )
  75. })
  76. }else{
  77. this.saveItem( item )
  78. }
  79. },
  80. saveItem( item ){
  81. let info = this.data.info;
  82. info.index = item.index;
  83. info.answers[ item.index] = item;
  84. wx.setStorageSync('@examinfo', info)
  85. console.log("item", item)
  86. this.setData({item, info})
  87. },
  88. checkboxChange( e ){
  89. let item = this.data.item
  90. item.select = parseInt(e.detail.value.sort().join(''))
  91. console.log( "radioChange", item)
  92. this.setData({item})
  93. },
  94. nextAnswer( ){
  95. console.log( "next")
  96. this.loadQuestion( this.data.info.index+1)
  97. },
  98. returnRecord(){
  99. wx.navigateBack({
  100. delta: 1,
  101. })
  102. },
  103. dosubmit(e){
  104. let examId = this.data.info.examId
  105. app.formPost("course.FinishExam", {examId}).then(res => {
  106. if (res.code == 200) {
  107. let { score, useTime } = res.data;
  108. this.setData({score, useTime, modalShow:true})
  109. wx.setStorageSync('@examinfo', {})
  110. if( this.data.timer) clearInterval(this.data.timer)
  111. }
  112. });
  113. },
  114. timeOut() {
  115. clearInterval(this.data.timer)
  116. this.setData({
  117. timeOutShow: true
  118. });
  119. },
  120. timeReduce() {
  121. let _this = this
  122. let remainTime = this.data.info.duration + this.data.info.startTime - parseInt(Date.now()/1000)
  123. this.setData({remainTime})
  124. let timer = setInterval(function() {
  125. let remainTime = _this.data.remainTime
  126. if (remainTime <= 0) {
  127. _this.timeOut()
  128. } else {
  129. _this.setData({
  130. remainTime: remainTime - 1,
  131. remainTimeStr: formatSeconds(remainTime),
  132. doTime: _this.data.doTime + 1
  133. });
  134. }
  135. }, 1000)
  136. _this.setData({
  137. timer: timer
  138. });
  139. }
  140. })