160 lines
4.2 KiB
Vue
160 lines
4.2 KiB
Vue
<!--
|
||
DetailPanel — 明细面板(通用)
|
||
|
||
根据 detailConfig 配置的 sections 渲染 el-descriptions 分区。
|
||
支持 text、tag、pre 三种渲染类型。
|
||
支持 translateField + tagResolver 集中式翻译。
|
||
支持 apiMethod 自动获取详情数据。
|
||
-->
|
||
<template>
|
||
<div class="detail-panel">
|
||
<div v-if="!row" class="empty-tip">请从上方列表中选择一项查看明细</div>
|
||
<template v-else>
|
||
<div v-for="(section, si) in detailConfig" :key="si" class="section">
|
||
<div class="section-title">{{ section.title }}</div>
|
||
<el-descriptions :column="section.columnCount" border size="small" class="detail-descriptions">
|
||
<el-descriptions-item
|
||
v-for="(field, fi) in section.fields"
|
||
:key="fi"
|
||
:label="field.label"
|
||
:span="field.span ?? 1"
|
||
>
|
||
<!-- tag 渲染 -->
|
||
<template v-if="field.renderType === 'tag'">
|
||
<el-tag size="small" :type="resolveTagType(field, getRowData()?.[field.key])">
|
||
{{ resolveDisplayValue(field, getRowData()?.[field.key]) }}
|
||
</el-tag>
|
||
</template>
|
||
<!-- pre 渲染 -->
|
||
<template v-else-if="field.renderType === 'pre'">
|
||
<pre class="order-list-pre">{{ getRowData()?.[field.key] ?? '—' }}</pre>
|
||
</template>
|
||
<!-- 默认文本渲染 -->
|
||
<template v-else>
|
||
{{ resolveDisplayValue(field, getRowData()?.[field.key]) }}
|
||
</template>
|
||
</el-descriptions-item>
|
||
</el-descriptions>
|
||
</div>
|
||
</template>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, watch } from 'vue'
|
||
import { TAG_RESOLVERS, translate } from '../template-config'
|
||
import type { DetailSectionConfig, DetailFieldConfig } from '../template-config'
|
||
|
||
defineOptions({ name: 'DetailPanel' })
|
||
|
||
const props = defineProps<{
|
||
row: Record<string, any> | null
|
||
detailConfig: DetailSectionConfig[]
|
||
/** 可选的 detail API 方法,传入后自动调用获取完整明细数据 */
|
||
apiMethod?: (...args: any[]) => Promise<any>
|
||
}>()
|
||
|
||
/** 通过 API 获取的增强数据(与 row 合并) */
|
||
const enrichedData = ref<Record<string, any> | null>(null)
|
||
|
||
watch(() => props.row, async (newRow) => {
|
||
if (newRow && props.apiMethod) {
|
||
try {
|
||
const res = await props.apiMethod({
|
||
id: newRow.id,
|
||
code: newRow.code
|
||
})
|
||
if (res && (res.code === 1 || res.code === 200)) {
|
||
enrichedData.value = res.data ?? res
|
||
} else {
|
||
enrichedData.value = null
|
||
}
|
||
} catch {
|
||
enrichedData.value = null
|
||
}
|
||
} else {
|
||
enrichedData.value = null
|
||
}
|
||
}, { immediate: true })
|
||
|
||
/** 合并 row + API 增强数据 */
|
||
function getRowData(): Record<string, any> | null {
|
||
if (!props.row) return null
|
||
if (enrichedData.value) {
|
||
return { ...props.row, ...enrichedData.value }
|
||
}
|
||
return props.row
|
||
}
|
||
|
||
/** 解析显示值:优先使用 translateField 翻译,否则显示原始值 */
|
||
function resolveDisplayValue(field: DetailFieldConfig, value: any): string {
|
||
if (field.translateField) {
|
||
return translate(field.translateField, value)
|
||
}
|
||
return value ?? '—'
|
||
}
|
||
|
||
/** 解析 tag 颜色 */
|
||
function resolveTagType(field: DetailFieldConfig, value: any): string {
|
||
if (field.tagResolver) {
|
||
const resolver = TAG_RESOLVERS[field.tagResolver]
|
||
if (resolver) return resolver(value)
|
||
}
|
||
if (field.tagType) {
|
||
if (typeof field.tagType === 'object') {
|
||
return field.tagType[String(value)] ?? 'info'
|
||
}
|
||
return field.tagType
|
||
}
|
||
return 'info'
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.detail-panel {
|
||
padding: 8px 0;
|
||
}
|
||
|
||
.empty-tip {
|
||
text-align: center;
|
||
font-size: 13px;
|
||
color: #909399;
|
||
padding: 40px 0;
|
||
}
|
||
|
||
.section {
|
||
margin-bottom: 12px;
|
||
|
||
&:last-child {
|
||
margin-bottom: 0;
|
||
}
|
||
|
||
.section-title {
|
||
font-size: 14px;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
margin-bottom: 10px;
|
||
padding-left: 8px;
|
||
border-left: 3px solid #409eff;
|
||
line-height: 1.4;
|
||
}
|
||
}
|
||
|
||
.detail-descriptions {
|
||
:deep(.el-descriptions__label) {
|
||
width: 120px;
|
||
background: #fafbfc;
|
||
font-weight: 500;
|
||
}
|
||
}
|
||
|
||
.order-list-pre {
|
||
margin: 0;
|
||
font-family: inherit;
|
||
font-size: 13px;
|
||
line-height: 1.8;
|
||
white-space: pre-wrap;
|
||
color: #606266;
|
||
}
|
||
</style>
|