index.js 4.1 KB

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