微信小店联盟带货小程序

index.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const app = getApp()
  2. Page({
  3. data: {
  4. categoryId: null,
  5. sortTypes: [
  6. { label: '综合', value: 'default' },
  7. { label: '价格', value: 'price' },
  8. { label: '销量', value: 'sales' },
  9. { label: '佣金', value: 'commission' }
  10. ],
  11. currentSort: 'default',
  12. goodsList: [],
  13. page: 1,
  14. pageSize: 10,
  15. loading: false,
  16. hasMore: true
  17. },
  18. onLoad(options) {
  19. const { id } = options
  20. if (id) {
  21. this.setData({ categoryId: id })
  22. wx.setNavigationBarTitle({
  23. title: options.name || '商品分类'
  24. })
  25. }
  26. this.loadGoodsList()
  27. },
  28. // 切换排序
  29. changeSort(e) {
  30. const { sort } = e.currentTarget.dataset
  31. if (this.data.currentSort === sort) return
  32. this.setData({
  33. currentSort: sort,
  34. goodsList: [],
  35. page: 1,
  36. hasMore: true
  37. }, () => {
  38. this.loadGoodsList()
  39. })
  40. },
  41. // 加载商品列表
  42. loadGoodsList() {
  43. if (this.data.loading || !this.data.hasMore) return
  44. this.setData({ loading: true })
  45. // TODO: 调用后端API获取商品列表
  46. setTimeout(() => {
  47. const mockGoods = [{
  48. id: 1,
  49. name: 'iPhone 14 Pro Max',
  50. price: '9299.00',
  51. commission: '186.00',
  52. image: '/static/images/goods/iphone.png'
  53. }, {
  54. id: 2,
  55. name: '华为 Mate60 Pro',
  56. price: '8999.00',
  57. commission: '180.00',
  58. image: '/static/images/goods/huawei.png'
  59. }, {
  60. id: 3,
  61. name: '小米14 Pro',
  62. price: '5999.00',
  63. commission: '120.00',
  64. image: '/static/images/goods/xiaomi.png'
  65. }]
  66. // 根据排序方式处理数据
  67. let sortedGoods = [...mockGoods]
  68. switch (this.data.currentSort) {
  69. case 'price':
  70. sortedGoods.sort((a, b) => parseFloat(a.price) - parseFloat(b.price))
  71. break
  72. case 'commission':
  73. sortedGoods.sort((a, b) => parseFloat(b.commission) - parseFloat(a.commission))
  74. break
  75. case 'sales':
  76. // 真实场景需要后端返回销量数据
  77. break
  78. }
  79. this.setData({
  80. goodsList: this.data.page === 1 ? sortedGoods : [...this.data.goodsList, ...sortedGoods],
  81. loading: false,
  82. hasMore: sortedGoods.length === this.data.pageSize,
  83. page: this.data.page + 1
  84. })
  85. }, 500)
  86. },
  87. // 跳转商品详情
  88. navigateToDetail(e) {
  89. const { id } = e.currentTarget.dataset
  90. wx.navigateTo({
  91. url: `/pages/goods/detail?id=${id}`
  92. })
  93. },
  94. // 下拉刷新
  95. onPullDownRefresh() {
  96. this.setData({
  97. goodsList: [],
  98. page: 1,
  99. hasMore: true
  100. }, () => {
  101. this.loadGoodsList()
  102. wx.stopPullDownRefresh()
  103. })
  104. },
  105. // 触底加载更多
  106. onReachBottom() {
  107. this.loadGoodsList()
  108. }
  109. })