
// pages/index3/index.js
var app = getApp()
// common.js
// var localData = require('../../data/question.js')
// es6 import
// import {localData} from '../../data/question'
// es6 require
let {
localData
} = require('../../data/question')
Page({
/**
* 页面的初始数据
*/
data: {
show: false,
duration: 300,
position: 'right',
round: false,
overlay: true,
customStyle: '',
overlayStyle: '',
percent: 0,
current: 1,
total: 1,
subject: null,
isSelect: false,
dataList: '',
answer: [{
id: "01",
value: "A"
}, {
id: "02",
value: "B"
}, {
id: "03",
value: "C"
}, {
id: "04",
value: "D"
}],
select: '',
userSelect: new Array(),
totalScore: -1, //用户总得分
userScore: 0, // 用户答对了几道题
totalError: 0 // 用户打错了几道题
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
// console.log("localdata1",localData)
let num = this.data.current
this.setData({
total: localData.title.length,
subject: localData.title[num - 1]
})
this.setData({
percent: (num - 1 / this.data.total * 100).toFixed(2)
})
console.log('onLoad', this.data.dataList);
},
// 用户选择答案
radioChange(e) {
console.log('radio发生change事件,携带value值为:', e)
let num = this.data.current
let id = e.currentTarget.dataset.index
let select = e.detail.value
let userSelect = this.data.userSelect
console.log(userSelect)
userSelect.splice(num - 1, 1, {
...this.data.subject,
userSelect: select
})
this.setData({
select: select,
userSelect: userSelect
})
console.log(this.data.userSelect)
},
// 提交答案并切换到下一题
// 提交答案
submit() {
// console.log(this.data.select)
if (!this.data.select) {
wx.showToast({
icon: 'none',
title: '您还没有选择呢',
})
return
}
let score = this.getScore(this.data.answer, this.data.userSelect)
// wx.showToast({
// title: '您的得分为'+ score + '.',
// })
this.setData({
totalScore: score,
totalError: this.data.total - score
})
// if (this.data.select=== this.data.answer ) {
// console.log("true")
// wx.showToast({
// title: "您答对了"
// })
// } else {
// wx.showToast({
// icon:"none",
// title: 'title',
// })
// }
let num = this.data.current
// 方式一 顺序答题
if (num >= localData.title.length) {
this.setData({
percent: ((num) / this.data.total * 100).toFixed(2)
})
wx.showToast({
title: '已经是最后一道题了',
})
return
}
// 方式二 循环答题
// if (num>=localData.title.length) {
// num = 0
// }
this.setData({
subject: localData.title[num],
current: num + 1,
isSelect: false,
select: '',
percent: ((num) / this.data.total * 100).toFixed(2)
})
},
// 判断是否正确答案,实际上应该是在后端判断
getScore(answer = [], userSelect = []) {
console.log(answer, userSelect)
// if (!userSelect.length || answer.length!== userSelect.length) {
// wx.showToast({
// icon: 'warn',
// title: '您还没有做完',
// })
// return false
// }
let score = 0
userSelect.map((element, index) => {
let result = answer.find(item => element !== undefined && item.id === element._id && item.value == element.userSelect)
console.log(result)
if (result) {
score += 1
element.answer = answer[index].value
} else {
if (element) {
element.answer = answer[index].value
}
}
})
return score
},
// 点击查看答错的题,方便查缺补漏
seeError() {
// 方式一 把错题存入本地缓存
wx.setStorageSync('errorQuestion', this.data.userSelect)
// 方式二 把错题存为全局变量,但刷新页面会丢失
app.globalData.globalErrorOptions = this.data.userSelect
wx.navigateTo({
url: '/pages/errorQuestion/errorQuestion',
})
},
selectSubject(e) {
console.log(e, e.currentTarget.dataset.index)
let num = e.currentTarget.dataset.index
this.setData({
subject: localData.title[num],
current: num + 1,
isSelect: false
})
},
popup(e) {
const position = "right"
let customStyle = ''
let duration = this.data.duration
switch (position) {
case 'top':
case 'bottom':
customStyle = 'height: 30%;'
break
case 'right':
customStyle = "width:40%;"
break
}
this.setData({
position,
show: true,
customStyle,
duration
})
},
exit() {
this.setData({
show: false
})
// wx.navigateBack()
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
wx.setStorageSync('titlename', "连续答题-单选1")
let title = wx.getStorageSync('titlename')
wx.setNavigationBarTitle({
title: title,
})
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})

// pages/errorQuestion/errorQuestion.js
var app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
errorQuestionList: [],
subject: null,
total: 0,
current: 1,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
let title = "错题集"
wx.setNavigationBarTitle({
title: title,
})
let errorQuestionList = wx.getStorageSync('errorQuestion')
// let errorQuestion = app.globalData.globalErrorOptions
console.log(errorQuestionList)
this.setData({
errorQuestionList: errorQuestionList,
total: errorQuestionList.length,
subject: errorQuestionList.length > 0 ? errorQuestionList[0] : null
})
this.setData({
percent: ((this.data.current) / this.data.total * 100).toFixed(2),
})
},
// 下一道错题
next(e) {
console.log(e.currentTarget.dataset.id)
let currentNum = this.data.current
if (currentNum === 1 && e.currentTarget.dataset.id < 0) {
wx.showToast({
icon: "error",
title: '已经是第一个了',
})
return
}
if (currentNum === this.data.errorQuestionList.length && e.currentTarget.dataset.id > 0) {
wx.showToast({
icon: "error",
title: '已经是最后一个了',
})
return
}
if (currentNum <= this.data.errorQuestionList.length) {
currentNum += +e.currentTarget.dataset.id
}
console.log(currentNum)
this.setData({
subject: this.data.errorQuestionList[currentNum - 1],
current: currentNum,
percent: ((currentNum) / this.data.total * 100).toFixed(2)
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})