From 9368a88fe792e686baf293db2e33272f940af271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Wed, 17 Dec 2025 17:02:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4API=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E8=87=AA=E5=8A=A8=E7=99=BB=E5=BD=95=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E6=94=B9=E4=B8=BA=E7=94=A8=E6=88=B7=E4=B8=BB?= =?UTF-8?q?=E5=8A=A8=E9=80=89=E6=8B=A9=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 37 ++++++++++ app.json | 4 ++ custom-tab-bar/index.js | 1 + pages/buyer/index.js | 40 ++++++++++- pages/favorites/index.js | 35 +++++++++- pages/profile/index.js | 2 +- utils/api.js | 141 +++++++-------------------------------- 7 files changed, 139 insertions(+), 121 deletions(-) diff --git a/app.js b/app.js index 3f5d21a..1621da3 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,41 @@ App({ + // 全局事件总线功能 + eventBus: { + listeners: {}, + + // 监听事件 + on: function (event, callback) { + if (!this.listeners[event]) { + this.listeners[event] = []; + } + this.listeners[event].push(callback); + }, + + // 触发事件 + emit: function (event, data) { + if (this.listeners[event]) { + this.listeners[event].forEach(callback => { + try { + callback(data); + } catch (error) { + console.error('事件处理错误:', error); + } + }); + } + }, + + // 移除事件监听 + off: function (event, callback) { + if (this.listeners[event]) { + if (callback) { + this.listeners[event] = this.listeners[event].filter(cb => cb !== callback); + } else { + delete this.listeners[event]; + } + } + } + }, + onLaunch: function () { // 初始化应用 console.log('App Launch') diff --git a/app.json b/app.json index 2b79313..13a2ed2 100644 --- a/app.json +++ b/app.json @@ -73,6 +73,10 @@ "pagePath": "pages/seller/index", "text": "货源" }, + { + "pagePath": "pages/favorites/index", + "text": "收藏" + }, { "pagePath": "pages/profile/index", "text": "我的" diff --git a/custom-tab-bar/index.js b/custom-tab-bar/index.js index 9c683d1..0bd5a5a 100644 --- a/custom-tab-bar/index.js +++ b/custom-tab-bar/index.js @@ -94,6 +94,7 @@ Component({ 'pages/index/index', 'pages/buyer/index', 'pages/seller/index', + 'pages/favorites/index', 'pages/profile/index' ]; diff --git a/pages/buyer/index.js b/pages/buyer/index.js index 396292f..99297b5 100644 --- a/pages/buyer/index.js +++ b/pages/buyer/index.js @@ -123,6 +123,14 @@ Page({ this.fallbackToLocalStorageWithPagination(); }); }); + + // 添加收藏状态变化事件监听 + const app = getApp(); + this.favoriteChangedHandler = (data) => { + console.log('收到收藏状态变化通知:', data); + this.updateGoodsFavoriteStatus(data.productId, data.isFavorite); + }; + app.eventBus.on('favoriteChanged', this.favoriteChangedHandler); }, // 点击"我想要"按钮 @@ -1069,6 +1077,7 @@ Page({ String(id) === String(item.id) || String(id) === String(item.productId) ), + isFavorite: false, // 初始化收藏状态为false reservedCount: finalReservationCount, currentImageIndex: item.currentImageIndex || 0, createdAt: item.createdAt || item.created_at || item.createTime || null @@ -2013,6 +2022,13 @@ Page({ // 更新商品的收藏状态 this.updateGoodsFavoriteStatus(productId, true); + // 触发全局事件,通知其他页面收藏状态已更改 + const app = getApp(); + app.eventBus.emit('favoriteChanged', { + productId: productId, + isFavorite: true + }); + // 显示成功提示 wx.showToast({ title: '收藏成功', @@ -2066,6 +2082,13 @@ Page({ // 更新商品的收藏状态 this.updateGoodsFavoriteStatus(productId, false); + // 触发全局事件,通知其他页面收藏状态已更改 + const app = getApp(); + app.eventBus.emit('favoriteChanged', { + productId: productId, + isFavorite: false + }); + // 显示成功提示 wx.showToast({ title: '取消收藏成功', @@ -2143,9 +2166,13 @@ Page({ .then(res => { console.log('loadUserFavorites - 获取收藏列表成功:', res); - if (res.success && res.favorites) { + // 修复:根据API实际返回格式处理数据 + const favorites = res.data && res.data.favorites ? res.data.favorites : []; + console.log('loadUserFavorites - 处理后的收藏列表:', favorites); + + if (favorites.length > 0) { // 提取收藏的商品ID列表 - const favoriteProductIds = res.favorites.map(item => String(item.productId || item.id)); + const favoriteProductIds = favorites.map(item => String(item.productId || item.id)); console.log('loadUserFavorites - 收藏的商品ID列表:', favoriteProductIds); // 更新商品的收藏状态 @@ -2167,5 +2194,14 @@ Page({ console.error('loadUserFavorites - 获取收藏列表失败:', err); // 错误处理,不影响页面显示 }); + }, + + // 页面卸载时移除事件监听 + onUnload: function () { + const app = getApp(); + if (this.favoriteChangedHandler) { + app.eventBus.off('favoriteChanged', this.favoriteChangedHandler); + console.log('移除收藏状态变化事件监听'); + } } }); \ No newline at end of file diff --git a/pages/favorites/index.js b/pages/favorites/index.js index 62415d0..3a98797 100644 --- a/pages/favorites/index.js +++ b/pages/favorites/index.js @@ -33,6 +33,15 @@ Page({ */ onLoad(options) { this.loadFavorites(); + + // 添加收藏状态变化事件监听 + const app = getApp(); + this.favoriteChangedHandler = (data) => { + console.log('收藏页面收到收藏状态变化通知:', data); + // 重新加载收藏列表以更新状态 + this.loadFavorites(); + }; + app.eventBus.on('favoriteChanged', this.favoriteChangedHandler); }, /** @@ -48,6 +57,18 @@ Page({ onShow() { // 每次显示页面时重新加载收藏列表 this.loadFavorites(); + + // 更新自定义tabBar状态 + if (typeof this.getTabBar === 'function' && this.getTabBar()) { + this.getTabBar().setData({ + selected: 3 + }); + } + // 更新全局tab状态 + const app = getApp(); + if (app.updateCurrentTab) { + app.updateCurrentTab('favorites'); + } }, /** @@ -61,7 +82,12 @@ Page({ * 生命周期函数--监听页面卸载 */ onUnload() { - + // 移除事件监听 + const app = getApp(); + if (this.favoriteChangedHandler) { + app.eventBus.off('favoriteChanged', this.favoriteChangedHandler); + console.log('收藏页面移除收藏状态变化事件监听'); + } }, /** @@ -161,6 +187,13 @@ Page({ title: '取消收藏成功', icon: 'success' }); + + // 触发全局事件,通知其他页面收藏状态已更改 + const app = getApp(); + app.eventBus.emit('favoriteChanged', { + productId: productId, + isFavorite: false + }); }).catch(err => { console.error('取消收藏失败:', err); wx.showToast({ diff --git a/pages/profile/index.js b/pages/profile/index.js index 8a3cc1c..11162c0 100644 --- a/pages/profile/index.js +++ b/pages/profile/index.js @@ -16,7 +16,7 @@ Page({ // 更新自定义tabBar状态 if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ - selected: 3 + selected: 4 }); } // 更新全局tab状态 diff --git a/utils/api.js b/utils/api.js index 8356359..31ac565 100644 --- a/utils/api.js +++ b/utils/api.js @@ -2041,40 +2041,16 @@ module.exports = { } } - // 如果没有openid,需要先登录 + // 如果没有openid,直接返回未登录错误 if (!openid) { - return this.login().then(loginRes => { - // 重新尝试添加收藏 - return this.addFavorite(productId); - }).catch(loginErr => { - reject(new Error('用户未登录,无法添加收藏')); - }); + reject(new Error('用户未登录,无法添加收藏')); + return; } - // 如果没有手机号,需要先获取 + // 如果没有手机号,直接返回错误 if (!userPhone) { - // 尝试获取用户信息 - return this.getUserInfo(openid).then(userInfoRes => { - let phoneNumber = null; - if (userInfoRes.data && userInfoRes.data.phoneNumber) { - phoneNumber = userInfoRes.data.phoneNumber; - } - - if (!phoneNumber) { - reject(new Error('无法获取用户手机号,无法添加收藏')); - } else { - // 保存手机号到本地 - if (userId) { - users[userId] = users[userId] || {}; - users[userId].phoneNumber = phoneNumber; - wx.setStorageSync('users', users); - } - // 重新尝试添加收藏 - return this.addFavorite(productId); - } - }).catch(err => { - reject(new Error('无法获取用户信息,无法添加收藏')); - }); + reject(new Error('无法获取用户手机号,无法添加收藏')); + return; } // 构建收藏数据 @@ -2119,39 +2095,16 @@ module.exports = { } } + // 如果没有openid,直接返回未登录错误 if (!openid) { - return this.login().then(loginRes => { - // 重新尝试取消收藏 - return this.cancelFavorite(productId); - }).catch(loginErr => { - reject(new Error('用户未登录,无法取消收藏')); - }); + reject(new Error('用户未登录,无法取消收藏')); + return; } - // 如果没有手机号,需要先获取 + // 如果没有手机号,直接返回错误 if (!userPhone) { - // 尝试获取用户信息 - return this.getUserInfo(openid).then(userInfoRes => { - let phoneNumber = null; - if (userInfoRes.data && userInfoRes.data.phoneNumber) { - phoneNumber = userInfoRes.data.phoneNumber; - } - - if (!phoneNumber) { - reject(new Error('无法获取用户手机号,无法取消收藏')); - } else { - // 保存手机号到本地 - if (userId) { - users[userId] = users[userId] || {}; - users[userId].phoneNumber = phoneNumber; - wx.setStorageSync('users', users); - } - // 重新尝试取消收藏 - return this.cancelFavorite(productId); - } - }).catch(err => { - reject(new Error('无法获取用户信息,无法取消收藏')); - }); + reject(new Error('无法获取用户手机号,无法取消收藏')); + return; } const cancelData = { @@ -2193,39 +2146,16 @@ module.exports = { } } + // 如果没有openid,直接返回未登录错误 if (!openid) { - return this.login().then(loginRes => { - // 重新尝试获取收藏列表 - return this.getFavorites(); - }).catch(loginErr => { - reject(new Error('用户未登录,无法获取收藏列表')); - }); + reject(new Error('用户未登录,无法获取收藏列表')); + return; } - // 如果没有手机号,需要先获取 + // 如果没有手机号,直接返回错误 if (!userPhone) { - // 尝试获取用户信息 - return this.getUserInfo(openid).then(userInfoRes => { - let phoneNumber = null; - if (userInfoRes.data && userInfoRes.data.phoneNumber) { - phoneNumber = userInfoRes.data.phoneNumber; - } - - if (!phoneNumber) { - reject(new Error('无法获取用户手机号,无法获取收藏列表')); - } else { - // 保存手机号到本地 - if (userId) { - users[userId] = users[userId] || {}; - users[userId].phoneNumber = phoneNumber; - wx.setStorageSync('users', users); - } - // 重新尝试获取收藏列表 - return this.getFavorites(); - } - }).catch(err => { - reject(new Error('无法获取用户信息,无法获取收藏列表')); - }); + reject(new Error('无法获取用户手机号,无法获取收藏列表')); + return; } const requestData = { @@ -2892,39 +2822,16 @@ module.exports = { } } + // 如果没有openid,直接返回未登录错误 if (!openid) { - return this.login().then(loginRes => { - // 重新尝试取消收藏 - return this.cancelFavorite(productId); - }).catch(loginErr => { - reject(new Error('用户未登录,无法取消收藏')); - }); + reject(new Error('用户未登录,无法取消收藏')); + return; } - // 如果没有手机号,需要先获取 + // 如果没有手机号,直接返回错误 if (!userPhone) { - // 尝试获取用户信息 - return this.getUserInfo(openid).then(userInfoRes => { - let phoneNumber = null; - if (userInfoRes.data && userInfoRes.data.phoneNumber) { - phoneNumber = userInfoRes.data.phoneNumber; - } - - if (!phoneNumber) { - reject(new Error('无法获取用户手机号,无法取消收藏')); - } else { - // 保存手机号到本地 - if (userId) { - users[userId] = users[userId] || {}; - users[userId].phoneNumber = phoneNumber; - wx.setStorageSync('users', users); - } - // 重新尝试取消收藏 - return this.cancelFavorite(productId); - } - }).catch(err => { - reject(new Error('无法获取用户信息,无法取消收藏')); - }); + reject(new Error('无法获取用户手机号,无法取消收藏')); + return; } const requestData = {