home.vue 4.2 KB

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