|
|
|
@ -462,6 +462,13 @@ Page({ |
|
|
|
offsetX: 0, // X轴偏移量
|
|
|
|
offsetY: 0, // Y轴偏移量
|
|
|
|
initialTouch: null, // 初始触摸点
|
|
|
|
// 对比价格相关状态
|
|
|
|
showCompareModal: false, // 是否显示对比价格弹窗
|
|
|
|
activeTab: 'home', // 当前激活的选项卡,'home'表示首页数据,'favorite'表示收藏数据
|
|
|
|
homeGoods: [], // 首页商品数据
|
|
|
|
favoriteGoods: [], // 收藏商品数据
|
|
|
|
loadingHome: false, // 首页数据加载状态
|
|
|
|
loadingFavorite: false, // 收藏数据加载状态
|
|
|
|
}, |
|
|
|
|
|
|
|
onLoad: function (options) { |
|
|
|
@ -2028,6 +2035,248 @@ Page({ |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 对比价格功能:处理按钮点击事件
|
|
|
|
onCompareClick: function () { |
|
|
|
console.log('用户点击了对比价格按钮,准备显示弹窗'); |
|
|
|
// 立即显示弹窗,不等待数据加载
|
|
|
|
this.setData({ |
|
|
|
showCompareModal: true, |
|
|
|
activeTab: 'home' // 默认显示首页数据选项卡
|
|
|
|
}); |
|
|
|
|
|
|
|
// 打印弹窗状态,用于调试
|
|
|
|
console.log('弹窗状态设置为:', this.data.showCompareModal); |
|
|
|
|
|
|
|
// 加载首页数据
|
|
|
|
this.loadHomeGoods(); |
|
|
|
|
|
|
|
// 加载收藏数据
|
|
|
|
this.loadFavoriteGoods(); |
|
|
|
}, |
|
|
|
|
|
|
|
// 关闭对比价格弹窗
|
|
|
|
closeCompareModal: function () { |
|
|
|
this.setData({ |
|
|
|
showCompareModal: false |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 阻止事件冒泡
|
|
|
|
stopPropagation: function () { |
|
|
|
// 空函数,用于阻止事件冒泡
|
|
|
|
}, |
|
|
|
|
|
|
|
// 切换选项卡
|
|
|
|
switchTab: function (e) { |
|
|
|
const tab = e.currentTarget.dataset.tab; |
|
|
|
this.setData({ |
|
|
|
activeTab: tab |
|
|
|
}); |
|
|
|
|
|
|
|
// 如果切换到收藏选项卡且收藏数据为空,则加载收藏数据
|
|
|
|
if (tab === 'favorite' && this.data.favoriteGoods.length === 0) { |
|
|
|
this.loadFavoriteGoods(); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
// 加载首页商品数据
|
|
|
|
loadHomeGoods: function () { |
|
|
|
this.setData({ loadingHome: true }); |
|
|
|
|
|
|
|
// 调用API获取首页商品列表
|
|
|
|
API.getProducts() |
|
|
|
.then(res => { |
|
|
|
console.log('获取首页商品数据成功:', res); |
|
|
|
// API.getProducts()直接返回商品数组,而不是包含data字段的对象
|
|
|
|
if (Array.isArray(res)) { |
|
|
|
// 为每个商品添加mediaItems字段
|
|
|
|
const processedGoods = res.map(goods => { |
|
|
|
return { |
|
|
|
...goods, |
|
|
|
mediaItems: processMediaUrls(goods.imageUrls) |
|
|
|
}; |
|
|
|
}); |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
homeGoods: processedGoods, |
|
|
|
loadingHome: false |
|
|
|
}); |
|
|
|
} else { |
|
|
|
this.setData({ loadingHome: false }); |
|
|
|
wx.showToast({ |
|
|
|
title: '获取首页数据失败', |
|
|
|
icon: 'none', |
|
|
|
duration: 2000 |
|
|
|
}); |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch(err => { |
|
|
|
console.error('获取首页商品数据失败:', err); |
|
|
|
this.setData({ loadingHome: false }); |
|
|
|
wx.showToast({ |
|
|
|
title: '获取首页数据失败', |
|
|
|
icon: 'none', |
|
|
|
duration: 2000 |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 加载已收藏商品数据
|
|
|
|
loadFavoriteGoods: function () { |
|
|
|
this.setData({ loadingFavorite: true }); |
|
|
|
|
|
|
|
// 获取用户手机号
|
|
|
|
let userPhone = ''; |
|
|
|
try { |
|
|
|
// 打印所有可能的存储位置内容,用于调试
|
|
|
|
console.log('调试信息 - 获取收藏数据前的存储状态:'); |
|
|
|
console.log('userId:', wx.getStorageSync('userId')); |
|
|
|
console.log('users:', wx.getStorageSync('users')); |
|
|
|
console.log('userInfo:', wx.getStorageSync('userInfo')); |
|
|
|
console.log('phoneNumber:', wx.getStorageSync('phoneNumber')); |
|
|
|
|
|
|
|
// 先尝试从最新的存储位置获取
|
|
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
|
if (userInfo && userInfo.phoneNumber) { |
|
|
|
userPhone = userInfo.phoneNumber; |
|
|
|
console.log('从userInfo获取到手机号:', userPhone); |
|
|
|
} else { |
|
|
|
// 然后尝试从users对象获取
|
|
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
if (userId && users[userId] && users[userId].phoneNumber) { |
|
|
|
userPhone = users[userId].phoneNumber; |
|
|
|
console.log('从users[' + userId + ']获取到手机号:', userPhone); |
|
|
|
} else { |
|
|
|
// 最后尝试从单独的phoneNumber字段获取
|
|
|
|
userPhone = wx.getStorageSync('phoneNumber'); |
|
|
|
console.log('从phoneNumber字段获取到手机号:', userPhone); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
console.error('获取用户手机号失败:', e); |
|
|
|
} |
|
|
|
|
|
|
|
// 验证手机号是否有效
|
|
|
|
if (!userPhone) { |
|
|
|
this.setData({ loadingFavorite: false }); |
|
|
|
console.log('用户未登录或未获取手机号,无法加载收藏数据'); |
|
|
|
wx.showToast({ |
|
|
|
title: '请先登录获取手机号', |
|
|
|
icon: 'none', |
|
|
|
duration: 2000 |
|
|
|
}); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 确保手机号格式正确
|
|
|
|
if (typeof userPhone !== 'string' || userPhone.length < 11) { |
|
|
|
this.setData({ loadingFavorite: false }); |
|
|
|
console.error('手机号格式不正确:', userPhone); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
console.log('准备调用API获取收藏数据,手机号:', userPhone); |
|
|
|
|
|
|
|
// 调用API获取收藏商品列表
|
|
|
|
API.getFavorites(userPhone) |
|
|
|
.then(res => { |
|
|
|
console.log('获取收藏商品数据成功:', res); |
|
|
|
|
|
|
|
// 处理API返回的数据结构
|
|
|
|
let favoritesList = []; |
|
|
|
if (res) { |
|
|
|
if (Array.isArray(res.data)) { |
|
|
|
favoritesList = res.data; |
|
|
|
} else if (res.data && Array.isArray(res.data.favorites)) { |
|
|
|
favoritesList = res.data.favorites; |
|
|
|
} else if (res.data && Array.isArray(res.data.data)) { |
|
|
|
favoritesList = res.data.data; |
|
|
|
} else if (Array.isArray(res)) { |
|
|
|
// 直接返回数组的情况
|
|
|
|
favoritesList = res; |
|
|
|
} |
|
|
|
|
|
|
|
console.log('解析后的收藏商品列表:', favoritesList); |
|
|
|
} |
|
|
|
|
|
|
|
// 检查收藏列表是否只包含productId
|
|
|
|
const hasOnlyProductId = favoritesList.length > 0 && |
|
|
|
favoritesList.every(item => |
|
|
|
(typeof item === 'string' || typeof item === 'number') || |
|
|
|
(typeof item === 'object' && item.productId && !item.name) |
|
|
|
); |
|
|
|
|
|
|
|
if (hasOnlyProductId) { |
|
|
|
console.log('收藏列表只包含productId,需要获取商品详情'); |
|
|
|
// 转换为productId数组
|
|
|
|
const productIds = favoritesList.map(item => |
|
|
|
typeof item === 'object' ? item.productId : item |
|
|
|
); |
|
|
|
|
|
|
|
console.log('提取的productId列表:', productIds); |
|
|
|
|
|
|
|
// 并行获取所有商品详情
|
|
|
|
const productPromises = productIds.map(productId => |
|
|
|
API.getProductDetail({ productId: productId }) |
|
|
|
.then(detailRes => { |
|
|
|
console.log('获取商品详情成功:', productId, detailRes); |
|
|
|
return detailRes && detailRes.data ? detailRes.data : null; |
|
|
|
}) |
|
|
|
.catch(err => { |
|
|
|
console.error('获取商品详情失败:', productId, err); |
|
|
|
return null; |
|
|
|
}) |
|
|
|
); |
|
|
|
|
|
|
|
// 等待所有商品详情获取完成
|
|
|
|
Promise.all(productPromises) |
|
|
|
.then(products => { |
|
|
|
// 过滤掉获取失败的商品,并为每个商品添加mediaItems字段
|
|
|
|
const validProducts = products |
|
|
|
.filter(product => product !== null) |
|
|
|
.map(product => ({ |
|
|
|
...product, |
|
|
|
mediaItems: processMediaUrls(product.imageUrls) |
|
|
|
})); |
|
|
|
console.log('所有商品详情获取完成,有效商品数量:', validProducts.length); |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
favoriteGoods: validProducts, |
|
|
|
loadingFavorite: false |
|
|
|
}); |
|
|
|
}) |
|
|
|
.catch(err => { |
|
|
|
console.error('获取商品详情列表失败:', err); |
|
|
|
this.setData({ |
|
|
|
favoriteGoods: [], |
|
|
|
loadingFavorite: false |
|
|
|
}); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
// 收藏列表已经包含完整商品信息,为每个商品添加mediaItems字段
|
|
|
|
const processedFavorites = favoritesList.map(product => ({ |
|
|
|
...product, |
|
|
|
mediaItems: processMediaUrls(product.imageUrls) |
|
|
|
})); |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
favoriteGoods: processedFavorites, |
|
|
|
loadingFavorite: false |
|
|
|
}); |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch(err => { |
|
|
|
console.error('获取收藏商品数据失败:', err); |
|
|
|
this.setData({ loadingFavorite: false }); |
|
|
|
wx.showToast({ |
|
|
|
title: '获取收藏数据失败: ' + (err && err.message ? err.message : '未知错误'), |
|
|
|
icon: 'none', |
|
|
|
duration: 3000 |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 返回上一页
|
|
|
|
goBack() { |
|
|
|
wx.navigateBack(); |
|
|
|
|