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);
|
||||
}
|
||||
};
|
||||
|
||||
94
enlish-vue/src/layouts/components/ExamGenerateDialog.vue
Normal file
94
enlish-vue/src/layouts/components/ExamGenerateDialog.vue
Normal file
@@ -0,0 +1,94 @@
|
||||
<template>
|
||||
<el-dialog v-model="visible" title="生成试题" width="520px" :close-on-click-modal="false">
|
||||
<div class="space-y-4" v-loading="loading">
|
||||
<el-form label-width="80px">
|
||||
<el-form-item label="年级">
|
||||
<el-select v-model="gradeId" placeholder="请选择年级" style="width: 240px">
|
||||
<el-option v-for="g in gradeOptions" :key="g.id" :label="g.title" :value="g.id" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item label="难度">
|
||||
<el-select v-model="level" placeholder="请选择难度" style="width: 240px">
|
||||
<el-option :label="'一级'" :value="1" />
|
||||
<el-option :label="'二级'" :value="2" />
|
||||
<el-option :label="'三级'" :value="3" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<div class="text-sm text-gray-500">
|
||||
已选学生数量:{{ studentIds.length }}
|
||||
</div>
|
||||
</div>
|
||||
<template #footer>
|
||||
<div class="flex justify-end gap-2">
|
||||
<el-button @click="visible = false">取消</el-button>
|
||||
<el-button type="primary" :disabled="!gradeId || !level" @click="handleGenerate">生成并下载</el-button>
|
||||
</div>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, computed, watch, onMounted } from 'vue'
|
||||
import { getGradeList } from '@/api/grade'
|
||||
import { generateExamWords } from '@/api/exam'
|
||||
|
||||
const props = defineProps({
|
||||
modelValue: { type: Boolean, default: false },
|
||||
studentIds: { type: Array, default: () => [] },
|
||||
defaultGradeId: { type: [Number, String], default: null }
|
||||
})
|
||||
const emit = defineEmits(['update:modelValue'])
|
||||
|
||||
const visible = computed({
|
||||
get: () => props.modelValue,
|
||||
set: (val) => emit('update:modelValue', val)
|
||||
})
|
||||
|
||||
const loading = ref(false)
|
||||
const gradeOptions = ref([])
|
||||
const gradeId = ref(null)
|
||||
const level = ref(null)
|
||||
|
||||
async function fetchGrades() {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await getGradeList(1, 100)
|
||||
const d = res?.data
|
||||
gradeOptions.value = Array.isArray(d?.data) ? d.data : []
|
||||
if (props.defaultGradeId && !gradeId.value) {
|
||||
gradeId.value = Number(props.defaultGradeId)
|
||||
}
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function handleGenerate() {
|
||||
if (!gradeId.value || !level.value || props.studentIds.length === 0) return
|
||||
await generateExamWords({
|
||||
gradeId: Number(gradeId.value),
|
||||
level: Number(level.value),
|
||||
studentIds: props.studentIds
|
||||
})
|
||||
ElMessage.success('生成任务已提交,正在下载')
|
||||
visible.value = false
|
||||
}
|
||||
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
(v) => {
|
||||
if (v) {
|
||||
level.value = null
|
||||
gradeId.value = props.defaultGradeId ? Number(props.defaultGradeId) : null
|
||||
fetchGrades()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
onMounted(() => {
|
||||
if (visible.value) fetchGrades()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped></style>
|
||||
@@ -35,8 +35,19 @@
|
||||
selectedGradeId }})</el-tag>
|
||||
<el-button type="primary" @click="fetchStudents">查询</el-button>
|
||||
<el-button @click="resetStudentFilters">重置</el-button>
|
||||
<el-button type="success" :disabled="selectedStudentIds.length === 0" @click="showGenerateDialog = true">
|
||||
生成试题
|
||||
</el-button>
|
||||
</div>
|
||||
<el-table :data="students" border class="w-full" v-loading="studentLoading">
|
||||
<el-table
|
||||
ref="studentTableRef"
|
||||
:data="students"
|
||||
border
|
||||
class="w-full"
|
||||
v-loading="studentLoading"
|
||||
@selection-change="onStudentSelectionChange"
|
||||
>
|
||||
<el-table-column type="selection" width="48" />
|
||||
<el-table-column prop="id" label="ID" width="80" />
|
||||
<el-table-column prop="name" label="姓名" min-width="120" />
|
||||
<el-table-column prop="classId" label="班级ID" width="100" />
|
||||
@@ -47,6 +58,11 @@
|
||||
:total="studentTotalCount" :page-size="studentPageSize" :current-page="studentPageNo"
|
||||
@current-change="handleStudentPageChange" @size-change="handleStudentSizeChange" />
|
||||
</div>
|
||||
<ExamGenerateDialog
|
||||
v-model="showGenerateDialog"
|
||||
:student-ids="selectedStudentIds"
|
||||
:default-grade-id="selectedGradeId"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6" v-loading="gradeLoading">
|
||||
@@ -76,6 +92,7 @@ import { ref, onMounted } from 'vue'
|
||||
import { getClassList } from '@/api/class'
|
||||
import { getGradeList } from '@/api/grade'
|
||||
import { getStudentList } from '@/api/student'
|
||||
import ExamGenerateDialog from '@/layouts/components/ExamGenerateDialog.vue'
|
||||
|
||||
const classes = ref([])
|
||||
const pageNo = ref(1)
|
||||
@@ -85,6 +102,7 @@ const loading = ref(false)
|
||||
const classTableRef = ref(null)
|
||||
const selectedClassId = ref(null)
|
||||
const selectedClassTitle = ref('')
|
||||
|
||||
const grades = ref([])
|
||||
const gradePageNo = ref(1)
|
||||
const gradePageSize = ref(10)
|
||||
@@ -93,12 +111,16 @@ const gradeLoading = ref(false)
|
||||
const gradeTableRef = ref(null)
|
||||
const selectedGradeId = ref(null)
|
||||
const selectedGradeTitle = ref('')
|
||||
|
||||
const students = ref([])
|
||||
const studentPageNo = ref(1)
|
||||
const studentPageSize = ref(10)
|
||||
const studentTotalCount = ref(0)
|
||||
const studentLoading = ref(false)
|
||||
const studentName = ref('')
|
||||
const studentTableRef = ref(null)
|
||||
const selectedStudentIds = ref([])
|
||||
const showGenerateDialog = ref(false)
|
||||
|
||||
async function fetchClasses() {
|
||||
loading.value = true
|
||||
@@ -178,6 +200,9 @@ function handleStudentSizeChange(s) {
|
||||
studentPageNo.value = 1
|
||||
fetchStudents()
|
||||
}
|
||||
function onStudentSelectionChange(rows) {
|
||||
selectedStudentIds.value = rows.map(r => r.id)
|
||||
}
|
||||
function onClassRowClick(row) {
|
||||
selectedClassId.value = row.id
|
||||
selectedClassTitle.value = row.title
|
||||
|
||||
Reference in New Issue
Block a user