index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import {
  2. formatSeconds
  3. } from '../../../utils/util.js'
  4. let app = getApp()
  5. Page({
  6. data: {
  7. list: [],
  8. redo: false,
  9. preList:["A","B","C", "D","E","F"],
  10. info:{},
  11. timer: null,
  12. doTime: 0,
  13. remainTime: 0,
  14. remainTimeStr: '',
  15. modalShow: false,
  16. result: {},
  17. startTime: "",
  18. timeOutShow: false
  19. },
  20. onLoad: function(options) {
  21. let groupId = +options.id||3
  22. let redo = !!options.redo;
  23. this.setData({redo})
  24. let method = redo?'Exam.examreStart':'Exam.examStart';
  25. let _this = this
  26. app.formPost(method, {groupId}).then(res => {
  27. if (res.code ==200) {
  28. let {startTime, info, list} = res.data;
  29. let duration = list.length * 60;
  30. list = list.map(item=>{
  31. delete( item["select"])
  32. return item
  33. }).sort( (i,j) =>{
  34. return i.type > j.type?1:-1;
  35. })
  36. _this.setData({
  37. list: list,
  38. info: info,
  39. startTime: startTime,
  40. remainTime: duration
  41. });
  42. _this.timeReduce()
  43. }
  44. })
  45. },
  46. timeReduce() {
  47. let _this = this
  48. let timer = setInterval(function() {
  49. let remainTime = _this.data.remainTime
  50. if (remainTime <= 0) {
  51. _this.timeOut()
  52. } else {
  53. _this.setData({
  54. remainTime: remainTime - 1,
  55. remainTimeStr: formatSeconds(remainTime),
  56. doTime: _this.data.doTime + 1
  57. });
  58. }
  59. }, 1000)
  60. _this.setData({
  61. timer: timer
  62. });
  63. },
  64. onUnload() {
  65. clearInterval(this.data.timer)
  66. },
  67. returnRecord() {
  68. if( this.data.redo){
  69. wx.reLaunch({
  70. url: '/pages/exam/error/index',
  71. });
  72. }else{
  73. wx.reLaunch({
  74. url: '/pages/record/index',
  75. });
  76. }
  77. },
  78. timeOut() {
  79. clearInterval(this.data.timer)
  80. this.setData({
  81. timeOutShow: true
  82. });
  83. },
  84. radioChange( e ){
  85. let index = e.currentTarget.dataset.index;
  86. let list = this.data.list
  87. list[index].select = +e.detail.value
  88. console.log("select", index, list[index].select )
  89. this.setData( {list} );
  90. },
  91. checkboxChange( e ){
  92. let index = e.currentTarget.dataset.index;
  93. let list = this.data.list
  94. list[index].select = +e.detail.value.sort().join("")
  95. console.log("select", index, list[index].select)
  96. this.setData( {list} );
  97. },
  98. formSubmit: function(e) {
  99. let _this = this
  100. let force = this.data.remainTime < 3;
  101. let isFinish = true;
  102. wx.showLoading({
  103. title: '提交中',
  104. mask: true
  105. })
  106. let info = this.data.info;
  107. let param ={};
  108. param.groupId = info.groupId;
  109. param.paperId = info.paperId;
  110. param.answers = [];
  111. param.result = [];
  112. param.correct = 0;
  113. param.counter = this.data.list.length
  114. param.groupName = info.title;
  115. param.useTime = this.data.doTime
  116. param.duration = info.duration
  117. param.startTime = this.data.startTime;
  118. let errIds = [];
  119. for( let i=0; i< this.data.list.length; i++){
  120. let item = this.data.list[i];
  121. param.answers.push( item.answerId );
  122. param.result.push( item.select || 0);
  123. if( item.select == item.result){
  124. param.correct += 1
  125. }else{
  126. errIds.push( item.answerId)
  127. }
  128. if( !item.select ) isFinish = false
  129. }
  130. if( !isFinish && !force){
  131. app.message("还未完成", 'error')
  132. wx.hideLoading()
  133. return
  134. }
  135. param.result = param.result.join(",")
  136. param.answers = param.answers.join(",")
  137. param.errors = errIds.join(",")
  138. param.isRedo = this.data.redo?1:0;
  139. app.formPost('Exam.ExamSubmit', param).then(res => {
  140. if (res.code === 200) {
  141. _this.setData({
  142. modalShow: true,
  143. result:param,
  144. });
  145. if (this.data.timer) {
  146. clearInterval(this.data.timer)
  147. }
  148. } else {
  149. app.message(res.msg, 'error')
  150. }
  151. wx.hideLoading()
  152. })
  153. }
  154. })