From cb71285913788c2bed6fc27df12b0aa45f3fa670 Mon Sep 17 00:00:00 2001 From: chenyuan Date: Wed, 2 Sep 2026 21:21:51 +0800 Subject: [PATCH] fix: generate teacher import template locally --- src/views/teacherEnd/teacher/index.vue | 36 ++++++++++++++----- tests/teacher-import-template.static.test.cjs | 18 ++++++++++ 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 tests/teacher-import-template.static.test.cjs diff --git a/src/views/teacherEnd/teacher/index.vue b/src/views/teacherEnd/teacher/index.vue index 379ccc5..44e4bfd 100644 --- a/src/views/teacherEnd/teacher/index.vue +++ b/src/views/teacherEnd/teacher/index.vue @@ -260,14 +260,34 @@ const exportTeacher = () => { URL.revokeObjectURL(url); }); }; -// 导入模版下载 -const downloadTemplate = () => { - const a = document.createElement("a"); - a.href = `/file/教师信息表格.xlsx`; - a.download = "模板文件.xlsx"; - document.body.appendChild(a); - a.click(); - document.body.removeChild(a); +// 导入模板下载 +const downloadTemplate = async () => { + const XLSX = await import("xlsx"); + const templateSheet = XLSX.utils.aoa_to_sheet([ + ["院校名称", "院系名称", "专业名称", "教师姓名", "工号", "电话", "邮箱"], + ["请填写系统中的院校名称", "请填写系统中的院系名称", "请填写系统中的专业名称", "张老师", "20260001", "13800000000", "teacher@example.com"], + ]); + templateSheet["!cols"] = [ + { wch: 26 }, + { wch: 24 }, + { wch: 24 }, + { wch: 16 }, + { wch: 18 }, + { wch: 18 }, + { wch: 28 }, + ]; + const guideSheet = XLSX.utils.aoa_to_sheet([ + ["填写说明"], + ["1. 院校、院系和专业名称必须与系统中已维护的名称完全一致。"], + ["2. 教师姓名和工号必填,工号只能填写数字且不能重复。"], + ["3. 电话和邮箱可以留空;填写时必须符合手机号和邮箱格式。"], + ["4. 默认密码为 123qwe,导入成功后请提醒教师及时修改密码。"], + ]); + guideSheet["!cols"] = [{ wch: 80 }]; + const workbook = XLSX.utils.book_new(); + XLSX.utils.book_append_sheet(workbook, templateSheet, "教师导入模板"); + XLSX.utils.book_append_sheet(workbook, guideSheet, "填写说明"); + XLSX.writeFile(workbook, "教师账号导入模板.xlsx"); }; onMounted(() => { diff --git a/tests/teacher-import-template.static.test.cjs b/tests/teacher-import-template.static.test.cjs new file mode 100644 index 0000000..2acc7e7 --- /dev/null +++ b/tests/teacher-import-template.static.test.cjs @@ -0,0 +1,18 @@ +const assert = require("assert"); +const fs = require("fs"); +const path = require("path"); + +const page = fs.readFileSync( + path.resolve(__dirname, "../src/views/teacherEnd/teacher/index.vue"), + "utf8" +); + +assert(/const downloadTemplate = async \(\) =>/.test(page)); +assert(/await import\("xlsx"\)/.test(page)); +assert(/\["院校名称", "院系名称", "专业名称", "教师姓名", "工号", "电话", "邮箱"\]/.test(page)); +assert(/XLSX\.utils\.book_append_sheet\(workbook, templateSheet, "教师导入模板"\)/.test(page)); +assert(/XLSX\.writeFile\(workbook, "教师账号导入模板\.xlsx"\)/.test(page)); +assert(!/\/file\/教师信息表格\.xlsx/.test(page)); +assert(!/a\.href\s*=/.test(page)); + +console.log("teacher import template download contract passed");