diff --git a/pages/index/index.js b/pages/index/index.js index 01faf87..d9e4024 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -100,6 +100,12 @@ Page({ lastDataTimestamp: 0, cacheValidDuration: 5 * 60 * 1000, // 缓存有效期5分钟 + // sold_out 加载状态 + isQueryingSoldOut: false, + soldOutPage: 1, + soldOutPageSize: 8, + publishedHasMore: true, + // 防抖定时器 searchDebounceTimer: null, scrollDebounceTimer: null, @@ -403,6 +409,9 @@ Page({ }) const timestamp = new Date().getTime(); + const currentCategory = this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory; + const currentKeyword = this.data.searchKeyword; + const pageSize = this.data.pageSize; // 强制刷新:清除所有缓存并重新从数据库加载 this.setData({ @@ -412,13 +421,14 @@ Page({ lastDataTimestamp: 0 }); - API.getProductList(['published', 'sold_out'], { + // 优先查询published状态的商品 + API.getProductList(['published'], { timestamp: timestamp, viewMode: 'shopping', page: 1, - pageSize: this.data.pageSize, - keyword: this.data.searchKeyword, - category: this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory + pageSize: pageSize, + keyword: currentKeyword, + category: currentCategory }) .then(res => { this.setData({ isRefreshing: false }) @@ -430,6 +440,43 @@ Page({ icon: 'success', duration: 1500 }) + } else if (res.products.length === 0) { + // 如果published状态没有商品,则查询sold_out状态 + console.log('没有published状态的商品,查询sold_out状态的商品'); + API.getProductList(['sold_out'], { + timestamp: timestamp, + viewMode: 'shopping', + page: 1, + pageSize: pageSize, + keyword: currentKeyword, + category: currentCategory + }) + .then(soldOutRes => { + this.setData({ isRefreshing: false }); + + if (soldOutRes.success && soldOutRes.products && soldOutRes.products.length > 0) { + this.processRefreshData(soldOutRes.products, existingGoods); + wx.showToast({ + title: '刷新成功', + icon: 'success', + duration: 1500 + }); + } else { + this.setData({ + isLoading: false, + loadingMore: false + }); + } + }) + .catch(err => { + this.setData({ isRefreshing: false }); + console.error('刷新sold_out商品数据失败:', err); + wx.showToast({ + title: '刷新失败,请稍后重试', + icon: 'none', + duration: 2000 + }); + }); } else { this.setData({ isLoading: false, @@ -838,7 +885,7 @@ Page({ }); }, - // 加载商品数据 - 优化版带缓存 + // 加载商品数据 - 优化版带缓存,支持状态优先级 loadGoods: function(isLoadMore = false, forceRefresh = false) { if (isLoadMore && !this.data.hasMoreData) { return @@ -848,6 +895,13 @@ Page({ const currentKeyword = this.data.searchKeyword; const cacheKey = `${currentCategory}_${currentKeyword}`; const now = new Date().getTime(); + const timestamp = new Date().getTime(); + + // 如果正在加载sold_out,继续加载sold_out + if (isLoadMore && this.data.isQueryingSoldOut) { + this.loadSoldOutData(cacheKey, currentKeyword, currentCategory, timestamp, isLoadMore); + return; + } // 只有非加载更多模式才使用缓存 if (!isLoadMore && !forceRefresh && @@ -864,29 +918,40 @@ Page({ if (isLoadMore) { this.setData({ loadingMore: true }) } else if (!forceRefresh) { // 只有非筛选的首次加载才显示骨架屏 - this.setData({ isLoading: true }) + this.setData({ isLoading: true, publishedHasMore: true, soldOutPage: 1 }) } - const timestamp = new Date().getTime(); const page = isLoadMore ? this.data.page : 1 + const pageSize = this.data.pageSize; - API.getProductList(['published', 'sold_out'], { + // 优先查询published状态的商品 + API.getProductList(['published'], { timestamp: timestamp, viewMode: 'shopping', page: page, - pageSize: this.data.pageSize, + pageSize: pageSize, keyword: currentKeyword, category: currentCategory }) .then(res => { wx.hideLoading(); - if (res.success && res.products) { + console.log('===== published状态查询结果 ====='); + console.log('res.success:', res.success); + console.log('res.products:', res.products); + console.log('res.products.length:', res.products ? res.products.length : 'undefined'); + console.log('page:', page); + + // 如果有published商品,直接处理 + if (res.success && res.products && res.products.length > 0) { + console.log('有published商品,处理商品数据'); const totalGoods = res.total || 0; - const totalPages = res.totalPages || Math.ceil(totalGoods / this.data.pageSize); - const hasMoreData = page < totalPages && res.products.length > 0; - const actualHasMoreData = res.products.length >= this.data.pageSize && page < totalPages; - + const totalPages = res.totalPages || Math.ceil(totalGoods / pageSize); + const publishedHasMore = page < totalPages && res.products.length >= pageSize; + + // 如果published商品数量不足pageSize,标记需要查询sold_out + const shouldQuerySoldOut = res.products.length < pageSize; + // 更新缓存(加载更多时追加数据) const updatedCache = { ...this.data.categoryQueryCache }; if (isLoadMore && updatedCache[cacheKey]) { @@ -898,29 +963,129 @@ Page({ // 首次加载或刷新时替换缓存 updatedCache[cacheKey] = res.products; } - + + // 计算sold_out起始页码和每页数量 + let soldOutPageNum = 1; + let soldOutPageSize = pageSize; + if (shouldQuerySoldOut) { + // 如果published不足一页,从第一页开始查sold_out补齐 + soldOutPageNum = 1; + soldOutPageSize = pageSize - res.products.length; + } else if (!publishedHasMore && isLoadMore) { + // 如果published已无更多数据,加载更多时查询sold_out下一页 + soldOutPageNum = this.data.soldOutPage + 1; + soldOutPageSize = pageSize; + } + this.setData({ - hasMoreData: actualHasMoreData, + hasMoreData: shouldQuerySoldOut || publishedHasMore, + isQueryingSoldOut: shouldQuerySoldOut || (!publishedHasMore && isLoadMore), + publishedHasMore: publishedHasMore, + soldOutPage: soldOutPageNum, + soldOutPageSize: soldOutPageSize, categoryQueryCache: updatedCache, lastDataTimestamp: now, goodsCache: updatedCache[cacheKey] - }) + }); + + this.processGoodsData(res.products, isLoadMore); + + // 如果需要查询sold_out + if (this.data.isQueryingSoldOut) { + this.loadSoldOutData(cacheKey, currentKeyword, currentCategory, timestamp, isLoadMore); + } - this.processGoodsData(res.products, isLoadMore) + return; + } + + // 如果没有published商品,查询sold_out状态 + if (page === 1) { + console.log('没有published商品,查询sold_out状态'); + console.log('查询参数 - page:', page, 'pageSize:', pageSize, 'keyword:', currentKeyword, 'category:', currentCategory); + this.setData({ isQueryingSoldOut: true, soldOutPage: 1, soldOutPageSize: pageSize }); + this.loadSoldOutData(cacheKey, currentKeyword, currentCategory, timestamp, isLoadMore); + return; } else { - this.setData({ loadingMore: false, isLoading: false }) + this.setData({ loadingMore: false, isLoading: false }); } }) .catch(err => { - console.error('加载商品数据失败:', err) + console.error('加载商品数据失败:', err); this.setData({ loadingMore: false, isLoading: false, isRefreshing: false - }) + }); }) }, + // 加载sold_out数据 + loadSoldOutData: function(cacheKey, currentKeyword, currentCategory, timestamp, isLoadMore) { + const pageSize = this.data.soldOutPageSize || 8; + const soldOutPageNum = this.data.soldOutPage || 1; + + console.log('加载sold_out数据 - page:', soldOutPageNum, 'pageSize:', pageSize); + console.log('查询参数 - page:', soldOutPageNum, 'pageSize:', pageSize, 'keyword:', currentKeyword, 'category:', currentCategory); + + API.getProductList(['sold_out'], { + timestamp: timestamp, + viewMode: 'shopping', + page: soldOutPageNum, + pageSize: pageSize, + keyword: currentKeyword, + category: currentCategory + }) + .then(soldOutRes => { + console.log('===== sold_out状态查询结果 ====='); + console.log('soldOutRes.success:', soldOutRes.success); + console.log('soldOutRes.products:', soldOutRes.products); + console.log('soldOutRes.products.length:', soldOutRes.products ? soldOutRes.products.length : 'undefined'); + + if (soldOutRes.success && soldOutRes.products && soldOutRes.products.length > 0) { + const soldOutTotal = soldOutRes.total || 0; + const soldOutTotalPages = soldOutRes.totalPages || Math.ceil(soldOutTotal / pageSize); + const soldOutHasMore = soldOutPageNum < soldOutTotalPages && soldOutRes.products.length >= pageSize; + + // 更新缓存 + const updatedCache = { ...this.data.categoryQueryCache }; + const existingCache = updatedCache[cacheKey] || []; + const existingIds = new Set(existingCache.map(item => item.id)); + const uniqueSoldOutProducts = soldOutRes.products.filter(item => !existingIds.has(item.id)); + + updatedCache[cacheKey] = [...existingCache, ...uniqueSoldOutProducts]; + + this.setData({ + hasMoreData: soldOutHasMore, + isQueryingSoldOut: soldOutHasMore, + soldOutPage: soldOutPageNum + 1, + categoryQueryCache: updatedCache, + lastDataTimestamp: new Date().getTime(), + goodsCache: updatedCache[cacheKey] + }); + + this.processGoodsData(updatedCache[cacheKey], isLoadMore); + console.log('sold_out数据加载完成,总商品数:', updatedCache[cacheKey].length, 'hasMoreData:', soldOutHasMore); + } else { + console.log('没有找到更多sold_out商品'); + this.setData({ + isQueryingSoldOut: false, + hasMoreData: false, + loadingMore: false, + isLoading: false + }); + } + }) + .catch(err => { + console.error('加载sold_out数据失败:', err); + this.setData({ + isQueryingSoldOut: false, + loadingMore: false, + isLoading: false, + isRefreshing: false + }); + }); + }, + // 处理缓存的商品数据 processCachedGoods: function(cachedGoods, isLoadMore) {