From b9e92f3073d6bb9bfb9bdcf3a302641b86f0c9a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8E=E8=82=96=E7=A3=8A?= <18851179580@163.com> Date: Mon, 9 Dec 2024 17:29:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/js/common/common.js | 22 ++++++++++ components/diy/modules/custom/model-icon.vue | 28 ++++++++---- components/diy/modules/custom/model-image.vue | 44 +++++++++++++------ components/diy/modules/custom/model-text.vue | 44 ++++++++++++++----- 4 files changed, 105 insertions(+), 33 deletions(-) diff --git a/common/js/common/common.js b/common/js/common/common.js index 568c6730..5dcf8068 100644 --- a/common/js/common/common.js +++ b/common/js/common/common.js @@ -13,6 +13,28 @@ export function is_obj_empty(obj) { return Object.keys(obj).length === 0; } +/** + * 获取嵌套对象的属性值 + * + * 该函数旨在通过指定的属性路径获取嵌套对象中的属性值它接受一个对象和一个属性路径字符串作为参数, + * 并返回对应路径的属性值如果输入的路径无效或对象中不存在该路径,则返回空字符串 + * + * @param {Object} obj - 要从中获取属性的嵌套对象 + * @param {string} path - 属性路径,使用点号分隔的字符串表示 + * @returns {string} - 返回指定路径的属性值,如果路径无效则返回空字符串 + */ +export function get_nested_property(obj, path) { + // 检查路径参数是否为字符串且非空,若不满足条件则返回空字符串 + if (typeof path !== 'string' || !path) return ''; + + // 将属性路径字符串拆分为属性键数组 + const keys = path.split('.'); + + // 使用reduce方法遍历属性键数组,逐层访问对象属性 + // 如果当前对象存在且拥有下一个属性键,则继续访问;否则返回空字符串 + return keys.reduce((o, key) => (o && o[key] ? o[key] : ''), obj) || ''; +} + /** * 指示器的样式 * diff --git a/components/diy/modules/custom/model-icon.vue b/components/diy/modules/custom/model-icon.vue index 04f403ee..ffb76f16 100644 --- a/components/diy/modules/custom/model-icon.vue +++ b/components/diy/modules/custom/model-icon.vue @@ -4,7 +4,7 @@