Files
2026-09-12 11:46:38 +08:00

171 lines
4.3 KiB
Smarty

<template>
<a-modal
v-model:visible="props.visible"
title="{{ module_comment }}详情"
width="750px"
@cancel="dialogClose"
:mask-closable="false"
>
<a-descriptions
class="margin-top"
:column="2"
bordered
:label-style="{ width: '120px' }"
>
{% for field in form_fields %}
<a-descriptions-item :label="'{{ field.label }}:'">
{% if field.is_image %}
<a-image
v-if="formData.{{ field.camel_name }}"
:src="formData.{{ field.camel_name }}"
:preview="true"
:width="80"
:height="80"
fit="cover"
/>
<span v-else>-</span>
{% elif field.is_rich_text %}
<div class="rich-text-content" v-html="formData.{{ field.camel_name }}"></div>
{% elif field.type == 'BooleanField' %}
<a-tag :color="formData.{{ field.camel_name }} ? 'success' : 'error'">
{{ '{{ formData.' + field.camel_name + ' ? "是" : "否" }}' }}
</a-tag>
{% elif field.type == 'IntegerField' and field.choices %}
<span>{{ '{{ getChoiceLabel(\'' + field.camel_name + '\', formData.' + field.camel_name + ') }}' }}</span>
{% else %}
<span>{{ '{{ formData.' + field.camel_name + ' || \'-\' }}' }}</span>
{% endif %}
</a-descriptions-item>
{% endfor %}
</a-descriptions>
<template #footer>
<span class="dialog-footer">
<a-button @click="dialogClose">关闭</a-button>
</span>
</template>
</a-modal>
</template>
<script lang="ts" setup>
import { get{{ model_class_name }}Detail } from '@/api/{{ api_path }}';
import { onMounted, reactive, watch } from 'vue';
{% if has_image_field %}
import { Image as AImage } from 'ant-design-vue';
{% endif %}
const emit = defineEmits(['update:visible']);
/**
* 定义表单参数
*/
const formData = reactive({
id: '',
{% for field in form_fields %}
{{ field.camel_name }}: {% if field.type == 'CharField' %}''{% elif field.type == 'IntegerField' %}0{% elif field.type == 'BooleanField' %}false{% elif field.type == 'FloatField' %}0{% else %}''{% endif %},
{% endfor %}
});
/**
* 定义接收的参数
*/
const props = defineProps({
visible: {
type: Boolean,
required: true,
default: false,
},
{{ primary_key }}: {
type: Number,
required: true,
default: 0,
},
});
/**
* 获取选项字段的标签
*/
const getChoiceLabel = (fieldName: string, value: any) => {
const choiceMap: Record<string, Record<string, string>> = {
{% for field in form_fields %}
{% if field.type == 'IntegerField' and field.choices %}
{{ field.camel_name }}: {
{% for choice in field.choices %}
{{ choice.value }}: '{{ choice.label }}',
{% endfor %}
},
{% endif %}
{% endfor %}
};
return choiceMap[fieldName]?.[value] || value || '-';
};
/**
* 关闭窗体
*/
const dialogClose = () => {
emit('update:visible', false);
};
/**
* 设置表单数据
*/
const setFormData = async () => {
try {
const data = await get{{ model_class_name }}Detail(props.{{ primary_key }});
for (const key in formData) {
if (data[key] != null && data[key] != undefined) {
//@ts-ignore
formData[key] = data[key];
}
}
} catch (error) {
console.error('获取{{ module_comment }}详情失败', error);
}
};
/**
* 重置表单数据
*/
const resetFormData = () => {
for (const key in formData) {
//@ts-ignore
if (typeof formData[key] === 'string') {
//@ts-ignore
formData[key] = '';
} else if (typeof formData[key] === 'number') {
//@ts-ignore
formData[key] = 0;
} else if (typeof formData[key] === 'boolean') {
//@ts-ignore
formData[key] = false;
}
}
};
/**
* 监听visible变化,重新加载数据
*/
watch(
() => props.visible,
(newVal) => {
if (newVal && props.{{ primary_key }}) {
setFormData();
} else if (newVal && !props.{{ primary_key }}) {
resetFormData();
}
},
{ immediate: true },
);
/**
* 钩子函数
*/
onMounted(() => {
if (props.visible && props.{{ primary_key }}) {
setFormData();
}
});
</script>
<style lang="scss" scoped></style>