123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- const app = getApp()
- Page({
- data: {
- categoryId: null,
- sortTypes: [
- { label: '综合', value: 'default' },
- { label: '价格', value: 'price' },
- { label: '销量', value: 'sales' },
- { label: '佣金', value: 'commission' }
- ],
- currentSort: 'default',
- goodsList: [],
- page: 1,
- pageSize: 10,
- loading: false,
- hasMore: true
- },
- onLoad(options) {
- const { id } = options
- if (id) {
- this.setData({ categoryId: id })
- wx.setNavigationBarTitle({
- title: options.name || '商品分类'
- })
- }
- this.loadGoodsList()
- },
- // 切换排序
- changeSort(e) {
- const { sort } = e.currentTarget.dataset
- if (this.data.currentSort === sort) return
- this.setData({
- currentSort: sort,
- goodsList: [],
- page: 1,
- hasMore: true
- }, () => {
- this.loadGoodsList()
- })
- },
- // 加载商品列表
- loadGoodsList() {
- if (this.data.loading || !this.data.hasMore) return
-
- this.setData({ loading: true })
-
- // TODO: 调用后端API获取商品列表
- setTimeout(() => {
- const mockGoods = [{
- id: 1,
- name: 'iPhone 14 Pro Max',
- price: '9299.00',
- commission: '186.00',
- image: '/static/images/goods/iphone.png'
- }, {
- id: 2,
- name: '华为 Mate60 Pro',
- price: '8999.00',
- commission: '180.00',
- image: '/static/images/goods/huawei.png'
- }, {
- id: 3,
- name: '小米14 Pro',
- price: '5999.00',
- commission: '120.00',
- image: '/static/images/goods/xiaomi.png'
- }]
- // 根据排序方式处理数据
- let sortedGoods = [...mockGoods]
- switch (this.data.currentSort) {
- case 'price':
- sortedGoods.sort((a, b) => parseFloat(a.price) - parseFloat(b.price))
- break
- case 'commission':
- sortedGoods.sort((a, b) => parseFloat(b.commission) - parseFloat(a.commission))
- break
- case 'sales':
- // 真实场景需要后端返回销量数据
- break
- }
- this.setData({
- goodsList: this.data.page === 1 ? sortedGoods : [...this.data.goodsList, ...sortedGoods],
- loading: false,
- hasMore: sortedGoods.length === this.data.pageSize,
- page: this.data.page + 1
- })
- }, 500)
- },
- // 跳转商品详情
- navigateToDetail(e) {
- const { id } = e.currentTarget.dataset
- wx.navigateTo({
- url: `/pages/goods/detail?id=${id}`
- })
- },
- // 下拉刷新
- onPullDownRefresh() {
- this.setData({
- goodsList: [],
- page: 1,
- hasMore: true
- }, () => {
- this.loadGoodsList()
- wx.stopPullDownRefresh()
- })
- },
- // 触底加载更多
- onReachBottom() {
- this.loadGoodsList()
- }
- })
|