const api = require("../../utils/api"); Page({ data: { guardian: null, child: null, wx: null, quota: 0, ent: null, entState: { text: "—", hint: "", active: false, cls: "" }, deletion: null, code: "", }, onShow() { if (!api.requireLogin()) return; const app = getApp(); this.setData({ guardian: wx.getStorageSync("ql_guardian") || null, child: app.globalData.child, }); this.load(); }, load() { Promise.all([ api.get("/api/wx/config").catch(() => null), api.get("/api/notify/quota").catch(() => null), api.get("/api/entitlement").catch(() => null), api.get("/api/me").catch(() => null), ]).then(([wxcfg, q, e, me]) => { this.setData({ wx: wxcfg, quota: q ? q.quota : 0, ent: e || null, entState: this.entState(e), deletion: (me && me.deletion) || null, }); }); }, /** 开通四态:试用中 / 有效 / 已过期 / 没用过 —— 每种都要说清「还能做什么」 */ entState(e) { const left = e && typeof e.days_left === "number" ? e.days_left : null; if (!e || !e.kind) { return { text: "未开通", hint: "先免费用 3 天。到期后已入库的错题仍然能复习,只是不能拍新卷子。", active: false, cls: "" }; } if (left !== null && left <= 0) { return { text: "已过期", hint: "已入库的错题还能继续做,拍不了新卷子。兑换激活码即可继续。", active: false, cls: "red", }; } const kind = e.kind_zh || e.kind; const day = String(e.expires_at || "").slice(0, 10); return { text: `${kind} · ${day} 到期${left !== null ? " · 剩 " + left + " 天" : ""}`, hint: "有效期内不限题数、不限孩子个数(2 个)。", active: true, cls: "green", }; }, onSubscribe() { const cfg = this.data.wx || {}; const tmpl = cfg.daily_template; if (!tmpl) { wx.showModal({ title: "还差模板 ID", content: "订阅消息模板要在微信公众平台「功能 → 订阅消息」里建,然后把 ID 配到后端环境变量 WX_TMPL_DAILY,这里才能拉起授权弹窗。", showCancel: false, }); return; } wx.requestSubscribeMessage({ tmplIds: [tmpl], success: (res) => { const ids = Object.keys(res).filter((k) => res[k] === "accept"); if (!ids.length) { wx.showToast({ title: "没同意,明天不会提醒", icon: "none" }); return; } api .post("/api/notify/subscribe", { template_ids: ids }) .then(() => { wx.showToast({ title: "已记下一条额度", icon: "success" }); this.load(); }) .catch(api.toastError); }, fail: () => wx.showToast({ title: "授权没成功", icon: "none" }), }); }, onCode(e) { this.setData({ code: e.detail.value }); }, onRedeem() { const code = (this.data.code || "").trim(); if (!code) return; api .post("/api/entitlement/redeem", { code }) .then((r) => { wx.showToast({ title: "已开通 " + (r.days || 0) + " 天", icon: "success" }); this.setData({ code: "" }); this.load(); }) .catch(api.toastError); }, onSwitch() { wx.navigateTo({ url: "/pages/children/children" }); }, onWorkbench() { wx.navigateTo({ url: "/pages/wb/wb" }); }, /** 删号:进 7 天冷静期,期间随时能撤销 */ onDelete() { wx.showModal({ title: "删除账号?", content: "会删掉:孩子档案、全部题目与错题、作答记录、周报。\n\n不是你点了就删——先进入 7 天冷静期,这 7 天里重新登录点「撤销删号」就能全部恢复。7 天后才真删,且不可恢复。", confirmText: "申请删号", confirmColor: "#C8362D", success: (r) => { if (!r.confirm) return; api .del("/api/me", { reason: "家长主动删除" }) .then((res) => { wx.showModal({ title: "已提交", content: `${res.purge_after} 之后删除,这之前随时可以撤销。`, showCancel: false, success: () => { getApp().clearAuth(); wx.reLaunch({ url: "/pages/login/login" }); }, }); }) .catch(api.toastError); }, }); }, onCancelDelete() { api .post("/api/me/cancel-deletion", {}) .then(() => { wx.showToast({ title: "已撤销", icon: "success" }); this.load(); }) .catch(api.toastError); }, onComputer() { wx.navigateTo({ url: "/pages/scan/scan" }); }, onLogout() { wx.showModal({ title: "退出登录", content: "只是退出这台设备,数据还在。", success: (r) => { if (!r.confirm) return; api.post("/api/auth/logout", {}).catch(() => {}); getApp().clearAuth(); wx.reLaunch({ url: "/pages/login/login" }); }, }); }, });