From 080f50005727204b99dc36df8962ffd5e280b9cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Mon, 5 Jan 2026 14:59:04 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E4=BC=98=E5=8C=96=E4=B9=B0=E5=AE=B6?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E3=80=81=E5=95=86=E5=93=81=E8=AF=A6=E6=83=85?= =?UTF-8?q?=E9=A1=B5=E5=92=8C=E9=A6=96=E9=A1=B5=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/buyer/index.js | 58 +++++------------ pages/buyer/index.wxml | 3 +- pages/goods-detail/goods-detail.js | 93 ++++++++++++++++++++++++++-- pages/goods-detail/goods-detail.wxml | 3 +- pages/goods/index.wxml | 5 +- pages/index/index.wxml | 6 +- 6 files changed, 114 insertions(+), 54 deletions(-) diff --git a/pages/buyer/index.js b/pages/buyer/index.js index f458f36..1fb588c 100644 --- a/pages/buyer/index.js +++ b/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 => { diff --git a/pages/buyer/index.wxml b/pages/buyer/index.wxml index 208b853..1a9f258 100644 --- a/pages/buyer/index.wxml +++ b/pages/buyer/index.wxml @@ -59,7 +59,8 @@ - {{item.supplyStatus || '暂无状态'}} + 售空 + {{item.supplyStatus}} {{item.name}} V diff --git a/pages/goods-detail/goods-detail.js b/pages/goods-detail/goods-detail.js index ae6cd22..099047a 100644 --- a/pages/goods-detail/goods-detail.js +++ b/pages/goods-detail/goods-detail.js @@ -173,7 +173,12 @@ function processWeightAndQuantityData(weightSpecString, quantityString, specStri // 组合显示:格式为"净重信息————件数" let display = ''; if (weightSpecDisplay && quantity) { - display = `${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('======================================'); - console.log('=== 处理结果调试信息 ==='); - console.log('weightSpecString:', weightSpecString); - console.log('quantityString:', quantityString); - console.log('weightQuantityData处理结果:', weightQuantityData); + // 检查是否为售空状态,如果是售空状态则直接返回售空信息 + 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('===== 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 // 优先使用预加载数据中的收藏状态 diff --git a/pages/goods-detail/goods-detail.wxml b/pages/goods-detail/goods-detail.wxml index 58e36cc..5ce9248 100644 --- a/pages/goods-detail/goods-detail.wxml +++ b/pages/goods-detail/goods-detail.wxml @@ -54,7 +54,8 @@ - {{goodsDetail.supplyStatus || '暂无状态'}} + 售空 + {{goodsDetail.supplyStatus}} {{goodsDetail.name}} V diff --git a/pages/goods/index.wxml b/pages/goods/index.wxml index 122c567..2576fcc 100644 --- a/pages/goods/index.wxml +++ b/pages/goods/index.wxml @@ -88,8 +88,9 @@ mode="aspectFill" lazy-load="true" > - 预售 - 现货 + 售空 + 预售 + 现货 diff --git a/pages/index/index.wxml b/pages/index/index.wxml index 68f3328..c7fbc2d 100644 --- a/pages/index/index.wxml +++ b/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;"> 预售 现货 - 已下架 + 售空 {{item.name}} {{item.displaySpecification}} | {{item.displayYolk}} {{item.sourceType || ''}} - {{item.negotiateStatus}} - 库存:{{item.totalStock && item.totalStock !== '充足' ? item.totalStock + '件' : (item.totalStock || '充足')}} + {{item.negotiateStatus}} + 库存:{{item.totalStock && item.totalStock !== '充足' ? item.totalStock + '件' : (item.totalStock || '充足')}} 已有{{item.reservedCount || 0}}人收藏