Compare commits

...

2 Commits

  1. 88
      pages/compare_price/index.js
  2. 11
      pages/compare_price/index.wxml
  3. 19
      pages/compare_price/index.wxss
  4. 13
      pages/goods-detail/goods-detail.js
  5. 1
      pages/goods-detail/goods-detail.wxml

88
pages/compare_price/index.js

@ -59,6 +59,64 @@ function formatPrice(price) {
return numPrice.toFixed(1); return numPrice.toFixed(1);
} }
// 计算单位价格
function calculateUnitPrice(price, spec) {
if (!price || !spec) {
return null;
}
// 转换价格为数字
const numPrice = parseFloat(price);
if (isNaN(numPrice)) {
return null;
}
// 检查是否需要计算单位价格(价格低于10元)
if (numPrice >= 10) {
return null;
}
// 提取重量范围
const weightMatch = spec.match(/(\d+)-(\d+)/);
if (!weightMatch) {
return null;
}
const minWeight = parseFloat(weightMatch[1]);
const maxWeight = parseFloat(weightMatch[2]);
if (isNaN(minWeight) || isNaN(maxWeight)) {
return null;
}
// 计算平均重量
const avgWeight = (minWeight + maxWeight) / 2;
// 检查是否是毛重
const isGrossWeight = spec.includes('毛重');
// 计算单位价格
let unitPrice;
if (isGrossWeight) {
unitPrice = (avgWeight - 5) * numPrice;
} else {
unitPrice = avgWeight * numPrice;
}
return unitPrice.toFixed(1);
}
// 获取商品状态信息
function getStatusInfo(status) {
switch (status) {
case 'published':
return { text: '在售', color: '#10B981' };
case 'sold_out':
return { text: '售罄', color: '#666666' };
default:
return { text: '未知', color: '#666666' };
}
}
Page({ Page({
data: { data: {
// 页面数据 // 页面数据
@ -142,6 +200,9 @@ Page({
} }
} }
// 计算当前商品的单位价格
goodsData.unitPrice = calculateUnitPrice(goodsData.price, specWeight || goodsData.specification);
// 保存选择的种类、规格和当前商品数据到页面数据 // 保存选择的种类、规格和当前商品数据到页面数据
this.setData({ this.setData({
selectedCategory: selectedCategory, selectedCategory: selectedCategory,
@ -695,10 +756,30 @@ Page({
} }
} }
// 计算单位价格
item.unitPrice = calculateUnitPrice(item.price, item.currentSpec || item.specification || (item.processedSpecs && item.processedSpecs.length > 0 ? item.processedSpecs.join(' | ') : ''));
// 计算价格涨幅 // 计算价格涨幅
if (goodsData.price) { if (goodsData.price) {
const currentPrice = parseFloat(goodsData.price); // 获取当前商品的价格(使用单位价格如果低于10元)
const itemPrice = parseFloat(item.price); let currentPrice = parseFloat(goodsData.price);
if (goodsData.unitPrice) {
currentPrice = parseFloat(goodsData.unitPrice);
}
// 获取对比商品的价格(根据规则使用合适的价格)
let itemPrice = parseFloat(item.price);
if (currentPrice >= 10 && item.unitPrice) {
// 如果当前商品价格高于10元,对比商品使用单位价格
itemPrice = parseFloat(item.unitPrice);
} else if (currentPrice < 10 && itemPrice < 10) {
// 如果两者都低于10元,使用原始价格
itemPrice = parseFloat(item.price);
} else if (currentPrice < 10 && itemPrice >= 10) {
// 如果当前商品低于10元,对比商品高于10元,当前商品使用单位价格
currentPrice = parseFloat(goodsData.unitPrice || goodsData.price);
}
if (!isNaN(currentPrice) && !isNaN(itemPrice)) { if (!isNaN(currentPrice) && !isNaN(itemPrice)) {
const priceDiff = itemPrice - currentPrice; const priceDiff = itemPrice - currentPrice;
const pricePercent = ((priceDiff / currentPrice) * 100).toFixed(1); const pricePercent = ((priceDiff / currentPrice) * 100).toFixed(1);
@ -823,6 +904,9 @@ Page({
} }
} }
// 计算当前商品的单位价格
currentGoods.unitPrice = calculateUnitPrice(currentGoods.price, specWeight || currentGoods.specification);
// 更新currentGoods // 更新currentGoods
this.setData({ this.setData({
currentGoods: currentGoods currentGoods: currentGoods

11
pages/compare_price/index.wxml

@ -46,14 +46,16 @@
</view> </view>
<view class="current-goods-card"> <view class="current-goods-card">
<view class="goods-header"> <view class="goods-header">
<view class="brand-tag">品牌</view> <view class="brand-tag">{{currentGoods.category || '品种'}}</view>
<text class="goods-name">{{currentGoods.productName || currentGoods.name || '商品'}}</text> <text class="goods-name">{{currentGoods.productName || currentGoods.name || '商品'}}</text>
<view class="status-badge" style="color: {{currentGoods.status === 'published' ? '#10B981' : '#666666'}}">{{currentGoods.status === 'published' ? '在售' : currentGoods.status === 'sold_out' ? '售罄' : '未知'}}</view>
<view class="price-container"> <view class="price-container">
<text class="currency-symbol" style="color: {{currentGoods.priceDiff > 0 ? '#FF4D4F' : currentGoods.priceDiff < 0 ? '#10B981' : '#666666'}}">¥</text> <text class="currency-symbol" style="color: {{currentGoods.priceDiff > 0 ? '#FF4D4F' : currentGoods.priceDiff < 0 ? '#10B981' : '#666666'}}">¥</text>
<text class="price-amount" style="color: {{currentGoods.priceDiff > 0 ? '#FF4D4F' : currentGoods.priceDiff < 0 ? '#10B981' : '#666666'}}">{{currentGoods.price}}</text> <text class="price-amount" style="color: {{currentGoods.priceDiff > 0 ? '#FF4D4F' : currentGoods.priceDiff < 0 ? '#10B981' : '#666666'}}">{{currentGoods.price}}</text>
<text wx:if="{{currentGoods.unitPrice}}" class="unit-price">件价: ¥{{currentGoods.unitPrice}}</text>
</view> </view>
</view> </view>
<text class="spec-info">{{currentGoods.specification || selectedSpec || '净重43-44g'}} | {{currentGoods.yolk || '绿壳'}} | 库存:{{currentGoods.totalStock || '450'}}</text> <text class="spec-info">{{currentGoods.specification || selectedSpec || '净重43-44g'}} | {{currentGoods.category || '绿壳'}} | 库存:{{currentGoods.totalStock || '450'}}</text>
<view class="merchant-info"> <view class="merchant-info">
<view class="merchant-left"> <view class="merchant-left">
<text class="merchant-icon">🏪</text> <text class="merchant-icon">🏪</text>
@ -86,13 +88,16 @@
bindtap="navigateToGoodsDetail" bindtap="navigateToGoodsDetail"
> >
<view class="compare-item-header"> <view class="compare-item-header">
<view class="brand-tag">{{item.category || '品种'}}</view>
<text class="compare-item-name">{{item.productName || item.name}}</text> <text class="compare-item-name">{{item.productName || item.name}}</text>
<view class="status-badge" style="color: {{item.status === 'published' ? '#10B981' : '#666666'}}">{{item.status === 'published' ? '在售' : item.status === 'sold_out' ? '售罄' : '未知'}}</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>
<text wx:if="{{item.unitPrice}}" class="unit-price">件价: ¥{{item.unitPrice}}</text>
</view> </view>
</view> </view>
<text class="compare-item-spec">{{item.currentSpec || item.specification || (item.processedSpecs && item.processedSpecs.length > 0 ? item.processedSpecs.join(' | ') : '净重:45-50g')}} | {{item.category || '绿壳'}} | {{item.yolk || '无'}} | 库存:{{item.totalStock || '450'}}</text> <text class="compare-item-spec">{{item.currentSpec || item.specification || (item.processedSpecs && item.processedSpecs.length > 0 ? item.processedSpecs.join(' | ') : '净重:45-50g')}} | {{item.category || '品种'}} | {{item.yolk || '无'}} | 库存:{{item.totalStock || '450'}}</text>
<view class="compare-item-footer"> <view class="compare-item-footer">
<view class="merchant-region"> <view class="merchant-region">
<text class="compare-item-merchant">🏪 {{item.sourceType || '生态农场'}}</text> <text class="compare-item-merchant">🏪 {{item.sourceType || '生态农场'}}</text>

19
pages/compare_price/index.wxss

@ -327,6 +327,15 @@
flex-shrink: 0; flex-shrink: 0;
} }
.status-badge {
background-color: #F3F4F6;
border-radius: 9999px;
padding: 2px 8px;
font-size: 12px;
flex-shrink: 0;
font-weight: 500;
}
.goods-name { .goods-name {
font-size: 16px; font-size: 16px;
color: #1A1A1A; color: #1A1A1A;
@ -351,6 +360,13 @@
font-weight: bold; font-weight: bold;
} }
.unit-price {
font-size: 12px;
color: #666666;
margin-left: 8px;
white-space: nowrap;
}
.spec-info { .spec-info {
font-size: 14px; font-size: 14px;
color: #666666; color: #666666;
@ -416,8 +432,8 @@
.compare-item-header { .compare-item-header {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: space-between;
gap: 12px; gap: 12px;
flex-wrap: wrap;
} }
.compare-item-name { .compare-item-name {
@ -425,6 +441,7 @@
color: #1A1A1A; color: #1A1A1A;
font-weight: 500; font-weight: 500;
flex: 1; flex: 1;
min-width: 0;
white-space: normal; white-space: normal;
word-break: break-word; word-break: break-word;
line-height: 1.4; line-height: 1.4;

13
pages/goods-detail/goods-detail.js

@ -3830,8 +3830,12 @@ Page({
const currentGoods = this.data.goodsDetail; const currentGoods = this.data.goodsDetail;
const currentSpecifications = currentGoods.weightQuantityData || []; const currentSpecifications = currentGoods.weightQuantityData || [];
// 品种默认选择全部品种 // 品种默认选择当前商品的品种
const selectedCategory = '全部'; const selectedCategory = currentGoods.category || '全部';
// 找到当前商品品种在选项中的索引
const categoryIndex = this.data.categoryOptions.indexOf(selectedCategory);
const selectedCategoryIndex = categoryIndex !== -1 ? categoryIndex : 0;
// 检查规格数量 // 检查规格数量
if (currentSpecifications.length === 1) { if (currentSpecifications.length === 1) {
@ -3879,6 +3883,7 @@ Page({
showCompareModal: true, showCompareModal: true,
compareStep: 2, // 直接显示规格选择步骤 compareStep: 2, // 直接显示规格选择步骤
selectedCategory: selectedCategory, selectedCategory: selectedCategory,
selectedCategoryIndex: selectedCategoryIndex,
selectedSpecIndex: 0, selectedSpecIndex: 0,
selectedSpec: currentSpecifications.length > 0 ? currentSpecifications[0] : null, selectedSpec: currentSpecifications.length > 0 ? currentSpecifications[0] : null,
currentSpecifications: currentSpecifications currentSpecifications: currentSpecifications
@ -3972,8 +3977,8 @@ Page({
} }
const currentGoods = this.data.goodsDetail; const currentGoods = this.data.goodsDetail;
// 品种默认选择全部品种 // 使用已选择的品种
const selectedCategory = '全部'; const selectedCategory = this.data.selectedCategory || currentGoods.category || '全部';
const selectedSpec = this.data.selectedSpec; const selectedSpec = this.data.selectedSpec;
// 显示提示信息 // 显示提示信息

1
pages/goods-detail/goods-detail.wxml

@ -103,7 +103,6 @@
style="display: flex; align-items: left; margin-right: 20rpx; padding: 8rpx 16rpx; border: 2rpx solid #4a90e2; border-radius: 20rpx; position: relative; left: 68rpx; top: 0rpx" style="display: flex; align-items: left; margin-right: 20rpx; padding: 8rpx 16rpx; border: 2rpx solid #4a90e2; border-radius: 20rpx; position: relative; left: 68rpx; top: 0rpx"
> >
<text style="font-size: 32rpx; color: #4a90e2; margin-right: 8rpx;">对比价格</text> <text style="font-size: 32rpx; color: #4a90e2; margin-right: 8rpx;">对比价格</text>
<text style="font-size: 28rpx; color: #4a90e2;">></text>
</view> </view>
<view <view
class="favorite-button" class="favorite-button"

Loading…
Cancel
Save