category.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <view class="container">
  3. <!-- 搜索框 -->
  4. <view class="search-wrap">
  5. <u-search placeholder="搜索" disabled height="32" bgColor="#f2f2f2" margin="0 20rpx" :show-action="false"
  6. @click="handleSearchClick"></u-search>
  7. </view>
  8. <!-- 分类内容 -->
  9. <view class="category-box">
  10. <!-- 左侧导航栏 -->
  11. <scroll-view scroll-y="true" class='box-left'>
  12. <view class="category-item" v-for="(item, index) in categoryList" :key="item.id">
  13. <view class="item-title" :class="{ active: currentIndex === index }" @click="handleCategoryClick(index)">
  14. <text>{{ item.name }}</text>
  15. </view>
  16. </view>
  17. </scroll-view>
  18. <!-- 右侧分类内容 -->
  19. <scroll-view scroll-y="true" class="box-right">
  20. <view class="category-image">
  21. <image :showLoading="true" :src="categoryList[currentIndex].picUrl" mode='widthFix' @click="click"></image>
  22. </view>
  23. <view class="sub-category-box" v-for="(item, index) in categoryList[currentIndex].children" :key="item.id">
  24. <view class="sub-category-header">
  25. <view class="title">{{ item.name }}</view>
  26. <view class="more" @click="handleCategory(item, 0)">查看更多</view>
  27. </view>
  28. <view class="sub-category-grid">
  29. <u-grid col="3">
  30. <u-grid-item v-for="(subItem, subIndex) in item.children" :key="subItem.id">
  31. <view class="sub-category-item" @click="handleCategory(item, subIndex)">
  32. <u-icon name="photo" :size="80" v-if="subItem.picUrl === null"></u-icon>
  33. <image :src="subItem.picUrl" v-if="subItem.picUrl != null" mode='widthFix' />
  34. <text class="sub-category-title">{{ subItem.name }}</text>
  35. </view>
  36. </u-grid-item>
  37. </u-grid>
  38. </view>
  39. </view>
  40. </scroll-view>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import { categoryListData } from '../../api/category';
  46. import { handleTree, convertTree } from '../../utils/tree.js';
  47. export default {
  48. data() {
  49. return {
  50. currentIndex: 0,
  51. categoryList: []
  52. }
  53. },
  54. onLoad() {
  55. this.handleCategoryList();
  56. },
  57. methods: {
  58. // 点击搜索框
  59. handleSearchClick(e) {
  60. uni.$u.route('/pages/search/search')
  61. },
  62. // 点击左侧导航栏
  63. handleCategoryClick(index) {
  64. if (this.currentIndex !== index) {
  65. this.currentIndex = index
  66. }
  67. },
  68. // 获取分类列表并构建树形结构
  69. handleCategoryList() {
  70. categoryListData().then(res => {
  71. this.categoryList = handleTree(res.data, "id", "parentId");
  72. })
  73. },
  74. handleCategory(item, index){
  75. // console.log(item)
  76. // console.log(index)
  77. uni.navigateTo({
  78. url:"./product-list?item="+encodeURIComponent(JSON.stringify(item))+"&index="+index
  79. })
  80. }
  81. }
  82. }
  83. </script>
  84. <style lang="scss" scoped>
  85. .search-wrap {
  86. background: #ffffff;
  87. position: fixed;
  88. top: 0;
  89. left: 0;
  90. box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.07);
  91. padding: 20rpx 0;
  92. width: 100%;
  93. z-index: 3;
  94. }
  95. .category-box {
  96. position: fixed;
  97. display: flex;
  98. overflow: hidden;
  99. margin-top: 100rpx;
  100. height: calc(100% - 100rpx);
  101. .box-left {
  102. width: 200rpx;
  103. padding-top: 5rpx;
  104. overflow: scroll;
  105. z-index: 2;
  106. background-color: #f2f2f2;
  107. .category-item {
  108. line-height: 80rpx;
  109. height: 80rpx;
  110. text-align: center;
  111. color: #777;
  112. .item-title {
  113. font-size: 28rpx;
  114. &.active {
  115. font-size: 28rpx;
  116. font-weight: bold;
  117. position: relative;
  118. background: #fff;
  119. color: $u-primary;
  120. }
  121. &.active::before {
  122. position: absolute;
  123. left: 0;
  124. content: "";
  125. width: 8rpx;
  126. height: 32rpx;
  127. top: 25rpx;
  128. background: $u-primary;
  129. }
  130. }
  131. }
  132. }
  133. .box-right {
  134. width: 550rpx;
  135. height: 100%;
  136. box-sizing: border-box;
  137. z-index: 1;
  138. .category-image {
  139. width: 510rpx;
  140. box-sizing: border-box;
  141. overflow: hidden;
  142. position: relative;
  143. margin: 30rpx 20rpx 0;
  144. image {
  145. width: 100%;
  146. }
  147. }
  148. .sub-category-box {
  149. .sub-category-header {
  150. @include flex-space-between;
  151. padding: 20rpx 20rpx;
  152. .title {
  153. font-size: 28rpx;
  154. font-weight: bolder;
  155. }
  156. .more {
  157. font-size: 22rpx;
  158. color: #939393;
  159. }
  160. }
  161. .sub-category-grid {
  162. padding: 0 15rpx;
  163. .sub-category-item {
  164. @include flex-center(column);
  165. background: #fff;
  166. image {
  167. text-align: center;
  168. width: 150rpx;
  169. height: 150rpx;
  170. line-height: 150rpx;
  171. font-size: 0;
  172. }
  173. .sub-category-title {
  174. margin: 15rpx 0;
  175. font-size: 22rpx;
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. </style>