cate.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <view>
  3. <my-search @click="gotoSearch"></my-search>
  4. <view class="scroll-view-container">
  5. <!-- 左侧的滚动视图区域 -->
  6. <scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
  7. <block v-for="(item, i) in cateList" :key="i">
  8. <view :class="['left-scroll-view-item', i === active ? 'active' : '']" @click="activeChanged(i)">
  9. {{item.cat_name}}</view>
  10. </block>
  11. </scroll-view>
  12. <!-- 右侧的滚动视图区域 -->
  13. <scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}" :scroll-top="scrollTop">
  14. <view class="cate-lv2" v-for="(item2, i2) in cateLevel2" :key="i2">
  15. <view class="cate-lv2-title">/ {{item2.cat_name}} /</view>
  16. <!-- 动态渲染三级分类的列表数据 -->
  17. <view class="cate-lv3-list">
  18. <!-- 三级分类 Item 项 -->
  19. <view class="cate-lv3-item" v-for="(item3, i3) in item2.children"
  20. :key="i3" @click="gotoGoodsList(item3)">
  21. <!-- 图片 -->
  22. <image :src="item3.cat_icon"></image>
  23. <!-- 文本 -->
  24. <text>{{item3.cat_name}}</text>
  25. </view>
  26. </view>
  27. </view>
  28. </scroll-view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. // 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar 高度
  37. wh: 0,
  38. // 分类数据列表
  39. cateList: [],
  40. // 当前选中项的索引,默认让第一项被选中
  41. active: 0,
  42. // 二级分类列表
  43. cateLevel2: [],
  44. // 滚动条距离顶部的距离
  45. scrollTop: 0
  46. };
  47. },
  48. onLoad() {
  49. // 获取当前系统的信息
  50. const sysInfo = uni.getSystemInfoSync()
  51. // 为 wh 窗口可用高度动态赋值
  52. // 可用高度 = 屏幕高度 - navigationBar高度 - tabBar高度 - 自定义的search组件高
  53. this.wh = sysInfo.windowHeight - 50
  54. // 调用获取分类列表数据的方法
  55. this.getCateList()
  56. },
  57. methods: {
  58. async getCateList() {
  59. // 发起请求
  60. const { data: res } = await uni.$http.get('/api/public/v1/categories')
  61. // 判断是否获取失败
  62. if (res.meta.status !== 200) return uni.$showMsg()
  63. // 转存数据
  64. this.cateList = res.message
  65. // 为二级分类赋值
  66. this.cateLevel2 = res.message[0].children
  67. },
  68. // 选中项改变的事件处理函数
  69. activeChanged(i) {
  70. this.active = i
  71. // 为二级分类列表重新赋值
  72. this.cateLevel2 = this.cateList[i].children
  73. // 让 scrollTop 的值在 0 与 1 之间切换
  74. this.scrollTop = this.scrollTop === 0 ? 1 : 0
  75. },
  76. // 点击三级分类项跳转到商品列表页面
  77. gotoGoodsList(item3) {
  78. uni.navigateTo({
  79. url: '/subpkg/goods_list/goods_list?cid=' + item3.cat_id
  80. })
  81. },
  82. // 跳转到分包中的搜索页面
  83. gotoSearch() {
  84. uni.navigateTo({
  85. url: '/subpkg/search/search'
  86. })
  87. }
  88. }
  89. }
  90. </script>
  91. <style lang="scss">
  92. .scroll-view-container {
  93. display: flex;
  94. .left-scroll-view {
  95. width: 120px;
  96. .left-scroll-view-item {
  97. line-height: 60px;
  98. background-color: #f7f7f7;
  99. text-align: center;
  100. font-size: 12px;
  101. // 激活项的样式
  102. &.active {
  103. background-color: #ffffff;
  104. position: relative;
  105. // 渲染激活项左侧的红色指示边线
  106. &::before {
  107. content: ' ';
  108. display: block;
  109. width: 3px;
  110. height: 30px;
  111. background-color: #c00000;
  112. position: absolute;
  113. left: 0;
  114. top: 50%;
  115. transform: translateY(-50%);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. .cate-lv2-title {
  122. font-size: 12px;
  123. font-weight: bold;
  124. text-align: center;
  125. padding: 15px 0;
  126. }
  127. .cate-lv3-list {
  128. display: flex;
  129. flex-wrap: wrap;
  130. .cate-lv3-item {
  131. width: 33.33%;
  132. margin-bottom: 10px;
  133. display: flex;
  134. flex-direction: column;
  135. align-items: center;
  136. image {
  137. width: 60px;
  138. height: 60px;
  139. }
  140. text {
  141. font-size: 12px;
  142. }
  143. }
  144. }
  145. </style>