105 lines
2.5 KiB
JavaScript
105 lines
2.5 KiB
JavaScript
const api = require("../../utils/api");
|
|
|
|
const GRADES = ["一年级", "二年级", "三年级", "四年级", "五年级", "六年级"];
|
|
|
|
Page({
|
|
data: {
|
|
list: [],
|
|
nickname: "",
|
|
gradeIndex: 1,
|
|
grades: GRADES,
|
|
math: true,
|
|
chinese: true,
|
|
saving: false,
|
|
},
|
|
|
|
onShow() {
|
|
if (!api.requireLogin()) return;
|
|
this.load();
|
|
},
|
|
|
|
load() {
|
|
api
|
|
.get("/api/children")
|
|
.then((d) => {
|
|
this.setData({ list: d.items || [] });
|
|
if (!d.items || !d.items.length) {
|
|
// 登录成功但还没有档案、也没做过同意 → 先去同意页
|
|
api
|
|
.get("/api/consent")
|
|
.then((c) => {
|
|
if (!c.consented) wx.reLaunch({ url: "/pages/consent/consent" });
|
|
})
|
|
.catch(() => {});
|
|
}
|
|
})
|
|
.catch(api.toastError);
|
|
},
|
|
|
|
onNick(e) {
|
|
this.setData({ nickname: e.detail.value });
|
|
},
|
|
|
|
onGrade(e) {
|
|
this.setData({ gradeIndex: Number(e.detail.value) });
|
|
},
|
|
|
|
toggleSubject(e) {
|
|
const k = e.currentTarget.dataset.k;
|
|
const next = {};
|
|
next[k] = !this.data[k];
|
|
this.setData(next);
|
|
},
|
|
|
|
onSave() {
|
|
const nickname = (this.data.nickname || "").trim();
|
|
if (!nickname) {
|
|
wx.showToast({ title: "给他起个昵称(不用真名)", icon: "none" });
|
|
return;
|
|
}
|
|
if (!this.data.math && !this.data.chinese) {
|
|
wx.showToast({ title: "至少开一科", icon: "none" });
|
|
return;
|
|
}
|
|
if (this.data.saving) return;
|
|
this.setData({ saving: true });
|
|
|
|
const subjects = [];
|
|
if (this.data.math) subjects.push("math");
|
|
if (this.data.chinese) subjects.push("chinese");
|
|
|
|
api
|
|
.post("/api/children", { nickname, grade: this.data.gradeIndex + 1, subjects })
|
|
.then((child) => {
|
|
getApp().setChild(child);
|
|
wx.reLaunch({ url: "/pages/home/home" });
|
|
})
|
|
.catch((e) => {
|
|
api.toastError(e);
|
|
this.setData({ saving: false });
|
|
});
|
|
},
|
|
|
|
onPick(e) {
|
|
const child = this.data.list[Number(e.currentTarget.dataset.i)];
|
|
getApp().setChild(child);
|
|
wx.reLaunch({ url: "/pages/home/home" });
|
|
},
|
|
|
|
onDemo() {
|
|
wx.showLoading({ title: "装演示数据…" });
|
|
api
|
|
.post("/api/dev/demo-data", {})
|
|
.then((d) => {
|
|
getApp().setChild(d.child);
|
|
wx.hideLoading();
|
|
wx.showToast({ title: "已装好 " + d.mistakes + " 道", icon: "success" });
|
|
setTimeout(() => wx.reLaunch({ url: "/pages/home/home" }), 700);
|
|
})
|
|
.catch((e) => {
|
|
wx.hideLoading();
|
|
api.toastError(e);
|
|
});
|
|
},
|
|
});
|