|
|
|
@ -71,6 +71,7 @@ Page({ |
|
|
|
previewImageUrls: [], // 预览的图片URL列表
|
|
|
|
previewImageIndex: 0, // 当前预览图片的索引
|
|
|
|
fromSeller: false, // 是否来自seller页面
|
|
|
|
isFavorite: false, // 当前商品是否已收藏
|
|
|
|
// 图片缩放相关状态
|
|
|
|
scale: 1, // 当前缩放比例
|
|
|
|
lastScale: 1, // 上一次缩放比例
|
|
|
|
@ -94,7 +95,8 @@ Page({ |
|
|
|
// 优先使用传入的商品数据中的联系人信息
|
|
|
|
this.setData({ |
|
|
|
goodsDetail: goodsData, |
|
|
|
fromSeller: options.fromSeller === 'true' |
|
|
|
fromSeller: options.fromSeller === 'true', |
|
|
|
isFavorite: goodsData.isFavorite || false // 初始化收藏状态
|
|
|
|
}); |
|
|
|
} catch (error) { |
|
|
|
console.error('解析商品数据失败:', error); |
|
|
|
@ -114,6 +116,28 @@ Page({ |
|
|
|
|
|
|
|
// 加载商品详情(即使已有goodsData,也调用API获取最新数据)
|
|
|
|
this.loadGoodsDetail(productId, goodsData); |
|
|
|
|
|
|
|
// 添加收藏状态变化事件监听
|
|
|
|
const app = getApp(); |
|
|
|
this.favoriteChangedHandler = (data) => { |
|
|
|
console.log('收到收藏状态变化通知:', data); |
|
|
|
// 如果通知的商品ID与当前页面的商品ID相同,则更新收藏状态
|
|
|
|
if (data.productId === String(productId) || data.productId === String(this.data.goodsDetail.id)) { |
|
|
|
this.setData({ |
|
|
|
isFavorite: data.isFavorite |
|
|
|
}); |
|
|
|
} |
|
|
|
}; |
|
|
|
app.eventBus.on('favoriteChanged', this.favoriteChangedHandler); |
|
|
|
}, |
|
|
|
|
|
|
|
onUnload: function () { |
|
|
|
// 页面卸载时移除事件监听
|
|
|
|
const app = getApp(); |
|
|
|
if (this.favoriteChangedHandler) { |
|
|
|
app.eventBus.off('favoriteChanged', this.favoriteChangedHandler); |
|
|
|
console.log('移除收藏状态变化事件监听'); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
loadGoodsDetail: function (productId, preloadedData = null) { |
|
|
|
@ -252,6 +276,9 @@ Page({ |
|
|
|
this.setData({ |
|
|
|
goodsDetail: formattedGoods |
|
|
|
}); |
|
|
|
|
|
|
|
// 加载商品的收藏状态
|
|
|
|
this.loadGoodsFavoriteStatus(productIdStr); |
|
|
|
} else { |
|
|
|
wx.showToast({ |
|
|
|
title: '获取商品详情失败', |
|
|
|
@ -272,6 +299,184 @@ Page({ |
|
|
|
wx.hideLoading(); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 加载商品的收藏状态
|
|
|
|
loadGoodsFavoriteStatus: function (productId) { |
|
|
|
const openid = wx.getStorageSync('openid'); |
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
|
|
|
|
// 如果用户未登录,不加载收藏状态
|
|
|
|
if (!openid || !userId) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 获取用户手机号
|
|
|
|
let userPhone = ''; |
|
|
|
try { |
|
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
|
if (userId && users[userId] && users[userId].phoneNumber) { |
|
|
|
userPhone = users[userId].phoneNumber; |
|
|
|
} else { |
|
|
|
const userInfo = wx.getStorageSync('userInfo'); |
|
|
|
if (userInfo && userInfo.phoneNumber) { |
|
|
|
userPhone = userInfo.phoneNumber; |
|
|
|
} else { |
|
|
|
userPhone = wx.getStorageSync('phoneNumber'); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
console.error('获取用户手机号失败:', e); |
|
|
|
} |
|
|
|
|
|
|
|
if (!userPhone) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 调用API获取用户收藏列表
|
|
|
|
API.getFavorites(userPhone) |
|
|
|
.then(res => { |
|
|
|
if (res && res.code === 200 && res.data && Array.isArray(res.data)) { |
|
|
|
const favoriteProductIds = res.data.map(item => String(item.productId || item.id)); |
|
|
|
const isFavorite = favoriteProductIds.includes(String(productId)); |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
isFavorite: isFavorite |
|
|
|
}); |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch(err => { |
|
|
|
console.error('获取收藏状态失败:', err); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 添加收藏
|
|
|
|
addFavorite: function () { |
|
|
|
const productId = String(this.data.goodsDetail.id || this.data.goodsDetail.productId); |
|
|
|
console.log('用户点击了收藏按钮,商品ID:', productId); |
|
|
|
|
|
|
|
// 检查用户登录状态
|
|
|
|
const openid = wx.getStorageSync('openid'); |
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
|
|
|
|
if (!openid || !userId) { |
|
|
|
console.log('用户未登录,显示一键登录弹窗'); |
|
|
|
// 由于商品详情页可能没有登录弹窗,这里直接提示用户登录
|
|
|
|
wx.showToast({ |
|
|
|
title: '请先登录', |
|
|
|
icon: 'none', |
|
|
|
duration: 2000 |
|
|
|
}); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
wx.showLoading({ title: '正在收藏...' }); |
|
|
|
|
|
|
|
// 调用API添加收藏
|
|
|
|
API.addFavorite(productId) |
|
|
|
.then(res => { |
|
|
|
wx.hideLoading(); |
|
|
|
console.log('添加收藏成功:', res); |
|
|
|
|
|
|
|
// 更新商品的收藏状态
|
|
|
|
this.setData({ |
|
|
|
isFavorite: true |
|
|
|
}); |
|
|
|
|
|
|
|
// 触发全局事件,通知其他页面收藏状态已更改
|
|
|
|
const app = getApp(); |
|
|
|
app.eventBus.emit('favoriteChanged', { |
|
|
|
productId: productId, |
|
|
|
isFavorite: true |
|
|
|
}); |
|
|
|
|
|
|
|
// 显示成功提示
|
|
|
|
wx.showToast({ |
|
|
|
title: '收藏成功', |
|
|
|
icon: 'success', |
|
|
|
duration: 1500 |
|
|
|
}); |
|
|
|
}) |
|
|
|
.catch(err => { |
|
|
|
wx.hideLoading(); |
|
|
|
console.error('添加收藏失败:', err); |
|
|
|
|
|
|
|
// 显示错误提示
|
|
|
|
wx.showToast({ |
|
|
|
title: '收藏失败,请稍后重试', |
|
|
|
icon: 'none', |
|
|
|
duration: 2000 |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 取消收藏
|
|
|
|
cancelFavorite: function () { |
|
|
|
const productId = String(this.data.goodsDetail.id || this.data.goodsDetail.productId); |
|
|
|
console.log('用户点击了取消收藏按钮,商品ID:', productId); |
|
|
|
|
|
|
|
// 检查用户登录状态
|
|
|
|
const openid = wx.getStorageSync('openid'); |
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
|
|
|
|
if (!openid || !userId) { |
|
|
|
console.log('用户未登录,显示一键登录弹窗'); |
|
|
|
// 由于商品详情页可能没有登录弹窗,这里直接提示用户登录
|
|
|
|
wx.showToast({ |
|
|
|
title: '请先登录', |
|
|
|
icon: 'none', |
|
|
|
duration: 2000 |
|
|
|
}); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
wx.showLoading({ title: '正在取消收藏...' }); |
|
|
|
|
|
|
|
// 调用API取消收藏
|
|
|
|
API.cancelFavorite(productId) |
|
|
|
.then(res => { |
|
|
|
wx.hideLoading(); |
|
|
|
console.log('取消收藏成功:', res); |
|
|
|
|
|
|
|
// 更新商品的收藏状态
|
|
|
|
this.setData({ |
|
|
|
isFavorite: false |
|
|
|
}); |
|
|
|
|
|
|
|
// 触发全局事件,通知其他页面收藏状态已更改
|
|
|
|
const app = getApp(); |
|
|
|
app.eventBus.emit('favoriteChanged', { |
|
|
|
productId: productId, |
|
|
|
isFavorite: false |
|
|
|
}); |
|
|
|
|
|
|
|
// 显示成功提示
|
|
|
|
wx.showToast({ |
|
|
|
title: '取消收藏成功', |
|
|
|
icon: 'success', |
|
|
|
duration: 1500 |
|
|
|
}); |
|
|
|
}) |
|
|
|
.catch(err => { |
|
|
|
wx.hideLoading(); |
|
|
|
console.error('取消收藏失败:', err); |
|
|
|
|
|
|
|
// 显示错误提示
|
|
|
|
wx.showToast({ |
|
|
|
title: '取消收藏失败,请稍后重试', |
|
|
|
icon: 'none', |
|
|
|
duration: 2000 |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 处理收藏按钮点击事件
|
|
|
|
onFavoriteClick: function () { |
|
|
|
if (this.data.isFavorite) { |
|
|
|
this.cancelFavorite(); |
|
|
|
} else { |
|
|
|
this.addFavorite(); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
// 预览图片
|
|
|
|
previewImage(e) { |
|
|
|
@ -666,4 +871,4 @@ Page({ |
|
|
|
goBack() { |
|
|
|
wx.navigateBack(); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |