goods_list.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <view>
  3. <view class="goods-list">
  4. <block v-for="(items, i) in goodsList" :key="i" @click="gotoDetail(item)">
  5. <!-- 为 my-goods 组件动态绑定 goods 属性的值 -->
  6. <my-goods :goods="item"></my-goods>
  7. </block>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. // 请求参数对象
  16. queryObj: {
  17. // 查询关键词
  18. query: '',
  19. // 商品分类Id
  20. cid: '',
  21. // 页码值
  22. pagenum: 1,
  23. // 每页显示多少条数据
  24. pagesize: 10
  25. },
  26. // 商品列表的数据
  27. goodsList: [],
  28. // 总数量,用来实现分页
  29. total: 0,
  30. // 是否正在请求数据
  31. isloading: false
  32. };
  33. },
  34. onLoad(options) {
  35. // 将页面参数转存到 this.queryObj 对象中
  36. this.queryObj.query = options.query || ''
  37. this.queryObj.cid = options.cid || ''
  38. // 调用获取商品列表数据的方法
  39. this.getGoodsList()
  40. },
  41. methods: {
  42. // 获取商品列表数据的方法
  43. async getGoodsList(cb) {
  44. // ** 打开节流阀
  45. this.isloading = true
  46. // 发起请求
  47. const { data: res } = await
  48. uni.$http.get('/api/public/v1/goods/search', this.queryObj)
  49. // ** 关闭节流阀
  50. this.isloading = false
  51. // 只要数据请求完毕,就立即按需调用 cb 回调函数
  52. cb && cb()
  53. if (res.meta.status !== 200) return uni.$showMsg()
  54. // 为数据赋值:通过展开运算符的形式,进行新旧数据的拼接
  55. this.goodsList = [...this.goodsList, ...res.message.goods]
  56. this.total = res.message.total
  57. },
  58. // 触底的事件
  59. onReachBottom() {
  60. // 判断是否还有下一页数据
  61. if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total)
  62. return uni.$showMsg('数据加载完毕!')
  63. // 判断是否正在请求其它数据,如果是,则不发起额外的请求
  64. if (this.isloading) return
  65. // 让页码值自增 +1
  66. this.queryObj.pagenum += 1
  67. // 重新获取列表数据
  68. this.getGoodsList()
  69. },
  70. // 下拉刷新的事件
  71. onPullDownRefresh() {
  72. // 1. 重置关键数据
  73. this.queryObj.pagenum = 1
  74. this.total = 0
  75. this.isloading = false
  76. this.goodsList = []
  77. // 2. 重新发起请求
  78. this.getGoodsList(() => uni.stopPullDownRefresh())
  79. },
  80. // 点击跳转到商品详情页面
  81. gotoDetail(item) {
  82. uni.navigateTo({
  83. url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
  84. })
  85. }
  86. }
  87. }
  88. </script>
  89. <style lang="scss">
  90. </style>