From 66e1621048a908c4a176771adaf591fed4eae3fc Mon Sep 17 00:00:00 2001 From: engigu Date: Sat, 6 Dec 2025 22:50:12 +0800 Subject: [PATCH] chore: opt template style --- .../pages/messageTemplate/TemplateEditor.vue | 78 +++++++++++++++---- 1 file changed, 61 insertions(+), 17 deletions(-) diff --git a/web/src/components/pages/messageTemplate/TemplateEditor.vue b/web/src/components/pages/messageTemplate/TemplateEditor.vue index e4da45a..62a4029 100644 --- a/web/src/components/pages/messageTemplate/TemplateEditor.vue +++ b/web/src/components/pages/messageTemplate/TemplateEditor.vue @@ -152,6 +152,30 @@ watch(placeholdersList, () => { }, 300) }, { deep: true }) +// 检查占位符 key 是否重复 +const isDuplicateKey = (key: string, currentIndex: number): boolean => { + if (!key.trim()) return false + return placeholdersList.value.some((p, index) => + index !== currentIndex && p.key.trim() === key.trim() + ) +} + +// 获取重复的 key 列表 +const getDuplicateKeys = computed(() => { + const keys = placeholdersList.value.map(p => p.key.trim()).filter(k => k) + const duplicates = new Set() + const seen = new Set() + + keys.forEach(key => { + if (seen.has(key)) { + duplicates.add(key) + } + seen.add(key) + }) + + return duplicates +}) + // 添加占位符 const addPlaceholder = () => { placeholdersList.value.push({ key: '', label: '', default: '' }) @@ -250,6 +274,19 @@ const saveTemplate = async () => { toast.error('至少需要填写一种格式的模板内容') return } + + // 验证占位符 key 不能为空且不能重复 + const emptyKeys = placeholdersList.value.filter(p => p.key.trim() === '') + if (emptyKeys.length > 0) { + toast.error('占位符 key 不能为空') + return + } + + if (getDuplicateKeys.value.size > 0) { + const duplicates = Array.from(getDuplicateKeys.value).join('、') + toast.error(`占位符 key 不能重复:${duplicates}`) + return + } // 同步占位符数据 formData.value.placeholders = JSON.stringify(placeholdersList.value) @@ -321,23 +358,30 @@ watch(() => props.open, (newVal) => { -
- - - - +
+
+
+ +

+ 该 key 已存在 +

+
+ + + +

在模板中使用 来引用占位符,例如:Hello