微信小店联盟带货小程序

result.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. Page({
  2. data: {
  3. keyword: '',
  4. sortType: 'default',
  5. sortOrder: 'desc',
  6. goodsList: [],
  7. pageNum: 1,
  8. pageSize: 10,
  9. isLoading: false,
  10. hasMore: true,
  11. isRefreshing: false
  12. },
  13. onLoad(options) {
  14. if (options.keyword) {
  15. this.setData({
  16. keyword: decodeURIComponent(options.keyword)
  17. }, () => {
  18. this.loadGoodsList(true)
  19. })
  20. }
  21. },
  22. // 切换排序方式
  23. onSortChange(e) {
  24. const newType = e.currentTarget.dataset.type
  25. let newOrder = 'desc'
  26. if (newType === this.data.sortType) {
  27. newOrder = this.data.sortOrder === 'desc' ? 'asc' : 'desc'
  28. }
  29. this.setData({
  30. sortType: newType,
  31. sortOrder: newOrder,
  32. goodsList: [],
  33. pageNum: 1,
  34. hasMore: true
  35. }, () => {
  36. this.loadGoodsList(true)
  37. })
  38. },
  39. // 加载商品列表
  40. async loadGoodsList(isRefresh = false) {
  41. if (this.data.isLoading || (!this.data.hasMore && !isRefresh)) return
  42. this.setData({ isLoading: true })
  43. try {
  44. // 模拟API延迟
  45. await new Promise(resolve => setTimeout(resolve, 500))
  46. // 模拟搜索逻辑
  47. let filteredProducts = this.mockProducts.filter(item =>
  48. item.title.toLowerCase().includes(this.data.keyword.toLowerCase())
  49. )
  50. // 根据排序类型和顺序处理数据
  51. if (this.data.sortType !== 'default') {
  52. filteredProducts.sort((a, b) => {
  53. const factor = this.data.sortOrder === 'asc' ? 1 : -1
  54. const aValue = parseFloat(a[this.data.sortType === 'sales' ? 'sales' : this.data.sortType])
  55. const bValue = parseFloat(b[this.data.sortType === 'sales' ? 'sales' : this.data.sortType])
  56. return (aValue - bValue) * factor
  57. })
  58. }
  59. // 模拟分页
  60. const start = (this.data.pageNum - 1) * this.data.pageSize
  61. const end = start + this.data.pageSize
  62. const pageData = filteredProducts.slice(start, end)
  63. this.setData({
  64. goodsList: isRefresh ? pageData : [...this.data.goodsList, ...pageData],
  65. pageNum: this.data.pageNum + 1,
  66. hasMore: end < filteredProducts.length
  67. })
  68. } catch (error) {
  69. wx.showToast({
  70. title: '加载失败',
  71. icon: 'none'
  72. })
  73. } finally {
  74. this.setData({
  75. isLoading: false,
  76. isRefreshing: false
  77. })
  78. }
  79. },
  80. // 下拉刷新
  81. onRefresh() {
  82. this.setData({
  83. isRefreshing: true,
  84. pageNum: 1,
  85. hasMore: true
  86. }, () => {
  87. this.loadGoodsList(true)
  88. })
  89. },
  90. // 加载更多
  91. loadMore() {
  92. this.loadGoodsList()
  93. },
  94. // 跳转到搜索页
  95. navigateToSearch() {
  96. wx.navigateBack()
  97. },
  98. // 跳转到商品详情
  99. goToDetail(e) {
  100. const { id } = e.currentTarget.dataset
  101. wx.navigateTo({
  102. url: `/pages/goods/detail?id=${id}`
  103. })
  104. },
  105. // 模拟商品数据
  106. mockProducts: [
  107. {
  108. id: '1',
  109. title: 'Apple iPhone 14 Pro Max 256GB 暗紫色 5G手机',
  110. price: '9299.00',
  111. commission: '186.00',
  112. sales: 8526,
  113. image: '/static/images/goods/iphone.png'
  114. },
  115. {
  116. id: '2',
  117. title: '华为 Mate60 Pro 12+512GB 雅川青 5G手机',
  118. price: '8999.00',
  119. commission: '180.00',
  120. sales: 12653,
  121. image: '/static/images/goods/huawei.png'
  122. },
  123. {
  124. id: '3',
  125. title: '小米14 Pro 16+1TB 钛金属黑 徕卡光学',
  126. price: '5999.00',
  127. commission: '120.00',
  128. sales: 6832,
  129. image: '/static/images/goods/xiaomi.png'
  130. },
  131. {
  132. id: '4',
  133. title: 'OPPO Find X7 Ultra 16+512GB 微晶玉 生态旗舰',
  134. price: '6999.00',
  135. commission: '140.00',
  136. sales: 4521,
  137. image: '/static/images/goods/oppo.png'
  138. },
  139. {
  140. id: '5',
  141. title: 'vivo X100 Pro+ 16+512GB 墨羽 蔡司影像',
  142. price: '6499.00',
  143. commission: '130.00',
  144. sales: 5643,
  145. image: '/static/images/goods/vivo.png'
  146. },
  147. {
  148. id: '6',
  149. title: 'Apple MacBook Pro 14 M3 Pro 18G 1TB',
  150. price: '18999.00',
  151. commission: '380.00',
  152. sales: 2341,
  153. image: '/static/images/goods/macbook.png'
  154. },
  155. {
  156. id: '7',
  157. title: '华为 MateBook X Pro 13.9英寸 i9 32G 2TB',
  158. price: '13999.00',
  159. commission: '280.00',
  160. sales: 1876,
  161. image: '/static/images/goods/matebook.png'
  162. },
  163. {
  164. id: '8',
  165. title: '小米 RedmiBook Pro 16 R7 32G 1TB',
  166. price: '6499.00',
  167. commission: '130.00',
  168. sales: 3654,
  169. image: '/static/images/goods/redmibook.png'
  170. }
  171. ]
  172. })