Page({ data: { keyword: '', sortType: 'default', sortOrder: 'desc', goodsList: [], pageNum: 1, pageSize: 10, isLoading: false, hasMore: true, isRefreshing: false }, onLoad(options) { if (options.keyword) { this.setData({ keyword: decodeURIComponent(options.keyword) }, () => { this.loadGoodsList(true) }) } }, // 切换排序方式 onSortChange(e) { const newType = e.currentTarget.dataset.type let newOrder = 'desc' if (newType === this.data.sortType) { newOrder = this.data.sortOrder === 'desc' ? 'asc' : 'desc' } this.setData({ sortType: newType, sortOrder: newOrder, goodsList: [], pageNum: 1, hasMore: true }, () => { this.loadGoodsList(true) }) }, // 加载商品列表 async loadGoodsList(isRefresh = false) { if (this.data.isLoading || (!this.data.hasMore && !isRefresh)) return this.setData({ isLoading: true }) try { // 模拟API延迟 await new Promise(resolve => setTimeout(resolve, 500)) // 模拟搜索逻辑 let filteredProducts = this.mockProducts.filter(item => item.title.toLowerCase().includes(this.data.keyword.toLowerCase()) ) // 根据排序类型和顺序处理数据 if (this.data.sortType !== 'default') { filteredProducts.sort((a, b) => { const factor = this.data.sortOrder === 'asc' ? 1 : -1 const aValue = parseFloat(a[this.data.sortType === 'sales' ? 'sales' : this.data.sortType]) const bValue = parseFloat(b[this.data.sortType === 'sales' ? 'sales' : this.data.sortType]) return (aValue - bValue) * factor }) } // 模拟分页 const start = (this.data.pageNum - 1) * this.data.pageSize const end = start + this.data.pageSize const pageData = filteredProducts.slice(start, end) this.setData({ goodsList: isRefresh ? pageData : [...this.data.goodsList, ...pageData], pageNum: this.data.pageNum + 1, hasMore: end < filteredProducts.length }) } catch (error) { wx.showToast({ title: '加载失败', icon: 'none' }) } finally { this.setData({ isLoading: false, isRefreshing: false }) } }, // 下拉刷新 onRefresh() { this.setData({ isRefreshing: true, pageNum: 1, hasMore: true }, () => { this.loadGoodsList(true) }) }, // 加载更多 loadMore() { this.loadGoodsList() }, // 跳转到搜索页 navigateToSearch() { wx.navigateBack() }, // 跳转到商品详情 goToDetail(e) { const { id } = e.currentTarget.dataset wx.navigateTo({ url: `/pages/goods/detail?id=${id}` }) }, // 模拟商品数据 mockProducts: [ { id: '1', title: 'Apple iPhone 14 Pro Max 256GB 暗紫色 5G手机', price: '9299.00', commission: '186.00', sales: 8526, image: '/static/images/goods/iphone.png' }, { id: '2', title: '华为 Mate60 Pro 12+512GB 雅川青 5G手机', price: '8999.00', commission: '180.00', sales: 12653, image: '/static/images/goods/huawei.png' }, { id: '3', title: '小米14 Pro 16+1TB 钛金属黑 徕卡光学', price: '5999.00', commission: '120.00', sales: 6832, image: '/static/images/goods/xiaomi.png' }, { id: '4', title: 'OPPO Find X7 Ultra 16+512GB 微晶玉 生态旗舰', price: '6999.00', commission: '140.00', sales: 4521, image: '/static/images/goods/oppo.png' }, { id: '5', title: 'vivo X100 Pro+ 16+512GB 墨羽 蔡司影像', price: '6499.00', commission: '130.00', sales: 5643, image: '/static/images/goods/vivo.png' }, { id: '6', title: 'Apple MacBook Pro 14 M3 Pro 18G 1TB', price: '18999.00', commission: '380.00', sales: 2341, image: '/static/images/goods/macbook.png' }, { id: '7', title: '华为 MateBook X Pro 13.9英寸 i9 32G 2TB', price: '13999.00', commission: '280.00', sales: 1876, image: '/static/images/goods/matebook.png' }, { id: '8', title: '小米 RedmiBook Pro 16 R7 32G 1TB', price: '6499.00', commission: '130.00', sales: 3654, image: '/static/images/goods/redmibook.png' } ] })