元数据模板导出 excel导入

This commit is contained in:
2026-10-23 18:45:12 +08:00
parent 8b6f00b117
commit ddc0864f06
8 changed files with 805 additions and 4 deletions
+209 -4
View File
@@ -1,12 +1,15 @@
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import { Refresh, Search } from '@element-plus/icons-vue'
import { Download, Refresh, Search, Upload } from '@element-plus/icons-vue'
import {
downloadTemplate,
getResource,
importResource,
pageResource,
removeResource,
saveResource,
type ImportResult,
type ResourceQuery,
type VersionResource
} from '@/api/resource'
@@ -113,6 +116,58 @@ async function saveEdit() {
}
}
/* ---------- 模板导出 / Excel 导入 ---------- */
const fileInput = ref<HTMLInputElement>()
const importing = ref(false)
const resultVisible = ref(false)
const importResult = ref<ImportResult | null>(null)
async function handleDownloadTemplate() {
const resp = await downloadTemplate()
const blob = new Blob([resp.data], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = '版本资源导入模板.xlsx'
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
}
function triggerImport() {
fileInput.value?.click()
}
async function handleFileChange(e: Event) {
const el = e.target as HTMLInputElement
const file = el.files?.[0]
if (!file) return
if (!/\.(xlsx|xls)$/i.test(file.name)) {
ElMessage.error('请选择 Excel 文件(.xlsx / .xls)')
el.value = ''
return
}
importing.value = true
try {
const res = await importResource(file)
importResult.value = res
resultVisible.value = true
if (res.inserted || res.updated) {
ElMessage.success(`导入完成:新增 ${res.inserted} 条,更新 ${res.updated} 条`)
query.current = 1
fetchData()
}
} finally {
importing.value = false
el.value = ''
}
}
onMounted(fetchData)
</script>
@@ -149,7 +204,28 @@ onMounted(fetchData)
</div>
<div class="page-card">
<h3 class="page-title">版本资源库(元数据)</h3>
<div class="table-head">
<h3 class="page-title">版本资源库(元数据)</h3>
<div class="head-actions">
<el-button :icon="Download" @click="handleDownloadTemplate">模板导出</el-button>
<el-button
type="primary"
plain
:icon="Upload"
:loading="importing"
@click="triggerImport"
>
Excel 导入
</el-button>
<input
ref="fileInput"
type="file"
accept=".xlsx,.xls"
class="file-input"
@change="handleFileChange"
/>
</div>
</div>
<el-table v-loading="loading" :data="list" border stripe>
<el-table-column type="index" label="#" width="50" />
<el-table-column prop="title" label="正题名" min-width="200" show-overflow-tooltip>
@@ -318,6 +394,38 @@ onMounted(fetchData)
<el-button type="primary" :loading="saving" @click="saveEdit">保存</el-button>
</template>
</el-drawer>
<!-- 导入结果 -->
<el-dialog v-model="resultVisible" title="导入结果" width="560px">
<div v-if="importResult" class="imp-summary">
<div class="imp-item">
<b>{{ importResult.total }}</b><em>解析行数</em>
</div>
<div class="imp-item ok">
<b>{{ importResult.inserted }}</b><em>新增</em>
</div>
<div class="imp-item upd">
<b>{{ importResult.updated }}</b><em>更新</em>
</div>
<div class="imp-item err">
<b>{{ importResult.failed }}</b><em>失败</em>
</div>
</div>
<div v-if="importResult?.errors?.length" class="imp-errors">
<div class="imp-errors-title">失败明细</div>
<ul>
<li v-for="(msg, i) in importResult.errors" :key="i">{{ msg }}</li>
</ul>
</div>
<p v-else-if="importResult && !importResult.failed" class="imp-ok-tip">
全部数据导入成功。
</p>
<template #footer>
<el-button type="primary" @click="resultVisible = false">知道了</el-button>
</template>
</el-dialog>
</div>
</template>
@@ -327,10 +435,107 @@ onMounted(fetchData)
justify-content: flex-end;
}
.table-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 16px;
.page-title {
margin: 0;
}
}
.head-actions {
display: flex;
align-items: center;
gap: 10px;
}
.file-input {
display: none;
}
/* 导入结果 */
.imp-summary {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
margin-bottom: 16px;
}
.imp-item {
padding: 14px 8px;
text-align: center;
background: var(--brand-light);
border: 1px solid var(--card-border);
border-radius: var(--radius-sm);
b {
display: block;
font-family: 'Noto Serif SC', 'Songti SC', serif;
font-size: 22px;
line-height: 1.3;
color: var(--brand-primary-deep);
}
em {
font-style: normal;
font-size: 12px;
color: var(--text-muted);
}
&.ok b {
color: #3f7d5c;
}
&.upd b {
color: #a3703f;
}
&.err b {
color: #c0392b;
}
}
.imp-errors {
max-height: 220px;
overflow-y: auto;
padding: 10px 12px;
background: #fdf6f4;
border: 1px solid #f2dcd6;
border-radius: var(--radius-sm);
.imp-errors-title {
margin-bottom: 6px;
font-size: 13px;
font-weight: 600;
color: #b03a2e;
}
ul {
margin: 0;
padding-left: 18px;
}
li {
font-size: 12px;
line-height: 1.9;
color: #8a5a50;
}
}
.imp-ok-tip {
margin: 0;
font-size: 13px;
color: #3f7d5c;
}
.special-tag {
margin-right: 6px;
background: var(--brand-gold-deep);
border-color: var(--brand-gold-deep);
background: var(--brand-primary);
border-color: var(--brand-primary);
color: #fff;
}