Files
cuotibiye/miniprogram/_legacy/quiz/quiz.js
T

113 lines
2.8 KiB
JavaScript

const api = require("../../utils/api");
Page({
data: {
loading: true,
items: [],
index: 0,
item: null,
total: 0,
answer: "",
submitting: false,
result: null,
},
onLoad(query) {
if (!api.requireChild()) return;
this.setData({ index: Number(query.i || 0) });
this.load();
},
load() {
const child = getApp().globalData.child;
api
.get("/api/today?child_id=" + child.id)
.then((d) => {
const items = d.items || [];
const index = Math.min(this.data.index, Math.max(items.length - 1, 0));
this.setData({
items,
index,
total: items.length,
item: items[index] || null,
loading: false,
answer: "",
result: null,
});
})
.catch((e) => {
api.toastError(e);
this.setData({ loading: false });
});
},
onInput(e) {
this.setData({ answer: e.detail.value });
},
onSubmit() {
const { item, answer, submitting } = this.data;
if (!item || submitting) return;
if (!(answer || "").trim()) {
wx.showToast({ title: "先写个答案", icon: "none" });
return;
}
this.setData({ submitting: true });
api
.post("/api/mistakes/" + item.id + "/attempt", {
child_id: getApp().globalData.child.id,
answer: answer.trim(),
})
.then((r) => {
this.setData({ result: r, submitting: false });
})
.catch((e) => {
api.toastError(e);
this.setData({ submitting: false });
});
},
onNext() {
const next = this.data.index + 1;
if (next >= this.data.total) {
wx.showModal({
title: "今天的 5 道做完了",
content: "不再追加。明天见。",
showCancel: false,
success: () => wx.reLaunch({ url: "/pages/home/home" }),
});
return;
}
const items = this.data.items;
this.setData({
index: next,
item: items[next],
answer: "",
result: null,
});
},
/** 没标准答案的题(拍照切出来的应用题/语文题):孩子自己判。 */
onSelfJudge(e) {
const { item, submitting } = this.data;
if (!item || submitting) return;
const correct = e.currentTarget.dataset.ok === "1";
this.setData({ submitting: true });
api
.post("/api/mistakes/" + item.id + "/self-judge", {
child_id: getApp().globalData.child.id,
correct,
})
.then((r) => this.setData({ result: r, submitting: false }))
.catch((err) => {
api.toastError(err);
this.setData({ submitting: false });
});
},
onExplainNow() {
const mid = this.data.result && this.data.result.mistake_id;
wx.redirectTo({ url: "/pages/explain/explain?mid=" + (mid || this.data.item.id) });
},
});