Index JS

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

const db = wx.cloud.

database()
const QDB = db.collection('question')
const RankDB = db.collection('rank')
Page({
data: {
question: "",
op0:'',
op1:'',
op2:'',
op3:'',
op4:'',
right:0,
nickName: "test",
questionDB:[],
nowQuestion:0
},

jumpPage() {
wx.navigateTo({
url: `/pages/rank/index?`
});
},
start(){
let that = this
QDB.get().then((res)=>{
that.setData({
questionDB: res.data
})
that.showQuestion()
})
},
showQuestion(){
console.log('刷新题目')
this.setData({
question: this.data.questionDB[this.data.nowQuestion].question,
op0:this.data.questionDB[this.data.nowQuestion].op[0],
op1:this.data.questionDB[this.data.nowQuestion].op[1],
op2:this.data.questionDB[this.data.nowQuestion].op[2],
op3:this.data.questionDB[this.data.nowQuestion].op[3]
})
},
choice(e){
let that = this
let num = e.currentTarget.dataset['index'] - 0
if(num==this.data.questionDB[this.data.nowQuestion].answer){
console.log('答对了');
this.data.right++
this.setData({
right:this.data.right
})
}
this.data.nowQuestion++
this.setData({
nowQuestion:this.data.nowQuestion
})
console.log(this.data.nowQuestion)
if ( this.data.nowQuestion< this.data.questionDB.length) {
this.showQuestion()
}else{
console.log('提交 rank 记录')
this.setData({
question: "",
op0:'',
op1:'',
op2:'',
op3:''
})
RankDB.add({
data:{
nickName: that.data.nickName,
score:that.data.right
}}).then(res => {
console.log(res)
}) .catch(console.error)
}
}
});

You might also like