Browse Source

优化对比页面的选择步骤

main
Trae AI 4 days ago
parent
commit
8ca89d22a3
  1. 97
      pages/compare_price/index.js
  2. 24
      pages/compare_price/index.wxml
  3. 101
      pages/compare_price/index.wxss
  4. 111
      pages/goods-detail/goods-detail.js

97
pages/compare_price/index.js

@ -597,19 +597,25 @@ Page({
}); });
} }
// 处理商品价格,使用选中规格的价格 // 处理商品价格和库存,使用选中规格的价格和库存
if (specWeight) { if (specWeight) {
if (item.weightQuantityData && Array.isArray(item.weightQuantityData)) { if (item.weightQuantityData && Array.isArray(item.weightQuantityData)) {
// 尝试从weightQuantityData中找到对应规格的价格 // 尝试从weightQuantityData中找到对应规格的价格和库存
const matchingSpec = item.weightQuantityData.find(spec => { const matchingSpec = item.weightQuantityData.find(spec => {
if (spec.weightSpec) { if (spec.weightSpec) {
return spec.weightSpec.trim() === specWeight; return spec.weightSpec.trim() === specWeight;
} }
return false; 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; const totalStock = quantity;
let displayStock; let displayStock;
@ -750,5 +772,70 @@ Page({
wx.navigateBack({ wx.navigateBack({
delta: 1 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);
} }
}); });

24
pages/compare_price/index.wxml

@ -2,6 +2,25 @@
<view class="page-container"> <view class="page-container">
<scroll-view class="scroll-view" scroll-y="true" enable-back-to-top="true"> <scroll-view class="scroll-view" scroll-y="true" enable-back-to-top="true">
<!-- 规格选择器 -->
<view wx:if="{{currentGoods && currentGoods.weightQuantityData && currentGoods.weightQuantityData.length > 1}}" class="spec-selector">
<view class="spec-selector-header">
<text class="spec-selector-title">选择规格</text>
</view>
<view class="spec-selector-list">
<view
wx:for="{{currentGoods.weightQuantityData}}"
wx:key="index"
class="spec-item {{selectedSpec === (item.weightSpec ? item.weightSpec.trim() : '') ? 'active' : ''}}"
bindtap="selectSpec"
data-spec="{{item}}"
>
<text class="spec-item-text">{{item.display || item.weightSpec}}</text>
<view wx:if="{{selectedSpec === (item.weightSpec ? item.weightSpec.trim() : '')}}" class="spec-item-check">✓</view>
</view>
</view>
</view>
<!-- 步骤完成提示区 --> <!-- 步骤完成提示区 -->
<view class="tips-section"> <view class="tips-section">
<view class="tips-container"> <view class="tips-container">
@ -67,10 +86,7 @@
bindtap="navigateToGoodsDetail" bindtap="navigateToGoodsDetail"
> >
<view class="compare-item-header"> <view class="compare-item-header">
<view class="compare-item-name-container"> <text class="compare-item-name">{{item.productName || item.name}}</text>
<text class="compare-item-name">{{item.productName || item.name}}</text>
<text wx:if="{{item.status === 'sold_out'}}" class="sold-out-tag">已售空</text>
</view>
<view class="compare-item-price"> <view class="compare-item-price">
<text class="currency-symbol" style="color: {{item.priceDiff > 0 ? '#FF4D4F' : item.priceDiff < 0 ? '#10B981' : '#666666'}}">¥</text> <text class="currency-symbol" style="color: {{item.priceDiff > 0 ? '#FF4D4F' : item.priceDiff < 0 ? '#10B981' : '#666666'}}">¥</text>
<text class="compare-price-amount" style="color: {{item.priceDiff > 0 ? '#FF4D4F' : item.priceDiff < 0 ? '#10B981' : '#666666'}}">{{item.price}}</text> <text class="compare-price-amount" style="color: {{item.priceDiff > 0 ? '#FF4D4F' : item.priceDiff < 0 ? '#10B981' : '#666666'}}">{{item.price}}</text>

101
pages/compare_price/index.wxss

@ -100,6 +100,90 @@
line-height: 1.4; 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 { .content-container {
padding: 0 16px; padding: 0 16px;
@ -336,14 +420,6 @@
gap: 12px; gap: 12px;
} }
.compare-item-name-container {
flex: 1;
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
}
.compare-item-name { .compare-item-name {
font-size: 15px; font-size: 15px;
color: #1A1A1A; color: #1A1A1A;
@ -354,15 +430,6 @@
line-height: 1.4; 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 { .compare-item-price {
display: flex; display: flex;
align-items: baseline; align-items: baseline;

111
pages/goods-detail/goods-detail.js

@ -3828,28 +3828,98 @@ Page({
// 获取当前商品的信息 // 获取当前商品的信息
const currentGoods = this.data.goodsDetail; const currentGoods = this.data.goodsDetail;
const currentCategory = currentGoods.category || '';
const currentSpecifications = currentGoods.weightQuantityData || []; const currentSpecifications = currentGoods.weightQuantityData || [];
// 找到当前商品种类在选项中的索引,默认为0(全部) // 品种默认选择全部品种
let defaultCategoryIndex = 0; const selectedCategory = '全部';
if (currentCategory) {
const index = this.data.categoryOptions.indexOf(currentCategory);
if (index !== -1) {
defaultCategoryIndex = index;
}
}
// 初始化弹窗状态 // 检查规格数量
this.setData({ if (currentSpecifications.length === 1) {
showCompareModal: true, // 只有一个规格,直接跳转
compareStep: 1, const selectedSpec = currentSpecifications[0];
selectedCategoryIndex: defaultCategoryIndex,
selectedCategory: this.data.categoryOptions[defaultCategoryIndex], // 构建要传递的数据
selectedSpecIndex: 0, const goodsData = {
selectedSpec: currentSpecifications.length > 0 ? currentSpecifications[0] : null, id: currentGoods.id || currentGoods.productId,
currentSpecifications: currentSpecifications 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 currentGoods = this.data.goodsDetail;
const selectedCategory = this.data.selectedCategory; // 品种默认选择全部品种
const selectedCategory = '全部';
const selectedSpec = this.data.selectedSpec; const selectedSpec = this.data.selectedSpec;
// 显示提示信息 // 显示提示信息

Loading…
Cancel
Save