diff --git a/pages/index/index.js b/pages/index/index.js index dd8c696..c56025d 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -33,6 +33,7 @@ Page({ selectedCategory: '全部', loadingMore: false, hasMoreData: true, + hasMore: true, // 用于显示"已加载全部商品"提示 page: 1, pageSize: 10, @@ -40,6 +41,10 @@ Page({ previewImageUrls: [], // 预览的图片URL列表 previewImageIndex: 0, // 当前预览图片的索引 showImagePreview: false, // 控制图片预览弹窗显示 + + // 导航栏显示控制 + showNavBar: true, // 控制导航栏显示/隐藏 + lastScrollTop: 0, // 上一次滚动位置 }, // 跳转到聊天页面 @@ -333,6 +338,7 @@ Page({ loadingMore: false, page: currentPage + 1, hasMoreData: hasMoreData, + hasMore: hasMoreData, totalGoods: totalGoods, totalPages: totalPages }) @@ -527,6 +533,42 @@ Page({ url: `/pages/goods-detail/goods-detail?goodsData=${encodeURIComponent(JSON.stringify(item))}&productId=${productId}` }) }, + + // 查看商品详情(与buyer页面保持一致的方法名) + showGoodsDetail: function (e) { + // 检查用户是否登录 + const openid = wx.getStorageSync('openid'); + const userId = wx.getStorageSync('userId'); + + if (!openid || !userId) { + console.log('用户未登录,直接显示授权登录弹窗'); + // 直接显示授权登录弹窗 + this.showOneKeyLogin(); + return; + } + + const goodsItem = e.currentTarget.dataset.item; + // 跳转到商品详情页面,并传递商品数据,使用encodeURIComponent编码JSON字符串 + wx.navigateTo({ + url: '/pages/goods-detail/goods-detail?goodsData=' + encodeURIComponent(JSON.stringify(goodsItem)) + }); + + // 同时尝试直接更新tabBar的选中状态 + if (typeof this.getTabBar === 'function' && this.getTabBar()) { + const tabBar = this.getTabBar(); + if (tabBar.setData) { + tabBar.setData({ show: false }); + } + } + + // 调用后端API执行商品联系人更新 + console.log('开始调用API.updateProductContacts()'); + API.updateProductContacts().then(function(res) { + console.log('商品联系人更新成功:', res); + }).catch(function(err) { + console.error('商品联系人更新失败:', err); + }); + }, // 跳转到入驻页面 navigateToSettlement: function() { @@ -587,33 +629,194 @@ Page({ onScroll: function(e) { // 获取滚动信息 const { scrollTop, scrollHeight, clientHeight } = e.detail; - const distanceToBottom = scrollHeight - scrollTop - clientHeight; - // 获取全局状态 - const app = getApp(); - if (!app || !app.globalData) { - return; - } + // 导航栏显示/隐藏逻辑 + const { lastScrollTop } = this.data; - // 当滚动到底部且没有更多数据时,隐藏tabBar - if (distanceToBottom < 100 && !this.data.hasMoreData) { - app.globalData.showTabBar = false; + // 下滑检测:如果当前滚动位置大于上一次滚动位置,且不在顶部(scrollTop > 10),则隐藏导航栏 + if (scrollTop > lastScrollTop && scrollTop > 10) { + this.setData({ + showNavBar: false + }); } - // 当往上滚动不在底部时,立即重新显示tabBar - else { - app.globalData.showTabBar = true; + // 上滑检测:如果当前滚动位置小于上一次滚动位置,或者回到顶部,则显示导航栏 + else if (scrollTop < lastScrollTop || scrollTop <= 10) { + this.setData({ + showNavBar: true + }); } + + // 更新上一次滚动位置 + this.setData({ + lastScrollTop: scrollTop + }); }, // 上拉加载更多 onReachBottom: function() { if (this.data.hasMoreData && !this.data.loadingMore) { this.loadGoods(true) - } else if (!this.data.hasMoreData) { - // 没有更多数据时,隐藏tabBar - const app = getApp(); - if (app && app.globalData) { - app.globalData.showTabBar = false; + } + }, + + // 切换图片 + swiperChange(e) { + const current = e.detail.current + const itemId = e.currentTarget.dataset.itemId + + // 更新对应商品项的currentImageIndex + this.setData({ + [`filteredGoods[${itemId}].currentImageIndex`]: current + }) + }, + + // 切换收藏状态 + toggleFavorite(e) { + const goodsItem = e.currentTarget.dataset.item; + console.log('用户点击了收藏按钮,商品信息:', goodsItem); + + // 检查用户登录状态 + const openid = wx.getStorageSync('openid'); + const userId = wx.getStorageSync('userId'); + + if (!openid || !userId) { + console.log('用户未登录,显示一键登录弹窗'); + this.setData({ + showOneKeyLoginModal: true + }); + return; + } + + // 获取商品ID + const productId = String(goodsItem.productId || goodsItem.id); + console.log('准备操作的商品ID:', productId); + + // 根据当前收藏状态决定是添加还是取消收藏 + if (goodsItem.isFavorite) { + this.cancelFavorite(e); + } else { + this.addFavorite(e); + } + }, + + // 添加收藏 + addFavorite: function (e) { + const goodsItem = e.currentTarget.dataset.item; + console.log('用户点击了收藏按钮,商品信息:', goodsItem); + + // 获取商品ID + const productId = String(goodsItem.productId || goodsItem.id); + console.log('准备收藏的商品ID:', productId); + + wx.showLoading({ title: '正在收藏...' }); + + // 调用API添加收藏 + API.addFavorite(productId) + .then(res => { + wx.hideLoading(); + console.log('添加收藏成功:', res); + + // 更新商品的收藏状态 + this.updateGoodsFavoriteStatus(productId, true); + + // 触发全局事件,通知其他页面收藏状态已更改 + const app = getApp(); + if (app.eventBus) { + 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 (e) { + const goodsItem = e.currentTarget.dataset.item; + console.log('用户点击了取消收藏按钮,商品信息:', goodsItem); + + // 获取商品ID + const productId = String(goodsItem.productId || goodsItem.id); + console.log('准备取消收藏的商品ID:', productId); + + wx.showLoading({ title: '正在取消收藏...' }); + + // 调用API取消收藏 + API.cancelFavorite(productId) + .then(res => { + wx.hideLoading(); + console.log('取消收藏成功:', res); + + // 更新商品的收藏状态 + this.updateGoodsFavoriteStatus(productId, false); + + // 触发全局事件,通知其他页面收藏状态已更改 + const app = getApp(); + if (app.eventBus) { + 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 + }); + }); + }, + + // 更新商品收藏状态 + updateGoodsFavoriteStatus: function (productId, isFavorite) { + // 找到商品在列表中的索引 + const goodsIndex = this.data.filteredGoods.findIndex(item => + String(item.id) === productId || String(item.productId) === productId + ); + + if (goodsIndex !== -1) { + // 更新商品的收藏状态 + const updateData = {}; + updateData[`filteredGoods[${goodsIndex}].isFavorite`] = isFavorite; + this.setData(updateData); + + // 同时更新原始goods数组 + const originalIndex = this.data.goods.findIndex(item => + String(item.id) === productId || String(item.productId) === productId + ); + if (originalIndex !== -1) { + updateData[`goods[${originalIndex}].isFavorite`] = isFavorite; + this.setData(updateData); } } }, diff --git a/pages/index/index.wxml b/pages/index/index.wxml index 344cb08..3d44801 100644 --- a/pages/index/index.wxml +++ b/pages/index/index.wxml @@ -1,27 +1,71 @@ - - 专业的鸡蛋现货交易平台 - - - - - - - - {{selectedRegion || '全国'}} - + + + + 专业的鸡蛋现货交易平台 + + + + + + + + {{selectedRegion || '全国'}} + + + + 🔍 + + + + + + + + + + + 全部 + + + 粉壳 + + + 红壳 + + + 绿壳 + + + 白壳 - - 🔍 - - @@ -44,47 +88,6 @@ - - - - - 全部 - - - 粉壳 - - - 红壳 - - - 绿壳 - - - 白壳 - - - - - - - - {{item.supplyStatus || '现货'}} - - - - {{item.name}} - {{item.specification || '无'}} | {{item.yolk || '无'}} - ¥{{item.price}} - - {{item.region}} - 已有{{item.reservedCount || 0}}人收藏 + + + + + + + + + + 暂无图片 + + + + + + + + + + + + + {{(item.currentImageIndex || 0) + 1}}/{{item.imageUrls.length}} + + + + + + + + + + + + {{item.supplyStatus || '暂无状态'}} + {{item.name}} + V + + + + + {{item.specification || '无'}} | {{item.yolk || '无'}} | {{item.minOrder || item.quantity || 1}}件 + + + + + + + + + + + 已有 + {{item.reservedCount || 0}} + 人收藏 + + + + + @@ -168,6 +229,11 @@ 暂无商品数据 + + + + {{hasMore ? '下拉加载更多' : '已加载全部商品'}} + diff --git a/pages/index/index.wxss b/pages/index/index.wxss index f29dfc2..890399d 100644 --- a/pages/index/index.wxss +++ b/pages/index/index.wxss @@ -13,7 +13,7 @@ page { margin: 0; width: 100%; height: 100vh; - display: flex;z + display: flex; flex-direction: column; box-sizing: border-box; background-color: #f5f7fa; @@ -369,16 +369,14 @@ page { font-weight: bold; } -/* 商品区域样式 - 调整为占据70%空间 */ +/* 商品区域样式 - 填充完整宽度 */ .goods-section { - background-color: white; - padding: 20rpx; - border-radius: 10rpx; - margin: 0 20rpx 20rpx 20rpx; - box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05); - flex: 7; + background-color: transparent; + padding: 0 20rpx; + margin: 0; + flex: 1; overflow-y: auto; - width: calc(100% - 40rpx); + width: 100%; box-sizing: border-box; } @@ -386,7 +384,7 @@ page { font-size: 28rpx; font-weight: bold; color: #333; - margin-bottom: 20rpx; + margin: 0 0 20rpx 0; } .goods-list { @@ -396,104 +394,50 @@ page { .goods-list-container { display: flex; - flex-wrap: wrap; - gap: 20rpx; - justify-content: flex-start; + flex-direction: column; + gap: 0; padding-bottom: 20rpx; } -.goods-item { - width: calc((100% - 20rpx) / 2); - background-color: #f9f9f9; - border-radius: 10rpx; +/* 商品卡片样式,与buyer页面保持一致,但减小间距 */ +.card { + background: white; + border-radius: 12rpx; + box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1); overflow: hidden; - box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05); - transition: all 0.3s ease; -} - -.goods-item:hover { - box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1); - transform: translateY(-2rpx); -} - -.goods-image-container { - position: relative; + margin-bottom: 10rpx; width: 100%; - padding-bottom: 100%; - overflow: hidden; + box-sizing: border-box; } -.goods-image { - position: absolute; - top: 0; - left: 0; +.image-swiper { width: 100%; height: 100%; - background-color: #eee; -} - -.goods-tag { - position: absolute; - top: 10rpx; - left: 10rpx; - background-color: rgba(255, 90, 90, 0.8); - color: white; - font-size: 20rpx; - padding: 5rpx 10rpx; - border-radius: 15rpx; - font-weight: bold; -} - -.goods-info { - padding: 15rpx; } -.goods-name { - font-size: 26rpx; - font-weight: bold; - color: #333; - margin-bottom: 10rpx; - overflow: hidden; - text-overflow: ellipsis; - display: -webkit-box; - -webkit-line-clamp: 2; - -webkit-box-orient: vertical; - min-height: 60rpx; -} - -.goods-spec { - font-size: 22rpx; - color: #999; - margin-bottom: 10rpx; - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; +/* 进一步减小收藏按钮大小并调整位置 */ +.card button { + width: 120rpx !important; + height: 48rpx !important; + font-size: 20rpx !important; + margin-left: 10rpx !important; } -.goods-price { - font-size: 32rpx; - font-weight: bold; - color: #ff4d4f; - margin-bottom: 10rpx; +/* 加深所有货源的规格信息行字体颜色 */ +.card view:nth-child(2) view:nth-child(2) view:nth-child(1) view:nth-child(2) { + color: #555 !important; + font-weight: 600 !important; } -.goods-footer { +/* 半页空白页样式,与buyer页面保持一致 */ +.half-page-blank { + height: 32vh; + width: 100%; + box-sizing: border-box; display: flex; - justify-content: space-between; - font-size: 20rpx; - color: #999; -} - -.goods-region { - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; -} - -.goods-reserved { - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; + justify-content: center; + align-items: flex-start; + padding-top: 20rpx; } /* 空商品样式 */