chore: 首次纳入版本控制(审查前基线,v1.2.0)
This commit is contained in:
@@ -0,0 +1,299 @@
|
||||
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));
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
mode: "photo",
|
||||
images: [],
|
||||
subject: "math",
|
||||
subjects: [
|
||||
{ k: "math", name: "数学" },
|
||||
{ k: "chinese", name: "语文" },
|
||||
],
|
||||
questions: [],
|
||||
cutInfo: "",
|
||||
cutting: 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, questions: [] });
|
||||
},
|
||||
|
||||
onToggleMode() {
|
||||
this.setData({ mode: this.data.mode === "photo" ? "manual" : "photo", 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: 3,
|
||||
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) this.setData({ images });
|
||||
});
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 太小的图先拦一下再上传。
|
||||
* 微信转发的图长边常被压到 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),
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
onCut() {
|
||||
if (!this.data.images.length) {
|
||||
wx.showToast({ title: "先拍一张", icon: "none" });
|
||||
return;
|
||||
}
|
||||
if (this.data.cutting) return;
|
||||
this.setData({ cutting: true });
|
||||
|
||||
const child = getApp().globalData.child;
|
||||
const fs = wx.getFileSystemManager();
|
||||
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 });
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
wx.compressImage({
|
||||
src: this.data.images[0],
|
||||
quality: 72,
|
||||
compressedWidth: 1600,
|
||||
success: (r) => send(r.tempFilePath || this.data.images[0]),
|
||||
fail: () => send(this.data.images[0]),
|
||||
});
|
||||
},
|
||||
|
||||
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 });
|
||||
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 }));
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
toggleQ(e) {
|
||||
const i = Number(e.currentTarget.dataset.i);
|
||||
const questions = this.data.questions.slice();
|
||||
questions[i].checked = !questions[i].checked;
|
||||
this.setData({ questions });
|
||||
},
|
||||
|
||||
onImport() {
|
||||
const ids = this.data.questions.filter((q) => q.checked).map((q) => q.id);
|
||||
if (!ids.length) {
|
||||
wx.showToast({ title: "一道都没选", icon: "none" });
|
||||
return;
|
||||
}
|
||||
if (this.data.importing) return;
|
||||
this.setData({ importing: true });
|
||||
|
||||
api
|
||||
.post("/api/mistakes/import", {
|
||||
child_id: getApp().globalData.child.id,
|
||||
question_ids: ids,
|
||||
})
|
||||
.then((r) => {
|
||||
const n = (r.created || []).length;
|
||||
this.setData({ importing: false, questions: [] });
|
||||
wx.showToast({ title: "入库 " + n + " 道", icon: "success" });
|
||||
this.loadQueue();
|
||||
})
|
||||
.catch((e) => {
|
||||
api.toastError(e);
|
||||
this.setData({ importing: false });
|
||||
});
|
||||
},
|
||||
|
||||
// ---------------- 手输一道
|
||||
|
||||
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" });
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user