// pages/exercise/exercise.js const app = getApp(); Page({ /** * 页面的初始数据 */ data: { exercise_questions: [], loading: true, exercise: {} }, pull_questions: function(){ app.client.start_answer_exercise({exercise_id: this.exercise_id}) .then(res=>{ console.log("pull questions"); console.log(res); this.setData({ exercise: res.data.exercise}); let questions = res.data.exercise_questions; for(var i=0;ianswer.choice_id==j)[0]||{}; console.log(user_answer); null_inputs.push({choice_id:j, answer_text:user_answer.answer_text||""}); console.log(null_inputs); } questions[i]["null_inputs"] = null_inputs; console.log(questions); } } this.setData({ exercise_questions: questions}); console.log(this.data); }).catch(console.error) }, answer_null_question: function ({ detail: { value }, currentTarget: { dataset } }){ console.log("answer_main_question"); console.log(value); console.log(dataset); app.client.answer_exercise_question({ question_id: dataset.question_id, exercise_choice_id:dataset.exercise_choice_id, answer_text: value }) .then(res => { console.log("answer_main_question"); console.log(res); }) .catch(error => { console.error(error); wx.showToast({ title: error.toString(), icon: "none" }) }); }, answer_main_question: function ({ detail: { value }, currentTarget: { dataset }}){ console.log("answer_main_question"); console.log(value); console.log(dataset); app.client.answer_exercise_question({ question_id: dataset.question_id, answer_text: value }) .then(res => { console.log("answer_main_question"); console.log(res); }) .catch(error => { console.error(error); wx.showToast({ title: error.toString(), icon: "none" }) }); }, answer_choice_question: function({detail: {value}, currentTarget: {dataset}}){ console.log("answer_question"); console.log(value); console.log(dataset); app.client.answer_exercise_question({ question_id: dataset.question_id, exercise_choice_id: value}) .then(res=>{console.log("answer_question");console.log(res);}) .catch(error=>{ console.error(error); wx.showToast({ title: error.toString(), icon: "none" }) }); }, save_exercise: function({show_loading=true}={}){ if(show_loading){ wx.showLoading({ title: '请稍候', }); } app.client.save_exercise({exercise_id: this.exercise_id, complete: wx.hideLoading}) .then(res=>{ console.log("保存答案"); console.log(res); if(show_loading){ wx.showToast({ title: "保存成功", }); } }).catch(console.error) }, commit_exercise: function(){ wx.showModal({ title: '确认', content: '交卷后不可更改,确定交卷吗?', success: res=>{ if(res.confirm){ app.client.save_exercise({ exercise_id: this.exercise_id}) .then( ()=>{ app.client.commit_exercise({exercise_id: this.exercise_id}) .then(res=>{ console.log("交卷"); console.log(res); wx.navigateBack({ delta:1 }) wx.showToast({ title: res.data.message }) }).catch(console.error); }).catch(console.error); } } }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.exercise_id = options.exercise_id; this.pull_questions(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, onError: function(e){ console.error(e); }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { this.save_exercise({show_loading: false}); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })