自定义数据优化

master
于肖磊 2024-12-09 17:29:48 +08:00
parent 9a73a20b82
commit b9e92f3073
4 changed files with 105 additions and 33 deletions

View File

@ -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) || '';
}
/**
* 指示器的样式
*

View File

@ -4,7 +4,7 @@
</view>
</template>
<script>
import { radius_computer, padding_computer, gradient_handle, isEmpty } from '@/common/js/common/common.js';
import { radius_computer, padding_computer, gradient_handle, isEmpty, get_nested_property } from '@/common/js/common/common.js';
export default {
props: {
@ -58,11 +58,23 @@
icon_class_value = this.propValue.icon_class;
} else {
if (!isEmpty(this.propSourceList)) {
//
let icon = this.propSourceList[this.propValue.data_source_id];
// , data
if (this.propIsCustom && !isEmpty(this.propSourceList.data)) {
icon = this.propSourceList.data[this.propValue.data_source_id];
let icon = '';
// ID
const data_source_id = this.propValue.data_source_id;
if (!data_source_id.includes('.')) {
//
icon = this.propSourceList[data_source_id];
// , data
if (this.propIsCustom && !isEmpty(this.propSourceList.data)) {
icon = this.propSourceList.data[data_source_id];
}
} else {
//
icon = get_nested_property(this.propSourceList, data_source_id);
// , data
if (this.propIsCustom && !isEmpty(this.propSourceList.data)) {
icon = get_nested_property(this.propSourceList.data, data_source_id);
}
}
icon_class_value = icon;
} else {
@ -73,9 +85,9 @@
if (!isEmpty(this.propValue.icon_link)) {
url = this.propValue.icon_link?.page || '';
} else if (!isEmpty(this.propSourceList.data)) {
url = this.propSourceList.data[this.propValue?.data_source_link] || '';
url = get_nested_property(this.propSourceList.data, this.propValue?.data_source_link);
} else {
url = this.propSourceList[this.propValue?.data_source_link] || '';
url = get_nested_property(this.propSourceList, this.propValue?.data_source_link);
}
this.setData({
form: this.propValue,

View File

@ -4,7 +4,7 @@
</view>
</template>
<script>
import { percentage_count, radius_computer, isEmpty } from '@/common/js/common/common.js';
import { percentage_count, radius_computer, isEmpty, get_nested_property } from '@/common/js/common/common.js';
import imageEmpty from '@/components/diy/modules/image-empty.vue';
export default {
components: {
@ -69,9 +69,9 @@
if (!isEmpty(this.propValue.link)) {
url = this.propValue.link?.page || '';
} else if (!isEmpty(this.propSourceList.data)) {
url = this.propSourceList.data[this.propValue?.data_source_link] || '';
url = get_nested_property(this.propSourceList.data, this.propValue?.data_source_link || '');
} else {
url = this.propSourceList[this.propValue?.data_source_link] || '';
url = get_nested_property(this.propSourceList, this.propValue?.data_source_link || '');
}
this.setData({
form: this.propValue,
@ -86,16 +86,34 @@
return form.img[0];
} else {
if (!isEmpty(this.propSourceList)) {
//
let image_url = this.propSourceList[form.data_source_id];
// , data
if (this.propIsCustom && !isEmpty(this.propSourceList.data)) {
//
if (form.data_source_id == this.propImgParams) {
//
image_url = !isEmpty(this.propSourceList.new_cover)? this.propSourceList.new_cover[0]?.url || '' : this.propSourceList.data[this.propImgParams];
} else {
image_url = this.propSourceList.data[form.data_source_id];
let image_url = '';
// ID
const data_source_id = form.data_source_id;
if (!data_source_id.includes('.')) {
//
image_url = this.propSourceList[data_source_id];
// , data
if (this.propIsCustom && !isEmpty(this.propSourceList.data)) {
//
if (data_source_id == this.propImgParams) {
//
image_url = !isEmpty(this.propSourceList.new_cover)? this.propSourceList.new_cover[0]?.url || '' : this.propSourceList.data[data_source_id];
} else {
image_url = this.propSourceList.data[data_source_id];
}
}
} else {
//
image_url = get_nested_property(this.propSourceList, data_source_id);
// , data
if (this.propIsCustom && !isEmpty(this.propSourceList.data)) {
//
if (data_source_id == this.propImgParams) {
//
image_url = !isEmpty(this.propSourceList.new_cover)? this.propSourceList.new_cover[0]?.url || '' : get_nested_property(this.propSourceList.data, data_source_id);
} else {
image_url = get_nested_property(this.propSourceList.data, data_source_id);
}
}
}
return image_url;

View File

@ -11,7 +11,7 @@
</view>
</template>
<script>
import { radius_computer, padding_computer, isEmpty, gradient_handle } from '@/common/js/common/common.js';
import { radius_computer, padding_computer, isEmpty, gradient_handle, get_nested_property } from '@/common/js/common/common.js';
export default {
props: {
@ -70,12 +70,13 @@
methods: {
init() {
let url = '';
const data_source_link = this.propValue?.data_source_link;
if (!isEmpty(this.propValue.text_link)) {
url = this.propValue.text_link?.page || '';
} else if (!isEmpty(this.propSourceList.data)) {
url = this.propSourceList.data[this.propValue?.data_source_link] || '';
url = get_nested_property(this.propSourceList.data, data_source_link);
} else {
url = this.propSourceList[this.propValue?.data_source_link] || '';
url = get_nested_property(this.propSourceList, data_source_link);
}
this.setData({
form: this.propValue,
@ -90,15 +91,34 @@
if (!isEmpty(form.text_title)) {
text = form.text_title;
} else {
text = this.propSourceList[form.data_source_id];
//
if (this.propIsCustom && !isEmpty(this.propSourceList.data)) {
// data
if (form.data_source_id == this.propTitleParams) {
//
text = !isEmpty(this.propSourceList.new_title) ? this.propSourceList.new_title : this.propSourceList.data[this.propTitleParams];
} else {
text = this.propSourceList.data[form.data_source_id];
let image_url = '';
// ID
const data_source_id = form.data_source_id;
if (!data_source_id.includes('.')) {
text = this.propSourceList[data_source_id];
//
if (this.propIsCustom && !isEmpty(this.propSourceList.data)) {
// data
if (data_source_id == this.propTitleParams) {
//
text = !isEmpty(this.propSourceList.new_title) ? this.propSourceList.new_title : this.propSourceList.data[data_source_id];
} else {
text = this.propSourceList.data[data_source_id];
}
}
} else {
//
const keys = data_source_id.split('.');
text = get_nested_property(this.propSourceList, data_source_id);
//
if (this.propIsCustom && !isEmpty(this.propSourceList.data)) {
// data
if (data_source_id == this.propTitleParams) {
//
text = !isEmpty(this.propSourceList.new_title) ? this.propSourceList.new_title : get_nested_property(this.propSourceList.data, data_source_id);
} else {
text = get_nested_property(this.propSourceList.data, data_source_id);
}
}
}
}