From 8e68a5d88c8782e33250c077ec7471ed9a34e1de Mon Sep 17 00:00:00 2001 From: Default User Date: Tue, 23 Dec 2025 17:12:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=95=86=E5=93=81=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E9=A1=B5=E9=9D=A2=EF=BC=9A=E6=B7=BB=E5=8A=A0=E5=8F=AF?= =?UTF-8?q?=E8=AE=AE=E4=BB=B7=E6=A0=87=E7=AD=BE=E5=B9=B6=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E7=88=B1=E5=BF=83=E6=8C=89=E9=92=AE=E5=B8=83=E5=B1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/goods-detail/goods-detail.js | 209 ++++++++++++++++++++++++++- pages/goods-detail/goods-detail.wxml | 24 ++- 2 files changed, 225 insertions(+), 8 deletions(-) diff --git a/pages/goods-detail/goods-detail.js b/pages/goods-detail/goods-detail.js index e90019f..be36cab 100644 --- a/pages/goods-detail/goods-detail.js +++ b/pages/goods-detail/goods-detail.js @@ -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(); } -}); +}); \ No newline at end of file diff --git a/pages/goods-detail/goods-detail.wxml b/pages/goods-detail/goods-detail.wxml index eddd496..16efdbe 100644 --- a/pages/goods-detail/goods-detail.wxml +++ b/pages/goods-detail/goods-detail.wxml @@ -32,18 +32,30 @@ - + {{goodsDetail.supplyStatus || '暂无状态'}} {{goodsDetail.name}} V - - {{goodsDetail.sourceType || '暂无'}} + + + {{goodsDetail.sourceType || '暂无'}} + - - 价格: - {{goodsDetail.price}} + + + 价格: + {{goodsDetail.price}} + (可议价) + + + {{isFavorite ? '❤️' : '🤍'}} +