测试
1 const https = require('../../public/js/douban.js'); 2 3 if(!Object.assign) { 4 Object.assign = require('../../public/core/object-assign.js') 5 } 6 //index.js 7 //获取应用实例 8 var app = getApp(); 9 Page({ 10 params:{ 11 12 }, 13 data: { 14 hourLong:45*60,//答题时长,单位秒 15 time:'45:00',//答题时长,单位秒 16 maxError:10,//最大错题数 17 userType:'xuechetiku', 18 isShowNewExam:false,//是否显示后台答案统计 19 isNewExam:false,//是否使用后台答案。为true时必须isShowNewExam也为true 20 isLoading:false,//加载 21 swiper:{ 22 active:0 23 }, 24 layerlayer:{ 25 isLayerShow:false,//默认弹窗 26 layerAnimation:{},//弹窗动画 27 }, 28 answerUrl:'weixin/small/1.0/?m=SmallApp&c=weixin&a=mnksHandPaper',//交卷URL 29 answers:{ 30 onLoadUrl:'weixin/small/1.0/?m=SmallApp&c=weixin&a=questionID',//题目号链接 31 start:0,//初始题号 32 end:0,//结束题号 33 allLists:[],//题号数据 34 activeNum:0,//当前条数 35 showActiveNum:0,//当前显示条数 36 onceLoadLength:5,//一次向俩端加载条数 37 url:'weixin/small/1.0/?m=SmallApp&c=weixin&a=getQuestion',//题目详情链接 38 isShowTip:false//默认是否显示提示 39 } 40 }, 41 //单选逻辑 42 tapRadio:function(e){ 43 //判断是否为已答题 44 if(this.data.answers.allLists[this.data.answers.activeNum].isNoFirst){ 45 return false; 46 } 47 var thisOption=e.currentTarget.dataset.option, 48 list = this.data.answers.allLists[this.data.answers.activeNum].options.map(function(option,i){ 49 if(thisOption == option.tip){ 50 if(!option.isSelect){ 51 // option.isActive = true; 52 option.isSelect = true; 53 }else{ 54 // option.isActive = false; 55 option.isSelect = false; 56 } 57 } 58 return option 59 }); 60 this.data.answers.allLists[this.data.answers.activeNum].options = list; 61 this.tapSelect(e); 62 }, 63 //多选逻辑 64 tapCheckbox:function(e){ 65 //判断是否为已答题 66 if(this.data.answers.allLists[this.data.answers.activeNum].isNoFirst){ 67 return false; 68 } 69 var thisOption=e.currentTarget.dataset.option, 70 list = this.data.answers.allLists[this.data.answers.activeNum].options.map(function(option,i){ 71 if(thisOption == option.tip){ 72 if(!option.isSelect){ 73 // option.isActive = true; 74 option.isSelect = true; 75 }else{ 76 // option.isActive = false; 77 option.isSelect = false; 78 } 79 } 80 return option 81 }); 82 this.data.answers.allLists[this.data.answers.activeNum].options = list; 83 this.setSwiperList(); 84 this.setData(this.data); 85 }, 86 //答案判断逻辑 87 tapSelect:function(e){ 88 //判断是否为已答题 89 if(this.data.answers.allLists[this.data.answers.activeNum].isNoFirst){ 90 return false; 91 } 92 93 var answered = 0,bool=true,that=this; 94 this.data.answers.allLists[this.data.answers.activeNum].options.forEach(function(option,i){ 95 //解析答案数字编码 96 if(option.isSelect){ 97 switch(option.tip){ 98 case 'A': 99 answered = + 16; 100 break; 101 case 'B': 102 answered = + 32; 103 break; 104 case 'C': 105 answered = + 64; 106 break; 107 case 'D': 108 answered = + 128; 109 break; 110 default: 111 console.log('超出设定'); 112 } 113 } 114 if(option.isSelect && !option.correct){ 115 bool=false; 116 } 117 if(!option.isSelect && option.correct){ 118 bool=false; 119 } 120 }); 121 //存放本次答案数字编码 122 this.data.answers.allLists[this.data.answers.activeNum].answered = answered; 123 124 //改变题目状态为已答 125 if(bool){ 126 //修正答案统计 127 if(this.data.answers.allLists[this.data.answers.activeNum].isAnswer == 0){ 128 this.data.answers.success++; 129 } 130 //修正答案统计 131 if(this.data.answers.allLists[this.data.answers.activeNum].isAnswer == 2){ 132 this.data.answers.success++; 133 this.data.answers.error--; 134 } 135 //设置为对题 136 this.data.answers.allLists[this.data.answers.activeNum].isAnswer = 1; 137 }else{ 138 //修正答案统计 139 if(this.data.answers.allLists[this.data.answers.activeNum].isAnswer == 0){ 140 this.data.answers.error++; 141 } 142 //修正答案统计 143 if(this.data.answers.allLists[this.data.answers.activeNum].isAnswer == 1){ 144 this.data.answers.success--; 145 this.data.answers.error++; 146 } 147 //设置为错题 148 this.data.answers.allLists[this.data.answers.activeNum].isAnswer = 2; 149 } 150 //改变为已答题状态 151 this.data.answers.allLists[this.data.answers.activeNum].isNoFirst = true; 152 this.data.isShowTip = !bool; 153 this.setSwiperList(); 154 this.setData(this.data); 155 if(this.data.maxError+1 == this.data.answers.error){ 156 wx.showModal({ 157 title:'提示', 158 content: `您已答错了${this.data.answers.error}题,成绩不合格,是否继续答题?` , 159 showCancel:true, 160 cancelText:'继续答题', 161 cancelColor:'#00bcd5', 162 confirmText:'交卷', 163 confirmColor:'#00bcd5', 164 success: function(res) { 165 if (res.confirm) { 166 that.setSubmit(); 167 }else{ 168 if(that.data.answers.activeNum + 1 < that.data.answers.allLists.length){ 169 setTimeout(() => that.onSwiper('left'),200); 170 } 171 } 172 } 173 }); 174 }else if(that.data.answers.activeNum + 1 < that.data.answers.allLists.length){ 175 setTimeout(() => that.onSwiper('left'),200); 176 } 177 if(this.data.answers.activeNum + 1 == that.data.answers.allLists.length){ 178 this.submitTip(); 179 } 180 }, 181 //页码切换列表效果 182 pageClick:function(){ 183 var layerAnimation = wx.createAnimation({ 184 transformOrigin: "50% 50%", 185 duration: 500, 186 timingFunction: "ease", 187 delay: 0 188 }); 189 if(!this.data.layerlayer.isLayerShow){ 190 layerAnimation.translate3d(0,0,0).step(); 191 }else{ 192 layerAnimation.translate3d(0,'100%',0).step(); 193 } 194 this.data.layerlayer.isLayerShow = !this.data.layerlayer.isLayerShow; 195 this.data.layerlayer.layerAnimation = layerAnimation; 196 this.setData(this.data); 197 }, 198 //页码切换列表收缩 199 layerFooterClick:function(){ 200 var layerAnimation = wx.createAnimation({ 201 transformOrigin: "50% 50%", 202 duration: 500, 203 timingFunction: "ease", 204 delay: 0 205 }); 206 layerAnimation.translate3d(0,'100%',0).step(); 207 this.data.layerlayer.isLayerShow = false; 208 this.data.layerlayer.layerAnimation = layerAnimation; 209 this.setData(this.data); 210 }, 211 //题号变更逻辑 212 setActiveNum:function(e){ 213 var thisOption=e.currentTarget.dataset.option - 0; 214 this.data.answers.activeNum = thisOption; 215 this.data.answers.showActiveNum = thisOption; 216 this.data.isLoading = false; 217 this.layerFooterClick(); 218 this.getSubject(); 219 }, 220 //swiper切换 221 setEvent:function(e){ 222 console.log('swiper切换') 223 this.data.swiper.touchstartEvent = e; 224 return false; 225 }, 226 //滑动结束 227 touchEnd:function(e){ 228 console.log('滑动结束') 229 this.onSwiper(this.getDirection(this.data.swiper.touchstartEvent,e)); 230 return false; 231 }, 232 //swiper切换 233 onSwiper:function(dire){ 234 console.log('swiper切换1') 235 var that = this, 236 active = 0, 237 storeSetTime, 238 animationO = wx.createAnimation({ 239 transformOrigin: "50% 50%", 240 duration: 200, 241 timingFunction: "linear", 242 delay: 0 243 }), 244 animationT = wx.createAnimation({ 245 transformOrigin: "50% 50%", 246 duration: 200, 247 timingFunction: "linear", 248 delay: 0 249 }), 250 animationS = wx.createAnimation({ 251 transformOrigin: "50% 50%", 252 duration: 200, 253 timingFunction: "linear", 254 delay: 0 255 }); 256 257 if(!this.$isLock){//锁屏控制 258 259 this.$isLock = true; 260 261 if(dire == 'bottom' || dire == 'top' || !dire){ 262 this.$isLock = false; 263 return false; 264 } 265 266 if(this.data.answers.activeNum >= this.data.answers.allLists.length - 1 && dire == 'left'){ 267 this.$isLock = false; 268 return false; 269 } 270 271 if(this.data.answers.activeNum <= 0 && dire == 'right'){ 272 this.$isLock = false; 273 return false; 274 } 275 276 if(dire == 'right'){ 277 animationO.translate3d('0',0,0).step(); 278 animationT.translate3d('100%',0,0).step(); 279 if(this.data.answers.activeNum > this.data.answers.start){ 280 active = - 1; 281 }else{ 282 this.$isLock = false; 283 return; 284 } 285 } 286 if(dire == 'left'){ 287 animationT.translate3d('-100%',0,0).step(); 288 animationS.translate3d('0',0,0).step(); 289 if(this.data.answers.activeNum < this.data.answers.end){ 290 active = 1; 291 }else{ 292 this.$isLock = false; 293 return; 294 } 295 } 296 this.data.swiper.animationO = animationO.export(); 297 this.data.swiper.animationT = animationT.export(); 298 this.data.swiper.animationS = animationS.export(); 299 this.data.answers.showActiveNum = this.data.answers.activeNum + active; 300 301 this.setData(this.data); 302 303 setTimeout(function(){ 304 that.setHtmlsetHtml(active); 305 },200); 306 } 307 }, 308 //修改页面至正常位置 309 setHtmlsetHtml:function(active){ 310 console.log('修改页面至正常位置') 311 var animationO = wx.createAnimation({ 312 transformOrigin: "50% 50%", 313 duration: 0, 314 delay: 0 315 }), 316 animationT = wx.createAnimation({ 317 transformOrigin: "50% 50%", 318 duration: 0, 319 delay: 0 320 }), 321 animationS = wx.createAnimation({ 322 transformOrigin: "50% 50%", 323 duration: 0, 324 delay: 0 325 }); 326 animationO.translate3d('-100%',0,0).step(); 327 animationT.translate3d('0',0,0).step(); 328 animationS.translate3d('100%',0,0).step(); 329 console.log('active',this.data.swiper.active, active) 330 this.data.swiper.active = this.data.swiper.active + active; 331 this.data.answers.activeNum = this.data.answers.activeNum + active; 332 this.data.answers.showActiveNum = this.data.answers.activeNum; 333 this.data.swiper.animationO = animationO; 334 this.data.swiper.animationT = animationT; 335 this.data.swiper.animationS = animationS; 336 this.setSwiperList(); 337 this.setData(this.data); 338 //调用加载数据方法 339 if( (this.data.swiper.active == 2 && this.data.answers.start > 0) || (this.data.swiper.active+2 == this.data.answers.list.length && this.data.answers.end+1 < this.data.answers.allLists.length)){ 340 this.getSubject(); 341 } 342 //调用滑动结束回调 343 if(this.isLockCall && typeof this.isLockCall == 'function'){ 344 this.isLockCall(); 345 this.isLockCall = false; 346 } 347 this.$isLock = false; 348 }, 349 //获得手势方向 350 getDirection:function(startEvent,endEvent){ 351 console.log('获得手势方向') 352 var x = endEvent.changedTouches[0].clientX - startEvent.changedTouches[0].clientX, 353 y = endEvent.changedTouches[0].clientY - startEvent.changedTouches[0].clientY, 354 pi=360*Math.atan(y/x)/(2*Math.PI); 355 if(pi<25 && pi>-25 && x>0 && Math.abs(x) > 10){ 356 return 'right'; 357 } 358 if(pi<25 && pi>-25 && x<0 && Math.abs(x) > 10){ 359 return 'left'; 360 } 361 if((pi<-75 || pi>750) && y>0 && Math.abs(y) > 10){ 362 return 'bottom'; 363 } 364 if((pi<-75 || pi>75) && y<0 && Math.abs(y) > 10){ 365 return 'top'; 366 } 367 }, 368 //切换题目逻辑 369 getSubject:function(callBack){ 370 console.log('切换题目逻辑') 371 console.log('0', this.data.answers.allLists) 372 var that=this,start = this.data.answers.activeNum - this.data.answers.onceLoadLength,end = this.data.answers.activeNum + this.data.answers.onceLoadLength,params; 373 console.log(start, end, this.data.answers.activeNum, this.data.answers.onceLoadLength) 374 start = start > 0 ? start : 0 ; 375 end = end+1 >= this.data.answers.allLists.length ? this.data.answers.allLists.length : end ; 376 //存放下次展示allallList数据 377 params = this.data.answers.allLists.slice(start,end+1); 378 //存放展示allallList数据ID 379 params = params.map(function(data){ 380 //后台需要int型 381 return data.id-0 382 }); 383 // console.log(this.data.answers.allLists) 384 // console.log(params) 385 https.find(this.data.answers.url,{questionID:params,subject:this.data.subject},{ 386 isNewExam:this.data.isShowNewExam && this.data.isNewExam 387 }) 388 .then(d => { 389 console.log(d) 390 //注册滑动结束回调 391 if(this.$isLock){ 392 this.isLockCall = ((d) => { 393 return this.callBackGetSubject(d,start,end); 394 })(d) 395 }else{ 396 this.callBackGetSubject(d,start,end); 397 } 398 if(typeof callBack == 'function'){ 399 callBack(); 400 } 401 }) 402 .catch(e => { 403 this.callBackError(e.message); 404 }) 405 }, 406 //详情数据加载的回调 407 callBackGetSubject:function(d,start,end){ 408 //数据 开始 结束 409 console.log('详情数据加载的回调') 410 console.log('1',d.data) 411 console.log('0', this.data.answers.allLists) 412 d.data.forEach((data,i) => { 413 this.data.answers.allLists[start+ i] = Object.assign({},data,this.data.answers.allLists[start + i]); 414 }) 415 console.log('1',this.data.answers.allLists) 416 this.data.answers.list = d.data; 417 this.data.isLoading = true; 418 this.data.answers.list = d.data; 419 this.data.answers.start = start; 420 this.data.answers.end = end; 421 this.data.swiper.active = this.data.answers.activeNum-this.data.answers.start; 422 this.setSwiperList(); 423 this.setData(this.data); 424 // console.log('当前',this.data.answers.activeNum,'开始',this.data.answers.start,'结束',this.data.answers.end,'' this.data.swiper.active) 425 }, 426 //错误的回调 427 callBackError:function(e){ 428 wx.showModal({ 429 title: '错误', 430 content: '错误提示是:'+ e , 431 showCancel:false, 432 confirmText:'确认关闭', 433 success: function(res) { 434 // if (res.confirm) { 435 // console.log('用户点击确定') 436 // } 437 } 438 }) 439 }, 440 //交卷 441 submitTip:function(){ 442 const that = this; 443 if(this.data.answers.allLists.length > this.data.answers.error + this.data.answers.success){ 444 wx.showModal({ 445 title:'提示', 446 content: `您已经回答了${this.data.answers.error + this.data.answers.success}题,还有${this.data.answers.allLists.length - this.data.answers.error - this.data.answers.success}题未答,确定要交卷吗?` , 447 showCancel:true, 448 cancelText:'继续答题', 449 cancelColor:'#00bcd5', 450 confirmText:'交卷', 451 confirmColor:'#00bcd5', 452 success: function(res) { 453 if (res.confirm) { 454 that.setSubmit(); 455 } 456 } 457 }); 458 }else{ 459 wx.showModal({ 460 title:'提示', 461 content: '已经是最后一道题了了,交卷后可立即查看成绩', 462 showCancel:false, 463 confirmText:'知道了', 464 confirmColor:'#00bcd5', 465 success: function(res) { 466 that.setSubmit(); 467 } 468 }) 469 } 470 }, 471 //提交函数 472 setSubmit:function(){ 473 var record = this.data.answers.allLists.map((option,i) => { 474 return { 475 id:option.id, 476 answer:option.isAnswer, 477 choose:option.answered || 0 478 }; 479 }); 480 https.setExamInfo(this.data.answerUrl,{ 481 subject:this.data.subject, 482 type:this.data.type,record, 483 subject:this.data.subject, 484 useTime:this.params.seconds, 485 city:app.globalData.getLocation 486 },{} 487 ) 488 .then((data) =>{ 489 if(data.data.status == 1){ 490 wx.redirectTo({ 491 url: `../../pages/answer_mark/mark?subject=${this.data.subject}&type=${this.data.type}&time=${this.params.seconds}&mark=${data.data.data.score}&mid=${data.data.data.mid}&error=${this.data.answers.error}` 492 }) 493 } 494 }) 495 }, 496 //计时 497 setTime:function(){ 498 let that = this,seconds = Math.floor((new Date().getTime() - this.data.startTime)/1000),minutes = 0; 499 this.params.seconds = seconds; 500 if(seconds >= this.data.hourLong){ 501 this.params.seconds = this.data.hourLong; 502 this.data.time = '00:00'; 503 this.setData(this.data); 504 wx.showModal({ 505 title:'提示', 506 content: '考试时间已到,交卷后可立即查看成绩', 507 showCancel:false, 508 confirmText:'知道了', 509 confirmColor:'#00bcd5', 510 success: function(res) { 511 that.setSubmit(); 512 } 513 }) 514 }else{ 515 seconds = this.data.hourLong - seconds; 516 minutes = Math.floor(seconds/60); 517 seconds = seconds%60; 518 this.data.time = `${minutes > 9 ? minutes: '0' + minutes}:${seconds > 9 ? seconds: '0' + seconds}`; 519 this.setData(this.data); 520 this.swiperTime = setTimeout(()=>{ 521 this.setTime(); 522 },1000); 523 } 524 525 }, 526 setSwiperList(){ 527 console.log('setSwiperList') 528 var oldStar = this.data.answers.activeNum-1, 529 oldEnd = this.data.answers.activeNum+1, 530 star = oldStar >= 0 ? oldStar : 0 , 531 end = oldEnd <= this.data.answers.allLists.length ? oldEnd : this.data.answers.allLists.length; 532 this.data.swiper.list = this.data.answers.allLists.slice(star,end+1); 533 534 console.log('swiper', this.data.swiper.list) 535 536 if(oldStar < 0 ){ 537 this.data.swiper.list.unshift({}); 538 } 539 if(oldEnd > this.data.answers.allLists.length){ 540 this.data.swiper.list.push({}); 541 } 542 }, 543 onLoad (params) { 544 var that = this; 545 this.data.subject = params.subject; 546 this.data.type = params.type; 547 if(params.subject == 'kemu3'){ 548 this.data.maxError = 5; 549 } 550 https.initialize(this.data.answers.onLoadUrl,{subject:params.subject,type:params.type},{ 551 isNewExam:this.data.isShowNewExam && this.data.isNewExam, 552 isShowNewExam:this.data.isShowNewExam 553 }) 554 .then(d => { 555 console.log('d',d) 556 this.data.answers.allLists = d.data; 557 this.data.answers.success = d.success; 558 this.data.answers.error = d.error; 559 this.data.answers.loading = false; 560 this.setData(this.data); 561 this.getSubject(() => { 562 this.data.startTime = new Date().getTime(); 563 this.setTime(); 564 }); 565 }) 566 .catch(e => { 567 this.callBackError(e.message); 568 // this.setData({ subtitle: '获取数据异常', movies: [], loading: false }) 569 // console.error(e) 570 }); 571 }, 572 onHide(){ 573 clearInterval(this.swiperTime); 574 }, 575 onUnload(){//页面卸载 576 clearInterval(this.swiperTime); 577 } 578 });