fix(admin): 复核页选择题选项渲染修复 + 阅卷导出 Word + 评分两位小数(R28)

针对测试人员反馈的四项:
1) 复核明细「选择题大量空白」:options 为 text 列存 JSON 字符串,原先被原样透传,
   前端 v-for 对字符串逐字符迭代(op.key/op.text 皆 undefined)⇒ 每字符渲染一行。
   新增 parseOptions()(仅 string 才 JSON.parse,失败降级 null)。
2) 选项与答案不随机:仅题目用 RAND() 抽题,normalizeQuestion 无 shuffle,字母无歧义。
3) 评分精度:DECIMAL(6,1) -> DECIMAL(6,2),migration 026 覆盖 17 列(与 025 同范围),
   lib/decimal_score.js 与 11 个 model 注释同步;汇总收敛 round2(×100/100)。
4) 阅卷导出:GET /api/admin/big-answers/:group/export?ids=
   单选考生 -> 直接 .docx;多位/全部 -> 每位考生单独 .docx 打包 .zip。
   路由为 3 段,注册在 :group 通配段之前。

验证:verify_big_question 51 PASS/1 FAIL(唯一 FAIL=测试人员新数据,非本次改动)、
verify_score_leak_all_channels 24/24、verify_question_no_answer_leak 4/3/0/1。
This commit is contained in:
2026-09-28 15:50:46 +08:00
parent 37d07907a1
commit 2ab0991310
16 changed files with 330 additions and 34 deletions

View File

