const api = require("../../utils/api"); /** 估算一道题要多久:口算 30 秒、字词 40 秒、应用题/自评 60 秒,够用了。 */ function estimate(queue) { const items = (queue && queue.items) || []; const sec = items.reduce((s, q) => { if (q.judge_type === "calc") return s + 30; if (q.judge_type === "choice") return s + 40; return s + 60; }, 0); return Math.max(1, Math.round(sec / 60)); } /** 一块里有几道:口算卡一块十几道,靠 subs 判断。 */ function qtyOf(block) { const n = (block && block.subs && block.subs.length) || 0; return n > 1 ? n : 1; } Page({ data: { mode: "photo", subject: "math", subjects: [ { k: "math", name: "数学" }, { k: "chinese", name: "语文" }, ], // 拍照 → 切块 → 点选 photo: "", draft: "", blocks: [], pickedBlocks: 0, pickedQty: 0, detecting: false, cutInfo: "", // 逐条勾选(一块多道) pickItems: [], pickOpen: false, importing: false, // 手输 stem: "", answer: "", isChoice: false, optionsText: "", saving: false, // 题单预览 queue: { n: 0, minutes: 0 }, }, onLoad() { if (!api.requireChild()) return; }, onShow() { if (getApp().globalData.child) this.loadQueue(); }, onSubject(e) { this.setData({ subject: e.currentTarget.dataset.k, blocks: [], pickedBlocks: 0, pickedQty: 0, cutInfo: "" }); }, onToggleMode() { const mode = this.data.mode === "photo" ? "manual" : "photo"; this.setData({ mode, cutInfo: "" }); }, /** 排完看一眼今天几道、大概几分钟 */ loadQueue() { const child = getApp().globalData.child; if (!child) return Promise.resolve(); return api .get("/api/today?child_id=" + child.id) .then((d) => this.setData({ queue: { n: (d.items || []).length, minutes: estimate(d) } })) .catch(() => {}); }, // ---------------- 拍一张 onPick() { wx.chooseMedia({ count: 1, mediaType: ["image"], sourceType: ["camera", "album"], camera: "back", success: (res) => { const images = (res.tempFiles || []).map((f) => f.tempFilePath); if (!images.length) return; this.checkSize(images).then((ok) => { if (!ok) return; this.setData({ photo: images[0], blocks: [], pickedBlocks: 0, pickedQty: 0, cutInfo: "" }); this.onDetect(); }); }, }); }, /** * 太小的图先拦一下再上传。 * 微信转发的图长边常被压到 400px,一行字只剩十几像素——OCR 认出来全是错字, * 与其等它在后面莫名其妙地失败,不如在这里就说清楚。 */ checkSize(images) { const info = (p) => new Promise((resolve) => { wx.getImageInfo({ src: p, success: (r) => resolve({ w: r.width || 0, h: r.height || 0 }), fail: () => resolve({ w: 0, h: 0 }), }); }); return Promise.all(images.map(info)).then((sizes) => { const bad = sizes.filter((s) => s.w && Math.min(s.w, s.h) < 1200); if (!bad.length) return true; const s = bad[0]; return new Promise((resolve) => { wx.showModal({ title: "这张图太小了", content: "只有 " + s.w + "×" + s.h + " 像素,一行字只剩十几像素,识别出来大概率是错字。" + "\n\n建议:在微信里选「原图」重发,或重新拍一张(一次拍半页)。", confirmText: "还是上传", cancelText: "重拍", success: (r) => resolve(!!r.confirm), fail: () => resolve(false), }); }); }); }, /** 整页 → 切成块 → 把框画在图上,等家长点 */ onDetect() { if (!this.data.photo || this.data.detecting) return; const child = getApp().globalData.child; if (!child) return; this.setData({ detecting: true, cutInfo: "" }); wx.showLoading({ title: "切题中", mask: true }); const done = () => { wx.hideLoading(); this.setData({ detecting: false }); }; const fail = (e) => { done(); const msg = (e && e.message) || "这次没切出来"; wx.showModal({ title: "这次没切出来", content: msg + "\n\n可以:① 卷子放平、光线亮一点重拍;② 换个科目试试;③ 这道题改成手输。", showCancel: false, }); }; // 手机拍出来 3–8MB,直接 base64 会撞 wx.request 的包体上限,先压到长边 1600、质量 72。 wx.compressImage({ src: this.data.photo, quality: 72, compressedWidth: 1600, success: (r) => this.sendDetect(r.tempFilePath || this.data.photo, done, fail), fail: () => this.sendDetect(this.data.photo, done, fail), }); }, sendDetect(filePath, done, fail) { wx.getFileSystemManager().readFile({ filePath, encoding: "base64", success: (f) => { api .post("/api/papers/scan/detect", { image_b64: f.data, filename: "page.jpg" }) .then((r) => this.afterDetect(r)) .catch((e) => fail(e)) .then(done); }, fail: () => { done(); wx.showToast({ title: "读不到这张图", icon: "none" }); }, }); }, afterDetect(r) { const w = r.width || 1; const h = r.height || 1; const blocks = (r.blocks || []).map((b) => { const box = b.box || { x: 0, y: 0, w: 0, h: 0 }; const n = qtyOf(b); return { i: b.i, stem: b.stem || "", judge_type: b.judge_type || "manual", n, on: false, px: (box.x / w) * 100, py: (box.y / h) * 100, pw: (box.w / w) * 100, ph: (box.h / h) * 100, }; }); const info = blocks.length ? "切出 " + blocks.length + " 块。点你觉得做错的那几道(可多选),再点下面的按钮。" : "这一页没切出整块的题。可以重拍一张,或者改成手输。"; this.setData({ draft: r.draft_id || "", blocks, cutInfo: info, pickedBlocks: 0, pickedQty: 0 }); }, toggleBlock(e) { const i = Number(e.currentTarget.dataset.i); const blocks = this.data.blocks.slice(); const idx = blocks.findIndex((b) => b.i === i); if (idx < 0) return; blocks[idx].on = !blocks[idx].on; if (wx.vibrateShort) wx.vibrateShort({ type: "light" }); const on = blocks.filter((b) => b.on); this.setData({ blocks, pickedBlocks: on.length, pickedQty: on.reduce((s, b) => s + b.n, 0), }); }, onRetake() { this.setData({ photo: "", draft: "", blocks: [], pickedBlocks: 0, pickedQty: 0, cutInfo: "" }); }, /** 入库前:一块多道就先摊开逐条勾;都是单题就直接入库 */ onImport() { const picked = this.data.blocks.filter((b) => b.on); if (!picked.length) { wx.showToast({ title: "先点选做错的题", icon: "none" }); return; } if (this.data.importing) return; if (!picked.some((b) => b.n > 1)) { this.doImport(picked.map((b) => b.i)); return; } this.preparePicks(picked.map((b) => b.i)); }, preparePicks(picks) { const child = getApp().globalData.child; this.setData({ importing: true }); wx.showLoading({ title: "整理中", mask: true }); api .post("/api/papers/scan/prepare", { child_id: child.id, subject: this.data.subject, draft_id: this.data.draft, picks, }) .then((r) => { const items = (r.items || []).map((it, k) => ({ k, block: it.block, sub: it.sub, stem: it.stem || "", tag: it.sub === null || it.sub === undefined ? "单题" : "第 " + (it.sub + 1) + " 道", on: true, })); this.setData({ pickItems: items, pickOpen: true, importing: false }); }) .catch((e) => { api.toastError(e); this.setData({ importing: false }); }) .then(() => wx.hideLoading()); }, togglePick(e) { const k = Number(e.currentTarget.dataset.k); const pickItems = this.data.pickItems.slice(); if (!pickItems[k]) return; pickItems[k].on = !pickItems[k].on; if (wx.vibrateShort) wx.vibrateShort({ type: "light" }); this.setData({ pickItems }); }, onPickAll() { const pickItems = this.data.pickItems.map((it) => ({ ...it, on: true })); this.setData({ pickItems }); }, onPickNone() { const pickItems = this.data.pickItems.map((it) => ({ ...it, on: false })); this.setData({ pickItems }); }, closePick() { this.setData({ pickOpen: false, pickItems: [] }); }, confirmPick() { const on = this.data.pickItems.filter((it) => it.on); if (!on.length) { wx.showToast({ title: "一道都没勾", icon: "none" }); return; } // 单题还原成块号;多道按块归组 {i, subs:[...]} const singles = []; const grouped = {}; on.forEach((it) => { if (it.sub === null || it.sub === undefined) singles.push(it.block); else { grouped[it.block] = grouped[it.block] || []; grouped[it.block].push(it.sub); } }); const picks = singles.slice(); Object.keys(grouped).forEach((b) => picks.push({ i: Number(b), subs: grouped[b] })); this.setData({ pickOpen: false, pickItems: [] }); this.doImport(picks); }, doImport(picks) { const child = getApp().globalData.child; if (this.data.importing) return; this.setData({ importing: true }); wx.showLoading({ title: "入库中", mask: true }); api .post("/api/papers/scan/import", { child_id: child.id, subject: this.data.subject, draft_id: this.data.draft, picks, }) .then((r) => { const ocr = r.ocr || {}; const n = ocr.created === undefined ? (ocr.questions || 0) : ocr.created; this.setData({ photo: "", draft: "", blocks: [], pickedBlocks: 0, pickedQty: 0, cutInfo: "", importing: false }); wx.showToast({ title: "入库 " + n + " 道", icon: "success" }); this.loadQueue(); }) .catch((e) => { api.toastError(e); this.setData({ importing: false }); }) .then(() => wx.hideLoading()); }, // ---------------- 手输一道 onStem(e) { this.setData({ stem: e.detail.value }); }, onAnswer(e) { this.setData({ answer: e.detail.value }); }, onChoice(e) { this.setData({ isChoice: e.detail.value }); }, onOptions(e) { this.setData({ optionsText: e.detail.value }); }, onManualAdd() { const stem = (this.data.stem || "").trim(); if (!stem) { wx.showToast({ title: "题干还没填", icon: "none" }); return; } if (this.data.saving) return; const body = { child_id: getApp().globalData.child.id, subject: this.data.subject, stem, answer: (this.data.answer || "").trim() || null, }; if (this.data.isChoice) { const opts = (this.data.optionsText || "") .split(/[、,,;;\s]+/) .map((s) => s.trim()) .filter(Boolean); if (opts.length < 2) { wx.showToast({ title: "选择题至少两个选项", icon: "none" }); return; } body.options = opts; } this.setData({ saving: true }); api .post("/api/mistakes/manual", body) .then(() => { this.setData({ saving: false, stem: "", answer: "", optionsText: "", isChoice: false }); wx.showToast({ title: "已加入", icon: "success" }); this.loadQueue(); }) .catch((e) => { api.toastError(e); this.setData({ saving: false }); }); }, onWorkbench() { wx.navigateTo({ url: "/pages/wb/wb" }); }, });