From ce6c5a983b08db3cff05f08dca23a174d17925cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Fri, 2 Jan 2026 15:58:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=A7=E5=93=81=E8=A7=84?= =?UTF-8?q?=E6=A0=BC=E6=98=BE=E7=A4=BA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加formatSpecification函数,智能处理规格字符串分割 - 只显示前两个规格,超过部分用...省略 - 在processGoodsData中调用格式化函数预处理数据 - 更新WXML模板使用新的格式化字段 - 改善商品列表页面布局和用户体验 --- pages/index/index.js | 46 +++++++++++++++++++++++++++++++++++++++++- pages/index/index.wxml | 2 +- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/pages/index/index.js b/pages/index/index.js index e00bee1..d7cc2c6 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -441,6 +441,45 @@ Page({ return region; }, + // 格式化商品规格显示 - 只显示前两个,后面加... + formatSpecification: function(spec, yolk) { + if (!spec || spec === '无') { + return { + displaySpec: '无', + displayYolk: yolk && yolk !== '无' ? yolk : '' + }; + } + + // 按常见的分隔符分割规格 + const separators = [',', ',', '、', '|', ';', ';']; + let parts = [spec]; + + for (let separator of separators) { + if (spec.includes(separator)) { + parts = spec.split(separator); + break; + } + } + + // 清理空值并限制为前两个 + const cleanParts = parts + .map(part => part.trim()) + .filter(part => part && part !== '无') + .slice(0, 2); + + let displaySpec = cleanParts.join(' | '); + + // 如果原规格分割后超过两个部分,添加省略号 + if (cleanParts.length < parts.filter(part => part.trim() && part.trim() !== '无').length) { + displaySpec += '...'; + } + + return { + displaySpec, + displayYolk: yolk && yolk !== '无' ? yolk : '' + }; + }, + // 处理商品数据 - 淘宝风格 processGoodsData: function(products, isLoadMore = false) { let newGoods = products.map(product => { @@ -495,6 +534,9 @@ Page({ displayStock: displayStock }); + // 格式化规格显示 + const formattedSpec = this.formatSpecification(product.specification || product.spec || '', product.yolk || ''); + return { ...product, id: productId, // 统一使用id字段 @@ -519,7 +561,9 @@ Page({ currentImageIndex: 0, imageUrls: formattedImageUrls, totalStock: displayStock, // 使用优化后的库存显示值 - originalTotalStock: totalStock // 保留原始计算值用于调试 + originalTotalStock: totalStock, // 保留原始计算值用于调试 + displaySpecification: formattedSpec.displaySpec, // 格式化后的规格 + displayYolk: formattedSpec.displayYolk // 格式化后的蛋黄 } }) diff --git a/pages/index/index.wxml b/pages/index/index.wxml index 9233547..fece6cc 100644 --- a/pages/index/index.wxml +++ b/pages/index/index.wxml @@ -160,7 +160,7 @@ {{item.name}} - {{item.specification || '无'}} | {{item.yolk}} + {{item.displaySpecification}} | {{item.displayYolk}} {{item.sourceType || ''}} {{item.negotiateStatus}}