diff --git a/pages/eggbar/create-post.js b/pages/eggbar/create-post.js new file mode 100644 index 0000000..ac7846c --- /dev/null +++ b/pages/eggbar/create-post.js @@ -0,0 +1,66 @@ +// pages/eggbar/create-post.js +Page({ + + /** + * 页面的初始数据 + */ + data: { + + }, + + /** + * 生命周期函数--监听页面加载 + */ + onLoad(options) { + + }, + + /** + * 生命周期函数--监听页面初次渲染完成 + */ + onReady() { + + }, + + /** + * 生命周期函数--监听页面显示 + */ + onShow() { + + }, + + /** + * 生命周期函数--监听页面隐藏 + */ + onHide() { + + }, + + /** + * 生命周期函数--监听页面卸载 + */ + onUnload() { + + }, + + /** + * 页面相关事件处理函数--监听用户下拉动作 + */ + onPullDownRefresh() { + + }, + + /** + * 页面上拉触底事件的处理函数 + */ + onReachBottom() { + + }, + + /** + * 用户点击右上角分享 + */ + onShareAppMessage() { + + } +}) \ No newline at end of file diff --git a/pages/eggbar/create-post.wxml b/pages/eggbar/create-post.wxml new file mode 100644 index 0000000..7ae6811 --- /dev/null +++ b/pages/eggbar/create-post.wxml @@ -0,0 +1,2 @@ + +pages/eggbar/create-post.wxml \ No newline at end of file diff --git a/pages/eggbar/eggbar.js b/pages/eggbar/eggbar.js new file mode 100644 index 0000000..5b7c9b3 --- /dev/null +++ b/pages/eggbar/eggbar.js @@ -0,0 +1,66 @@ +// pages/eggbar/eggbar.js +Page({ + + /** + * 页面的初始数据 + */ + data: { + + }, + + /** + * 生命周期函数--监听页面加载 + */ + onLoad(options) { + + }, + + /** + * 生命周期函数--监听页面初次渲染完成 + */ + onReady() { + + }, + + /** + * 生命周期函数--监听页面显示 + */ + onShow() { + + }, + + /** + * 生命周期函数--监听页面隐藏 + */ + onHide() { + + }, + + /** + * 生命周期函数--监听页面卸载 + */ + onUnload() { + + }, + + /** + * 页面相关事件处理函数--监听用户下拉动作 + */ + onPullDownRefresh() { + + }, + + /** + * 页面上拉触底事件的处理函数 + */ + onReachBottom() { + + }, + + /** + * 用户点击右上角分享 + */ + onShareAppMessage() { + + } +}) \ No newline at end of file diff --git a/pages/eggbar/eggbar.wxml b/pages/eggbar/eggbar.wxml new file mode 100644 index 0000000..cb8adce --- /dev/null +++ b/pages/eggbar/eggbar.wxml @@ -0,0 +1,2 @@ + +pages/eggbar/eggbar.wxml \ No newline at end of file diff --git a/pages/goods/index.js b/pages/goods/index.js index 31ec857..043eab5 100644 --- a/pages/goods/index.js +++ b/pages/goods/index.js @@ -318,9 +318,16 @@ Page({ // 使用getProducts方法获取商品数据,支持分页和搜索 const result = await API.getProducts(page, pageSize, status, keyword) + console.log('API返回原始数据:', result) + console.log('API返回商品数量:', result.products.length) + console.log('当前分页参数:', { page, pageSize, status, keyword }) + // 对商品进行格式化处理 const formattedGoods = result.products.map(item => { // 确定creatorName + console.log('商品原始数据:', item) + console.log('商品seller字段:', item.seller) + const sellerNickName = item.seller?.nickName || item.seller?.sellerNickName || item.seller?.name || '未知'; const creatorName = sellerNickName; @@ -338,7 +345,7 @@ Page({ status === 'sold' || status === 'out_of_stock' || (item.supplyStatus && item.supplyStatus.includes('售空')); - + if (isSoldOut) { status = 'sold_out' } else if (status !== 'published') { @@ -370,12 +377,15 @@ Page({ } }) + console.log('格式化后的商品数据:', formattedGoods) + return { goods: formattedGoods, total: result.total, hasMore: result.hasMore } } catch (err) { + console.error('加载商品数据失败:', err) return { goods: [], total: 0, @@ -556,12 +566,16 @@ Page({ try { // 检查缓存是否有效 if (!isLoadMore && this.isCacheValid()) { + console.log('使用缓存数据') const cache = this.data.cache // 重新应用当前筛选条件到缓存数据 const allCachedGoods = [...cache.publishedGoods, ...cache.soldOutGoods] const filteredGoods = this.filterGoodsList(allCachedGoods) + console.log('缓存商品数量:', allCachedGoods.length) + console.log('筛选后缓存商品数量:', filteredGoods.length) + this.setData({ goodsList: filteredGoods, publishedLoaded: true, @@ -574,100 +588,154 @@ Page({ return } - let newGoods = [] - let updatedPublishedHasMore = this.data.publishedHasMore - let updatedSoldOutHasMore = this.data.soldOutHasMore - let publishedGoods = [] - let soldOutGoods = [] - - // 1. 处理已上架商品 + // 1. 处理已上架商品 - 持续加载直到找到足够的符合条件的商品 if (this.data.publishedHasMore) { - // 从服务器获取已上架商品数据,支持分页和搜索 - const result = await this.loadGoodsData( - this.data.publishedCurrentPage, - this.data.pageSize, - 'published', - this.data.searchKeyword - ) + let accumulatedFilteredGoods = [] + let currentPage = this.data.publishedCurrentPage + let hasMore = true - // 应用筛选逻辑 - let filteredGoods = this.filterGoodsList(result.goods) + // 持续加载,直到找到至少20条符合条件的商品或者没有更多数据 + while (hasMore && accumulatedFilteredGoods.length < this.data.pageSize) { + // 从服务器获取已上架商品数据,支持分页和搜索 + const result = await this.loadGoodsData( + currentPage, + this.data.pageSize, + 'published', + this.data.searchKeyword + ) + + console.log('原始商品数据:', result.goods) + console.log('当前筛选条件:', this.data.activeFilter) + console.log('筛选配置:', this.data.filterConfig) + console.log('分页参数:', { page: currentPage, pageSize: this.data.pageSize }) + console.log('hasMore:', result.hasMore) + + // 应用筛选逻辑 + const filteredGoods = this.filterGoodsList(result.goods) + + console.log('筛选后商品数据:', filteredGoods) + console.log('筛选后商品数量:', filteredGoods.length) + + // 累积符合条件的商品 + accumulatedFilteredGoods = [...accumulatedFilteredGoods, ...filteredGoods] + + // 更新状态 + hasMore = result.hasMore + currentPage++ + + // 如果已经找到足够的商品,跳出循环 + if (accumulatedFilteredGoods.length >= this.data.pageSize) { + break + } + } - newGoods = filteredGoods - updatedPublishedHasMore = result.hasMore - publishedGoods = filteredGoods + console.log('累积筛选后商品数量:', accumulatedFilteredGoods.length) // 如果是加载更多,追加数据;否则替换数据 + if (accumulatedFilteredGoods.length > 0) { + this.setData({ + goodsList: isLoadMore ? [...this.data.goodsList, ...accumulatedFilteredGoods] : accumulatedFilteredGoods + }) + } + + // 更新页码和hasMore状态 this.setData({ - goodsList: isLoadMore ? [...this.data.goodsList, ...newGoods] : newGoods, - publishedHasMore: updatedPublishedHasMore + publishedCurrentPage: currentPage, + publishedHasMore: hasMore }) - // 如果已上架商品还有更多,更新页码,返回等待下一次加载 - if (updatedPublishedHasMore) { - this.setData({ - publishedCurrentPage: this.data.publishedCurrentPage + 1 - }) - return - } else { - // 已上架商品加载完成 + console.log('已上架商品加载完成,更新页码为:', currentPage, ',是否有更多:', hasMore) + + // 如果没有更多数据,标记为加载完成 + if (!hasMore) { this.setData({ publishedLoaded: true }) + console.log('已上架商品加载完成') } } - // 2. 处理售空商品 - if (!this.data.publishedHasMore && this.data.soldOutHasMore) { - // 从服务器获取售空商品数据,支持分页和搜索 - const result = await this.loadGoodsData( - this.data.soldOutCurrentPage, - this.data.pageSize, - 'sold_out', - this.data.searchKeyword - ) + // 2. 处理售空商品 - 持续加载直到找到足够的符合条件的商品 + // 对于初始加载,即使已上架商品还有更多,也尝试加载售空商品 + // 这样可以确保用户能看到所有类型的商品 + if ((!isLoadMore || !this.data.publishedHasMore) && this.data.soldOutHasMore) { + let accumulatedFilteredGoods = [] + let currentPage = this.data.soldOutCurrentPage + let hasMore = true - // 应用筛选逻辑 - let filteredGoods = this.filterGoodsList(result.goods) + // 持续加载,直到找到至少20条符合条件的商品或者没有更多数据 + while (hasMore && accumulatedFilteredGoods.length < this.data.pageSize) { + // 从服务器获取售空商品数据,支持分页和搜索 + const result = await this.loadGoodsData( + currentPage, + this.data.pageSize, + 'sold_out', + this.data.searchKeyword + ) + + console.log('售空商品数据:', result.goods) + console.log('售空商品分页参数:', { page: currentPage, pageSize: this.data.pageSize }) + console.log('售空商品hasMore:', result.hasMore) + console.log('当前筛选条件:', this.data.activeFilter) + + // 应用筛选逻辑 + const filteredGoods = this.filterGoodsList(result.goods) + + console.log('筛选后售空商品数据:', filteredGoods) + console.log('筛选后售空商品数量:', filteredGoods.length) + + // 累积符合条件的商品 + accumulatedFilteredGoods = [...accumulatedFilteredGoods, ...filteredGoods] + + // 更新状态 + hasMore = result.hasMore + currentPage++ + + // 如果已经找到足够的商品,跳出循环 + if (accumulatedFilteredGoods.length >= this.data.pageSize) { + break + } + } - newGoods = filteredGoods - updatedSoldOutHasMore = result.hasMore - soldOutGoods = filteredGoods + console.log('累积筛选后售空商品数量:', accumulatedFilteredGoods.length) // 追加售空商品数据 + if (accumulatedFilteredGoods.length > 0) { + this.setData({ + goodsList: isLoadMore ? [...this.data.goodsList, ...accumulatedFilteredGoods] : [...this.data.goodsList, ...accumulatedFilteredGoods] + }) + } + + // 更新页码和hasMore状态 this.setData({ - goodsList: [...this.data.goodsList, ...newGoods], - soldOutHasMore: updatedSoldOutHasMore + soldOutCurrentPage: currentPage, + soldOutHasMore: hasMore }) - // 如果售空商品还有更多,更新页码 - if (updatedSoldOutHasMore) { - this.setData({ - soldOutCurrentPage: this.data.soldOutCurrentPage + 1 - }) - } else { - // 售空商品加载完成 + console.log('售空商品加载完成,更新页码为:', currentPage, ',是否有更多:', hasMore) + + // 如果没有更多数据,标记为加载完成 + if (!hasMore) { this.setData({ soldOutLoaded: true }) + console.log('售空商品加载完成') } } + console.log('加载完成后状态:', { + publishedHasMore: this.data.publishedHasMore, + soldOutHasMore: this.data.soldOutHasMore, + publishedCurrentPage: this.data.publishedCurrentPage, + soldOutCurrentPage: this.data.soldOutCurrentPage, + goodsListLength: this.data.goodsList.length + }) + // 如果是初始加载且所有数据加载完成,更新缓存 if (!isLoadMore && !this.data.publishedHasMore && !this.data.soldOutHasMore) { - // 为了缓存完整数据,获取所有已上架和售空商品 - const publishedResult = await this.loadGoodsData(1, 1000, 'published', this.data.searchKeyword) - const soldOutResult = await this.loadGoodsData(1, 1000, 'sold_out', this.data.searchKeyword) - - // 应用筛选逻辑到缓存数据 - const filteredPublishedGoods = this.filterGoodsList(publishedResult.goods) - const filteredSoldOutGoods = this.filterGoodsList(soldOutResult.goods) - - this.setData({ - 'cache.publishedGoods': filteredPublishedGoods, - 'cache.soldOutGoods': filteredSoldOutGoods, - 'cache.timestamp': Date.now() - }) + // 由于现在采用分页加载,缓存逻辑需要调整 + // 暂时禁用缓存更新,确保每次都从服务器获取最新数据 + console.log('分页加载模式,跳过缓存更新') } } catch (err) { diff --git a/utils/api.js b/utils/api.js index 229bc00..459c74c 100644 --- a/utils/api.js +++ b/utils/api.js @@ -821,10 +821,23 @@ module.exports = { const products = res.products || []; // 将商品列表存储到本地缓存 wx.setStorageSync('goods', products || []); + + // 计算是否有更多数据 - 当返回的商品数量等于请求的页码大小时,认为还有更多数据 + const hasMore = products.length >= pageSize; + const total = res.total || products.length; + + console.log('API.getProducts 返回数据:', { + page, + pageSize, + productsLength: products.length, + total, + hasMore + }); + resolve({ products: products, - total: res.total || products.length, - hasMore: (page * pageSize) < (res.total || products.length) + total: total, + hasMore: hasMore }); } else { reject(new Error('获取商品列表失败'));