feat(plan): 支持学案文件下载功能
- 新增 DownLoadLessonPlanReqVO 请求类用于下载请求封装 - 在前端学案列表增加“下载”按钮,支持单条学案下载操作 - 实现前端下载接口,处理后端返回的 Blob 文件流并触发文件保存 - 后端新增下载接口,根据学案 ID 生成对应的 Word 文档并作为附件响应 - WordExportUtil 中新增按模板生成学案 Word 文档方法,支持工作日和周末模板切换 - LessonPlansService 新增根据 ID 查询学案的方法及对应 Mapper 实现 - 修改学案列表中“学案ID”标签为“计划ID”,提升表述准确性 - 下载过程中添加加载状态和错误信息提示,提升用户体验
This commit is contained in:
@@ -1,16 +1,23 @@
|
|||||||
package com.yinlihupo.enlish.service.controller;
|
package com.yinlihupo.enlish.service.controller;
|
||||||
|
|
||||||
|
import com.yinlihupo.enlish.service.domain.dataobject.LessonPlansDO;
|
||||||
import com.yinlihupo.enlish.service.model.vo.plan.AddLessonPlanReqVO;
|
import com.yinlihupo.enlish.service.model.vo.plan.AddLessonPlanReqVO;
|
||||||
|
import com.yinlihupo.enlish.service.model.vo.plan.DownLoadLessonPlanReqVO;
|
||||||
import com.yinlihupo.enlish.service.service.LessonPlansService;
|
import com.yinlihupo.enlish.service.service.LessonPlansService;
|
||||||
|
import com.yinlihupo.enlish.service.utils.WordExportUtil;
|
||||||
import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog;
|
import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog;
|
||||||
import com.yinlihupo.framework.common.response.Response;
|
import com.yinlihupo.framework.common.response.Response;
|
||||||
|
import com.yinlihupo.framework.common.util.JsonUtils;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
@RequestMapping("/plan/")
|
@RequestMapping("/plan/")
|
||||||
@@ -24,6 +31,11 @@ public class LessonPlanController {
|
|||||||
@Resource(name = "taskExecutor")
|
@Resource(name = "taskExecutor")
|
||||||
private Executor taskExecutor;
|
private Executor taskExecutor;
|
||||||
|
|
||||||
|
@Value("${templates.plan.weekday}")
|
||||||
|
private String planWeekday;
|
||||||
|
@Value("${templates.plan.weekend}")
|
||||||
|
private String planWeekend;
|
||||||
|
|
||||||
@PostMapping("generate")
|
@PostMapping("generate")
|
||||||
@ApiOperationLog(description = "生成学案")
|
@ApiOperationLog(description = "生成学案")
|
||||||
public Response<String> generateLessonPlan(@RequestBody AddLessonPlanReqVO addLessonPlanReqVO) {
|
public Response<String> generateLessonPlan(@RequestBody AddLessonPlanReqVO addLessonPlanReqVO) {
|
||||||
@@ -38,4 +50,22 @@ public class LessonPlanController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("download")
|
||||||
|
public void downloadLessonPlan(@RequestBody DownLoadLessonPlanReqVO downLoadLessonPlanReqVO, HttpServletResponse response) {
|
||||||
|
Integer id = downLoadLessonPlanReqVO.getId();
|
||||||
|
LessonPlansDO lessonPlanById = lessonPlanService.findLessonPlanById(id);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Map<String, Object> map = JsonUtils.parseMap(lessonPlanById.getContentDetails(), String.class, Object.class);
|
||||||
|
if (!lessonPlanById.getTitle().contains("复习")) {
|
||||||
|
WordExportUtil.generateLessonPlanDocx(map, lessonPlanById.getTitle(), response, planWeekday, true);
|
||||||
|
} else {
|
||||||
|
WordExportUtil.generateLessonPlanDocx(map, lessonPlanById.getTitle(), response, planWeekend, false);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,4 +12,6 @@ public interface LessonPlansDOMapper {
|
|||||||
LessonPlansDO selectById(Integer id);
|
LessonPlansDO selectById(Integer id);
|
||||||
|
|
||||||
List<LessonPlansDO> findLessonPlansByStudentId(@Param("ids") List<Integer> ids);
|
List<LessonPlansDO> findLessonPlansByStudentId(@Param("ids") List<Integer> ids);
|
||||||
|
|
||||||
|
LessonPlansDO selectByLessonId(@Param("lessonId") Integer lessonId);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.yinlihupo.enlish.service.model.vo.plan;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class DownLoadLessonPlanReqVO {
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
|
}
|
||||||
@@ -8,4 +8,6 @@ public interface LessonPlansService {
|
|||||||
void generateLessonPlans(Integer studentId, Integer unitId);
|
void generateLessonPlans(Integer studentId, Integer unitId);
|
||||||
|
|
||||||
List<LessonPlansDO> findLessonPlans(List<Integer> ids);
|
List<LessonPlansDO> findLessonPlans(List<Integer> ids);
|
||||||
|
|
||||||
|
LessonPlansDO findLessonPlanById(Integer id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,6 +154,11 @@ public class LessonPlansServiceImpl implements LessonPlansService {
|
|||||||
return lessonPlansDOMapper.findLessonPlansByStudentId(ids);
|
return lessonPlansDOMapper.findLessonPlansByStudentId(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LessonPlansDO findLessonPlanById(Integer id) {
|
||||||
|
return lessonPlansDOMapper.selectByLessonId(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private Map<String, Object> generateWeekendPlans(List<VocabularyBankDO> checkList,
|
private Map<String, Object> generateWeekendPlans(List<VocabularyBankDO> checkList,
|
||||||
int day,
|
int day,
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ import java.util.zip.ZipOutputStream;
|
|||||||
public class WordExportUtil {
|
public class WordExportUtil {
|
||||||
|
|
||||||
private static final Configure config;
|
private static final Configure config;
|
||||||
|
private static final Configure configLessonPlanWeekday;
|
||||||
|
private static final Configure configLessonPlanWeekend;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
|
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
|
||||||
@@ -23,6 +25,24 @@ public class WordExportUtil {
|
|||||||
.bind("words", policy)
|
.bind("words", policy)
|
||||||
.bind("answer", policy)
|
.bind("answer", policy)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
LoopRowTableRenderPolicy policyLessonPlanWeekday = new LoopRowTableRenderPolicy();
|
||||||
|
configLessonPlanWeekday = Configure.builder()
|
||||||
|
.bind("syncVocabList", policyLessonPlanWeekday)
|
||||||
|
.bind("gapVocabList", policyLessonPlanWeekday)
|
||||||
|
.bind("reviewVocabList", policyLessonPlanWeekday)
|
||||||
|
.bind("drillRound1", policyLessonPlanWeekday)
|
||||||
|
.bind("drillRound2", policyLessonPlanWeekday)
|
||||||
|
.bind("drillRound3", policyLessonPlanWeekday)
|
||||||
|
.bind("mixedDrill", policyLessonPlanWeekday)
|
||||||
|
.bind("checkList", policyLessonPlanWeekday)
|
||||||
|
.bind("checkListAns", policyLessonPlanWeekday)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
LoopRowTableRenderPolicy policyLessonPlan = new LoopRowTableRenderPolicy();
|
||||||
|
configLessonPlanWeekend = Configure.builder()
|
||||||
|
.bind("checkList", policyLessonPlan)
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,6 +67,28 @@ public class WordExportUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void generateLessonPlanDocx(Map<String, Object> map, String fileName, HttpServletResponse response, String templateWordPath, boolean isWeekday) throws IOException {
|
||||||
|
fileName = URLEncoder.encode(fileName + ".docx", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
|
||||||
|
|
||||||
|
// 3. 设置响应头
|
||||||
|
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||||
|
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName);
|
||||||
|
|
||||||
|
try (InputStream inputStream = new FileInputStream(templateWordPath)) {
|
||||||
|
XWPFTemplate template;
|
||||||
|
if (isWeekday) {
|
||||||
|
template = XWPFTemplate.compile(inputStream, configLessonPlanWeekday);
|
||||||
|
} else {
|
||||||
|
template = XWPFTemplate.compile(inputStream, configLessonPlanWeekend);
|
||||||
|
}
|
||||||
|
OutputStream out = response.getOutputStream();
|
||||||
|
template.render(map);
|
||||||
|
template.write(out);
|
||||||
|
template.close();
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 核心补充:批量渲染并打包为 ZIP
|
* 核心补充:批量渲染并打包为 ZIP
|
||||||
*/
|
*/
|
||||||
@@ -103,7 +145,7 @@ public class WordExportUtil {
|
|||||||
*/
|
*/
|
||||||
private static void generateExamWordsDocx(Map<String, Object> data, HttpServletResponse response, String templateWordPath) throws IOException {
|
private static void generateExamWordsDocx(Map<String, Object> data, HttpServletResponse response, String templateWordPath) throws IOException {
|
||||||
|
|
||||||
String fileName = URLEncoder.encode("摸底测试" + ".docx", StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20");
|
String fileName = URLEncoder.encode("摸底测试" + ".docx", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
|
||||||
|
|
||||||
// 3. 设置响应头
|
// 3. 设置响应头
|
||||||
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||||
|
|||||||
@@ -33,4 +33,10 @@
|
|||||||
</if>
|
</if>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="selectByLessonId" resultMap="ResultMapWithBLOBs">
|
||||||
|
select *
|
||||||
|
from lesson_plans
|
||||||
|
where id = #{lessonId}
|
||||||
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -5,4 +5,65 @@ export function generateLessonPlan(studentId, unitId) {
|
|||||||
studentId: studentId,
|
studentId: studentId,
|
||||||
unitId: unitId
|
unitId: unitId
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function downloadLessonPlan(data) {
|
||||||
|
return axios.post('/plan/download', 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -19,16 +19,25 @@
|
|||||||
<div class="p-3">
|
<div class="p-3">
|
||||||
<div class="text-sm font-semibold mb-2">学案</div>
|
<div class="text-sm font-semibold mb-2">学案</div>
|
||||||
<el-table :data="row.plans || []" size="small" border>
|
<el-table :data="row.plans || []" size="small" border>
|
||||||
<el-table-column prop="id" label="学案ID" width="100" />
|
<el-table-column prop="id" label="计划ID" width="100" />
|
||||||
<el-table-column prop="title" label="标题" min-width="280" />
|
<el-table-column prop="title" label="标题" min-width="280" />
|
||||||
<el-table-column label="状态" width="120">
|
<el-table-column label="状态" width="120">
|
||||||
<template #default="{ row: plan }">
|
<template #default="{ row: plan }">
|
||||||
<el-tag :type="plan.isFinished === 1 ? 'success' : 'info'"
|
<el-tag :type="plan.isFinished === 1 ? 'success' : 'info'" effect="plain">
|
||||||
effect="plain">
|
|
||||||
{{ plan.isFinished === 1 ? '已完成' : '未完成' }}
|
{{ plan.isFinished === 1 ? '已完成' : '未完成' }}
|
||||||
</el-tag>
|
</el-tag>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
|
<el-table-column label="操作" width="120" fixed="right">
|
||||||
|
<template #default="{ row: plan }">
|
||||||
|
<el-button
|
||||||
|
type="primary"
|
||||||
|
size="small"
|
||||||
|
:loading="downloadingIds.includes(plan.id)"
|
||||||
|
@click="onDownload(plan)"
|
||||||
|
>下载</el-button>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
</el-table>
|
</el-table>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@@ -54,6 +63,8 @@
|
|||||||
import Header from '@/layouts/components/Header.vue'
|
import Header from '@/layouts/components/Header.vue'
|
||||||
import { ref, onMounted } from 'vue'
|
import { ref, onMounted } from 'vue'
|
||||||
import { findStudentLessonPlans } from '@/api/studentLessonPlans'
|
import { findStudentLessonPlans } from '@/api/studentLessonPlans'
|
||||||
|
import { downloadLessonPlan } from '@/api/plan'
|
||||||
|
import { showMessage } from '@/composables/util'
|
||||||
|
|
||||||
const rows = ref([])
|
const rows = ref([])
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
@@ -62,6 +73,7 @@ const pageSize = ref(10)
|
|||||||
const totalCount = ref(0)
|
const totalCount = ref(0)
|
||||||
const searchName = ref('')
|
const searchName = ref('')
|
||||||
const tableRef = ref(null)
|
const tableRef = ref(null)
|
||||||
|
const downloadingIds = ref([])
|
||||||
|
|
||||||
async function fetchLessonPlans() {
|
async function fetchLessonPlans() {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
@@ -96,6 +108,22 @@ function onReset() {
|
|||||||
fetchLessonPlans()
|
fetchLessonPlans()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function onDownload(plan) {
|
||||||
|
if (!plan?.id) {
|
||||||
|
showMessage('无效的计划ID', 'error')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (!downloadingIds.value.includes(plan.id)) {
|
||||||
|
downloadingIds.value = [...downloadingIds.value, plan.id]
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await downloadLessonPlan({ id: plan.id })
|
||||||
|
showMessage('开始下载', 'success')
|
||||||
|
} finally {
|
||||||
|
downloadingIds.value = downloadingIds.value.filter(id => id !== plan.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
fetchLessonPlans()
|
fetchLessonPlans()
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user