2026-03-18 17:47:43 +08:00

261 lines
6.5 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="canvas-template-editor">
<!-- 模板列表 -->
<div class="left">
<div class="title">
<el-icon><Document /></el-icon> 模板列表
</div>
<div class="template-tree">
<el-tree
ref="presetTreeRef"
:data="filteredPresetTree"
:props="treeProps"
node-key="id"
accordion
highlight-current
:expand-on-click-node="false"
@node-click="handlePresetClick"
>
<template #default="{ node, data }">
<span class="custom-tree-node">
<span class="tree-node-content">
<el-icon v-if="data.type" class="tree-icon">
<Folder v-if="data.type === 'category'" />
<Document v-else />
</el-icon>
<span class="node-label">{{ $t(data.title) }}</span>
<span v-if="data.count" class="count-badge">{{ data.count }}</span>
</span>
</span>
</template>
</el-tree>
</div>
</div>
<div class="right">
<template-editor
v-if="hasTemplate"
:print-items="printItems"
@save="saveTemplateInfo"
@reset="resetTemplateInfo"
@undo="undoTemplateChange"
/>
<div v-else class="empty-state">
<el-empty description="请选择一个模板进行编辑" />
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Document, Folder } from '@element-plus/icons-vue'
import TemplateEditor from './components/TemplateEditor.vue'
import * as TemplatesApi from '@/api/printTemplate'
import { usePrintTemplateStore } from '@/stores/modules/printTemplate'
import { storeToRefs } from 'pinia'
const printTemplateStore = usePrintTemplateStore()
const { templateInfo, templateObjects } = storeToRefs(printTemplateStore)
const filteredPresetTree = ref([])
const treeProps = {
children: 'children',
label: 'title'
}
const templateSelectItem = ref<any>(null)
const printItems = ref<any[]>([])
const hasTemplate = computed(() => {
return !!templateInfo.value.width
})
const handlePresetClick = async (data: any, node: any) => {
// 针对 RECEIPT 这种 businessType 节点,它可能既是 category 又包含 printItems,
// 且没有子节点(children 为空),这种情况下应该允许加载
if (data.type === 'category' && data.children && data.children.length > 0) {
node.expanded = !node.expanded
return
}
// 查找可用元素列表(printItems)
// 优先从当前节点找,如果没有则从父节点找
if (data.printItems && data.printItems.length > 0) {
printItems.value = data.printItems
} else if (node.parent && node.parent.data && node.parent.data.printItems) {
printItems.value = node.parent.data.printItems
} else {
printItems.value = []
}
templateSelectItem.value = JSON.parse(JSON.stringify(data))
try {
await printTemplateStore.loadTemplate(templateSelectItem.value)
console.log('模板加载成功:', templateInfo.value)
ElMessage.success('模板加载成功')
} catch (error: any) {
console.error('加载模板失败:', error)
ElMessage.error('加载模板失败: ' + (error.message || '未知错误'))
}
}
const loadTemplates = async () => {
try {
const res: any = await TemplatesApi.listTemplates({})
if (res.code === 200) {
filteredPresetTree.value = res.presetTree || [];
console.log('加载模板树成功:', filteredPresetTree.value)
} else {
ElMessage.error('加载模板失败: ' + (res.message || '未知错误'))
}
} catch (error) {
console.error('加载模板树失败:', error)
ElMessage.error('加载模板失败')
}
}
const saveTemplateInfo = async () => {
const info = templateInfo.value
const saveData = {
id: info.id,
template_name: info.template_name,
business_type: info.businessType,
page_width: info.width,
page_height: info.height,
template_content: templateObjects.value
}
try {
const res: any = await TemplatesApi.saveTemplate(saveData)
if (res.code === 200) {
ElMessage.success('模板保存成功')
// 保存成功后更新初始快照,这样重置时会回到这个已保存的状态
printTemplateStore.updateInitialSnapshot()
loadTemplates() // 刷新列表
} else {
ElMessage.error('模板保存失败: ' + (res.message || '未知错误'))
}
} catch (error) {
ElMessage.error('保存失败')
}
}
const resetTemplateInfo = async () => {
if (!templateSelectItem.value) {
ElMessage.warning('请先选择一个模板')
return
}
try {
// 使用 store 提供的完整重置方法,恢复为初始载入状态并清空历史
printTemplateStore.resetTemplate()
ElMessage.success('重置成功,已撤销所有操作')
} catch (error) {
console.error('重置模板失败:', error)
ElMessage.error('重置失败')
}
}
const undoTemplateChange = () => {
printTemplateStore.undo()
}
onMounted(() => {
loadTemplates()
})
</script>
<style scoped lang="scss">
.canvas-template-editor {
height: calc(100vh - 84px);
width: 100%;
display: flex;
flex-direction: row;
background: #f5f7fa;
overflow: hidden;
.left {
width: 300px;
height: 100%;
background: #ffffff;
border-right: 1px solid #dcdfe6;
flex-shrink: 0;
display: flex;
flex-direction: column;
.title {
font-size: 16px;
font-weight: 600;
padding: 20px;
border-bottom: 1px solid #dcdfe6;
display: flex;
align-items: center;
gap: 8px;
}
}
.right {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
position: relative;
background: #f0f2f5;
.empty-state {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
}
}
.template-tree {
padding: 12px;
overflow: auto;
flex: 1;
}
.tree-icon {
color: #409eff;
font-size: 14px;
margin-right: 6px;
}
.custom-tree-node {
display: flex;
align-items: center;
width: 100%;
}
.tree-node-content {
display: flex;
align-items: center;
flex: 1;
}
.count-badge {
margin-left: auto;
background: #f0f2f5;
padding: 2px 6px;
border-radius: 10px;
font-size: 12px;
color: #909399;
}
@media (max-width: 992px) {
.canvas-template-editor {
flex-direction: column;
.left {
width: 100%;
height: 300px;
border-right: none;
border-bottom: 1px solid #dcdfe6;
}
}
}
</style>