|
|
|
@ -245,6 +245,37 @@ Page({ |
|
|
|
} |
|
|
|
}); |
|
|
|
}, 500); |
|
|
|
|
|
|
|
// 添加收藏状态变化事件监听
|
|
|
|
const app = getApp(); |
|
|
|
this.favoriteChangedHandler = (data) => { |
|
|
|
console.log('收到收藏状态变化通知:', data); |
|
|
|
|
|
|
|
// 更新商品列表中对应商品的收藏状态
|
|
|
|
const updatedGoods = this.data.goods.map(item => { |
|
|
|
if (String(item.id || item.productId) === data.productId) { |
|
|
|
return { |
|
|
|
...item, |
|
|
|
isFavorite: data.isFavorite |
|
|
|
}; |
|
|
|
} |
|
|
|
return item; |
|
|
|
}); |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
goods: updatedGoods |
|
|
|
}, () => { |
|
|
|
// 重新应用筛选条件,确保显示的商品收藏状态也更新
|
|
|
|
const filteredGoods = this.applyFilters(this.data.goods); |
|
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods); |
|
|
|
this.setData({ |
|
|
|
filteredGoods: filteredGoods, |
|
|
|
leftColumnGoods: leftColumnGoods, |
|
|
|
rightColumnGoods: rightColumnGoods |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
app.eventBus.on('favoriteChanged', this.favoriteChangedHandler); |
|
|
|
}, |
|
|
|
|
|
|
|
onShow: function () { |
|
|
|
@ -271,6 +302,15 @@ Page({ |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
onUnload: function () { |
|
|
|
// 页面卸载时移除收藏状态变化事件监听
|
|
|
|
const app = getApp(); |
|
|
|
if (this.favoriteChangedHandler) { |
|
|
|
app.eventBus.off('favoriteChanged', this.favoriteChangedHandler); |
|
|
|
console.log('移除收藏状态变化事件监听'); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
onPullDownRefresh: function() { |
|
|
|
this.onRefresh() |
|
|
|
}, |
|
|
|
@ -404,6 +444,9 @@ Page({ |
|
|
|
|
|
|
|
newGoods = newGoods.filter(item => (item.status || '').toLowerCase() !== 'hidden') |
|
|
|
|
|
|
|
// 更新商品的收藏状态
|
|
|
|
this.updateGoodsFavoriteStatus(newGoods, isLoadMore) |
|
|
|
|
|
|
|
// 只在第一页或刷新时在商品列表最前面插入广告
|
|
|
|
if ((!isLoadMore || this.data.page === 1) && newGoods.length > 0) { |
|
|
|
// 确保广告位在最前面
|
|
|
|
@ -580,13 +623,7 @@ Page({ |
|
|
|
|
|
|
|
if (this.data.selectedCategory !== '全部') { |
|
|
|
const category = this.data.selectedCategory |
|
|
|
let keyword = category |
|
|
|
if (category === '粉壳') keyword = '粉' |
|
|
|
else if (category === '绿壳') keyword = '绿' |
|
|
|
else if (category === '红壳') keyword = '红' |
|
|
|
else if (category === '白壳') keyword = '白' |
|
|
|
|
|
|
|
filtered = filtered.filter(item => item.isAd || (item.name || '').includes(keyword)) |
|
|
|
filtered = filtered.filter(item => item.isAd || (item.category === category)) |
|
|
|
} |
|
|
|
|
|
|
|
if (this.data.searchKeyword) { |
|
|
|
@ -954,6 +991,91 @@ Page({ |
|
|
|
app.globalData.showTabBar = true; |
|
|
|
}, |
|
|
|
|
|
|
|
// 更新商品的收藏状态
|
|
|
|
updateGoodsFavoriteStatus: function(goods, isLoadMore) { |
|
|
|
// 获取用户手机号
|
|
|
|
let userPhone = ''; |
|
|
|
try { |
|
|
|
const users = wx.getStorageSync('users') || {}; |
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
|
|
|
|
// 尝试从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 { |
|
|
|
// 尝试从直接存储的phoneNumber获取
|
|
|
|
userPhone = wx.getStorageSync('phoneNumber'); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
console.error('获取用户手机号失败:', e); |
|
|
|
} |
|
|
|
|
|
|
|
if (!userPhone) { |
|
|
|
// 用户未登录,无法获取收藏状态
|
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 调用API获取用户收藏列表
|
|
|
|
API.getFavorites(userPhone) |
|
|
|
.then(res => { |
|
|
|
if (res && res.code === 200) { |
|
|
|
// 检查API返回的数据结构,确保我们获取到正确的收藏列表
|
|
|
|
let favoritesList = []; |
|
|
|
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; |
|
|
|
} |
|
|
|
|
|
|
|
// 从收藏列表中提取商品ID
|
|
|
|
const favoriteProductIds = favoritesList.map(item => { |
|
|
|
// 尝试从不同的字段名获取商品ID
|
|
|
|
return String(item.productId || item.id || item.product_id || ''); |
|
|
|
}).filter(id => id !== ''); // 过滤掉空字符串
|
|
|
|
|
|
|
|
// 更新商品的isFavorite状态
|
|
|
|
const updatedGoods = goods.map(item => ({ |
|
|
|
...item, |
|
|
|
isFavorite: favoriteProductIds.includes(String(item.id || item.productId)) |
|
|
|
})); |
|
|
|
|
|
|
|
// 更新商品列表
|
|
|
|
let updatedGoodsList = this.data.goods; |
|
|
|
if (!isLoadMore || this.data.page === 1) { |
|
|
|
// 第一页或刷新时,直接替换全部商品
|
|
|
|
updatedGoodsList = updatedGoods; |
|
|
|
} else { |
|
|
|
// 加载更多时,追加到现有列表
|
|
|
|
updatedGoodsList = [...this.data.goods, ...updatedGoods]; |
|
|
|
} |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
goods: updatedGoodsList |
|
|
|
}, () => { |
|
|
|
// 重新应用筛选条件,确保显示的商品收藏状态也更新
|
|
|
|
const filteredGoods = this.applyFilters(this.data.goods); |
|
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods); |
|
|
|
this.setData({ |
|
|
|
filteredGoods: filteredGoods, |
|
|
|
leftColumnGoods: leftColumnGoods, |
|
|
|
rightColumnGoods: rightColumnGoods |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch(err => { |
|
|
|
console.error('获取收藏状态失败:', err); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 回到顶部
|
|
|
|
scrollToTop: function() { |
|
|
|
this.setData({ |
|
|
|
|