category.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <view class="container">
  3. <!-- 头部搜索区 -->
  4. <view class="search-bar">
  5. <view
  6. class="search-box"
  7. @tap="toSearchPage"
  8. >
  9. <image
  10. src="@/static/images/icon/search.png"
  11. class="search-img"
  12. />
  13. <text class="sear-input">
  14. 搜索您想要的商品
  15. </text>
  16. </view>
  17. </view>
  18. <!-- 滚动内容区 -->
  19. <view class="main">
  20. <!-- 左侧菜单start -->
  21. <scroll-view
  22. scroll-y="true"
  23. class="leftmenu"
  24. >
  25. <block
  26. v-for="(item, index) in categoryList"
  27. :key="index"
  28. >
  29. <view
  30. :class="'menu-item ' + (selIndex==index?'active':'') + ' '"
  31. :data-index="index"
  32. :data-id="item.categoryId"
  33. @tap="onMenuTab"
  34. >
  35. {{ item.categoryName }}
  36. </view>
  37. </block>
  38. <view
  39. v-if="!categoryList || !categoryList.length"
  40. class="ca-empty"
  41. >
  42. {{ categoryList && categoryList.length ? '该分类下暂无商品' : '暂无商品' }}
  43. </view>
  44. </scroll-view>
  45. <!-- 左侧菜单end -->
  46. <!-- 右侧内容start -->
  47. <scroll-view
  48. scroll-y="true"
  49. class="rightcontent"
  50. >
  51. <view class="adver-map">
  52. <view class="item-a">
  53. <image
  54. :src="util.checkFileUrl(categoryImg)"
  55. mode="widthFix"
  56. />
  57. </view>
  58. </view>
  59. <!-- 子分类 -->
  60. <view
  61. v-if="subCategoryList.length"
  62. class="th-cate-con"
  63. >
  64. <block
  65. v-for="(thCateItem, index) in subCategoryList"
  66. :key="index"
  67. >
  68. <view class="sub-category">
  69. <view
  70. class="sub-category-item"
  71. :data-categoryid="thCateItem.categoryId"
  72. :data-parentid="thCateItem.parentId"
  73. @tap="toCatePage"
  74. >
  75. <image
  76. :src="util.checkFileUrl(thCateItem.pic)"
  77. class="more-pic"
  78. mode="widthFix"
  79. />
  80. <text>{{ thCateItem.categoryName }}</text>
  81. </view>
  82. </view>
  83. </block>
  84. </view>
  85. <view
  86. v-else
  87. class="cont-item empty"
  88. >
  89. 该分类下暂无子分类~
  90. </view>
  91. </scroll-view>
  92. <!-- 右侧内容end -->
  93. </view>
  94. </view>
  95. </template>
  96. <script setup>
  97. import util from '@/utils/util.js'
  98. const categoryList = ref([])
  99. const subCategoryList = ref([])
  100. const categoryImg = ref('')
  101. const parentId = ref('')
  102. /**
  103. * 生命周期函数--监听页面加载
  104. */
  105. onLoad(() => {
  106. // 加载分类列表
  107. http.request({
  108. url: '/category/categoryInfo',
  109. method: 'GET',
  110. data: {
  111. parentId: ''
  112. }
  113. })
  114. .then(({ data }) => {
  115. categoryImg.value = data[0].pic
  116. categoryList.value = data
  117. getProdList(data[0].categoryId)
  118. parentId.value = categoryList.value[0].categoryId
  119. })
  120. })
  121. const selIndex = ref(0)
  122. /**
  123. * 分类点击事件
  124. */
  125. const onMenuTab = (e) => {
  126. const index = e.currentTarget.dataset.index
  127. getProdList(categoryList.value[index].categoryId)
  128. parentId.value = categoryList.value[index].categoryId
  129. categoryImg.value = categoryList.value[index].pic
  130. selIndex.value = index
  131. }
  132. /**
  133. * 跳转搜索页
  134. */
  135. const toSearchPage = () => {
  136. uni.navigateTo({
  137. url: '/pages/search-page/search-page'
  138. })
  139. }
  140. const getProdList = (categoryId) => {
  141. // 加载分类列表
  142. http.request({
  143. url: '/category/categoryInfo',
  144. method: 'GET',
  145. data: {
  146. parentId: categoryId
  147. }
  148. })
  149. .then(({ data }) => {
  150. subCategoryList.value = data
  151. })
  152. }
  153. /**
  154. * 跳转子分类商品页面
  155. */
  156. const toCatePage = (e) => {
  157. const { categoryid } = e.currentTarget.dataset
  158. uni.navigateTo({
  159. url: `/pages/sub-category/sub-category?parentId=${parentId.value}&categoryId=${categoryid}`
  160. })
  161. }
  162. </script>
  163. <style scoped lang="scss">
  164. @import "./category.scss";
  165. </style>