@@ -360,13 +360,14 @@ class BigQuestionService extends Service {
}
const n = Number(raw);
if (!Number.isFinite(n) || n < 0 || n > per) {
return { ok: false, code: 'INVALID_SCORE', message: `第 ${i + 1} 小问得分须为 0-${per} 的数值(可含一位小数)` };
return { ok: false, code: 'INVALID_SCORE', message: `第 ${i + 1} 小问得分须为 0-${per} 的数值(可含两位小数)` };
}
// 2026-09-23:人工评分允许小数点后一位 ⇒ 收敛到 1 位小数,⛔ 不可用 Math.round 抹成整数
parsed.push(Math.round(n * 10) / 10);
// 2026-09-28:评分精确到小数点后**两位**(测试人员反馈)⇒ 收敛到 2 位小数,
// ⛔ 不可用 Math.round 抹成整数(会丢掉 8.25 这类分数)
parsed.push(Math.round(n * 100) / 100);
}
// 浮点求和再收敛一次,避免 0.1+0.2=0.30000000000000004 之类脏值落库/回显
const total = Math.round(parsed.reduce((a, b) => a + b, 0) * 10) / 10;
const total = Math.round(parsed.reduce((a, b) => a + b, 0) * 100) / 100;
await record.update({
score_1: parsed[0],
@@ -529,6 +530,140 @@ class BigQuestionService extends Service {
};
}
/**
* 导出「试题 + 考生作答」阅卷用 Word(2026-09-28 测试人员需求)
*
* 与 exportQuestions 的区别(⛔ 勿混淆):
* · exportQuestions = 导出**题库题目**(供留存 / 打印 / 分发),不含任何考生信息;
* · exportAnswers = 导出**考生答卷**(供离线阅卷 / 打印 / 归档),内容 =
* 考生身份 + 抽题时刻题干快照 + 三小问作答 + 逐问得分 + 合计。
*
* 粒度(测试人员明确要求,勿改):
* · 命中 1 位考生 → 直接返回 .docx;
* · 命中多位 / 不传 ids(该组全部已抽题考生)→ **每人一个独立 .docx** 再打包为 .zip;
* · ⛔ 绝不把多位考生塞进同一个 Word —— 否则无法分别打印 / 分发 / 归档。
*
* 只读操作:不写库、不改任何状态。
*
* @param {string} group
* @param {Array<number>|string} [ids] - 考生 ID(contestantId)数组或逗号分隔串
* @returns {Promise<Object>} { ok:true, fileName, buffer, count, isZip? } 或 { ok:false, code, message }
*/
async exportAnswers(group, ids) {
if (!this.isEnabledGroup(group)) {
return { ok: false, code: 'GROUP_NOT_ENABLED', message: '该组别未启用综合大题' };
}
const all = await this.listAnswersForAdmin(group);
let picked = all;
if (ids !== undefined && ids !== null && ids !== '') {
const raw = Array.isArray(ids) ? ids : String(ids).split(',');
const idList = raw
.map(v => Number(String(v).trim()))
.filter(v => Number.isInteger(v) && v > 0);
if (!idList.length) {
return { ok: false, code: 'INVALID_IDS', message: '导出的考生 ID 列表非法' };
}
const wanted = new Set(idList);
picked = all.filter(x => wanted.has(Number(x.contestantId)));
if (!picked.length) {
return { ok: false, code: 'NOT_FOUND', message: '选中的考生不属于该组别或已不存在' };
}
}
// 只导出「已抽题」考生:未抽题者没有题面与作答,导出只会得到空白页
const exportable = picked.filter(x => x.drawn);
if (!exportable.length) {
return { ok: false, code: 'NOT_FOUND', message: '该范围内没有已抽取大题的考生,无可导出的答卷' };
}
const docx = require('../lib/docx');
const groupLabel = GROUP_LABELS[group] || group;
const pad = n => String(n).padStart(2, '0');
const fmtTime = v => {
if (!v) return '未提交';
const d = new Date(v);
if (isNaN(d.getTime())) return String(v);
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} `
+ `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
};
// 评分统一显示两位小数(与 DECIMAL(6,2) 口径一致)
const fmtScore = v => (v == null || v === '' ? '(未评分)' : Number(v).toFixed(2));
const files = [];
for (const s of exportable) {
const snap = s.questionSnapshot || {};
const prompts = Array.isArray(snap.sub_prompts) ? snap.sub_prompts : [];
const per = snap.score_per_sub != null ? Number(snap.score_per_sub) : null;
const answers = [s.answer1, s.answer2, s.answer3];
const scores = [s.score1, s.score2, s.score3];
const blocks = [];
// ---- 抬头:身份信息(阅卷核对用,与成绩复核同口径) ----
blocks.push({
type: 'p',
align: 'center',
runs: [{ text: `${groupLabel}综合大题阅卷表`, bold: true, size: 32 }],
});
blocks.push({ type: 'p', runs: [{ text: `姓名:${s.name || '--'}  身份证号:${s.idNumber || '--'}  手机号:${s.phone || '--'}` }] });
blocks.push({ type: 'p', runs: [{ text: `抽到题目:${snap.title || '--'}(${s.bigQuestionUid || '--'})` }] });
blocks.push({ type: 'p', runs: [{ text: `提交时间:${fmtTime(s.submitTime)}  已答小问:${s.answeredCount} / ${SUB_QUESTION_COUNT}` }] });
// ---- 试题(抽题时刻快照:题目后续被改不影响存证) ----
blocks.push({ type: 'p', runs: [] });
blocks.push({ type: 'p', runs: [{ text: '【试题】(抽题时刻快照,防纠纷)', bold: true, size: 26 }] });
blocks.push(...docx.htmlToBlocks(snap.description || ''));
// ---- 三小问:小问题目 + 考生作答 + 得分 ----
for (let i = 0; i < SUB_QUESTION_COUNT; i++) {
blocks.push({ type: 'p', runs: [] });
blocks.push({
type: 'p',
runs: [{
text: `【第 ${i + 1} 小问】${prompts[i] ? ':' + prompts[i] : ''}${per != null ? `(${per} 分)` : ''}`,
bold: true,
size: 26,
}],
});
const a = String(answers[i] || '').trim();
blocks.push({ type: 'p', runs: [{ text: '考生作答:', bold: true }] });
blocks.push(...docx.textToBlocks(a || '(未作答)'));
blocks.push({ type: 'p', runs: [{ text: `得分:${fmtScore(scores[i])}${per != null ? ` / ${Number(per).toFixed(2)}` : ''} 分` }] });
}
// ---- 合计 ----
blocks.push({ type: 'p', runs: [] });
blocks.push({
type: 'p',
runs: [{
text: `本大题合计:${fmtScore(s.bigScore)}${per != null ? ` / ${(per * SUB_QUESTION_COUNT).toFixed(2)}` : ''} 分`,
bold: true,
}],
});
blocks.push({ type: 'p', runs: [{ text: `(导出时间:${fmtTime(new Date())})` }] });
const buffer = await docx.buildDocx(blocks);
files.push({
name: `${groupLabel}_${safeFilePart(s.name || '考生')}_${safeFilePart(s.idNumber || s.contestantId)}.docx`,
buffer,
});
}
if (files.length === 1) {
return { ok: true, fileName: files[0].name, buffer: files[0].buffer, count: 1 };
}
const buffer = await docx.zipDocxFiles(files);
return {
ok: true,
isZip: true,
fileName: `${groupLabel}_大题答卷_${files.length}人.zip`,
buffer,
count: files.length,
};
}
/**
* 新建大题
* @param {string} group