You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.5 KiB

// 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});
if (!res.data.exercise_questions){
if(res.data.status==401)
wx.showToast({
title: '请登陆后重试',
icon: "none"
})
//console.log("require_login");
return;
}
let questions = res.data.exercise_questions;
for(var i=0;i<questions.length;i++){
if (questions[i]["question_type"]==3){
let null_inputs=[];
for (var j = 1; j<= questions[i]["multi_count"];j++){
var user_answer = questions[i]["user_answer"].filter(answer=>answer.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, loading: false});
console.log(this.data);
}).catch(error => {
console.error(error);
wx.showToast({
title: error.toString(),
icon: "none",
duration: 5000
})
});
},
answer_null_question: function ({ detail: { value }, currentTarget: { dataset } }){
console.log("answer_main_question");
console.log(value);
console.log(dataset);
app.client.answer_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_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);
let exercise_choice_id;
if(Array.isArray(value)){
exercise_choice_id = [];
for(var i of value){
exercise_choice_id.push(parseInt(i));
}
console.log(exercise_choice_id);
}else{
exercise_choice_id = parseInt(value);
console.log(exercise_choice_id);
}
app.client.answer_question({ question_id: dataset.question_id, exercise_choice_id})
.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: '请稍候',
});
}
console.log("保存答案中");
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.course_name = options.exercise_name;//todo finish
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
if(this.data.loading){
this.pull_questions();
}
},
onError: function(e){
console.error(e);
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
this.save_exercise({show_loading: false});
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
this.save_exercise({ show_loading: false });
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})