// 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 { (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 () { } })