From abd03df40d546554e64728c7dfc2194c54c2595e Mon Sep 17 00:00:00 2001 From: Default User Date: Sat, 27 Dec 2025 16:05:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=BB=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E8=B4=A7=E6=BA=90=E6=94=B6=E8=97=8F=E7=8A=B6=E6=80=81=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E4=B8=8D=E4=B8=80=E8=87=B4=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/index.js | 136 +++++++++++++++++++++++++++++++-- server-example/server-mysql.js | 15 +++- utils/api.js | 11 +++ 3 files changed, 153 insertions(+), 9 deletions(-) diff --git a/pages/index/index.js b/pages/index/index.js index 954f1c7..25271ef 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -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 () { @@ -270,6 +301,15 @@ Page({ sidebarBtnHidden: true }); }, + + 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) { @@ -946,6 +983,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({ diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index a91d31d..26d4589 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -834,6 +834,11 @@ Product.init({ type: DataTypes.STRING(50), allowNull: true, comment: '供应状态' + }, + category: { + type: DataTypes.STRING(50), + allowNull: true, + comment: '商品种类' } }, { sequelize, @@ -1777,7 +1782,7 @@ app.post('/api/user/update', async (req, res) => { // 获取商品列表 - 优化版本确保状态筛选正确应用 app.post('/api/product/list', async (req, res) => { try { - const { openid, status, keyword, page = 1, pageSize = 20, testMode = false, viewMode = 'shopping' } = req.body; + const { openid, status, keyword, category, page = 1, pageSize = 20, testMode = false, viewMode = 'shopping' } = req.body; // 查找用户 - 如果提供了openid,则查找用户信息,否则允许匿名访问 let user = null; @@ -1869,6 +1874,11 @@ app.post('/api/product/list', async (req, res) => { if (keyword) { where.productName = { [Sequelize.Op.like]: `%${keyword}%` }; } + + // 分类筛选 + if (category) { + where.category = { [Sequelize.Op.eq]: category }; + } // 计算偏移量 const offset = (page - 1) * pageSize; @@ -1887,7 +1897,8 @@ app.post('/api/product/list', async (req, res) => { include: [ 'region', // 【新增】确保返回地区字段 'sourceType', // 【新增】确保返回货源类型字段 - 'supplyStatus' // 【新增】确保返回供应状态字段 + 'supplyStatus', // 【新增】确保返回供应状态字段 + 'category' // 【新增】确保返回商品种类字段 ] }, order: [['created_at', 'DESC']], diff --git a/utils/api.js b/utils/api.js index 57d2254..c2c7779 100644 --- a/utils/api.js +++ b/utils/api.js @@ -887,6 +887,7 @@ module.exports = { yolk: product.yolk || '', specification: product.specification || '', region: product.region || '', // 【新增】添加地区字段 + category: product.category || '', // 【新增】添加种类字段 // imageUrls字段会被忽略,因为图片会通过wx.uploadFile上传 } @@ -1410,6 +1411,16 @@ module.exports = { requestData.viewMode = options.viewMode; } + // 如果options中包含category,则添加到请求数据中 + if (options.category) { + requestData.category = options.category; + } + + // 如果options中包含keyword,则添加到请求数据中 + if (options.keyword) { + requestData.keyword = options.keyword; + } + console.log('API.getProductList - 分页参数:', { page: page, pageSize: pageSize }); console.log('API.getProductList - 请求数据:', requestData);