From 8ca89d22a34d444c0b9a8064b9b3a798aad8b3db Mon Sep 17 00:00:00 2001 From: Trae AI Date: Tue, 3 Mar 2026 11:25:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=B9=E6=AF=94=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E7=9A=84=E9=80=89=E6=8B=A9=E6=AD=A5=E9=AA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/compare_price/index.js | 97 +++++++++++++++++++++++-- pages/compare_price/index.wxml | 24 +++++-- pages/compare_price/index.wxss | 101 +++++++++++++++++++++----- pages/goods-detail/goods-detail.js | 111 +++++++++++++++++++++++------ 4 files changed, 287 insertions(+), 46 deletions(-) diff --git a/pages/compare_price/index.js b/pages/compare_price/index.js index c20dcf1..4b50681 100644 --- a/pages/compare_price/index.js +++ b/pages/compare_price/index.js @@ -597,19 +597,25 @@ Page({ }); } - // 处理商品价格,使用选中规格的价格 + // 处理商品价格和库存,使用选中规格的价格和库存 if (specWeight) { if (item.weightQuantityData && Array.isArray(item.weightQuantityData)) { - // 尝试从weightQuantityData中找到对应规格的价格 + // 尝试从weightQuantityData中找到对应规格的价格和库存 const matchingSpec = item.weightQuantityData.find(spec => { if (spec.weightSpec) { return spec.weightSpec.trim() === specWeight; } return false; }); - if (matchingSpec && matchingSpec.price) { + if (matchingSpec) { // 确保价格是一个单一的值,而不是多个价格的组合 - item.price = matchingSpec.price; + if (matchingSpec.price) { + item.price = matchingSpec.price; + } + // 确保库存是对应规格的库存 + if (matchingSpec.quantity) { + item.specStock = matchingSpec.quantity; + } } } } @@ -639,7 +645,23 @@ Page({ } // 处理库存显示逻辑 - const quantity = item.quantity || item.minOrder || item.stock || item.inventory || item.availableStock || item.totalAvailable; + // 优先使用对应规格的库存,如果没有则使用默认库存 + let quantity = item.specStock || item.quantity || item.minOrder || item.stock || item.inventory || item.availableStock || item.totalAvailable; + + // 清理库存字段,确保只显示单一值 + if (quantity) { + // 如果库存是字符串且包含逗号,只取第一个库存 + if (typeof quantity === 'string' && quantity.includes(',')) { + quantity = quantity.split(',')[0].trim(); + } + // 如果库存是数组,只取第一个库存 + if (Array.isArray(quantity)) { + quantity = quantity[0]; + } + // 转换为数字 + quantity = Number(quantity); + } + const totalStock = quantity; let displayStock; @@ -750,5 +772,70 @@ Page({ wx.navigateBack({ delta: 1 }); + }, + + // 选择规格 + selectSpec: function (e) { + const selectedSpec = e.currentTarget.dataset.spec; + if (!selectedSpec) { + return; + } + + const specWeight = selectedSpec.weightSpec.trim(); + const currentGoods = this.data.currentGoods; + + // 更新选中的规格 + this.setData({ + selectedSpec: specWeight, + loading: true + }); + + // 处理当前商品的价格,使用选中规格的价格 + let currentPrice = currentGoods.price; + if (selectedSpec && selectedSpec.price) { + // 如果selectedSpec包含价格信息,使用它 + currentPrice = selectedSpec.price; + } else if (currentGoods.weightQuantityData && Array.isArray(currentGoods.weightQuantityData)) { + // 如果weightQuantityData存在,尝试从中找到对应规格的价格 + const matchingSpec = currentGoods.weightQuantityData.find(spec => { + if (spec.weightSpec) { + return spec.weightSpec.trim() === specWeight; + } + return false; + }); + if (matchingSpec && matchingSpec.price) { + currentPrice = matchingSpec.price; + } + } + + // 更新currentGoods的价格 + currentGoods.price = currentPrice; + + // 清理价格字段,确保只显示单一价格 + if (currentGoods.price) { + // 如果价格是字符串且包含逗号,只取第一个价格 + if (typeof currentGoods.price === 'string' && currentGoods.price.includes(',')) { + currentGoods.price = currentGoods.price.split(',')[0].trim(); + } + // 如果价格是数组,只取第一个价格 + if (Array.isArray(currentGoods.price)) { + currentGoods.price = currentGoods.price[0]; + } + } + + // 更新currentGoods + this.setData({ + currentGoods: currentGoods + }); + + // 初始化商品数据 + this.setData({ + goods: [], + currentPage: 1, + hasMore: true + }); + + // 加载商品数据 + this.loadGoodsData(1, this.data.selectedCategory, specWeight, currentGoods); } }); \ No newline at end of file diff --git a/pages/compare_price/index.wxml b/pages/compare_price/index.wxml index d52f600..5d272e7 100644 --- a/pages/compare_price/index.wxml +++ b/pages/compare_price/index.wxml @@ -2,6 +2,25 @@ + + + + 选择规格 + + + + {{item.display || item.weightSpec}} + + + + + @@ -67,10 +86,7 @@ bindtap="navigateToGoodsDetail" > - - {{item.productName || item.name}} - 已售空 - + {{item.productName || item.name}} ¥ {{item.price}} diff --git a/pages/compare_price/index.wxss b/pages/compare_price/index.wxss index de7b949..3ebd079 100644 --- a/pages/compare_price/index.wxss +++ b/pages/compare_price/index.wxss @@ -100,6 +100,90 @@ line-height: 1.4; } +/* 规格选择器 */ +.spec-selector { + padding: 0 16px 12px; +} + +.spec-selector-header { + margin-bottom: 8px; +} + +.spec-selector-title { + font-size: 14px; + color: #1A1A1A; + font-weight: 500; +} + +.spec-selector-list { + display: flex; + flex-wrap: wrap; + gap: 8px; +} + +.spec-item { + padding: 6px 12px; + border: 1px solid #E5E7EB; + border-radius: 8px; + background-color: #FFFFFF; + font-size: 14px; + color: #666666; + cursor: pointer; + transition: all 0.3s ease; + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; +} + +.spec-item:active { + transform: scale(0.98); +} + +.spec-item.active { + border-color: #165DFF; + background-color: #EFF6FF; + color: #165DFF; + box-shadow: 0 0 0 2px rgba(22, 93, 255, 0.2); +} + +.spec-item-text { + white-space: nowrap; + flex: 1; + font-weight: 500; +} + +.spec-item-check { + width: 18px; + height: 18px; + border-radius: 50%; + background-color: #165DFF; + color: #FFFFFF; + font-size: 14px; + display: flex; + align-items: center; + justify-content: center; + flex-shrink: 0; + font-weight: bold; + box-shadow: 0 1px 3px rgba(22, 93, 255, 0.3); + animation: pulse 0.3s ease-in-out; +} + +@keyframes pulse { + 0% { + transform: scale(0.8); + opacity: 0.5; + } + 50% { + transform: scale(1.1); + opacity: 1; + } + 100% { + transform: scale(1); + opacity: 1; + } +} + /* 内容容器 */ .content-container { padding: 0 16px; @@ -336,14 +420,6 @@ gap: 12px; } -.compare-item-name-container { - flex: 1; - display: flex; - align-items: center; - gap: 8px; - flex-wrap: wrap; -} - .compare-item-name { font-size: 15px; color: #1A1A1A; @@ -354,15 +430,6 @@ line-height: 1.4; } -.sold-out-tag { - background-color: #FEE2E2; - color: #EF4444; - font-size: 12px; - padding: 2px 8px; - border-radius: 9999px; - flex-shrink: 0; -} - .compare-item-price { display: flex; align-items: baseline; diff --git a/pages/goods-detail/goods-detail.js b/pages/goods-detail/goods-detail.js index 19a1406..032c664 100644 --- a/pages/goods-detail/goods-detail.js +++ b/pages/goods-detail/goods-detail.js @@ -3828,28 +3828,98 @@ Page({ // 获取当前商品的信息 const currentGoods = this.data.goodsDetail; - const currentCategory = currentGoods.category || ''; const currentSpecifications = currentGoods.weightQuantityData || []; - // 找到当前商品种类在选项中的索引,默认为0(全部) - let defaultCategoryIndex = 0; - if (currentCategory) { - const index = this.data.categoryOptions.indexOf(currentCategory); - if (index !== -1) { - defaultCategoryIndex = index; - } - } + // 品种默认选择全部品种 + const selectedCategory = '全部'; - // 初始化弹窗状态 - this.setData({ - showCompareModal: true, - compareStep: 1, - selectedCategoryIndex: defaultCategoryIndex, - selectedCategory: this.data.categoryOptions[defaultCategoryIndex], - selectedSpecIndex: 0, - selectedSpec: currentSpecifications.length > 0 ? currentSpecifications[0] : null, - currentSpecifications: currentSpecifications - }); + // 检查规格数量 + if (currentSpecifications.length === 1) { + // 只有一个规格,直接跳转 + const selectedSpec = currentSpecifications[0]; + + // 构建要传递的数据 + const goodsData = { + id: currentGoods.id || currentGoods.productId, + name: currentGoods.name || '', + price: currentGoods.price || '', + imageUrls: currentGoods.imageUrls || [], + region: currentGoods.region || '', + weightQuantityData: currentGoods.weightQuantityData || [], + category: selectedCategory, + yolk: currentGoods.yolk || '', + sourceType: currentGoods.sourceType || '', + supplyStatus: currentGoods.supplyStatus || '', + mediaItems: currentGoods.mediaItems || [], + frequency: currentGoods.frequency || 0, + status: currentGoods.status || 'published', + totalStock: currentGoods.totalStock || '充足', + selectedSpec: selectedSpec + }; + + console.log('准备跳转到对比价格页面,传递的数据:', goodsData); + + // 跳转到对比价格页面 + wx.navigateTo({ + url: `/pages/compare_price/index?goodsData=${encodeURIComponent(JSON.stringify(goodsData))}`, + success: function () { + console.log('成功跳转到对比价格页面'); + }, + fail: function (error) { + console.error('跳转到对比价格页面失败:', error); + wx.showToast({ + title: '跳转失败,请稍后重试', + icon: 'none' + }); + } + }); + } else if (currentSpecifications.length > 1) { + // 存在多个规格,弹出弹窗让用户选择 + this.setData({ + showCompareModal: true, + compareStep: 2, // 直接显示规格选择步骤 + selectedCategory: selectedCategory, + selectedSpecIndex: 0, + selectedSpec: currentSpecifications.length > 0 ? currentSpecifications[0] : null, + currentSpecifications: currentSpecifications + }); + } else { + // 没有规格,直接跳转 + const goodsData = { + id: currentGoods.id || currentGoods.productId, + name: currentGoods.name || '', + price: currentGoods.price || '', + imageUrls: currentGoods.imageUrls || [], + region: currentGoods.region || '', + weightQuantityData: currentGoods.weightQuantityData || [], + category: selectedCategory, + yolk: currentGoods.yolk || '', + sourceType: currentGoods.sourceType || '', + supplyStatus: currentGoods.supplyStatus || '', + mediaItems: currentGoods.mediaItems || [], + frequency: currentGoods.frequency || 0, + status: currentGoods.status || 'published', + totalStock: currentGoods.totalStock || '充足', + selectedSpec: null + }; + + console.log('准备跳转到对比价格页面,传递的数据:', goodsData); + + // 跳转到对比价格页面 + wx.navigateTo({ + url: `/pages/compare_price/index?goodsData=${encodeURIComponent(JSON.stringify(goodsData))}`, + success: function () { + console.log('成功跳转到对比价格页面'); + }, + fail: function (error) { + console.error('跳转到对比价格页面失败:', error); + wx.showToast({ + title: '跳转失败,请稍后重试', + icon: 'none' + }); + } + }); + } }, // 选择品种 @@ -3902,7 +3972,8 @@ Page({ } const currentGoods = this.data.goodsDetail; - const selectedCategory = this.data.selectedCategory; + // 品种默认选择全部品种 + const selectedCategory = '全部'; const selectedSpec = this.data.selectedSpec; // 显示提示信息