feat(training): limit reference answer uploads to PDF

dev-QQq
chenyuan 1 month ago
parent b4a2d0c8ad
commit abba858cd6

@ -6,7 +6,7 @@ export const CATEGORY_EXCEL_COLUMNS = [
{ key: "inventoryCapital", label: "库存资金占用(万元)" },
{ key: "capitalOccupancyRate", label: "资金占用率(%)" },
{ key: "sellThroughRate", label: "近30天动销率(%)" },
{ key: "grossMargin", label: "毛利率" },
{ key: "grossMargin", label: "毛利率(%)" },
];
const boundedRates = new Set(["capitalOccupancyRate", "sellThroughRate"]);

@ -77,7 +77,7 @@
<th>库存资金占用(万元)</th>
<th>资金占用率(%)</th>
<th>近30天动销率(%)</th>
<th>毛利率</th>
<th>毛利率(%)</th>
</tr>
</thead>
<tbody>

@ -429,7 +429,7 @@ const materialSlots = [
{ key: "trainingData", label: "案例数据" },
{ key: "guideManual", label: "实训指导手册" },
{ key: "aiManual", label: "AI辅助指导手册" },
{ key: "referenceAnswer", label: "参考答案", accept: ".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
{ key: "referenceAnswer", label: "参考答案", accept: ".pdf,application/pdf" },
];
const tasks = ref([]);
const dialogVisible = ref(false);
@ -692,12 +692,13 @@ async function handleImportChange(file) {
async function handleMaterialChange(slotKey, file) {
const rawFile = file?.raw;
if (!rawFile) return;
if (slotKey === "referenceAnswer" && !isWordFile(rawFile)) {
proxy?.$modal?.msgError?.("参考答案仅支持 Word 文件(.doc 或 .docx)");
if (slotKey === "referenceAnswer" && !isPdfFile(rawFile)) {
proxy?.$modal?.msgError?.("参考答案仅支持 PDF 文件(.pdf)");
return;
}
const data = new FormData();
data.append("file", rawFile);
data.append("materialType", slotKey);
uploadingField.value = slotKey;
try {
const res = await uploadTrainingTaskMaterial(data);
@ -711,9 +712,9 @@ async function handleMaterialChange(slotKey, file) {
}
}
function isWordFile(file) {
function isPdfFile(file) {
const name = String(file?.name || "").trim().toLowerCase();
return name.endsWith(".doc") || name.endsWith(".docx");
return name.endsWith(".pdf");
}
function clearMaterial(slotKey) {

@ -339,7 +339,7 @@
<el-dialog v-model="referenceAnswerDialogVisible" title="参考答案" width="900px" append-to-body class="reference-answer-dialog">
<iframe v-if="referenceAnswerSrc" :src="referenceAnswerSrc" class="reference-answer-frame" title="参考答案在线预览" />
<el-empty v-else description="暂无可预览的 Word 参考答案" />
<el-empty v-else description="暂无可预览的参考答案" />
</el-dialog>
</div>
</template>

@ -394,7 +394,7 @@ const materialSlots = [
{ key: "trainingData", label: "案例数据" },
{ key: "guideManual", label: "实训指导手册" },
{ key: "aiManual", label: "AI辅助指导手册" },
{ key: "referenceAnswer", label: "参考答案", accept: ".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
{ key: "referenceAnswer", label: "参考答案", accept: ".pdf,application/pdf" },
];
const tasks = ref([]);
const classList = ref([]);
@ -717,12 +717,13 @@ function removeObjective(index) {
async function handleMaterialChange(slotKey, file) {
const rawFile = file?.raw;
if (!rawFile) return;
if (slotKey === "referenceAnswer" && !isWordFile(rawFile)) {
proxy?.$modal?.msgError?.("参考答案仅支持 Word 文件(.doc 或 .docx)");
if (slotKey === "referenceAnswer" && !isPdfFile(rawFile)) {
proxy?.$modal?.msgError?.("参考答案仅支持 PDF 文件(.pdf)");
return;
}
const data = new FormData();
data.append("file", rawFile);
data.append("materialType", slotKey);
uploadingField.value = slotKey;
try {
const res = await uploadTrainingTaskMaterial(data);
@ -736,9 +737,9 @@ async function handleMaterialChange(slotKey, file) {
}
}
function isWordFile(file) {
function isPdfFile(file) {
const name = String(file?.name || "").trim().toLowerCase();
return name.endsWith(".doc") || name.endsWith(".docx");
return name.endsWith(".pdf");
}
function clearMaterial(slotKey) {

@ -0,0 +1,51 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
const { parse, compileScript, compileTemplate } = require("@vue/compiler-sfc");
const editors = ["teacherEnd", "schoolAdmin"].map((role) => path.resolve(__dirname, `../src/views/${role}/trainingTask/index.vue`));
function page(filename) {
const source = fs.readFileSync(filename, "utf8");
const calls = [], errors = [];
const context = { taskForm: { referenceAnswerName: "existing.pdf", referenceAnswerUrl: "/file/existing.pdf" }, uploadingField: { value: "" },
proxy: { $modal: { msgError: (text) => errors.push(text), msgSuccess() {} } },
FormData: class { constructor() { this.values = new Map(); } append(key, value) { this.values.set(key, value); } get(key) { return this.values.get(key); } },
normalizeMaterialUrl: (value) => value, materialLabel: (key) => key,
uploadTrainingTaskMaterial: async (data) => { calls.push(data); return { originalFilename: data.get("file").name, url: "/file/uploaded.pdf" }; },
};
const handlers = source.slice(source.indexOf("async function handleMaterialChange("), source.indexOf("function clearMaterial("));
const slots = source.match(/const materialSlots = \[[\s\S]*?\];/)[0];
vm.runInNewContext(slots + "\n" + handlers + "\nglobalThis.page={handleMaterialChange,materialSlots};", context);
return { ...context, ...context.page, source, calls, errors };
}
test("both task editors block non PDF reference uploads without replacing existing files", async () => {
for (const filename of editors) {
const editor = page(filename);
assert.equal(editor.materialSlots.find((slot) => slot.key === "referenceAnswer").accept, ".pdf,application/pdf");
for (const name of ["answer.doc", "answer.docx", "answer.xlsx", "answer.pdf.exe"]) {
await editor.handleMaterialChange("referenceAnswer", { raw: { name, type: "application/pdf" } });
}
assert.equal(editor.calls.length, 0);
assert.equal(editor.errors.length, 4);
assert.match(editor.errors[0], /仅支持 PDF/);
assert.equal(editor.taskForm.referenceAnswerName, "existing.pdf");
assert.equal(editor.taskForm.referenceAnswerUrl, "/file/existing.pdf");
}
});
test("PDF reference uploads and other materials keep separate upload rules", async () => {
for (const filename of editors) {
const editor = page(filename);
await editor.handleMaterialChange("referenceAnswer", { raw: { name: "答案.PDF" } });
assert.equal(editor.calls[0].get("materialType"), "referenceAnswer");
assert.equal(editor.taskForm.referenceAnswerName, "答案.PDF");
assert.equal(editor.taskForm.referenceAnswerUrl, "/file/uploaded.pdf");
await editor.handleMaterialChange("material", { raw: { name: "案例.docx" } });
assert.equal(editor.calls[1].get("materialType"), "material");
assert.equal(editor.taskForm.materialName, "案例.docx");
const { descriptor, errors } = parse(editor.source);
assert.deepEqual(errors, []);
compileScript(descriptor, { id: "reference-answer-upload" });
assert.deepEqual(compileTemplate({ source: descriptor.template.content, filename, id: "reference-answer-upload" }).errors, []);
}
});

@ -9,14 +9,14 @@ const editor = fs.readFileSync(
);
assert(/key: "referenceAnswer",\s*label: "参考答案"/.test(editor));
assert(/accept: "\.doc,\.docx/.test(editor), "reference answers should expose Word file types");
assert(/accept: "\.pdf,application\/pdf"/.test(editor), "reference answers should expose PDF file types");
assert(/referenceAnswerName: ""/.test(editor));
assert(/referenceAnswerUrl: ""/.test(editor));
assert(/referenceAnswerName: referenceAnswer\.name/.test(editor));
assert(/referenceAnswerUrl: referenceAnswer\.url/.test(editor));
assert(/referenceAnswerName: taskForm\.referenceAnswerName/.test(editor));
assert(/referenceAnswerUrl: taskForm\.referenceAnswerUrl/.test(editor));
assert(/slotKey === "referenceAnswer" && !isWordFile\(rawFile\)/.test(editor));
assert(/name\.endsWith\("\.doc"\) \|\| name\.endsWith\("\.docx"\)/.test(editor));
assert(/slotKey === "referenceAnswer" && !isPdfFile\(rawFile\)/.test(editor));
assert(/name\.endsWith\("\.pdf"\)/.test(editor));
console.log("teacher reference-answer upload contract passed");

Loading…
Cancel
Save