feat(exam): 实现按学生批量生成并下载试题功能
- 增加学生多选功能和生成试题按钮,支持批量操作 - 新增ExamGenerateDialog组件,提供选择年级和难度界面 - 设计后端接口支持多个学生ID,生成对应的试题文档 - 在后端实现批量生成Word文档并压缩打包下载 - 新增StudentDetail业务对象,完善学生信息展示 - 优化了Mapper接口及XML,支持批量查询学生和班级数据 - 提供前端API封装用于调用试题生成和下载服务 - 实现下载失败时的错误处理与提示机制
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import axios from "@/axios";
|
||||
import { showMessage } from "../composables/util";
|
||||
|
||||
export function uploadExamWordsPng(data) {
|
||||
return axios.post('/exam/words/submit', data)
|
||||
@@ -16,3 +17,64 @@ export function getExamWordsDetailResult(id) {
|
||||
id: id
|
||||
})
|
||||
}
|
||||
|
||||
export function generateExamWords(data) {
|
||||
return axios.post('/exam/words/generate', data, {
|
||||
// 1. 重要:必须指定响应类型为 blob,否则下载的文件会损坏(乱码)
|
||||
responseType: 'blob',
|
||||
headers: {
|
||||
'Content-Type': 'application/json; application/octet-stream' // 根据需要调整
|
||||
}
|
||||
}).then(response => {
|
||||
// 2. 提取文件名 (处理后端设置的 Content-Disposition)
|
||||
// 后端示例: header("Content-Disposition", "attachment; filename*=UTF-8''" + fileName)
|
||||
let fileName = 'download.zip'; // 默认兜底文件名
|
||||
|
||||
const contentDisposition = response.headers['content-disposition'];
|
||||
if (contentDisposition) {
|
||||
// 正则提取 filename*=utf-8''xxx.zip 或 filename="xxx.zip"
|
||||
const fileNameMatch = contentDisposition.match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/);
|
||||
if (fileNameMatch && fileNameMatch[1]) {
|
||||
// 后端如果用了 URLEncoder,这里需要 decode
|
||||
fileName = decodeURIComponent(fileNameMatch[1]);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 开始下载流程
|
||||
resolveBlob(response.data, fileName);
|
||||
}).catch(error => {
|
||||
console.error('下载失败', error);
|
||||
showMessage('下载失败' + error, 'error');
|
||||
// 注意:如果后端报错返回 JSON,因为 responseType 是 blob,
|
||||
// 这里看到的 error.response.data 也是 blob,需要转回文本才能看到错误信息
|
||||
const reader = new FileReader();
|
||||
reader.readAsText(error.response.data);
|
||||
reader.onload = () => {
|
||||
console.log(JSON.parse(reader.result)); // 打印后端实际报错
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const resolveBlob = (res, fileName) => {
|
||||
// 创建 Blob 对象,可以指定 type,也可以让浏览器自动推断
|
||||
const blob = new Blob([res], { type: 'application/octet-stream' });
|
||||
|
||||
// 兼容 IE/Edge (虽然现在很少用了)
|
||||
if (window.navigator.msSaveOrOpenBlob) {
|
||||
navigator.msSaveBlob(blob, fileName);
|
||||
} else {
|
||||
// 创建一个临时的 URL 指向 Blob
|
||||
const link = document.createElement('a');
|
||||
link.href = window.URL.createObjectURL(blob);
|
||||
link.download = fileName;
|
||||
|
||||
// 触发点击
|
||||
link.style.display = 'none';
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
|
||||
// 清理资源
|
||||
document.body.removeChild(link);
|
||||
window.URL.revokeObjectURL(link.href);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user