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