index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.StartMockExamV1', {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. let param = {id: item.id, examId}
  58. let ansewersMap = this.data.ansewersMap
  59. let index = item.index||0;
  60. if( !item.select ){
  61. return;
  62. }
  63. if( item.type == 4){
  64. let answers = {};
  65. for( let i in item.child){
  66. let select = item.child[i].select||0;
  67. if( item.child[i].type == 3 && !Number(select)) {
  68. console.log("select", select)
  69. select = +select.join("")
  70. };
  71. answers[item.child[i].answerId] = select
  72. }
  73. param.answers = answers
  74. let prevSubMit = ansewersMap[index];
  75. let answerStr = JSON.stringify(answers)
  76. if( prevSubMit == answerStr ) {
  77. console.log("submit exit", index, prevSubMit)
  78. return;
  79. }
  80. ansewersMap[index] = answerStr
  81. }else{
  82. let select = item.select||0;
  83. if( item.type == 3 && !Number(select)) select = +select.join("");
  84. let prevSubMit = ansewersMap[index];
  85. if( prevSubMit == select ) {
  86. console.log("submit exit", index, prevSubMit)
  87. return;
  88. }
  89. ansewersMap[index] = select
  90. param.select = select
  91. }
  92. this.setData({ansewersMap});
  93. app.formPost('Exam.SubmitMockAnswerV1', param).then(res => {
  94. goNext && this.nextAnswer( )
  95. })
  96. },
  97. radioChange( e ){
  98. let cid=e.currentTarget.dataset.cid;
  99. let item = this.data.item
  100. item.select = +e.detail.value;
  101. this.setData({item})
  102. if( !item.child ){
  103. this.checkAnswer( true )
  104. }else{
  105. item.child[cid].select = item.select;
  106. this.setData({item})
  107. }
  108. },
  109. loadQuestion( index ) {
  110. this.checkAnswer( false )
  111. let item = this.data.info.answers[index];
  112. if( !item ) return;
  113. item.index = index;
  114. if( !item.title ){
  115. app.formPost("Exam.loadMockAnswerV1", {id:item.id} ).then(res => {
  116. if( res.code != 200) return;
  117. Object.assign( item, res.data)
  118. this.saveItem( item )
  119. })
  120. }else{
  121. this.saveItem( item )
  122. }
  123. wx.pageScrollTo({
  124. scrollTop: 0
  125. })
  126. },
  127. saveItem( item ){
  128. let info = this.data.info;
  129. info.index = item.index;
  130. info.answers[ item.index] = item;
  131. wx.setStorageSync(this.data.cacheKey, info)
  132. this.setData({item, info})
  133. },
  134. checkboxChange( e ){
  135. let cid=e.currentTarget.dataset.cid;
  136. let item = this.data.item
  137. item.select = parseInt(e.detail.value.sort().join(''))
  138. if( item.child ){
  139. item.child[cid].select = item.select
  140. }
  141. console.log("item", cid, item.select )
  142. this.setData({item})
  143. },
  144. nextAnswer( ){
  145. let nextId = (this.data.item.index||0)+1
  146. if( nextId >= this.data.info.answers.length){
  147. util.showModel("错误提示","已经最后一题" )
  148. return
  149. }
  150. this.loadQuestion( nextId )
  151. },
  152. returnRecord(){
  153. wx.navigateBack({
  154. delta: 1,
  155. })
  156. },
  157. dosubmit(e){
  158. this.checkAnswer( false );
  159. let examId = this.data.info.examId;
  160. app.formPost("Exam.FinishMockExam", {examId}).then(res => {
  161. if (res.code == 200) {
  162. let { score, useTime } = res.data;
  163. this.setData({score, useTime, modalShow:true})
  164. wx.setStorageSync(this.data.cacheKey, {})
  165. if( this.data.timer) clearInterval(this.data.timer)
  166. }
  167. });
  168. },
  169. timeOut() {
  170. clearInterval(this.data.timer)
  171. this.setData({
  172. timeOutShow: true
  173. });
  174. },
  175. timeReduce() {
  176. let _this = this
  177. let remainTime = this.data.info.duration + this.data.info.startTime - parseInt(Date.now()/1000)
  178. console.log("remainTime", remainTime);
  179. this.setData({remainTime})
  180. let timer = setInterval(function() {
  181. let remainTime = _this.data.remainTime
  182. if (remainTime <= 0) {
  183. _this.timeOut()
  184. } else {
  185. _this.setData({
  186. remainTime: remainTime - 1,
  187. remainTimeStr: formatSeconds(remainTime),
  188. doTime: _this.data.doTime + 1
  189. });
  190. }
  191. }, 1000)
  192. _this.setData({
  193. timer: timer
  194. });
  195. }
  196. })