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}}