小程序拍题:接整页点选链路(detect→点框→prepare 逐条勾→import),换相机图标
This commit is contained in:
+203
-100
@@ -11,25 +11,42 @@ function estimate(queue) {
|
||||
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",
|
||||
images: [],
|
||||
subject: "math",
|
||||
subjects: [
|
||||
{ k: "math", name: "数学" },
|
||||
{ k: "chinese", name: "语文" },
|
||||
],
|
||||
questions: [],
|
||||
|
||||
// 拍照 → 切块 → 点选
|
||||
photo: "",
|
||||
draft: "",
|
||||
blocks: [],
|
||||
pickedBlocks: 0,
|
||||
pickedQty: 0,
|
||||
detecting: false,
|
||||
cutInfo: "",
|
||||
cutting: false,
|
||||
|
||||
// 逐条勾选(一块多道)
|
||||
pickItems: [],
|
||||
pickOpen: false,
|
||||
importing: false,
|
||||
|
||||
// 手输
|
||||
stem: "",
|
||||
answer: "",
|
||||
isChoice: false,
|
||||
optionsText: "",
|
||||
saving: false,
|
||||
|
||||
// 题单预览
|
||||
queue: { n: 0, minutes: 0 },
|
||||
},
|
||||
@@ -43,11 +60,12 @@ Page({
|
||||
},
|
||||
|
||||
onSubject(e) {
|
||||
this.setData({ subject: e.currentTarget.dataset.k, questions: [] });
|
||||
this.setData({ subject: e.currentTarget.dataset.k, blocks: [], pickedBlocks: 0, pickedQty: 0, cutInfo: "" });
|
||||
},
|
||||
|
||||
onToggleMode() {
|
||||
this.setData({ mode: this.data.mode === "photo" ? "manual" : "photo", cutInfo: "" });
|
||||
const mode = this.data.mode === "photo" ? "manual" : "photo";
|
||||
this.setData({ mode, cutInfo: "" });
|
||||
},
|
||||
|
||||
/** 排完看一眼今天几道、大概几分钟 */
|
||||
@@ -64,7 +82,7 @@ Page({
|
||||
|
||||
onPick() {
|
||||
wx.chooseMedia({
|
||||
count: 3,
|
||||
count: 1,
|
||||
mediaType: ["image"],
|
||||
sourceType: ["camera", "album"],
|
||||
camera: "back",
|
||||
@@ -72,7 +90,9 @@ Page({
|
||||
const images = (res.tempFiles || []).map((f) => f.tempFilePath);
|
||||
if (!images.length) return;
|
||||
this.checkSize(images).then((ok) => {
|
||||
if (ok) this.setData({ images });
|
||||
if (!ok) return;
|
||||
this.setData({ photo: images[0], blocks: [], pickedBlocks: 0, pickedQty: 0, cutInfo: "" });
|
||||
this.onDetect();
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -111,131 +131,214 @@ Page({
|
||||
});
|
||||
},
|
||||
|
||||
onCut() {
|
||||
if (!this.data.images.length) {
|
||||
wx.showToast({ title: "先拍一张", icon: "none" });
|
||||
return;
|
||||
}
|
||||
if (this.data.cutting) return;
|
||||
this.setData({ cutting: true });
|
||||
|
||||
/** 整页 → 切成块 → 把框画在图上,等家长点 */
|
||||
onDetect() {
|
||||
if (!this.data.photo || this.data.detecting) return;
|
||||
const child = getApp().globalData.child;
|
||||
const fs = wx.getFileSystemManager();
|
||||
wx.showLoading({ title: "识别中", mask: true });
|
||||
if (!child) return;
|
||||
this.setData({ detecting: true, cutInfo: "" });
|
||||
wx.showLoading({ title: "切题中", mask: true });
|
||||
|
||||
// 手机拍出来 3–8MB,直接 base64 会撞 wx.request 的包体上限,先压到长边 1600、质量 72。
|
||||
const send = (filePath) => {
|
||||
fs.readFile({
|
||||
filePath,
|
||||
encoding: "base64",
|
||||
success: (f) => {
|
||||
api
|
||||
.post("/api/papers/ocr", {
|
||||
child_id: child.id,
|
||||
subject: this.data.subject,
|
||||
image_b64: f.data,
|
||||
filename: "page.jpg",
|
||||
fallback_demo: false,
|
||||
})
|
||||
.then((p) => this.afterCut(p))
|
||||
.catch((e) => this.onCutFail(e))
|
||||
.then(() => wx.hideLoading());
|
||||
},
|
||||
fail: () => {
|
||||
wx.hideLoading();
|
||||
wx.showToast({ title: "读不到这张图", icon: "none" });
|
||||
this.setData({ cutting: false });
|
||||
},
|
||||
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.images[0],
|
||||
src: this.data.photo,
|
||||
quality: 72,
|
||||
compressedWidth: 1600,
|
||||
success: (r) => send(r.tempFilePath || this.data.images[0]),
|
||||
fail: () => send(this.data.images[0]),
|
||||
success: (r) => this.sendDetect(r.tempFilePath || this.data.photo, done, fail),
|
||||
fail: () => this.sendDetect(this.data.photo, done, fail),
|
||||
});
|
||||
},
|
||||
|
||||
afterCut(p) {
|
||||
const questions = (p.questions || []).map((q) => ({
|
||||
...q,
|
||||
checked: true,
|
||||
tag: (q.judge_type === "manual" ? "你自己判" : "机器判") + " · " + (q.subject === "chinese" ? "语文" : "数学"),
|
||||
}));
|
||||
const ocr = p.ocr || {};
|
||||
let info = "";
|
||||
if (ocr.ok) {
|
||||
info = "识别到 " + ocr.lines + " 行,切出 " + questions.length + " 道题";
|
||||
if (ocr.warn) info += "\n" + ocr.warn;
|
||||
} else {
|
||||
info = "没认出题,可以手动拍清楚一点,或者改成手输";
|
||||
// 小图 / 太糊时,后端会带一句「这张图只有 400×520」——比「没认出题」有用得多
|
||||
if (ocr.warn) info = ocr.warn + "\n" + info;
|
||||
}
|
||||
this.setData({ questions, cutting: false, cutInfo: info });
|
||||
},
|
||||
|
||||
/** 识别失败:给「用演示题库」这条后路,而不是卡死在这儿。 */
|
||||
onCutFail(e) {
|
||||
this.setData({ cutting: false });
|
||||
wx.hideLoading();
|
||||
const msg = (e && e.message) || "没认出来";
|
||||
wx.showModal({
|
||||
title: "这次没认出来",
|
||||
content: msg + "\n\n可以:① 把卷子放平、光线亮一点重拍;② 这道题改成手输;③ 先用演示题库走一遍流程。",
|
||||
confirmText: "用演示题库",
|
||||
cancelText: "重新拍",
|
||||
success: (r) => {
|
||||
if (!r.confirm) return;
|
||||
this.setData({ cutting: true });
|
||||
sendDetect(filePath, done, fail) {
|
||||
wx.getFileSystemManager().readFile({
|
||||
filePath,
|
||||
encoding: "base64",
|
||||
success: (f) => {
|
||||
api
|
||||
.post("/api/papers/ocr", {
|
||||
child_id: getApp().globalData.child.id,
|
||||
subject: this.data.subject,
|
||||
image_b64: "ZGVtbw==",
|
||||
filename: "demo.jpg",
|
||||
fallback_demo: true,
|
||||
})
|
||||
.then((p) => this.afterCut(p))
|
||||
.catch((err) => api.toastError(err))
|
||||
.then(() => this.setData({ cutting: false }));
|
||||
.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" });
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
toggleQ(e) {
|
||||
const i = Number(e.currentTarget.dataset.i);
|
||||
const questions = this.data.questions.slice();
|
||||
questions[i].checked = !questions[i].checked;
|
||||
this.setData({ questions });
|
||||
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 ids = this.data.questions.filter((q) => q.checked).map((q) => q.id);
|
||||
if (!ids.length) {
|
||||
wx.showToast({ title: "一道都没选", icon: "none" });
|
||||
const picked = this.data.blocks.filter((b) => b.on);
|
||||
if (!picked.length) {
|
||||
wx.showToast({ title: "先点选做错的题", icon: "none" });
|
||||
return;
|
||||
}
|
||||
if (this.data.importing) return;
|
||||
this.setData({ importing: true });
|
||||
|
||||
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/mistakes/import", {
|
||||
child_id: getApp().globalData.child.id,
|
||||
question_ids: ids,
|
||||
.post("/api/papers/scan/prepare", {
|
||||
child_id: child.id,
|
||||
subject: this.data.subject,
|
||||
draft_id: this.data.draft,
|
||||
picks,
|
||||
})
|
||||
.then((r) => {
|
||||
const n = (r.created || []).length;
|
||||
this.setData({ importing: false, questions: [] });
|
||||
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());
|
||||
},
|
||||
|
||||
// ---------------- 手输一道
|
||||
|
||||
Reference in New Issue
Block a user