123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- // pages/contest/answer/answer.js
- import {
- answer
- } from '../../../api/api.js'
- let timer
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- pagination: {
- page: 1,
- rows: 10
- },
- show: false,
- pattern: false,
- isupdate: false,
- paperId: '',
- paperScore: 0,
- questionWrong: '',
- questionList: [],
- questionTotal: [],
- chooseList: [],
- time: 0,
- allAnswer: false,
- statusBar: 0
- },
- attendContest () {
- answer.attendContest({reqdata:{}},r => {
- let {paperId,questions} = r.object
- this.setData({
- paperId,
- questionTotal: this.shuffle(questions).map(item => {
- item.options = this.shuffle(item.options)
- return item
- })
- }, () => {
- timer = setInterval(() => {
- this.setData({
- time: ++this.data.time
- })
- }, 1000)
- this.setData({
- questionList: this.data.questionTotal.filter((item,index)=>index<10)
- })
- })
- })
- },
- goHome (e) {
- wx.showModal({
- title: '确定要离开吗?',
- content: '一旦离开,本次答题成绩不予记录!',
- confirmColor: '#ff0000',
- success (res) {
- if (res.confirm) {
- wx.reLaunch({
- url: '/pages/home/home',
- })
- } else if (res.cancel) {
- console.log('用户点击取消')
- }
- }
- })
- },
- showToggle(e) {
- this.setData({
- show: !this.data.show,
- allAnswer: this.data.chooseList.every(item => item || item === 0)
- })
- },
- patternToggle (e) {
- this.setData({
- pattern: !this.data.pattern
- })
- this.showToggle()
- this.bindNavTagger({currentTarget:{dataset:{index:1}}})
- },
- updateTagger (e) {
- this.setData({
- pattern: false,
- isupdate: true
- })
- this.bindNavTagger({currentTarget:{dataset:{index:1}}})
- },
- bindNavTagger(e) {
- let i = e.currentTarget.dataset.index
- this.setData({
- ['pagination.page']: i-0
- },()=>{
- if (wx.pageScrollTo) {
- wx.pageScrollTo({
- scrollTop: 0
- })
- } else {
- wx.showModal({
- title: '提示',
- content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
- })
- }
- this.setData({
- questionList: this.data.questionTotal.filter((item,index)=>index>=(i-1)*10&&index<i*10)
- })
- })
- },
- clickChoose (e) {
- let {questionindex,index} = e.currentTarget.dataset
- let {chooseList} = this.data
- chooseList[questionindex] = index-0
- this.setData({
- chooseList
- })
- },
- clickConfim (e) {
- let {paperId,chooseList,questionTotal,time} = this.data
- let paperScore = 0
- let questionWrong = []
- questionTotal.forEach((item,index) => {
- (chooseList[index]||chooseList[index]===0)&&item.options[chooseList[index]].optionAnswer
- ? paperScore += 2
- : questionWrong.push({[item.id]: (chooseList[index]||chooseList[index]===0)?item.options[chooseList[index]].optionId:''})
- })
- answer.submitUserAnswer({reqdata:{
- paperId,
- userId: wx.getStorageSync('id'),
- questionWrong: JSON.stringify(questionWrong),
- paperScore,
- paperDuration:time
- }},r => {
- let {rank,id} = r.object
- wx.navigateTo({
- url: `/pages/contest/score/score?rank=${rank}&id=${id}&score=${paperScore}`,
- })
- })
- },
- // 数组乱序
- shuffle(arr) {
- let length = arr.length,
- r = length,
- rand = 0;
- while (r) {
- rand = Math.floor(Math.random() * r--);
- [arr[r], arr[rand]] = [arr[rand], arr[r]];
- }
- return arr;
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- this.attendContest()
- let {chooseList} = this.data
- for (let index = 0; index < 50; index++) {
- chooseList[index] = null
- }
- this.setData({
- chooseList
- })
- wx.getSystemInfo({
- success: e => {
- this.setData({
- statusBar: e.statusBarHeight + 13
- })
- }
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- clearInterval(timer)
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|