73 lines
2.8 KiB
JavaScript
73 lines
2.8 KiB
JavaScript
export default {
|
|
methods: {
|
|
/**
|
|
* 读取后台装修下发的布尔开关
|
|
* 依次取 names 中的字段, 兼容 1/0、"1"/"0"、true/false、"true"/"false" 等值类型
|
|
* 命中返回 true/false; 所有字段都未下发时返回 undefined (由调用方决定兜底)
|
|
*
|
|
* 匹配顺序:
|
|
* 1. 精确命中 (showAddCart / show_add_cart ...)
|
|
* 2. 忽略大小写、下划线、中划线、空格的差异 (showaddcart === showAddCart === show_add_cart)
|
|
* —— 后台实际字段名与前端约定写法不一致时, 这一步兜底
|
|
* 3. 常见嵌套容器 (value.style / value.config / value.setting / value.data)
|
|
*/
|
|
getDiyToggle(names) {
|
|
const v = this.value || {};
|
|
const parse = val => {
|
|
if (val === undefined || val === null || val === '') return undefined;
|
|
if (val === true || val === 1 || val === '1' || val === 'true') return true;
|
|
if (val === false || val === 0 || val === '0' || val === 'false') return false;
|
|
return !!val;
|
|
};
|
|
const pick = obj => {
|
|
if (!obj || typeof obj != 'object') return undefined;
|
|
// 1. 精确命中
|
|
let i, res;
|
|
for (i = 0; i < names.length; i++) {
|
|
res = parse(obj[names[i]]);
|
|
if (res !== undefined) return res;
|
|
}
|
|
// 2. 忽略大小写/下划线等差异
|
|
const norm = k => String(k).toLowerCase().replace(/[\s_-]/g, '');
|
|
const wanted = names.map(norm);
|
|
for (const key in obj) {
|
|
if (wanted.indexOf(norm(key)) != -1) {
|
|
res = parse(obj[key]);
|
|
if (res !== undefined) return res;
|
|
}
|
|
}
|
|
return undefined;
|
|
};
|
|
|
|
const root = pick(v);
|
|
if (root !== undefined) return root;
|
|
|
|
// 3. 嵌套容器兜底
|
|
const containers = ['style', 'config', 'setting', 'data'];
|
|
for (let i = 0; i < containers.length; i++) {
|
|
const res = pick(v[containers[i]]);
|
|
if (res !== undefined) return res;
|
|
}
|
|
return undefined;
|
|
},
|
|
|
|
// 异步触发事件
|
|
async __$emitEvent(payload = {eventName: '__unnamedEvent', data: {}, promiseCallback: null}) {
|
|
await this.$eventBus.emit(payload.eventName, payload.data, payload.promiseCallback)
|
|
},
|
|
|
|
// 预览图片
|
|
previewImg(img) {
|
|
// #ifdef MP-WEIXIN
|
|
uni.previewImage({
|
|
current: 0,
|
|
urls: [this.$util.img(img)],
|
|
success: function (res) { },
|
|
fail: function (res) { },
|
|
complete: function (res) { },
|
|
})
|
|
// #endif
|
|
},
|
|
}
|
|
}
|