Browse Source

update: 优化买家页面、商品详情页和首页界面

pull/6/head
徐飞洋 2 months ago
parent
commit
080f500057
  1. 58
      pages/buyer/index.js
  2. 3
      pages/buyer/index.wxml
  3. 91
      pages/goods-detail/goods-detail.js
  4. 3
      pages/goods-detail/goods-detail.wxml
  5. 5
      pages/goods/index.wxml
  6. 6
      pages/index/index.wxml

58
pages/buyer/index.js

@ -1,33 +1,23 @@
// pages/buyer/index.js
const API = require('../../utils/api.js')
console.log('API对象内容:', API)
console.log('API方法列表:', Object.keys(API))
// 格式化毛重显示的辅助函数
function formatGrossWeight(grossWeight, weight) {
// 添加详细的日志记录,帮助诊断问题
console.log('===== formatGrossWeight 函数调用 =====');
console.log('输入参数:');
console.log('- grossWeight:', grossWeight, '(类型:', typeof grossWeight, ')');
console.log('- weight:', weight, '(类型:', typeof weight, ')');
// 1. 优先使用grossWeight,只要它不是null、不是undefined、不是空字符串
if (grossWeight !== null && grossWeight !== undefined && grossWeight !== '') {
console.log('使用grossWeight参数');
// 保持原始字符串类型,不再强制转换为数字
console.log('返回结果:', grossWeight);
return grossWeight;
}
// 如果grossWeight无效,尝试使用weight字段
if (weight !== null && weight !== undefined && weight !== '') {
console.log('使用weight参数');
// 保持原始字符串类型
console.log('返回结果:', weight);
return weight;
}
// 3. 新增逻辑:如果grossWeight和weight都无效,返回空字符串以支持文字输入
console.log('两个参数都无效,返回空字符串');
return ""; // 返回空字符串以支持文字输入
}
@ -131,7 +121,7 @@ Page({
},
onLoad() {
console.log('买家页面加载完成')
// 从本地存储加载已预约商品ID列表
const reservedGoodsIds = wx.getStorageSync('reservedGoodsIds') || []
this.setData({
@ -147,7 +137,7 @@ Page({
loadingMore: false
}, () => {
this.loadGoods().then(() => {
console.log('onLoad加载商品数据完成');
}).catch(err => {
console.error('onLoad加载商品数据失败:', err);
this.fallbackToLocalStorageWithPagination();
@ -166,7 +156,7 @@ Page({
// 点击"我想要"按钮
onClickWant: function (e) {
const goodsId = e.currentTarget.dataset.id;
console.log('用户点击了"我想要"按钮,商品ID:', goodsId, '类型:', typeof goodsId);
// 保存当前商品ID
this.setData({
@ -177,10 +167,7 @@ Page({
const openid = wx.getStorageSync('openid');
const userId = wx.getStorageSync('userId');
console.log('检查用户授权状态 - openid:', !!openid, 'userId:', !!userId);
if (!openid || !userId) {
console.log('用户未登录,显示一键登录弹窗');
// 显示一键登录弹窗,让用户确认是否要登录
this.showOneKeyLogin();
return;
@ -227,11 +214,11 @@ Page({
return;
}
console.log('找到商品信息:', goodsItem)
// 检查商品是否已预约
if (goodsItem.isReserved) {
console.log('商品已预约,无需重复操作');
return;
}
@ -249,17 +236,11 @@ Page({
testMode: false
}
console.log('找到的完整商品信息:', goodsItem)
wx.showLoading({ title: '正在预约...' })
// 调用API增加预约人数
console.log('准备调用API.addToCart,传递完整的product对象');
API.addToCart(product)
.then(res => {
wx.hideLoading()
console.log('增加预约人数成功:', res)
console.log('API.addToCart返回的数据结构:', JSON.stringify(res))
// 增强的成功检测逻辑:即使服务器没有明确返回success:true,也尝试更新UI
const isSuccess = res && (res.success || res.code === 200 || res.status === 'success');
@ -290,10 +271,9 @@ Page({
updateData[`filteredGoods[${goodsIndex}].reservationCount`] = newReservedCount;
// 4. 应用更新并验证
console.log('准备更新商品状态:', updateData);
this.setData(updateData, () => {
console.log('商品状态更新成功');
console.log('更新后商品状态:', this.data.filteredGoods[goodsIndex].isReserved);
});
} else {
console.warn('未找到对应商品索引,无法即时更新UI状态');
@ -351,7 +331,7 @@ Page({
// 对于需要处理的特殊情况,仍然在后台默默处理
if (err.isForeignKeyError) {
console.log('检测到外键约束错误,自动刷新商品列表', { productId: err.productId });
// 先尝试在本地更新商品状态为预约中,提供即时反馈
const goodsItem = this.findGoodsItemById(String(goodsId));
@ -366,7 +346,7 @@ Page({
updateData[`filteredGoods[${goodsIndex}].reservationCount`] =
this.data.filteredGoods[goodsIndex].reservationCount + 1;
}
console.log('临时更新商品状态,等待刷新确认:', updateData);
this.setData(updateData);
}
}
@ -377,7 +357,7 @@ Page({
}, 500);
}
else if (err.message.includes('刷新')) {
console.log('需要刷新商品列表');
// 后台静默刷新商品列表
setTimeout(() => {
this.refreshGoodsList();
@ -442,9 +422,7 @@ Page({
})
// 保存到本地存储
wx.setStorageSync('reservedGoodsIds', newReservedGoodsIds)
console.log('已更新预约列表:', newReservedGoodsIds)
} else {
console.log('商品已在预约列表中')
}
},
@ -462,7 +440,6 @@ Page({
}, () => {
// 调用loadGoods函数重新加载第一页数据
this.loadGoods().then(() => {
console.log('刷新商品列表完成');
// 调用调试函数检查创建时间字段
this.debugCreatedAtFields();
}).catch(err => {
@ -472,7 +449,6 @@ Page({
},
onShow() {
console.log('页面显示,开始重新加载数据 - 使用分页加载')
// 确保用户身份被设置为买家
const userId = wx.getStorageSync('userId');
@ -513,7 +489,7 @@ Page({
}, () => {
// 调用loadGoods函数加载第一页数据
this.loadGoods().then((result) => {
console.log('onShow加载商品数据完成');
// 记录浏览行为
this.recordBehavior('browse', 'goods');
}).catch(err => {

3
pages/buyer/index.wxml

@ -59,7 +59,8 @@
<view>
<view style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 10rpx;">
<view style="display: flex; align-items: center; flex: 1;">
<view style="display: inline-block; margin-right: 10rpx; font-size: 18rpx; color: #fff; background: {{item.supplyStatus === '现货' ? 'rgba(76, 175, 80, 0.8)' : 'rgba(218, 165, 32, 0.8)'}}; padding: 4rpx 10rpx; border-radius: 15rpx; vertical-align: middle; backdrop-filter: blur(10rpx); border: 1rpx solid rgba(255, 255, 255, 0.3); box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15), inset 0 1rpx 0 rgba(255, 255, 255, 0.5); text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.2); font-weight: bold; margin-top: -0.1rpx;">{{item.supplyStatus || '暂无状态'}}</view>
<view wx:if="{{item.status === 'sold_out'}}" style="display: inline-block; margin-right: 10rpx; font-size: 18rpx; color: #fff; background: linear-gradient(135deg, #8c8c8c 0%, #a6a6a6 100%); padding: 4rpx 10rpx; border-radius: 15rpx; vertical-align: middle; backdrop-filter: blur(10rpx); border: 1rpx solid rgba(255, 255, 255, 0.3); box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15), inset 0 1rpx 0 rgba(255, 255, 255, 0.5); text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.2); font-weight: bold; margin-top: -0.1rpx;">售空</view>
<view wx:elif="{{item.supplyStatus}}" style="display: inline-block; margin-right: 10rpx; font-size: 18rpx; color: #fff; background: {{item.supplyStatus === '现货' ? 'rgba(76, 175, 80, 0.8)' : 'rgba(218, 165, 32, 0.8)'}}; padding: 4rpx 10rpx; border-radius: 15rpx; vertical-align: middle; backdrop-filter: blur(10rpx); border: 1rpx solid rgba(255, 255, 255, 0.3); box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15), inset 0 1rpx 0 rgba(255, 255, 255, 0.5); text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.2); font-weight: bold; margin-top: -0.1rpx;">{{item.supplyStatus}}</view>
<text style="font-size: 36rpx; font-weight: bold;">{{item.name}}</text>
<span style="vertical-align: middle; font-size: 12rpx; color: white; background: linear-gradient(135deg, #4a90e2 0%, #2b66f0 50%, #1a4bbd 100%); padding: 4rpx 10rpx; clip-path: polygon(50% 0%, 70% 10%, 100% 30%, 100% 70%, 70% 90%, 50% 100%, 30% 90%, 0% 70%, 0% 30%, 30% 10%); margin-left: 8rpx; box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.3), inset 0 1rpx 2rpx rgba(255, 255, 255, 0.5); text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.5); font-weight: bold; margin-top: -20rpx;">V</span>
</view>

91
pages/goods-detail/goods-detail.js

@ -173,7 +173,12 @@ function processWeightAndQuantityData(weightSpecString, quantityString, specStri
// 组合显示:格式为"净重信息————件数"
let display = '';
if (weightSpecDisplay && quantity) {
// 检查是否为售空状态,如果是售空状态则显示"售空"
if (weightSpecDisplay.includes('售空') || quantity === '售空') {
display = `${weightSpecDisplay}————售空`;
} else {
display = `${weightSpecDisplay}————${quantity}`;
}
} else if (weightSpecDisplay) {
display = weightSpecDisplay;
} else if (quantity) {
@ -538,12 +543,80 @@ Page({
quantityString: quantityString
});
const weightQuantityData = processWeightAndQuantityData(weightSpecString, quantityString, '');
// 检查商品状态和规格信息处理
console.log('===== 商品状态检查 =====');
console.log('商品状态:', product.status);
console.log('商品完整对象关键字段:', {
status: product.status,
supplyStatus: product.supplyStatus,
weightSpecString: weightSpecString,
quantityString: quantityString,
weightSpec: product.weightSpec,
quantity: product.quantity
});
// 检查多种可能的售空状态
const isSoldOut = product.status === 'sold_out' ||
product.status === 'sold' ||
product.status === 'out_of_stock' ||
(product.supplyStatus && product.supplyStatus.includes('售空'));
console.log('===== 售空判断详细分析 =====');
console.log('商品状态值:', product.status, '(类型:', typeof product.status, ')');
console.log('商品供应状态:', product.supplyStatus);
console.log('判断条件:');
console.log(' - status === "sold_out":', product.status === 'sold_out');
console.log(' - status === "sold":', product.status === 'sold');
console.log(' - status === "out_of_stock":', product.status === 'out_of_stock');
console.log(' - supplyStatus包含"售空":', product.supplyStatus && product.supplyStatus.includes('售空'));
console.log('最终判断结果 isSoldOut:', isSoldOut);
console.log('======================================');
// 检查是否为售空状态,如果是售空状态则直接返回售空信息
let weightQuantityData = [];
console.log('===== weightQuantityData初始化 =====');
console.log('当前isSoldOut值:', isSoldOut);
console.log('即将执行的条件分支...');
if (isSoldOut) {
// 售空状态的商品,只显示规格信息,不显示件数
if (weightSpecString) {
// 处理净重/规格字符串,只显示规格信息
const weightSpecArray = weightSpecString.split(/[,,、]/).map(item => item.trim()).filter(item => item);
weightQuantityData = weightSpecArray.map(spec => ({
weightSpec: spec.includes('毛重') ? spec : `毛重${spec}`,
quantity: '售空',
display: spec.includes('毛重') ? spec : `毛重${spec}`
}));
} else {
// 如果没有规格信息,则显示默认的售空信息
weightQuantityData = [{
weightSpec: '规格信息',
quantity: '售空',
display: '规格信息'
}];
}
console.log('✓ 售空分支执行: 只显示规格信息');
console.log('weightQuantityData设置为:', weightQuantityData);
} else {
// 非售空状态,正常处理规格信息,但不显示件数
console.log('× 非售空分支执行: 只显示规格信息');
console.log('输入参数: weightSpecString="', weightSpecString, '"');
if (weightSpecString) {
// 处理净重/规格字符串,只显示规格信息
const weightSpecArray = weightSpecString.split(/[,,、]/).map(item => item.trim()).filter(item => item);
weightQuantityData = weightSpecArray.map(spec => ({
weightSpec: spec.includes('毛重') ? spec : `毛重${spec}`,
quantity: '',
display: spec.includes('毛重') ? spec : `毛重${spec}`
}));
}
console.log('× 非售空分支结果:', weightQuantityData);
}
console.log('=== 处理结果调试信息 ===');
console.log('weightSpecString:', weightSpecString);
console.log('quantityString:', quantityString);
console.log('weightQuantityData处理结果:', weightQuantityData);
console.log('===== weightQuantityData最终结果 =====');
console.log('weightQuantityData:', JSON.stringify(weightQuantityData, null, 2));
// 转换supplyStatus字段值
let supplyStatusValue = product.supplyStatus || '';
@ -608,6 +681,9 @@ Page({
// 转换商品数据格式
const formattedGoods = {
// 优先设置售空状态标记,放在最前面确保不被覆盖
_isSoldOut: isSoldOut,
// 其他字段
id: productIdStr,
productId: productIdStr,
// 直接使用数据库字段名
@ -659,6 +735,11 @@ Page({
// 保存预加载数据中的isFavorite状态,确保是布尔值
const preloadedFavoriteStatus = preloadedData ? (preloadedData.isFavorite || false) : false;
console.log('===== 设置页面数据 =====');
console.log('formattedGoods._isSoldOut:', formattedGoods._isSoldOut);
console.log('商品状态:', formattedGoods.status);
console.log('weightQuantityData:', formattedGoods.weightQuantityData);
this.setData({
goodsDetail: formattedGoods,
isFavorite: preloadedFavoriteStatus // 优先使用预加载数据中的收藏状态

3
pages/goods-detail/goods-detail.wxml

@ -54,7 +54,8 @@
<view class="goods-info">
<view style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 10rpx;">
<view style="display: flex; align-items: center; flex: 1;">
<view style="display: inline-block; margin-right: 10rpx; font-size: 18rpx; color: #fff; background: rgba(218, 165, 32, 0.8); padding: 4rpx 10rpx; border-radius: 15rpx; vertical-align: middle; backdrop-filter: blur(10rpx); border: 1rpx solid rgba(255, 255, 255, 0.3); box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15), inset 0 1rpx 0 rgba(255, 255, 255, 0.5); text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.2); font-weight: bold; margin-top: -20rpx;">{{goodsDetail.supplyStatus || '暂无状态'}}</view>
<view wx:if="{{goodsDetail.status === 'sold_out'}}" style="display: inline-block; margin-right: 10rpx; font-size: 18rpx; color: #fff; background: linear-gradient(135deg, #8c8c8c 0%, #a6a6a6 100%); padding: 4rpx 10rpx; border-radius: 15rpx; vertical-align: middle; backdrop-filter: blur(10rpx); border: 1rpx solid rgba(255, 255, 255, 0.3); box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15), inset 0 1rpx 0 rgba(255, 255, 255, 0.5); text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.2); font-weight: bold; margin-top: -20rpx;">售空</view>
<view wx:elif="{{goodsDetail.supplyStatus}}" style="display: inline-block; margin-right: 10rpx; font-size: 18rpx; color: #fff; background: rgba(218, 165, 32, 0.8); padding: 4rpx 10rpx; border-radius: 15rpx; vertical-align: middle; backdrop-filter: blur(10rpx); border: 1rpx solid rgba(255, 255, 255, 0.3); box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15), inset 0 1rpx 0 rgba(255, 255, 255, 0.5); text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.2); font-weight: bold; margin-top: -20rpx;">{{goodsDetail.supplyStatus}}</view>
<text class="goods-name">{{goodsDetail.name}}</text>
<span style="vertical-align: middle; font-size: 20rpx; color: white; background: linear-gradient(135deg, #4a90e2 0%, #2b66f0 50%, #1a4bbd 100%); padding: 4rpx 8rpx; clip-path: polygon(50% 0%, 70% 10%, 100% 30%, 100% 70%, 70% 90%, 50% 100%, 30% 90%, 0% 70%, 0% 30%, 30% 10%); margin-left: 8rpx; box-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.3), inset 0 1rpx 2rpx rgba(255, 255, 255, 0.5); text-shadow: 0 1rpx 2rpx rgba(0, 0, 0, 0.5); font-weight: bold; margin-top: -20rpx;">V</span>
</view>

5
pages/goods/index.wxml

@ -88,8 +88,9 @@
mode="aspectFill"
lazy-load="true"
></image>
<view wx:if="{{item.supplyStatus === '预售'}}" class="promo-tag presale">预售</view>
<view wx:if="{{item.supplyStatus === '现货'}}" class="promo-tag in-stock">现货</view>
<view wx:if="{{item.status === 'sold_out'}}" class="promo-tag sold-out">售空</view>
<view wx:elif="{{item.supplyStatus === '预售'}}" class="promo-tag presale">预售</view>
<view wx:elif="{{item.supplyStatus === '现货'}}" class="promo-tag in-stock">现货</view>
</view>
<view class="product-info">
<view class="product-title-row">

6
pages/index/index.wxml

@ -179,15 +179,15 @@
style="height: 270rpx; display: block; box-sizing: border-box; width: 337rpx; position: absolute; top: 0; left: 0; right: 0; bottom: 0; z-index: 1;"></image>
<view wx:if="{{item.supplyStatus === '预售'}}" class="promo-tag presale">预售</view>
<view wx:if="{{item.supplyStatus === '现货'}}" class="promo-tag in-stock">现货</view>
<view wx:if="{{item.status === 'sold_out'}}" class="promo-tag sold-out">已下架</view>
<view wx:if="{{item.status === 'sold_out'}}" class="promo-tag sold-out">售空</view>
</view>
<view class="product-info" style="height: 190rpx; display: flex; box-sizing: border-box">
<view class="product-title" style="height: 38rpx; display: -webkit-box; box-sizing: border-box">{{item.name}}</view>
<view class="product-spec" style="width: 308rpx; height: 29rpx; display: block; box-sizing: border-box">{{item.displaySpecification}}<text wx:if="{{item.displayYolk}}"> | {{item.displayYolk}}</text></view>
<view class="product-status-row" style="width: 339rpx; display: block; box-sizing: border-box; height: 60rpx; left: -10rpx; top: 0rpx">
<view class="status-tag source-{{item.sourceType === '三方认证' ? 'third' : (item.sourceType === '平台货源' ? 'platform' : 'unverified')}}">{{item.sourceType || ''}}</view>
<view class="status-tag negotiate-{{item.negotiateStatus === '可议价' ? 'yes' : 'no'}}" style="width: 70rpx; display: inline-block; box-sizing: border-box">{{item.negotiateStatus}}</view>
<view class="status-tag item-count">库存:{{item.totalStock && item.totalStock !== '充足' ? item.totalStock + '件' : (item.totalStock || '充足')}}</view>
<view wx:if="{{item.status !== 'sold_out'}}" class="status-tag negotiate-{{item.negotiateStatus === '可议价' ? 'yes' : 'no'}}" style="width: 70rpx; display: inline-block; box-sizing: border-box">{{item.negotiateStatus}}</view>
<view wx:if="{{item.status !== 'sold_out'}}" class="status-tag item-count">库存:{{item.totalStock && item.totalStock !== '充足' ? item.totalStock + '件' : (item.totalStock || '充足')}}</view>
</view>
<view class="product-meta">
<text class="sales-count">已有{{item.reservedCount || 0}}人收藏</text>

Loading…
Cancel
Save