goods-coupon-list.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.productName"></cm-simple-search>
  7. </view>
  8. <simple-safe-view>
  9. <!-- 商品列表 -->
  10. <view class="product-list">
  11. <view class="product-item" v-for="(product, index) in productList" :key="product.productId">
  12. <cm-product :data="product"></cm-product>
  13. </view>
  14. </view>
  15. <!-- 加载更多 -->
  16. <cm-loadmore :hasNextPage="hasNextPage" :isLoading="isLoading" :visiable="!hideLoadmore"></cm-loadmore>
  17. </simple-safe-view>
  18. <!-- 购物车 -->
  19. <cm-drag :cartNum="kindCount" @btnClick="toCart"></cm-drag>
  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. listQuery: makeQuery(),
  32. navInfo: {},
  33. productList: [],
  34. hasNextPage: true,
  35. total: 0,
  36. isLoading: false
  37. }
  38. },
  39. computed: {
  40. ...mapGetters(['userId', 'kindCount']),
  41. hideLoadmore() {
  42. return this.isRequest || this.productList.length <= this.listQuery.pageSize
  43. }
  44. },
  45. onReachBottom() {
  46. this.loadMore()
  47. },
  48. onLoad(options) {
  49. this.initQueryList(options)
  50. },
  51. methods: {
  52. // 跳转到购物车
  53. toCart() {
  54. this.$router.redirectTo('cart/cart')
  55. },
  56. // 搜索商品
  57. onSearch() {
  58. this.filterProductList()
  59. },
  60. // 取消搜索
  61. onCancel() {
  62. this.listQuery.name = ''
  63. this.filterProductList()
  64. },
  65. // 初始化查询参数
  66. initQueryList(options) {
  67. this.listQuery.userId = this.userId
  68. this.listQuery.couponId = options.couponId
  69. this.listQuery.listType = 5
  70. this.fetchProductList()
  71. },
  72. // 加载更多
  73. loadMore() {
  74. if (!this.hasNextPage) return
  75. this.isLoading = true
  76. this.fetchProductList()
  77. },
  78. // 搜索商品
  79. filterProductList() {
  80. this.productList = []
  81. this.listQuery.pageNum = 1
  82. this.fetchProductList()
  83. },
  84. // 获取商品列表
  85. fetchProductList: debounce(async function() {
  86. try {
  87. const { data } = await fetchProductList(this.listQuery)
  88. this.productList = [...this.productList, ...data.pageInfo.results]
  89. this.hasNextPage = data.pageInfo.hasNextPage
  90. this.total = data.pageInfo.totalRecord
  91. this.listQuery.pageNum++
  92. this.isRequest = false
  93. this.isLoading = false
  94. } catch (e) {
  95. console.log(e)
  96. console.log('获取商品列表失败')
  97. }
  98. }, 200)
  99. }
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. .goods-list {
  104. box-sizing: border-box;
  105. min-height: 100vh;
  106. .product-list {
  107. display: grid;
  108. grid-template-columns: repeat(3, 1fr);
  109. grid-row-gap: 16rpx;
  110. grid-column-gap: 16rpx;
  111. padding: 16rpx 24rpx;
  112. }
  113. }
  114. </style>