From da96ccfe439c3e7f893adebd1ef8fa1bb916e752 Mon Sep 17 00:00:00 2001 From: shaohuzhang1 Date: Fri, 25 Oct 2024 14:36:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Djson=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E6=A1=86=E6=9C=AA=E8=BD=AC=E6=8D=A2=E4=B8=BA=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dynamics-form/DemoConstructor.vue | 19 ++++++++- ui/src/components/dynamics-form/FormItem.vue | 1 + .../items/JsonInputConstructor.vue | 23 ++++------ .../dynamics-form/items/JsonInput.vue | 42 ++++++++++++++++--- 4 files changed, 63 insertions(+), 22 deletions(-) diff --git a/ui/src/components/dynamics-form/DemoConstructor.vue b/ui/src/components/dynamics-form/DemoConstructor.vue index a575cbde7..aceecb503 100644 --- a/ui/src/components/dynamics-form/DemoConstructor.vue +++ b/ui/src/components/dynamics-form/DemoConstructor.vue @@ -21,8 +21,10 @@ :render_data="form_item_list" ref="dynamicsFormRef" > - + + 校验 + diff --git a/ui/src/components/dynamics-form/FormItem.vue b/ui/src/components/dynamics-form/FormItem.vue index 30b834848..fbf26b188 100644 --- a/ui/src/components/dynamics-form/FormItem.vue +++ b/ui/src/components/dynamics-form/FormItem.vue @@ -114,6 +114,7 @@ const errMsg = computed(() => { const to_rule = (rule: any) => { if (rule.validator) { let validator = (rule: any, value: string, callback: any) => {} + eval(rule.validator) return { ...rule, validator } } return rule diff --git a/ui/src/components/dynamics-form/constructor/items/JsonInputConstructor.vue b/ui/src/components/dynamics-form/constructor/items/JsonInputConstructor.vue index 5a7d442b6..e69ca2f66 100644 --- a/ui/src/components/dynamics-form/constructor/items/JsonInputConstructor.vue +++ b/ui/src/components/dynamics-form/constructor/items/JsonInputConstructor.vue @@ -5,15 +5,16 @@ prop="default_value" :rules="[default_value_rule]" > - + diff --git a/ui/src/components/dynamics-form/items/JsonInput.vue b/ui/src/components/dynamics-form/items/JsonInput.vue index e4bd6fa45..661feeff2 100644 --- a/ui/src/components/dynamics-form/items/JsonInput.vue +++ b/ui/src/components/dynamics-form/items/JsonInput.vue @@ -43,46 +43,76 @@ import { oneDark } from '@codemirror/theme-one-dark' import { Codemirror } from 'vue-codemirror' import { linter } from '@codemirror/lint' import { computed, ref } from 'vue' -const props = withDefaults(defineProps<{ modelValue?: string }>(), { modelValue: '{}' }) +const props = withDefaults(defineProps<{ modelValue?: any }>(), { modelValue: () => {} }) const emit = defineEmits(['update:modelValue']) + +const cache_model_value_str = ref() + const model_value = computed({ get: () => { - if (!props.modelValue) { - emit('update:modelValue', '{}') + if (cache_model_value_str.value) { + return cache_model_value_str.value } - return props.modelValue + return JSON.stringify(props.modelValue, null, 4) }, set: (v: string) => { if (!v) { - emit('update:modelValue', '{}') + emit('update:modelValue', JSON.parse('{}')) } else { - emit('update:modelValue', v) + try { + cache_model_value_str.value = v + const result = JSON.parse(v) + emit('update:modelValue', result) + } catch (e) {} } } }) const extensions = [json(), linter(jsonParseLinter()), oneDark] + const codemirrorStyle = { height: '210px!important', width: '100%' } + // 弹出框相关代码 const dialogVisible = ref(false) + const cloneContent = ref('') + const openCodemirrorDialog = () => { cloneContent.value = model_value.value dialogVisible.value = true } + const format = () => { try { const json_str = JSON.parse(model_value.value) model_value.value = JSON.stringify(json_str, null, 4) } catch (e) {} } + function submitDialog() { model_value.value = cloneContent.value dialogVisible.value = false } +/** + * 校验格式 + * @param rule + * @param value + * @param callback + */ +const validate_rules = (rule: any, value: any, callback: any) => { + if (model_value.value) { + try { + JSON.parse(model_value.value) + } catch (e) { + callback(new Error('JSON格式不正确')) + } + } +} + +defineExpose({ validate_rules: validate_rules })