From fa503efda675eac47d181cccf96d4ed9de2f8941 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: Sat, 24 Jan 2026 13:19:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=95=86=E5=93=81=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E5=92=8CAPI=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/goods/index.js | 165 ++++++++++++++++++++----------------------- utils/api.js | 23 +++--- 2 files changed, 88 insertions(+), 100 deletions(-) diff --git a/pages/goods/index.js b/pages/goods/index.js index 846828d..29db90b 100644 --- a/pages/goods/index.js +++ b/pages/goods/index.js @@ -8,12 +8,7 @@ function isVideoUrl(url) { } const lowerUrl = url.toLowerCase(); const videoExtensions = ['.mp4', '.mov', '.avi', '.wmv', '.flv', '.webm', '.m4v', '.3gp']; - for (const ext of videoExtensions) { - if (lowerUrl.endsWith(ext)) { - return true; - } - } - return false; + return videoExtensions.some(ext => lowerUrl.endsWith(ext)); } Page({ @@ -279,29 +274,27 @@ Page({ if (!dateString) return '未知时间' // 检查是否已经是格式化好的北京时间字符串(如:2026-01-05 17:30) - if (typeof dateString === 'string' && /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2})?$/.test(dateString)) { - // 直接返回格式化好的字符串,只保留到分钟 - return dateString.slice(0, 16) - } - - // 检查是否是ISO格式的字符串(如:2026-01-05T12:00:00.000Z) - if (typeof dateString === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/.test(dateString)) { - // 转换ISO格式为本地时间 - const date = new Date(dateString) - if (isNaN(date.getTime())) { - return '未知时间' + if (typeof dateString === 'string') { + if (dateString.includes(' ')) { + // 直接返回格式化好的字符串,只保留到分钟 + return dateString.slice(0, 16) + } else if (dateString.includes('T')) { + // 转换ISO格式为本地时间 + const date = new Date(dateString) + if (isNaN(date.getTime())) { + return '未知时间' + } + + const year = date.getFullYear() + const month = (date.getMonth() + 1).toString().padStart(2, '0') + const day = date.getDate().toString().padStart(2, '0') + const hours = date.getHours().toString().padStart(2, '0') + const minutes = date.getMinutes().toString().padStart(2, '0') + return `${year}-${month}-${day} ${hours}:${minutes}` } - - const year = date.getFullYear() - const month = (date.getMonth() + 1).toString().padStart(2, '0') - const day = date.getDate().toString().padStart(2, '0') - const hours = date.getHours().toString().padStart(2, '0') - const minutes = date.getMinutes().toString().padStart(2, '0') - return `${year}-${month}-${day} ${hours}:${minutes}` } // 对于其他格式的字符串,直接返回,不进行转换 - // 这是为了避免对后端返回的北京时间字符串进行错误的时区转换 return dateString }, @@ -310,7 +303,6 @@ Page({ */ onGoodsItemClick(e) { const goodsItem = e.currentTarget.dataset.item; - console.log('点击了货源项:', goodsItem); // 跳转到货源详情页面(goods-update) wx.navigateTo({ @@ -319,15 +311,15 @@ Page({ }, /** - * 加载所有商品数据 - 辅助方法 + * 加载商品数据 - 支持分页 */ - async loadAllGoodsData() { + async loadGoodsData(page = 1, pageSize = this.data.pageSize, status = 'all', keyword = '') { try { - // 使用getProducts方法获取所有商品数据 - const allGoods = await API.getProducts() + // 使用getProducts方法获取商品数据,支持分页和搜索 + const result = await API.getProducts(page, pageSize, status, keyword) - // 对所有商品进行格式化处理 - const formattedGoods = allGoods.map(item => { + // 对商品进行格式化处理 + const formattedGoods = result.products.map(item => { // 确定creatorName const sellerNickName = item.seller?.nickName || item.seller?.sellerNickName || item.seller?.name || '未知'; const creatorName = sellerNickName; @@ -346,7 +338,7 @@ Page({ status === 'sold' || status === 'out_of_stock' || (item.supplyStatus && item.supplyStatus.includes('售空')); - + if (isSoldOut) { status = 'sold_out' } else if (status !== 'published') { @@ -378,10 +370,17 @@ Page({ } }) - return formattedGoods + return { + goods: formattedGoods, + total: result.total, + hasMore: result.hasMore + } } catch (err) { - console.error('加载所有商品数据失败:', err) - return [] + return { + goods: [], + total: 0, + hasMore: false + } } }, @@ -554,52 +553,42 @@ Page({ }) } - console.log('开始加载货源列表,参数:', { - keyword: this.data.searchKeyword, - activeFilter: this.data.activeFilter, - useCache: this.isCacheValid(), - isLoadMore: isLoadMore, - publishedCurrentPage: this.data.publishedCurrentPage, - publishedHasMore: this.data.publishedHasMore, - soldOutCurrentPage: this.data.soldOutCurrentPage, - soldOutHasMore: this.data.soldOutHasMore - }) - try { - // 为了确保获取最新数据,暂时禁用缓存,直接从服务器获取 - // 后续可以根据需要恢复缓存逻辑 - // if (!isLoadMore && this.isCacheValid()) { - // console.log('使用缓存数据') - // const cache = this.data.cache - // - // this.setData({ - // goodsList: [...cache.publishedGoods, ...cache.soldOutGoods], - // publishedLoaded: true, - // soldOutLoaded: true, - // publishedHasMore: false, - // soldOutHasMore: false, - // total: cache.publishedGoods.length + cache.soldOutGoods.length - // }) - // - // return - // } - - console.log('缓存无效或加载更多,从服务器获取数据') + // 检查缓存是否有效 + if (!isLoadMore && this.isCacheValid()) { + const cache = this.data.cache + + this.setData({ + goodsList: [...cache.publishedGoods, ...cache.soldOutGoods], + publishedLoaded: true, + soldOutLoaded: true, + publishedHasMore: false, + soldOutHasMore: false, + total: cache.publishedGoods.length + cache.soldOutGoods.length + }) + + return + } let newGoods = [] let updatedPublishedHasMore = this.data.publishedHasMore let updatedSoldOutHasMore = this.data.soldOutHasMore + let publishedGoods = [] + let soldOutGoods = [] - // 1. 如果已上架商品还有更多,继续加载已上架商品 + // 1. 处理已上架商品 if (this.data.publishedHasMore) { - console.log('加载已上架商品第', this.data.publishedCurrentPage, '页') - const publishedResult = await this.loadPublishedGoods( + // 从服务器获取已上架商品数据,支持分页和搜索 + const result = await this.loadGoodsData( this.data.publishedCurrentPage, - this.data.pageSize + this.data.pageSize, + 'published', + this.data.searchKeyword ) - newGoods = publishedResult.goods - updatedPublishedHasMore = publishedResult.hasMore + newGoods = result.goods + updatedPublishedHasMore = result.hasMore + publishedGoods = result.goods // 如果是加载更多,追加数据;否则替换数据 this.setData({ @@ -619,21 +608,21 @@ Page({ publishedLoaded: true }) } - - // 添加缓冲,避免请求太快 - await new Promise(resolve => setTimeout(resolve, 500)) } - // 2. 如果已上架商品加载完,开始加载售空商品 + // 2. 处理售空商品 if (!this.data.publishedHasMore && this.data.soldOutHasMore) { - console.log('加载售空商品第', this.data.soldOutCurrentPage, '页') - const soldOutResult = await this.loadSoldOutGoods( + // 从服务器获取售空商品数据,支持分页和搜索 + const result = await this.loadGoodsData( this.data.soldOutCurrentPage, - this.data.pageSize + this.data.pageSize, + 'sold_out', + this.data.searchKeyword ) - newGoods = soldOutResult.goods - updatedSoldOutHasMore = soldOutResult.hasMore + newGoods = result.goods + updatedSoldOutHasMore = result.hasMore + soldOutGoods = result.goods // 追加售空商品数据 this.setData({ @@ -654,23 +643,20 @@ Page({ } } - console.log('货源列表加载完成,当前总数:', this.data.goodsList.length) - // 如果是初始加载且所有数据加载完成,更新缓存 if (!isLoadMore && !this.data.publishedHasMore && !this.data.soldOutHasMore) { - // 分离已上架和售空商品 - const publishedGoods = this.data.goodsList.filter(item => item.status === 'published') - const soldOutGoods = this.data.goodsList.filter(item => item.status === 'sold_out') + // 为了缓存完整数据,获取所有已上架和售空商品 + const publishedResult = await this.loadGoodsData(1, 1000, 'published', this.data.searchKeyword) + const soldOutResult = await this.loadGoodsData(1, 1000, 'sold_out', this.data.searchKeyword) this.setData({ - 'cache.publishedGoods': publishedGoods, - 'cache.soldOutGoods': soldOutGoods, + 'cache.publishedGoods': publishedResult.goods, + 'cache.soldOutGoods': soldOutResult.goods, 'cache.timestamp': Date.now() }) } } catch (err) { - console.error('获取货源列表失败:', err) wx.showToast({ title: '获取货源列表失败: ' + err.message, icon: 'none' @@ -678,7 +664,6 @@ Page({ // 加载失败时,如果是初始加载,尝试使用缓存数据 if (!isLoadMore && (this.data.cache.publishedGoods.length > 0 || this.data.cache.soldOutGoods.length > 0)) { - console.log('使用缓存数据作为 fallback') const cache = this.data.cache this.setData({ goodsList: [...cache.publishedGoods, ...cache.soldOutGoods], diff --git a/utils/api.js b/utils/api.js index e6daaf0..2bad893 100644 --- a/utils/api.js +++ b/utils/api.js @@ -803,7 +803,7 @@ module.exports = { }, // 获取商品列表的方法(用于刷新商品信息)- 修复接口路径和请求方式 - getProducts: function () { + getProducts: function (page = 1, pageSize = 20, status = 'all', keyword = '') { return new Promise((resolve, reject) => { // 从本地存储获取openid const openid = wx.getStorageSync('openid') || ''; @@ -811,22 +811,25 @@ module.exports = { // 使用正确的接口路径和POST请求方式 request('/api/product/list', 'POST', { openid: openid, - status: 'all', // 请求所有状态的商品(除了hidden) + status: status, // 请求指定状态的商品 viewMode: 'buyer', // 使用buyer模式获取所有商品,而非仅当前用户的商品 - page: 1, - pageSize: 1000 // 增加pageSize以获取更多商品 + page: page, + pageSize: pageSize, + keyword: keyword // 添加关键词搜索 }).then(res => { - if (res && (res.code === 200 || res.success) && res.products) { - console.log('API.getProducts - 获取到的商品数量:', res.products.length); + if (res && (res.code === 200 || res.success)) { + const products = res.products || []; // 将商品列表存储到本地缓存 - wx.setStorageSync('goods', res.products || []); - resolve(res.products); + wx.setStorageSync('goods', products || []); + resolve({ + products: products, + total: res.total || products.length, + hasMore: (page * pageSize) < (res.total || products.length) + }); } else { - console.error('API.getProducts - 获取商品列表失败:', res); reject(new Error('获取商品列表失败')); } }).catch(err => { - console.error('API.getProducts - 获取商品列表失败:', err); reject(new Error('获取商品列表失败,请稍后重试')); }); });