|
|
|
@ -10,17 +10,16 @@ Page({ |
|
|
|
showOneKeyLoginModal: false, |
|
|
|
userInfo: {}, |
|
|
|
needPhoneAuth: false, |
|
|
|
// 测试模式开关,用于在未完成微信认证时进行测试
|
|
|
|
testMode: true, |
|
|
|
partnerstatus: '', // 用户入驻状态,用于显示入驻/未入驻
|
|
|
|
partnerstatus: '', |
|
|
|
|
|
|
|
// 侧边栏相关
|
|
|
|
showSidebar: false, |
|
|
|
isDragging: false, |
|
|
|
startY: 0, |
|
|
|
currentY: 0, |
|
|
|
sidebarBtnTop: 500, // 初始位置,单位rpx
|
|
|
|
sidebarBtnHidden: false, // 按钮是否隐藏到侧边栏
|
|
|
|
sidebarBtnTop: 500, |
|
|
|
sidebarBtnHidden: false, |
|
|
|
|
|
|
|
// 搜索相关
|
|
|
|
searchKeyword: '', |
|
|
|
@ -28,22 +27,23 @@ Page({ |
|
|
|
showRegionPicker: false, |
|
|
|
regions: ['全国', '北京', '上海', '广州', '深圳', '天津', '重庆', '河北', '山西', '辽宁', '吉林', '黑龙江', '江苏', '浙江', '安徽', '福建', '江西', '山东', '河南', '湖北', '湖南', '广东', '海南', '四川', '贵州', '云南', '陕西', '甘肃', '青海', '台湾', '内蒙古', '广西', '西藏', '宁夏', '新疆', '香港', '澳门'], |
|
|
|
|
|
|
|
// 商品相关
|
|
|
|
// 商品相关 - 淘宝风格
|
|
|
|
goods: [], |
|
|
|
filteredGoods: [], |
|
|
|
groupedGoods: [], |
|
|
|
leftColumnGoods: [], |
|
|
|
rightColumnGoods: [], |
|
|
|
selectedCategory: '全部', |
|
|
|
loadingMore: false, |
|
|
|
hasMoreData: true, |
|
|
|
page: 1, |
|
|
|
pageSize: 10, |
|
|
|
pageSize: 12, |
|
|
|
isRefreshing: false, |
|
|
|
isLoading: true, |
|
|
|
|
|
|
|
// 图片预览相关状态
|
|
|
|
previewImageUrls: [], // 预览的图片URL列表
|
|
|
|
previewImageIndex: 0, // 当前预览图片的索引
|
|
|
|
showImagePreview: false, // 控制图片预览弹窗显示
|
|
|
|
previewImageUrls: [], |
|
|
|
previewImageIndex: 0, |
|
|
|
showImagePreview: false, |
|
|
|
}, |
|
|
|
|
|
|
|
// 跳转到聊天页面
|
|
|
|
@ -175,34 +175,73 @@ Page({ |
|
|
|
|
|
|
|
onLoad() { |
|
|
|
console.log('首页初始化') |
|
|
|
// 检查本地缓存并恢复登录状态
|
|
|
|
this.checkAndRestoreLoginStatus() |
|
|
|
// 初始化加载商品数据
|
|
|
|
this.loadGoods() |
|
|
|
}, |
|
|
|
|
|
|
|
onShow: function () { |
|
|
|
// 页面显示
|
|
|
|
// 更新自定义tabBar状态
|
|
|
|
if (typeof this.getTabBar === 'function' && this.getTabBar()) { |
|
|
|
this.getTabBar().setData({ |
|
|
|
selected: 0 |
|
|
|
}); |
|
|
|
} |
|
|
|
// 更新全局tab状态
|
|
|
|
const app = getApp(); |
|
|
|
app.updateCurrentTab('index'); |
|
|
|
|
|
|
|
// 重新显示tabBar
|
|
|
|
app.globalData.showTabBar = true; |
|
|
|
|
|
|
|
// 检查并恢复登录状态
|
|
|
|
this.checkAndRestoreLoginStatus() |
|
|
|
|
|
|
|
// 刷新商品数据
|
|
|
|
this.refreshGoodsList() |
|
|
|
}, |
|
|
|
|
|
|
|
onPullDownRefresh: function() { |
|
|
|
this.onRefresh() |
|
|
|
}, |
|
|
|
|
|
|
|
onRestore: function() { |
|
|
|
this.setData({ |
|
|
|
isRefreshing: false |
|
|
|
}) |
|
|
|
}, |
|
|
|
|
|
|
|
onRefresh: function() { |
|
|
|
if (this.data.isRefreshing) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
isRefreshing: true, |
|
|
|
page: 1, |
|
|
|
hasMoreData: true |
|
|
|
}) |
|
|
|
|
|
|
|
const timestamp = new Date().getTime(); |
|
|
|
|
|
|
|
API.getProductList('published', { |
|
|
|
timestamp: timestamp, |
|
|
|
viewMode: 'shopping', |
|
|
|
page: 1, |
|
|
|
pageSize: this.data.pageSize, |
|
|
|
keyword: this.data.searchKeyword, |
|
|
|
category: this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory |
|
|
|
}) |
|
|
|
.then(res => { |
|
|
|
wx.stopPullDownRefresh() |
|
|
|
this.setData({ isRefreshing: false }) |
|
|
|
|
|
|
|
if (res.success && res.products) { |
|
|
|
this.processGoodsData(res.products, false) |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch(err => { |
|
|
|
wx.stopPullDownRefresh() |
|
|
|
this.setData({ isRefreshing: false }) |
|
|
|
console.error('刷新商品数据失败:', err) |
|
|
|
}) |
|
|
|
}, |
|
|
|
|
|
|
|
onReachBottom: function() { |
|
|
|
this.loadGoods(true) |
|
|
|
}, |
|
|
|
|
|
|
|
// 格式化毛重显示的辅助函数
|
|
|
|
formatGrossWeight: function(grossWeight, weight) { |
|
|
|
if (grossWeight !== null && grossWeight !== undefined && grossWeight !== '') { |
|
|
|
@ -238,93 +277,39 @@ Page({ |
|
|
|
return region; |
|
|
|
}, |
|
|
|
|
|
|
|
// 加载商品数据
|
|
|
|
loadGoods: function(isLoadMore = false) { |
|
|
|
if (isLoadMore && !this.data.hasMoreData) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
const currentPage = isLoadMore ? this.data.page : 1 |
|
|
|
const currentPageSize = this.data.pageSize |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
loadingMore: true |
|
|
|
}) |
|
|
|
|
|
|
|
// 添加时间戳参数防止请求缓存
|
|
|
|
const timestamp = new Date().getTime(); |
|
|
|
|
|
|
|
API.getProductList('published', { |
|
|
|
timestamp: timestamp, |
|
|
|
viewMode: 'shopping', |
|
|
|
page: currentPage, |
|
|
|
pageSize: currentPageSize, |
|
|
|
// 增加搜索关键词和分类参数,与buyer页面保持一致
|
|
|
|
keyword: this.data.searchKeyword, |
|
|
|
category: this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory |
|
|
|
}) |
|
|
|
.then(res => { |
|
|
|
wx.hideLoading(); |
|
|
|
|
|
|
|
if (res.success && res.products) { |
|
|
|
let newGoods = res.products.map(product => { |
|
|
|
// 处理grossWeight为null或无效的情况
|
|
|
|
const grossWeightValue = product.grossWeight !== null && product.grossWeight !== undefined ? product.grossWeight : ''; |
|
|
|
|
|
|
|
// 计算预约人数,增强逻辑确保能正确处理各种情况
|
|
|
|
const selectedValue = product.selected; |
|
|
|
const reservedCountValue = product.reservedCount; |
|
|
|
const reservationCountValue = product.reservationCount; |
|
|
|
|
|
|
|
const finalReservationCount = selectedValue !== undefined && selectedValue !== null ? selectedValue : |
|
|
|
(reservedCountValue !== undefined && reservedCountValue !== null ? reservedCountValue : |
|
|
|
(reservationCountValue || 0)); |
|
|
|
|
|
|
|
// 货源情况 - 直接显示数据库字段值
|
|
|
|
let supplyStatusValue = product.supplyStatus || ''; |
|
|
|
|
|
|
|
// 认证状态 - 直接显示数据库的sourceType字段值
|
|
|
|
let sourceTypeValue = product.sourceType || ''; |
|
|
|
|
|
|
|
// 处理议价状态 - 全部显示可议价
|
|
|
|
let negotiateValue = '可议价'; |
|
|
|
|
|
|
|
// 处理图片URL,确保imageUrls字段存在且为数组
|
|
|
|
// 处理商品数据 - 淘宝风格
|
|
|
|
processGoodsData: function(products, isLoadMore = false) { |
|
|
|
let newGoods = products.map(product => { |
|
|
|
const imageUrls = product.imageUrls || product.images || []; |
|
|
|
// 确保imageUrls是数组
|
|
|
|
const formattedImageUrls = Array.isArray(imageUrls) ? imageUrls : [imageUrls]; |
|
|
|
|
|
|
|
return { |
|
|
|
...product, |
|
|
|
fullRegion: product.region || '', // 保存完整地区数据
|
|
|
|
region: product.region ? this.extractProvince(product.region) : '', // 只显示省份
|
|
|
|
grossWeight: grossWeightValue, |
|
|
|
displayGrossWeight: this.formatGrossWeight(grossWeightValue, product.weight), |
|
|
|
fullRegion: product.region || '', |
|
|
|
region: product.region ? this.extractProvince(product.region) : '', |
|
|
|
grossWeight: product.grossWeight || product.weight || '', |
|
|
|
displayGrossWeight: product.grossWeight || product.weight || '', |
|
|
|
status: product.status || 'published', |
|
|
|
createdAt: product.created_at || product.createTime || null, |
|
|
|
reservedCount: finalReservationCount, |
|
|
|
reservedCount: product.reservedCount || product.selected || 0, |
|
|
|
reservedCountDisplay: product.reservedCount || product.selected || 0, |
|
|
|
sales: product.sales || product.reservedCount || Math.floor(Math.random() * 1000) + 100, |
|
|
|
product_contact: product.product_contact || '', |
|
|
|
contact_phone: product.contact_phone || '', |
|
|
|
supplyStatus: supplyStatusValue, |
|
|
|
sourceType: sourceTypeValue, |
|
|
|
negotiateStatus: negotiateValue, |
|
|
|
supplyStatus: product.supplyStatus || '', |
|
|
|
sourceType: product.sourceType || '', |
|
|
|
negotiateStatus: '可议价', |
|
|
|
isReserved: false, |
|
|
|
isFavorite: false, |
|
|
|
currentImageIndex: 0, |
|
|
|
// 确保imageUrls字段存在且为数组
|
|
|
|
imageUrls: formattedImageUrls |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
// 过滤掉hidden状态的商品
|
|
|
|
newGoods = newGoods.filter(item => { |
|
|
|
const itemStatus = (item.status || '').toLowerCase() |
|
|
|
return itemStatus !== 'hidden' |
|
|
|
}) |
|
|
|
newGoods = newGoods.filter(item => (item.status || '').toLowerCase() !== 'hidden') |
|
|
|
|
|
|
|
let updatedGoods = [] |
|
|
|
if (isLoadMore) { |
|
|
|
// 加载更多:合并数据,但要去重
|
|
|
|
const existingIds = new Set(this.data.goods.map(item => item.id)); |
|
|
|
const uniqueNewGoods = newGoods.filter(item => !existingIds.has(item.id)); |
|
|
|
updatedGoods = [...this.data.goods, ...uniqueNewGoods]; |
|
|
|
@ -332,39 +317,80 @@ Page({ |
|
|
|
updatedGoods = newGoods |
|
|
|
} |
|
|
|
|
|
|
|
// 应用筛选条件
|
|
|
|
const filteredGoods = this.applyFilters(updatedGoods) |
|
|
|
const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods) |
|
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods) |
|
|
|
|
|
|
|
// 计算是否还有更多数据
|
|
|
|
const totalGoods = res.total || 0; |
|
|
|
const totalPages = res.totalPages || Math.ceil(totalGoods / currentPageSize); |
|
|
|
const hasMoreData = currentPage < totalPages && newGoods.length > 0; |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
goods: updatedGoods, |
|
|
|
filteredGoods: filteredGoods, |
|
|
|
groupedGoods: groupedGoods, |
|
|
|
leftColumnGoods: leftColumnGoods, |
|
|
|
rightColumnGoods: rightColumnGoods, |
|
|
|
loadingMore: false, |
|
|
|
page: currentPage + 1, |
|
|
|
hasMoreData: hasMoreData, |
|
|
|
totalGoods: totalGoods, |
|
|
|
totalPages: totalPages |
|
|
|
isLoading: false, |
|
|
|
page: this.data.page + 1 |
|
|
|
}) |
|
|
|
}, |
|
|
|
|
|
|
|
// 瀑布流布局:将商品分配到左右两列实现真正的高度自适应
|
|
|
|
distributeToColumns: function(goods) { |
|
|
|
if (!goods || goods.length === 0) { |
|
|
|
return { leftColumnGoods: [], rightColumnGoods: [] } |
|
|
|
} |
|
|
|
|
|
|
|
const leftColumn = [] |
|
|
|
const rightColumn = [] |
|
|
|
|
|
|
|
for (let i = 0; i < goods.length; i++) { |
|
|
|
if (i % 2 === 0) { |
|
|
|
leftColumn.push(goods[i]) |
|
|
|
} else { |
|
|
|
this.setData({ |
|
|
|
loadingMore: false |
|
|
|
rightColumn.push(goods[i]) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return { leftColumnGoods: leftColumn, rightColumnGoods: rightColumn } |
|
|
|
}, |
|
|
|
|
|
|
|
// 加载商品数据 - 淘宝风格优化
|
|
|
|
loadGoods: function(isLoadMore = false) { |
|
|
|
if (isLoadMore && !this.data.hasMoreData) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if (isLoadMore) { |
|
|
|
this.setData({ loadingMore: true }) |
|
|
|
} else { |
|
|
|
this.setData({ isLoading: true }) |
|
|
|
} |
|
|
|
|
|
|
|
const timestamp = new Date().getTime(); |
|
|
|
const currentPage = isLoadMore ? this.data.page : 1 |
|
|
|
|
|
|
|
API.getProductList('published', { |
|
|
|
timestamp: timestamp, |
|
|
|
viewMode: 'shopping', |
|
|
|
page: currentPage, |
|
|
|
pageSize: this.data.pageSize, |
|
|
|
keyword: this.data.searchKeyword, |
|
|
|
category: this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory |
|
|
|
}) |
|
|
|
.then(res => { |
|
|
|
wx.hideLoading(); |
|
|
|
|
|
|
|
if (res.success && res.products) { |
|
|
|
const totalGoods = res.total || 0; |
|
|
|
const totalPages = res.totalPages || Math.ceil(totalGoods / this.data.pageSize); |
|
|
|
const hasMoreData = currentPage < totalPages && res.products.length > 0; |
|
|
|
|
|
|
|
this.setData({ hasMoreData }) |
|
|
|
this.processGoodsData(res.products, isLoadMore) |
|
|
|
} else { |
|
|
|
this.setData({ loadingMore: false, isLoading: false }) |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch(err => { |
|
|
|
console.error('加载商品数据失败:', err) |
|
|
|
this.setData({ |
|
|
|
loadingMore: false |
|
|
|
}) |
|
|
|
this.setData({ loadingMore: false, isLoading: false }) |
|
|
|
}) |
|
|
|
}, |
|
|
|
|
|
|
|
@ -385,62 +411,38 @@ Page({ |
|
|
|
applyFilters: function(goods) { |
|
|
|
let filtered = [...goods] |
|
|
|
|
|
|
|
// 按品种筛选
|
|
|
|
if (this.data.selectedCategory !== '全部') { |
|
|
|
const category = this.data.selectedCategory |
|
|
|
let keyword = category |
|
|
|
if (category === '粉壳') keyword = '粉' |
|
|
|
else if (category === '绿壳') keyword = '绿' |
|
|
|
else if (category === '红壳') keyword = '红' |
|
|
|
else if (category === '白壳') keyword = '白' |
|
|
|
|
|
|
|
// 根据品种确定对应的关键字
|
|
|
|
if (category === '粉壳') { |
|
|
|
keyword = '粉' |
|
|
|
} else if (category === '绿壳') { |
|
|
|
keyword = '绿' |
|
|
|
} else if (category === '红壳') { |
|
|
|
keyword = '红' |
|
|
|
} else if (category === '白壳') { |
|
|
|
keyword = '白' |
|
|
|
filtered = filtered.filter(item => (item.name || '').includes(keyword)) |
|
|
|
} |
|
|
|
|
|
|
|
filtered = filtered.filter(item => { |
|
|
|
const name = item.name || '' |
|
|
|
return name.includes(keyword) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
// 按搜索关键词筛选(商品名称和地区都要匹配)
|
|
|
|
if (this.data.searchKeyword) { |
|
|
|
const keyword = this.data.searchKeyword.toLowerCase() |
|
|
|
filtered = filtered.filter(item => { |
|
|
|
const name = item.name || '' |
|
|
|
const region = item.region || '' |
|
|
|
return name.toLowerCase().includes(keyword) || region.toLowerCase().includes(keyword) |
|
|
|
}) |
|
|
|
filtered = filtered.filter(item => |
|
|
|
(item.name || '').toLowerCase().includes(keyword) || |
|
|
|
(item.region || '').toLowerCase().includes(keyword) |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
// 按地区筛选
|
|
|
|
if (this.data.selectedRegion !== '全国') { |
|
|
|
filtered = filtered.filter(item => { |
|
|
|
return item.region === this.data.selectedRegion |
|
|
|
}) |
|
|
|
filtered = filtered.filter(item => item.region === this.data.selectedRegion) |
|
|
|
} |
|
|
|
|
|
|
|
// 优先排序:按收藏人数、价格、创建时间排序
|
|
|
|
filtered.sort((a, b) => { |
|
|
|
// 首先按收藏人数降序排序
|
|
|
|
const reservedCountA = a.reservedCount || 0 |
|
|
|
const reservedCountB = b.reservedCount || 0 |
|
|
|
if (reservedCountB !== reservedCountA) { |
|
|
|
return reservedCountB - reservedCountA |
|
|
|
} |
|
|
|
if (reservedCountB !== reservedCountA) return reservedCountB - reservedCountA |
|
|
|
|
|
|
|
// 然后按价格升序排序
|
|
|
|
const priceA = parseFloat(a.price || 0) |
|
|
|
const priceB = parseFloat(b.price || 0) |
|
|
|
if (!isNaN(priceB) && !isNaN(priceA) && priceA !== priceB) { |
|
|
|
return priceA - priceB |
|
|
|
} |
|
|
|
if (!isNaN(priceB) && !isNaN(priceA) && priceA !== priceB) return priceA - priceB |
|
|
|
|
|
|
|
// 最后按创建时间降序排序
|
|
|
|
const createdAtA = new Date(a.createdAt || 0).getTime() |
|
|
|
const createdAtB = new Date(b.createdAt || 0).getTime() |
|
|
|
return createdAtB - createdAtA |
|
|
|
@ -449,6 +451,35 @@ Page({ |
|
|
|
return filtered |
|
|
|
}, |
|
|
|
|
|
|
|
// 瀑布流布局 - 淘宝风格左右交替
|
|
|
|
distributeToColumns: function(goods) { |
|
|
|
if (!goods || goods.length === 0) { |
|
|
|
return { leftColumnGoods: [], rightColumnGoods: [] } |
|
|
|
} |
|
|
|
|
|
|
|
const leftColumn = [] |
|
|
|
const rightColumn = [] |
|
|
|
|
|
|
|
for (let i = 0; i < goods.length; i += 2) { |
|
|
|
const currentRow = Math.floor(i / 2) |
|
|
|
const isEvenRow = currentRow % 2 === 0 |
|
|
|
|
|
|
|
if (i < goods.length) { |
|
|
|
leftColumn.push({ ...goods[i], isLong: isEvenRow }) |
|
|
|
} |
|
|
|
|
|
|
|
if (i + 1 < goods.length) { |
|
|
|
rightColumn.push({ ...goods[i + 1], isLong: !isEvenRow }) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return { leftColumnGoods: leftColumn, rightColumnGoods: rightColumn } |
|
|
|
}, |
|
|
|
}) |
|
|
|
|
|
|
|
return filtered |
|
|
|
}, |
|
|
|
|
|
|
|
// 分组商品用于交错布局(左长右短,左短右长交替)
|
|
|
|
groupGoodsForStaggeredLayout: function(goods) { |
|
|
|
if (!goods || goods.length === 0) { |
|
|
|
|