diff --git a/pages/goods/index.js b/pages/goods/index.js index 903d30c..ec49f14 100644 --- a/pages/goods/index.js +++ b/pages/goods/index.js @@ -40,8 +40,8 @@ Page({ searchKeyword: '', activeFilter: 'all', // 当前筛选条件:all, small, large filterConfig: { - small: ['何佳芹', '李真音'], // 小品种创建者 - large: ['吴海燕', '陈骏', '刘琴', '杨率','汤敏'] // 大贸易创建者 + small: [], // 小品种创建者,从数据库获取 + large: [] // 大贸易创建者,从数据库获取 }, total: 0, // 总数据条数 searchTimer: null, // 搜索防抖定时器 @@ -69,7 +69,91 @@ Page({ * 生命周期函数--监听页面加载 */ onLoad(options) { - this.loadGoodsList() + this.loadGoodsRootData() + }, + + /** + * 加载goods_root数据并填充filterConfig + */ + async loadGoodsRootData() { + try { + console.log('开始加载goods_root数据') + const roots = await API.getGoodsRoot() + + console.log('API.getGoodsRoot返回的完整数据:', roots) + console.log('返回的数据长度:', roots.length) + + // 处理返回的数据,分类小品种和大贸易 + const smallCreators = [] + const largeCreators = [] + + console.log('开始处理数据:') + roots.forEach(item => { + console.log('处理item:', item) + // 尝试获取name字段,支持不同的字段名 + const name = item.name || item.creatorName || item.userName || '' + // 尝试获取root字段,支持不同的字段名 + const root = item.root || item.category || item.type || '' + + if (root === '小品种') { + console.log('添加到小品种:', name) + if (name) smallCreators.push(name) + } else if (root === '大贸易') { + console.log('添加到大贸易:', name) + if (name) largeCreators.push(name) + } else { + console.log('未知分类:', root, ',跳过') + } + }) + + console.log('小品种创建者:', smallCreators) + console.log('大贸易创建者:', largeCreators) + + // 如果从数据库获取失败,使用默认数据 + let finalSmallCreators = smallCreators + let finalLargeCreators = largeCreators + + if (smallCreators.length === 0 || largeCreators.length === 0) { + console.log('从数据库获取数据失败,使用默认数据') + finalSmallCreators = ['何佳芹', '李真音', '王磊'] // 小品种创建者 + finalLargeCreators = ['吴海燕', '陈骏', '刘琴', '杨率', '汤敏'] // 大贸易创建者 + } + + // 更新filterConfig + this.setData({ + filterConfig: { + small: finalSmallCreators, + large: finalLargeCreators + }, + // 清除缓存,确保重新加载商品列表时使用新的filterConfig + 'cache.publishedGoods': [], + 'cache.soldOutGoods': [], + 'cache.timestamp': 0 + }) + + console.log('filterConfig更新成功:', this.data.filterConfig) + console.log('缓存已清除,准备重新加载商品列表') + + // 加载商品列表 + this.loadGoodsList() + } catch (err) { + console.error('加载goods_root数据失败:', err) + // 使用默认数据作为fallback + console.log('API调用失败,使用默认数据') + this.setData({ + filterConfig: { + small: ['何佳芹', '李真音', '王磊'], // 小品种创建者 + large: ['吴海燕', '陈骏', '刘琴', '杨率', '汤敏'] // 大贸易创建者 + }, + // 清除缓存,确保重新加载商品列表时使用新的filterConfig + 'cache.publishedGoods': [], + 'cache.soldOutGoods': [], + 'cache.timestamp': 0 + }) + console.log('使用默认数据更新filterConfig成功:', this.data.filterConfig) + // 即使获取失败,也继续加载商品列表 + this.loadGoodsList() + } }, /** @@ -257,14 +341,34 @@ Page({ filterGoodsList(goodsList) { const { activeFilter, filterConfig } = this.data + console.log('filterGoodsList 开始筛选,activeFilter:', activeFilter) + console.log('filterGoodsList filterConfig:', filterConfig) + console.log('filterGoodsList 原始商品数量:', goodsList.length) + if (activeFilter === 'all') { + console.log('filterGoodsList 筛选条件为all,返回所有商品') return goodsList } const allowedCreators = filterConfig[activeFilter] || [] - return goodsList.filter(item => { - return allowedCreators.includes(item.creatorName) + console.log('filterGoodsList 允许的创建者:', allowedCreators) + + // 处理allowedCreators,去除空格并转换为小写,用于宽松比较 + const processedAllowedCreators = allowedCreators.map(name => name.trim().toLowerCase()) + console.log('filterGoodsList 处理后的允许创建者:', processedAllowedCreators) + + const filteredGoods = goodsList.filter(item => { + const creatorName = item.creatorName || '' + const processedCreatorName = creatorName.trim().toLowerCase() + const isAllowed = processedAllowedCreators.includes(processedCreatorName) + console.log('filterGoodsList 检查商品:', creatorName, '(处理后:', processedCreatorName, ') 是否在允许列表中:', isAllowed) + return isAllowed }) + + console.log('filterGoodsList 筛选后商品数量:', filteredGoods.length) + console.log('filterGoodsList 筛选后商品:', filteredGoods) + + return filteredGoods }, /** @@ -412,21 +516,28 @@ Page({ }) try { - // 先获取所有商品数据 - const allGoods = await this.loadAllGoodsData() + // 从服务器获取已上架商品数据,支持分页和搜索 + const result = await this.loadGoodsData( + page, + pageSize, + 'published', + this.data.searchKeyword + ) - // 过滤出已上架状态的商品 - let publishedGoods = allGoods.filter(item => item.status === 'published') + console.log('原始商品数据:', result.goods) + console.log('当前筛选条件:', this.data.activeFilter) + console.log('筛选配置:', this.data.filterConfig) + console.log('分页参数:', { page, pageSize }) + console.log('hasMore:', result.hasMore) - // 应用筛选和搜索 - if (this.data.searchKeyword && this.data.searchKeyword.trim() !== '') { - publishedGoods = this.searchGoodsList(publishedGoods, this.data.searchKeyword) - } else { - publishedGoods = this.filterGoodsList(publishedGoods) - } + // 应用筛选逻辑 + const filteredGoods = this.filterGoodsList(result.goods) + + console.log('筛选后商品数据:', filteredGoods) + console.log('筛选后商品数量:', filteredGoods.length) // 排序逻辑:没有销售价格的商品排在最前面,然后按时间倒序排序 - publishedGoods.sort((a, b) => { + filteredGoods.sort((a, b) => { // 检查是否有销售价格 const hasPriceA = !!(a.price && a.price.trim() !== ''); const hasPriceB = !!(b.price && b.price.trim() !== ''); @@ -442,15 +553,9 @@ Page({ return timeB - timeA; }) - // 进行分页处理 - const startIndex = (page - 1) * pageSize - const endIndex = startIndex + pageSize - const paginatedGoods = publishedGoods.slice(startIndex, endIndex) - const hasMore = endIndex < publishedGoods.length + console.log('已上架货源加载完成,数量:', filteredGoods.length, ',是否有更多:', result.hasMore) - console.log('已上架货源加载完成,数量:', paginatedGoods.length, ',是否有更多:', hasMore) - - return { goods: paginatedGoods, hasMore: hasMore } + return { goods: filteredGoods, hasMore: result.hasMore } } catch (err) { console.error('加载已上架货源失败:', err) return { goods: [], hasMore: false } @@ -480,21 +585,28 @@ Page({ }) try { - // 先获取所有商品数据 - const allGoods = await this.loadAllGoodsData() + // 从服务器获取售空商品数据,支持分页和搜索 + const result = await this.loadGoodsData( + page, + pageSize, + 'sold_out', + this.data.searchKeyword + ) - // 过滤出售空状态的商品 - let soldOutGoods = allGoods.filter(item => item.status === 'sold_out') + console.log('售空商品数据:', result.goods) + console.log('售空商品分页参数:', { page, pageSize }) + console.log('售空商品hasMore:', result.hasMore) + console.log('当前筛选条件:', this.data.activeFilter) + console.log('筛选配置:', this.data.filterConfig) - // 应用筛选和搜索 - if (this.data.searchKeyword && this.data.searchKeyword.trim() !== '') { - soldOutGoods = this.searchGoodsList(soldOutGoods, this.data.searchKeyword) - } else { - soldOutGoods = this.filterGoodsList(soldOutGoods) - } + // 应用筛选逻辑 + const filteredGoods = this.filterGoodsList(result.goods) + + console.log('筛选后售空商品数据:', filteredGoods) + console.log('筛选后售空商品数量:', filteredGoods.length) // 排序逻辑:没有销售价格的商品排在最前面,然后按时间倒序排序 - soldOutGoods.sort((a, b) => { + filteredGoods.sort((a, b) => { // 检查是否有销售价格 const hasPriceA = !!(a.price && a.price.trim() !== ''); const hasPriceB = !!(b.price && b.price.trim() !== ''); @@ -510,15 +622,9 @@ Page({ return timeB - timeA; }) - // 进行分页处理 - const startIndex = (page - 1) * pageSize - const endIndex = startIndex + pageSize - const paginatedGoods = soldOutGoods.slice(startIndex, endIndex) - const hasMore = endIndex < soldOutGoods.length - - console.log('售空货源加载完成,数量:', paginatedGoods.length, ',是否有更多:', hasMore) + console.log('售空货源加载完成,数量:', filteredGoods.length, ',是否有更多:', result.hasMore) - return { goods: paginatedGoods, hasMore: hasMore } + return { goods: filteredGoods, hasMore: result.hasMore } } catch (err) { console.error('加载售空货源失败:', err) return { goods: [], hasMore: false } @@ -545,6 +651,10 @@ Page({ async loadGoodsList(isLoadMore = false) { if (this.data.isLoading) return + console.log('loadGoodsList 开始加载,isLoadMore:', isLoadMore) + console.log('loadGoodsList 当前activeFilter:', this.data.activeFilter) + console.log('loadGoodsList 当前filterConfig:', this.data.filterConfig) + // 如果是初始加载,重置分页状态 if (!isLoadMore) { this.setData({ @@ -566,15 +676,18 @@ Page({ try { // 检查缓存是否有效 if (!isLoadMore && this.isCacheValid()) { - console.log('使用缓存数据') + console.log('loadGoodsList 使用缓存数据') const cache = this.data.cache // 重新应用当前筛选条件到缓存数据 const allCachedGoods = [...cache.publishedGoods, ...cache.soldOutGoods] + console.log('loadGoodsList 缓存商品数量:', allCachedGoods.length) + console.log('loadGoodsList 缓存商品:', allCachedGoods) + const filteredGoods = this.filterGoodsList(allCachedGoods) - console.log('缓存商品数量:', allCachedGoods.length) - console.log('筛选后缓存商品数量:', filteredGoods.length) + console.log('loadGoodsList 筛选后缓存商品数量:', filteredGoods.length) + console.log('loadGoodsList 筛选后缓存商品:', filteredGoods) this.setData({ goodsList: filteredGoods, @@ -585,6 +698,7 @@ Page({ total: filteredGoods.length }) + console.log('loadGoodsList 缓存数据加载完成,商品数量:', filteredGoods.length) return } @@ -604,17 +718,17 @@ Page({ 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) + console.log('loadGoodsList 原始已上架商品数据:', result.goods) + console.log('loadGoodsList 当前筛选条件:', this.data.activeFilter) + console.log('loadGoodsList 筛选配置:', this.data.filterConfig) + console.log('loadGoodsList 分页参数:', { page: currentPage, pageSize: this.data.pageSize }) + console.log('loadGoodsList hasMore:', result.hasMore) // 应用筛选逻辑 const filteredGoods = this.filterGoodsList(result.goods) - console.log('筛选后商品数据:', filteredGoods) - console.log('筛选后商品数量:', filteredGoods.length) + console.log('loadGoodsList 筛选后已上架商品数据:', filteredGoods) + console.log('loadGoodsList 筛选后已上架商品数量:', filteredGoods.length) // 累积符合条件的商品 accumulatedFilteredGoods = [...accumulatedFilteredGoods, ...filteredGoods] @@ -629,13 +743,14 @@ Page({ } } - console.log('累积筛选后商品数量:', accumulatedFilteredGoods.length) + console.log('loadGoodsList 累积筛选后已上架商品数量:', accumulatedFilteredGoods.length) // 如果是加载更多,追加数据;否则替换数据 if (accumulatedFilteredGoods.length > 0) { this.setData({ goodsList: isLoadMore ? [...this.data.goodsList, ...accumulatedFilteredGoods] : accumulatedFilteredGoods }) + console.log('loadGoodsList 已上架商品数据更新完成,当前商品数量:', this.data.goodsList.length) } // 更新页码和hasMore状态 @@ -644,14 +759,14 @@ Page({ publishedHasMore: hasMore }) - console.log('已上架商品加载完成,更新页码为:', currentPage, ',是否有更多:', hasMore) + console.log('loadGoodsList 已上架商品加载完成,更新页码为:', currentPage, ',是否有更多:', hasMore) // 如果没有更多数据,标记为加载完成 if (!hasMore) { this.setData({ publishedLoaded: true }) - console.log('已上架商品加载完成') + console.log('loadGoodsList 已上架商品加载完成') } } @@ -673,16 +788,17 @@ Page({ 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) + console.log('loadGoodsList 原始售空商品数据:', result.goods) + console.log('loadGoodsList 售空商品分页参数:', { page: currentPage, pageSize: this.data.pageSize }) + console.log('loadGoodsList 售空商品hasMore:', result.hasMore) + console.log('loadGoodsList 当前筛选条件:', this.data.activeFilter) + console.log('loadGoodsList 筛选配置:', this.data.filterConfig) // 应用筛选逻辑 const filteredGoods = this.filterGoodsList(result.goods) - console.log('筛选后售空商品数据:', filteredGoods) - console.log('筛选后售空商品数量:', filteredGoods.length) + console.log('loadGoodsList 筛选后售空商品数据:', filteredGoods) + console.log('loadGoodsList 筛选后售空商品数量:', filteredGoods.length) // 累积符合条件的商品 accumulatedFilteredGoods = [...accumulatedFilteredGoods, ...filteredGoods] @@ -697,13 +813,14 @@ Page({ } } - console.log('累积筛选后售空商品数量:', accumulatedFilteredGoods.length) + console.log('loadGoodsList 累积筛选后售空商品数量:', accumulatedFilteredGoods.length) // 追加售空商品数据 if (accumulatedFilteredGoods.length > 0) { this.setData({ goodsList: isLoadMore ? [...this.data.goodsList, ...accumulatedFilteredGoods] : [...this.data.goodsList, ...accumulatedFilteredGoods] }) + console.log('loadGoodsList 售空商品数据更新完成,当前商品数量:', this.data.goodsList.length) } // 更新页码和hasMore状态 @@ -712,18 +829,18 @@ Page({ soldOutHasMore: hasMore }) - console.log('售空商品加载完成,更新页码为:', currentPage, ',是否有更多:', hasMore) + console.log('loadGoodsList 售空商品加载完成,更新页码为:', currentPage, ',是否有更多:', hasMore) // 如果没有更多数据,标记为加载完成 if (!hasMore) { this.setData({ soldOutLoaded: true }) - console.log('售空商品加载完成') + console.log('loadGoodsList 售空商品加载完成') } } - console.log('加载完成后状态:', { + console.log('loadGoodsList 加载完成后状态:', { publishedHasMore: this.data.publishedHasMore, soldOutHasMore: this.data.soldOutHasMore, publishedCurrentPage: this.data.publishedCurrentPage, @@ -735,10 +852,11 @@ Page({ if (!isLoadMore && !this.data.publishedHasMore && !this.data.soldOutHasMore) { // 由于现在采用分页加载,缓存逻辑需要调整 // 暂时禁用缓存更新,确保每次都从服务器获取最新数据 - console.log('分页加载模式,跳过缓存更新') + console.log('loadGoodsList 分页加载模式,跳过缓存更新') } } catch (err) { + console.error('loadGoodsList 获取货源列表失败:', err) wx.showToast({ title: '获取货源列表失败: ' + err.message, icon: 'none' @@ -755,6 +873,7 @@ Page({ soldOutHasMore: false, total: cache.publishedGoods.length + cache.soldOutGoods.length }) + console.log('loadGoodsList 加载失败,使用缓存数据,商品数量:', this.data.goodsList.length) } } finally { // 结束下拉刷新和加载状态 @@ -762,6 +881,7 @@ Page({ isLoading: false, isRefreshing: false }) + console.log('loadGoodsList 加载完成,结束加载状态') } } }) \ No newline at end of file diff --git a/utils/api.js b/utils/api.js index 459c74c..a87b2fc 100644 --- a/utils/api.js +++ b/utils/api.js @@ -967,6 +967,36 @@ module.exports = { }); }, + // 获取goods_root表数据 + getGoodsRoot: function () { + return new Promise((resolve, reject) => { + // 从本地存储获取openid + const openid = wx.getStorageSync('openid') || ''; + + console.log('API.getGoodsRoot 开始请求,openid:', openid); + + // 使用POST请求获取goods_root数据,与其他API方法保持一致 + request('/api/goods/root', 'POST', { + openid: openid + }).then(res => { + console.log('API.getGoodsRoot 响应数据:', res); + + if (res && (res.code === 200 || res.success)) { + const roots = res.roots || []; + console.log('API.getGoodsRoot 返回的roots数据:', roots); + console.log('API.getGoodsRoot 返回的roots长度:', roots.length); + resolve(roots); + } else { + console.error('API.getGoodsRoot 响应失败:', res); + reject(new Error('获取货源分类失败')); + } + }).catch(err => { + console.error('API.getGoodsRoot 请求失败:', err); + reject(new Error('获取货源分类失败,请稍后重试')); + }); + }); + }, + // 从所有用户的购物车中移除指定商品 removeFromAllCarts: function (supplyId) { var openid = wx.getStorageSync('openid');