feat(cargo-type): 优化货物类型导入模板下载及导入逻辑

- 将导入模板下载改为直接获取工程内置静态文件,取消本地生成Excel逻辑
- 下载模板含一级19类及全部二级示例,便于用户填写层级关系和编码规则
- 后端导入逻辑改为两遍法,先验证保存所有一级类型,再导入二级,确保导入顺序无影响
- 保持事务回滚、失败明细Excel生成及校验文案一致
- 提示模板文件缺失或下载失败的错误信息,提升用户体验
- 前端改动已生效,后端需重新编译部署以完成加固
This commit is contained in:
2026-07-28 16:13:13 +08:00
parent 72c6abf122
commit d48b18c99e
3 changed files with 16 additions and 67 deletions

View File

@@ -584,76 +584,20 @@ export default {
},
handleTemplate() {
NProgress.start();
// 本地生成带示例的导入模板(不依赖后端空模板),表头与可正常导入的模板一致
import('exceljs')
.then(ExcelJS => {
const ExcelJSLib = ExcelJS.default || ExcelJS;
const wb = new ExcelJSLib.Workbook();
const ws = wb.addWorksheet('货物类型导入模板');
const headers = [
'*类型',
'上级货物类型(如为一级则不需填写)',
'上级货物类型编码(如为一级则不需填写)',
'*货物类型',
'*货物类型编码',
'备注',
];
ws.addRow(headers);
// 示例数据:一级 + 二级各一条,演示层级关系与填写格式(当前环境可正常导入)
ws.addRow(['一级货物类型', null, null, '电子产品2', '117', '消费类电子']);
ws.addRow(['二级货物类型', '电子产品2', '117', '手机2', '11014', '智能手机']);
// 列宽:表头含长说明,适度加宽
const widths = [14, 24, 26, 16, 16, 18];
ws.columns.forEach((col, i) => {
col.width = widths[i] || 16;
});
// 编码列设为文本,避免前导零丢失
[3, 5].forEach(idx => {
const col = ws.getColumn(idx);
if (col) col.numFmt = '@';
});
// 表头与数据区样式:统一灰色表头 + 黑色加粗居中 + 细边框
const headerFill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'FFD9D9D9' },
};
const thinBorder = {
top: { style: 'thin', color: { argb: 'FFD0D7E2' } },
left: { style: 'thin', color: { argb: 'FFD0D7E2' } },
bottom: { style: 'thin', color: { argb: 'FFD0D7E2' } },
right: { style: 'thin', color: { argb: 'FFD0D7E2' } },
};
ws.eachRow(row => {
row.eachCell(cell => {
cell.border = thinBorder;
if (row.number === 1) {
// 表头:灰色底色 + 黑字加粗 + 居中对齐
cell.fill = headerFill;
cell.font = { bold: true, color: { argb: 'FF000000' } };
cell.alignment = {
wrapText: true,
vertical: 'middle',
horizontal: 'center',
};
} else {
// 数据行:左对齐,垂直居中
cell.alignment = {
wrapText: true,
vertical: 'middle',
horizontal: 'left',
};
}
});
});
// 表头行高适度,兼顾两行说明与视觉紧凑
ws.getRow(1).height = 30;
return wb.xlsx.writeBuffer();
// 直接下载工程内置的标准导入模板public 目录静态资源),表头与可正常导入的模板一致
// 含一级19 类)及全部二级示例,便于用户参照填写层级关系与编码规则
const templateUrl = `${import.meta.env.BASE_URL}cargo-type-import-template.xlsx`;
fetch(templateUrl)
.then(res => {
if (!res.ok) throw new Error('模板文件不存在');
return res.blob();
})
.then(buf => {
const blob = new Blob([buf], { type: 'application/vnd.ms-excel' });
.then(blob => {
downloadXls(blob, '货物类型导入模板.xlsx');
})
.catch(() => {
this.$message.error('模板下载失败,请稍后重试');
})
.finally(() => {
NProgress.done();
});