home.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view>
  3. <!-- 轮播图区域 -->
  4. <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
  5. <!-- 循环渲染轮播图的 item 项 -->
  6. <swiper-item v-for="(item, i) in swiperList" :key="i">
  7. <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
  8. <!-- 动态绑定图片的 src 属性 -->
  9. <image :src="item.image_src"></image>
  10. </navigator>
  11. </swiper-item>
  12. </swiper>
  13. <!-- 分类导航区域 -->
  14. <view class="nav-list">
  15. <view class="nav-item" v-for="(item, i) in navList" :key="i" @click="navClickHandler(item)">
  16. <image :src="item.image_src" class="nav-img"></image>
  17. </view>
  18. </view>
  19. <!-- 楼层区域 -->
  20. <view class="floor-list">
  21. <!-- 楼层 item 项 -->
  22. <view class="floor-item" v-for="(item, i) in floorList" :key="i">
  23. <!-- 楼层标题 -->
  24. <image :src="item.floor_title.image_src" class="floor-title"></image>
  25. <!-- 楼层图片区域 -->
  26. <view class="floor-img-box">
  27. <!-- 左侧大图片的盒子 -->
  28. <view class="left-img-box">
  29. <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
  30. </view>
  31. <!-- 右侧 4 个小图片的盒子 -->
  32. <view class="right-img-box">
  33. <navigator class="right-img-item" v-for="(item2, i2) in item.product_list" :key="i2" v-if="i2 !== 0" :url="item2.url">
  34. <image :src="item2.image_src" mode="widthFix" :style="{width:item2.image_width + 'rpx'}"></image>
  35. </navigator>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. export default {
  44. data() {
  45. return {
  46. // 1. 轮播图的数据列表,默认为空数组
  47. swiperList: [],
  48. // 1. 分类导航的数据列表
  49. navList: [],
  50. // 1. 楼层的数据列表
  51. floorList: [],
  52. }
  53. },
  54. onLoad() {
  55. // 2. 在小程序页面刚加载的时候,调用获取轮播图数据的方法
  56. this.getSwiperList(),
  57. // 分类导航
  58. this.getNavList(),
  59. // 2. 在 onLoad 中调用获取楼层数据的方法
  60. this.getFloorList()
  61. },
  62. methods: {
  63. // 3. 获取轮播图数据的方法
  64. async getSwiperList() {
  65. // 3.1 发起请求
  66. const { data: res } = await uni.$http.get('/api/public/v1/home/swiperdata')
  67. // 3.2 请求失败
  68. if (res.meta.status !== 200) return uni.$showMsg()
  69. // 3.3 请求成功,为 data 中的数据赋值
  70. this.swiperList = res.message
  71. },
  72. async getNavList() {
  73. const { data: res } = await uni.$http.get('/api/public/v1/home/catitems')
  74. if (res.meta.status !== 200) return uni.$showMsg()
  75. this.navList = res.message
  76. },
  77. // nav-item 项被点击时候的事件处理函数
  78. navClickHandler(item) {
  79. // 判断点击的是哪个 nav
  80. if (item.name === '分类') {
  81. uni.switchTab({
  82. url: '/pages/cate/cate'
  83. })
  84. }
  85. },
  86. // 3. 定义获取楼层列表数据的方法
  87. async getFloorList() {
  88. const { data: res } = await uni.$http.get('/api/public/v1/home/floordata')
  89. if (res.meta.status !== 200) return uni.$showMsg()
  90. // 通过双层 forEach 循环,处理 URL 地址
  91. res.message.forEach(floor => {
  92. floor.product_list.forEach(prod => {
  93. prod.url = '/subpkg/goods_list/goods_list?' +
  94. prod.navigator_url.split('?')[1]
  95. })
  96. })
  97. this.floorList = res.message
  98. }
  99. },
  100. }
  101. </script>
  102. <style lang="scss">
  103. swiper {
  104. height: 330rpx;
  105. .swiper-item,
  106. image {
  107. width: 100%;
  108. height: 100%;
  109. }
  110. }
  111. .nav-list {
  112. display: flex;
  113. justify-content: space-around;
  114. margin: 15px 0;
  115. .nav-img {
  116. width: 128rpx;
  117. height: 140rpx;
  118. }
  119. }
  120. .floor-title {
  121. height: 60rpx;
  122. width: 100%;
  123. display: flex;
  124. }
  125. .right-img-box {
  126. display: flex;
  127. flex-wrap: wrap;
  128. justify-content: space-around;
  129. }
  130. .floor-img-box {
  131. display: flex;
  132. padding-left: 10rpx;
  133. }
  134. </style>