goods-list.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view class="goods-list">
  3. <tui-skeleton v-if="isRequest" :loadingType="3" :isLoading="true"></tui-skeleton>
  4. <view class="sticky-top">
  5. <!-- 搜索框 -->
  6. <cm-simple-search @search="onSearch" @cancel="onCancel" v-model="listQuery.name"></cm-simple-search>
  7. <!-- 筛选 -->
  8. <goods-filter @filter="onFilter"></goods-filter>
  9. </view>
  10. <!-- 商品列表 -->
  11. <view class="product-list">
  12. <view class="product-item" v-for="(product, index) in productList" :key="index">
  13. <cm-product :data="product"></cm-product>
  14. </view>
  15. </view>
  16. <!-- 加载更多 -->
  17. <cm-loadmore :hasNextPage="hasNextPage" :isLoading="isLoading" :visiable="!hideLoadmore"></cm-loadmore>
  18. <!-- 回到顶部 -->
  19. <tui-scroll-top :scrollTop="scrollTop" :bottom="30" :duration="600"></tui-scroll-top>
  20. <!-- 安全区域 -->
  21. <cm-safe-area-bottom v-if="hideLoadmore"></cm-safe-area-bottom>
  22. </view>
  23. </template>
  24. <script>
  25. import { fetchProductList } from '@/services/api/goods.js'
  26. import { debounce } from '@/common/utils.js'
  27. import { mapGetters } from 'vuex'
  28. import { makeQuery } from '@/pages/views/goods/config/config.js'
  29. export default {
  30. data() {
  31. return {
  32. isRequest: true,
  33. scrollTop: 0,
  34. listQuery: makeQuery(),
  35. navInfo: {},
  36. productList: [],
  37. hasNextPage: true,
  38. total: 0,
  39. isLoading: false
  40. }
  41. },
  42. computed: {
  43. ...mapGetters(['userId']),
  44. hideLoadmore() {
  45. return this.isRequest || this.productList.length <= this.listQuery.pageSize
  46. }
  47. },
  48. onPageScroll(e) {
  49. this.scrollTop = e.scrollTop
  50. },
  51. onReachBottom() {
  52. this.loadMore()
  53. },
  54. onLoad() {
  55. this.initQueryList()
  56. },
  57. methods: {
  58. // 搜索商品
  59. onSearch() {
  60. this.filterProductList()
  61. },
  62. // 取消搜索
  63. onCancel() {
  64. this.listQuery.name = ''
  65. this.filterProductList()
  66. },
  67. // 筛选商品
  68. onFilter(e) {
  69. const { current, sort } = e
  70. if (current === 0) {
  71. // 综合排序
  72. this.listQuery.sortType = 1
  73. } else if (current === 1) {
  74. // 价格高低
  75. this.listQuery.sortType = sort === 'desc' ? 3 : 2
  76. } else {
  77. // 最新上架
  78. this.listQuery.sortType = 4
  79. }
  80. this.filterProductList()
  81. },
  82. // 初始化查询参数
  83. initQueryList() {
  84. this.listQuery.userId = this.userId
  85. this.navInfo = this.$getStorage('NAVBAR')
  86. if (this.navInfo.type === 'navbar') {
  87. // 从金刚区菜单进入
  88. this.listQuery.homeTypeId = this.navInfo.id
  89. this.listQuery.listType = 2
  90. } else if (this.navInfo.type === 'floor') {
  91. // 从楼层进入
  92. this.listQuery.homeFloorId = this.navInfo.id
  93. this.listQuery.listType = 3
  94. } else if (this.navInfo.type === 'second') {
  95. // 从二级菜单进入
  96. this.listQuery.smallTypeId = this.navInfo.id
  97. this.listQuery.listType = 4
  98. }
  99. this.fetchProductList()
  100. },
  101. // 加载更多
  102. loadMore() {
  103. if (!this.hasNextPage) return
  104. this.isLoading = true
  105. this.fetchProductList()
  106. },
  107. // 搜索商品
  108. filterProductList() {
  109. this.productList = []
  110. this.listQuery.pageNum = 1
  111. this.fetchProductList()
  112. },
  113. // 获取商品列表
  114. fetchProductList: debounce(async function() {
  115. try {
  116. if (this.listQuery.sortType === 1) {
  117. this.listQuery.productIds = this.productList.map(item => item.productId).join(',')
  118. } else {
  119. this.listQuery.productIds = ''
  120. }
  121. const { data } = await fetchProductList(this.listQuery)
  122. this.productList = [...this.productList, ...data.pageInfo.results]
  123. this.hasNextPage = data.pageInfo.hasNextPage
  124. this.total = data.pageInfo.totalRecord
  125. this.listQuery.pageNum++
  126. this.isRequest = false
  127. this.isLoading = false
  128. } catch (e) {
  129. console.log('获取商品列表失败')
  130. }
  131. }, 200)
  132. }
  133. }
  134. </script>
  135. <style lang="scss" scoped>
  136. .goods-list {
  137. box-sizing: border-box;
  138. min-height: 100vh;
  139. .product-list {
  140. display: grid;
  141. grid-template-columns: repeat(3, 1fr);
  142. grid-row-gap: 16rpx;
  143. grid-column-gap: 16rpx;
  144. padding: 16rpx 24rpx;
  145. }
  146. }
  147. </style>