You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
store_node/miniprogram/pages/secondhand/index.js

179 lines
4.2 KiB

Page({
data: {
currentCategory: 'all',
goodsList: [],
pageNum: 1,
pageSize: 10,
hasMoreGoods: true,
isLoading: false
},
onLoad: function (options) {
console.log('二手超市页面加载');
this.loadGoodsList(true);
},
onPullDownRefresh: function () {
console.log('下拉刷新');
this.setData({
pageNum: 1,
hasMoreGoods: true
});
this.loadGoodsList(true);
},
switchCategory: function (e) {
const category = e.currentTarget.dataset.category;
console.log('切换分类:', category);
this.setData({
currentCategory: category,
pageNum: 1,
goodsList: [],
hasMoreGoods: true
});
this.loadGoodsList(true);
},
searchGoods: function (e) {
const keyword = e.detail.value;
if (!keyword.trim()) {
return;
}
console.log('搜索商品:', keyword);
this.setData({
pageNum: 1,
goodsList: [],
hasMoreGoods: true
});
this.loadGoodsList(true, keyword);
},
loadGoodsList: function (refresh, keyword) {
refresh = refresh || false;
keyword = keyword || '';
console.log('加载商品列表, 刷新:', refresh, '关键词:', keyword);
// 模拟商品数据
const mockGoods = [
{
id: 1,
title: 'iPad Pro 2021 二手95新',
price: 4500,
condition: '95新',
images: ['/images/ipad.jpg'],
publishTime: '3小时前',
category: 'electronics'
},
{
id: 2,
title: '微积分教材 同济第七版',
price: 20,
condition: '8成新',
images: ['/images/book.jpg'],
publishTime: '1天前',
category: 'books'
},
{
id: 3,
title: '耐克运动鞋 Air Max 270',
price: 350,
condition: '9成新',
images: ['/images/shoes.jpg'],
publishTime: '2天前',
category: 'clothes'
},
{
id: 4,
title: '宿舍小桌子 可折叠',
price: 50,
condition: '全新',
images: ['/images/table.jpg'],
publishTime: '3天前',
category: 'daily'
},
{
id: 5,
title: '自行车 捷安特 ATX',
price: 800,
condition: '8成新',
images: ['/images/bike.jpg'],
publishTime: '5天前',
category: 'others'
},
{
id: 6,
title: 'AirPods Pro 无线耳机',
price: 850,
condition: '9成新',
images: ['/images/airpods.jpg'],
publishTime: '1周前',
category: 'electronics'
}
];
// 根据分类和关键词筛选商品
var filteredGoods = mockGoods;
var self = this;
if (this.data.currentCategory !== 'all') {
filteredGoods = mockGoods.filter(function(item) {
return item.category === self.data.currentCategory;
});
}
if (keyword) {
filteredGoods = filteredGoods.filter(function(item) {
return item.title.toLowerCase().indexOf(keyword.toLowerCase()) !== -1;
});
}
// 模拟分页
var start = (this.data.pageNum - 1) * this.data.pageSize;
var end = start + this.data.pageSize;
var pageGoods = filteredGoods.slice(start, end);
// 更新数据
if (refresh) {
this.setData({
goodsList: pageGoods,
hasMoreGoods: pageGoods.length === this.data.pageSize,
pageNum: this.data.pageNum + 1
});
wx.stopPullDownRefresh();
} else {
this.setData({
goodsList: this.data.goodsList.concat(pageGoods),
hasMoreGoods: pageGoods.length === this.data.pageSize,
pageNum: this.data.pageNum + 1
});
}
},
loadMore: function () {
console.log('加载更多商品');
if (this.data.hasMoreGoods) {
this.loadGoodsList();
}
},
navigateToDetail: function (e) {
var goodsId = e.currentTarget.dataset.id;
wx.navigateTo({
url: '/pages/secondhand/detail?id=' + goodsId
});
},
navigateToPublish: function () {
wx.navigateTo({
url: '/pages/secondhand/publish'
});
},
// 处理图片加载错误
handleImageError: function() {
console.log('图片加载错误处理已注册');
}
})