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.

196 lines
4.9 KiB

import {throttle} from "../../../js/utils";
const app = getApp();
Page({
data: {
exercise_questions: [],
loading: true,
exercise: {},
count_down:'试卷不限时'
},
scrollToAnswerSheet(){
wx.pageScrollTo({
selector:".exercise-status"
})
},
scrollToQues(e){
let {target: {dataset:{ques_id}}} = e;
console.log(ques_id);
if(!ques_id) return;
wx.pageScrollTo({
selector:"#q-"+ques_id
});
},
onAnswer(e){
console.log(e);
let {detail:{q_position,answered}} = e;
let key = "question_status[" + (q_position - 1) +"].ques_status"
this.setData({[key]: answered});
this.refreshAnsweredCount();
},
startCountDown({left_time}){
var left = left_time;
var setCountDown = (time)=>{
let s = time%60;
let m = parseInt(time/60)%60;
let h = parseInt(time/3600);
if(s<10)
s = '0' +s;
if(m<10)
m = '0' + m;
if(h<10)
h = '0' + h;
let count_down = h + ":" + m + ":" + s;
this.setData({count_down});
}
setCountDown(--left);
var id = setInterval(()=>{
if(left<=1){
return this.stopCountDown(id);
}
setCountDown(--left);
},1000);
},
stopCountDown(id){
clearInterval(id);
var showToast = throttle(wx.showToast, 800, {});//延时Toast
showToast({
title: '时间已到,系统自动为你提交中',icon:"none"
})
app.api("exercises.commit_exercise")({exercise_id:this.exercise_id, commit_method:2})
.then(res=>{
showToast({
title: "试卷提交成功!"
});
//this.pull_questions();
}).catch(e=>{
//app.showError(e);
var title = '提交失败';
//if(e.code==-2)
// title = '您未作答任何题';
showToast({
title,
icon:"none"
});
let db = wx.cloud.database();
db.collection("data").add({
data: {
name: "exercises.commit_exercise",
e,
createdAt: db.serverDate()
}
});
})
this.setData({count_down: '考试时间已截止'});
},
refreshAnsweredCount(){
if(!this.data.question_status)
return;
let answered_count = 0;
this.data.question_status.map(i=>{
answered_count += i.ques_status;
});
this.setData({answered_count});
},
pull_questions: function(){
app.api("exercises.start_answer")({exercise_id: this.exercise_id})
.then(res=>{
console.log("pull questions");
this.setData(res);
this.setData({loading: false});
if(this.data.question_status)
this.refreshAnsweredCount();
if(res.exercise.left_time)
this.startCountDown({left_time: res.exercise.left_time});
}).catch(e => {
this.setData({status:e.code});
app.showError(e);
});
},
save_exercise: function({show_loading=true}={}){
if(show_loading){
wx.showLoading({
title: '请稍候',
});
}
console.log("保存答案中");
app.callApi({ name:"exercises.begin_commit", data:{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.showLoading({title: '检查作答情况中'});
app.api("exercises.begin_commit")({ exercise_id: this.exercise_id})
.then(resp=>{
let {question_undo, shixun_undo} = resp;
if(question_undo==0)
var content = "交卷后无法更改作答,是否确认?"
else {
var content = "您还有"+question_undo+"题未作答";
if(shixun_undo>0)
content += ",包含"+shixun_undo+"道实训题";
content +=",是否现在交卷呢?";
}
//wx.hideLoading();
wx.showModal({
title:"提示",
content,
success:res=>{
if(res.confirm){
app.api("exercises.commit_exercise")({exercise_id: this.exercise_id})
.then(res=>{
console.log("交卷");
console.log(res);
app.showMsg(res);
setTimeout(()=>{
wx.navigateBack({
delta:1
})
}, 800);
})
.catch(e=>{
app.showError(e);
console.error(e);
});
}
}
})
})
.catch(e=>{
app.showError(e);
console.error(e);
});
},
onLoad: function (options) {
this.exercise_id = options.exercise_id;
this.course_name = options.exercise_name;
this.pull_questions();
},
onShow: function () {
},
onHide: function () {
this.save_exercise({show_loading: false});
},
onUnload: function () {
this.save_exercise({ show_loading: false });
},
onPullDownRefresh: function () {
}
})