|
|
|
@ -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], |
|
|
|
|