Browse Source

修复售空状态下规格显示错误的问题

main
TraeAI 5 hours ago
parent
commit
fdbd49a0ae
  1. 119
      pages/freight-calculator/index.js
  2. 2
      pages/freight-calculator/index.wxml
  3. 20
      pages/goods-detail/goods-detail.js

119
pages/freight-calculator/index.js

@ -17,6 +17,8 @@ Page({
district: '',
detail: ''
},
// 是否从货源详情页进入(携带产品ID)
isFromGoodsDetail: false,
// 货物信息
goodsInfo: {
weight: '', // 重量(kg)
@ -155,65 +157,80 @@ Page({
// 初始化地址选择器数据
this.initRegionData();
// 如果从商品详情页跳转过来,获取商品信息
if (options.goodsId) {
this.loadGoodsInfo(options.goodsId);
}
// 如果从商品详情页或货源详情页跳转过来,设置标志并获取商品信息
if (options.goodsId || options.goodsInfo || options.goodsData) {
this.setData({ isFromGoodsDetail: true });
// 如果直接传递了货源信息 (goodsInfo)
if (options.goodsInfo) {
try {
const goodsInfo = JSON.parse(options.goodsInfo);
this.setData({
selectedGoods: goodsInfo,
'goodsInfo.weight': goodsInfo.grossWeight || '',
'goodsInfo.quantity': 1
});
} catch (e) {
console.error('解析货源信息失败:', e);
if (options.goodsId) {
this.loadGoodsInfo(options.goodsId);
}
}
// 如果直接传递了货源信息 (goodsData)
if (options.goodsData) {
try {
// 尝试直接解析
let goodsData;
// 如果直接传递了货源信息 (goodsInfo)
if (options.goodsInfo) {
try {
goodsData = JSON.parse(options.goodsData);
} catch (e1) {
// 如果直接解析失败,尝试解码后再解析
try {
const decodedGoodsData = decodeURIComponent(options.goodsData);
goodsData = JSON.parse(decodedGoodsData);
} catch (e2) {
console.error('解析货源信息失败 (已尝试解码):', e2);
throw e2;
const goodsInfo = JSON.parse(options.goodsInfo);
let weight = goodsInfo.grossWeight || '';
if (!weight && goodsInfo.netWeight && goodsInfo.quantity) {
// 计算总重量:净重(斤) * 件数 / 2(换算为公斤)
weight = (parseFloat(goodsInfo.netWeight) * parseInt(goodsInfo.quantity)) / 2;
}
this.setData({
selectedGoods: goodsInfo,
'goodsInfo.weight': weight,
'goodsInfo.quantity': goodsInfo.quantity || 1
});
} catch (e) {
console.error('解析货源信息失败:', e);
}
}
this.setData({
selectedGoods: goodsData,
'goodsInfo.weight': goodsData.grossWeight || '',
'goodsInfo.quantity': 1
});
// 如果直接传递了货源信息 (goodsData)
if (options.goodsData) {
try {
// 尝试直接解析
let goodsData;
try {
goodsData = JSON.parse(options.goodsData);
} catch (e1) {
// 如果直接解析失败,尝试解码后再解析
try {
const decodedGoodsData = decodeURIComponent(options.goodsData);
goodsData = JSON.parse(decodedGoodsData);
} catch (e2) {
console.error('解析货源信息失败 (已尝试解码):', e2);
throw e2;
}
}
let weight = goodsData.grossWeight || '';
if (!weight && goodsData.netWeight && goodsData.quantity) {
// 计算总重量:净重(斤) * 件数 / 2(换算为公斤)
weight = (parseFloat(goodsData.netWeight) * parseInt(goodsData.quantity)) / 2;
}
// 设置出发地为商品所在地
const regionToUse = goodsData.fullRegion || goodsData.region;
if (regionToUse) {
console.log('商品所在地原始地址:', regionToUse);
const regionInfo = this.parseRegion(regionToUse);
console.log('解析后的地址:', regionInfo);
this.setData({
'origin.province': regionInfo.province || '',
'origin.city': regionInfo.city || '',
'origin.district': regionInfo.district || '',
'origin.detail': regionInfo.detail || ''
selectedGoods: goodsData,
'goodsInfo.weight': weight,
'goodsInfo.quantity': goodsData.quantity || 1
});
console.log('已设置出发地为商品所在地:', regionInfo);
// 设置出发地为商品所在地
const regionToUse = goodsData.fullRegion || goodsData.region;
if (regionToUse) {
console.log('商品所在地原始地址:', regionToUse);
const regionInfo = this.parseRegion(regionToUse);
console.log('解析后的地址:', regionInfo);
this.setData({
'origin.province': regionInfo.province || '',
'origin.city': regionInfo.city || '',
'origin.district': regionInfo.district || '',
'origin.detail': regionInfo.detail || ''
});
console.log('已设置出发地为商品所在地:', regionInfo);
}
} catch (e) {
console.error('解析货源信息失败:', e);
}
} catch (e) {
console.error('解析货源信息失败:', e);
}
}
@ -266,6 +283,12 @@ Page({
this.setData({
'goodsInfo.weight': goodsItem.grossWeight
});
} else if (goodsItem.netWeight && goodsItem.quantity) {
// 计算总重量:净重(斤) * 件数 / 2(换算为公斤)
const totalWeight = (parseFloat(goodsItem.netWeight) * parseInt(goodsItem.quantity)) / 2;
this.setData({
'goodsInfo.weight': totalWeight
});
}
}
},

2
pages/freight-calculator/index.wxml

@ -2,7 +2,7 @@
<view class="container">
<view class="main-content">
<!-- 出发地信息 -->
<view class="section">
<view class="section" wx:if="{{!isFromGoodsDetail}}">
<text class="section-title">出发地</text>
<view class="address-form">
<view class="form-item address-row">

20
pages/goods-detail/goods-detail.js

@ -2223,13 +2223,27 @@ Page({
const price = priceArray[index] || '';
// 使用实际的quantity字段,而不是从spec中提取
const soldQuantity = quantityArray[index] || product.quantity || 0;
// 处理规格显示格式 - 根据内容类型添加相应前缀
let weightSpecDisplay = '';
if (spec) {
if (spec.includes('净重')) {
// 如果已包含"净重"前缀,保持不变
weightSpecDisplay = spec;
} else if (spec.includes('毛重')) {
// 如果已包含"毛重"前缀,保持不变
weightSpecDisplay = spec;
} else {
// 如果都不包含,默认为净重
weightSpecDisplay = `净重${spec}`;
}
}
return {
weightSpec: spec.includes('毛重') ? spec : `毛重${spec}`,
weightSpec: weightSpecDisplay,
quantity: soldQuantity,
price: price,
display: price ?
`${spec.includes('毛重') ? spec : `毛重${spec}`}【已售${soldQuantity}件】¥${price}` :
`${spec.includes('毛重') ? spec : `毛重${spec}`}【已售${soldQuantity}件】`,
`${weightSpecDisplay}【已售${soldQuantity}件】¥${price}` :
`${weightSpecDisplay}【已售${soldQuantity}件】`,
isSoldOut: true
};
});

Loading…
Cancel
Save