goods-list.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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="keyword"></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="product.productId">
  13. <cm-product :data="product"></cm-product>
  14. </view>
  15. </view>
  16. <!-- 加载更多 -->
  17. <cm-loadmore :hasNextPage="hasNextPage" :isLoading="isLoading" :visiable="!isRequest"></cm-loadmore>
  18. <!-- 回到顶部 -->
  19. <tui-scroll-top :scrollTop="scrollTop" :bottom="30" :duration="600"></tui-scroll-top>
  20. </view>
  21. </template>
  22. <script>
  23. import { fetchProductList } from '@/services/api/goods.js'
  24. import { debounce } from '@/common/utils.js'
  25. import { mapGetters } from 'vuex'
  26. import { makeQuery } from '@/pages/views/goods/config/config.js'
  27. export default {
  28. data() {
  29. return {
  30. isRequest: true,
  31. scrollTop: 0,
  32. keyword: '',
  33. listQuery: makeQuery(),
  34. navInfo: {},
  35. productList: [],
  36. hasNextPage: true,
  37. total: 0,
  38. isLoading: false
  39. }
  40. },
  41. computed: {
  42. ...mapGetters(['userId'])
  43. },
  44. onPageScroll(e) {
  45. this.scrollTop = e.scrollTop
  46. },
  47. onReachBottom() {
  48. this.loadMore()
  49. },
  50. onLoad() {
  51. this.initQueryList()
  52. },
  53. methods: {
  54. onSearch() {},
  55. onCancel() {},
  56. onFilter(e) {
  57. console.log(e)
  58. },
  59. // 初始化查询参数
  60. initQueryList() {
  61. this.listQuery.userId = this.userId
  62. this.navInfo = this.$getStorage('NAVBAR')
  63. if (this.navInfo.type === 'navbar') {
  64. // 从金刚区菜单进入
  65. this.listQuery.homeTypeId = this.navInfo.id
  66. this.listType = 2
  67. } else if (this.navInfo.type === 'floor') {
  68. // 从楼层进入
  69. this.homeFloorId = this.navInfo.id
  70. this.listType = 3
  71. } else if (this.navInfo.type === 'second') {
  72. // 从二级菜单进入
  73. this.smallTypeId = this.navInfo.id
  74. }
  75. this.fetchProductList()
  76. },
  77. // 加载更多
  78. loadMore() {
  79. if (!this.hasNextPage) return
  80. this.fetchProductList()
  81. },
  82. // 获取商品列表
  83. fetchProductList: debounce(async function() {
  84. try {
  85. const resultProductData = await fetchProductList(this.listQuery)
  86. this.productList = [...this.productList, ...resultProductData.data.results]
  87. this.hasNextPage = resultProductData.data.hasNextPage
  88. this.total = resultProductData.data.totalRecord
  89. this.listQuery.pageNum++
  90. this.isRequest = false
  91. } catch (e) {
  92. console.log('获取商品列表失败')
  93. }
  94. }, 200)
  95. }
  96. }
  97. </script>
  98. <style lang="scss" scoped>
  99. .goods-list {
  100. box-sizing: border-box;
  101. min-height: 100vh;
  102. .product-list {
  103. // @extend .cm-flex-between;
  104. // flex-wrap: wrap;
  105. display: grid;
  106. grid-template-columns: repeat(3, 1fr);
  107. // grid-template-rows: repeat(2, 1fr);
  108. grid-row-gap: 12rpx;
  109. padding: 0 24rpx;
  110. .product-item {
  111. width: 224rpx;
  112. }
  113. }
  114. }
  115. </style>