From 21167c1b6c1264832da8cc698927cfc7c099d7c4 Mon Sep 17 00:00:00 2001 From: Xym <1697403759@qq.com> Date: Fri, 19 Jul 2024 17:03:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=AF=BC=E5=85=A5JSON?= =?UTF-8?q?=E7=9A=84=E9=AA=8C=E8=AF=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Container.vue | 45 +++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/components/Container.vue b/src/components/Container.vue index 9c6932e..3193695 100644 --- a/src/components/Container.vue +++ b/src/components/Container.vue @@ -412,10 +412,49 @@ export default { return generateCode(JSON.stringify(this.widgetForm)) }, setJSON (json) { - this.widgetForm = json + try { + // 验证 JSON 结构 + this.validateJSON(json); + + // 设置表单数据 + this.widgetForm = json; + if (json.list.length > 0) { + this.widgetFormSelect = json.list[0]; + } - if (json.list.length> 0) { - this.widgetFormSelect = json.list[0] + // 导入成功后的操作 + this.$message.success('导入成功!'); + + } catch (error) { + // 清除表单数据 + this.clear(); + + // 显示错误消息 + this.$message.error(`导入失败: ${error.message}`); + } + }, + + validateJSON(json) { + if (!json) { + throw new Error('JSON 对象为空'); + } + if (!Array.isArray(json.list)) { + throw new Error('"list" 属性不是数组'); + } + if (!json.config) { + throw new Error('缺少 "config" 属性'); + } + if (typeof json.config.labelWidth !== 'number') { + throw new Error('"config.labelWidth" 必须是数字'); + } + if (typeof json.config.labelPosition !== 'string') { + throw new Error('"config.labelPosition" 必须是字符串'); + } + if (typeof json.config.size !== 'string') { + throw new Error('"config.size" 必须是字符串'); + } + if (!['small', 'mini', 'medium'].includes(json.config.size)) { + throw new Error('"config.size" 的值只能是 "small", "mini" 或 "medium"'); } }, handleInput (val) { -- Gitee