chore:商品分类模板2,4加购按钮显示正常

This commit is contained in:
2026-09-07 16:52:27 +08:00
parent 9739eea32a
commit 8443cadba9
4 changed files with 45 additions and 11 deletions

View File

@@ -578,10 +578,10 @@ export default {
};
},
computed: {
// 后台新增开关:加购按钮
// 后台开启就显示, 关闭就不显示; 未下发新字段时兼容旧字段 quickBuy
// 后台开关:加购按钮 = cartBtn; 未下发 cartBtn 时兼容旧字段 quickBuy
// 后台开启就显示, 关闭就不显示
showAddCart() {
const t = this.getDiyToggle(['showAddCart', 'show_add_cart']);
const t = this.getDiyToggle(['showAddCart', 'show_add_cart', 'cartBtn', 'cart_btn']);
if (t !== undefined) return t;
const legacy = this.getDiyToggle(['quickBuy', 'quick_buy']);
return legacy !== undefined ? legacy : false;

View File

@@ -326,8 +326,8 @@ export default {
let money = parseFloat(this.cartMoney).toFixed(2);
return money.split('.');
},
// 后台新增开关:快捷购买(显示购物车及结算按钮) / 加购按钮
// 后台开启哪个就显示哪个, 关闭哪个就不显示哪个; 未下发新字段时兼容旧字段 quickBuy
// 后台开关:快捷购买(显示购物车及结算按钮) = quickBuy; 加购按钮 = cartBtn
// 后台开启哪个就显示哪个, 关闭哪个就不显示哪个; 未下发 cartBtn 时兼容旧字段 quickBuy
showCartSettlement() {
const t = this.getDiyToggle(['showCartSettlement', 'show_cart_settlement']);
if (t !== undefined) return t;
@@ -335,7 +335,7 @@ export default {
return legacy !== undefined ? legacy : false;
},
showAddCart() {
const t = this.getDiyToggle(['showAddCart', 'show_add_cart']);
const t = this.getDiyToggle(['showAddCart', 'show_add_cart', 'cartBtn', 'cart_btn']);
if (t !== undefined) return t;
const legacy = this.getDiyToggle(['quickBuy', 'quick_buy']);
return legacy !== undefined ? legacy : false;

View File

@@ -4,15 +4,49 @@ export default {
* 读取后台装修下发的布尔开关
* 依次取 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 || {};
for (let i = 0; i < names.length; i++) {
const val = v[names[i]];
if (val === undefined || val === null || val === '') continue;
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;
},
@@ -35,4 +69,4 @@ export default {
// #endif
},
}
}
}