巴青农资商城

index.vue 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <view class="page-a">
  3. <search-entry />
  4. <view v-if="pageLoading" class="page-a__loading">
  5. <u-loading-icon mode="circle" text="加载中" />
  6. </view>
  7. <view v-else-if="!treeList.length" class="page-a__empty">
  8. <u-empty mode="list" :text="loadFailed ? '分类加载失败' : '暂无分类'" icon-size="80" />
  9. </view>
  10. <view v-else class="page-a__body">
  11. <scroll-view class="page-a__left" scroll-y>
  12. <view
  13. v-for="(item, index) in treeList"
  14. :key="item.categoryId"
  15. class="left-item"
  16. :class="{ 'left-item--active': selectedIndex === index }"
  17. @click="onSelectLevel1(index)"
  18. >
  19. <text class="left-item__name">{{ item.categoryName }}</text>
  20. <text v-if="item.isHot" class="left-item__hot">热</text>
  21. </view>
  22. </scroll-view>
  23. <scroll-view class="page-a__right" scroll-y>
  24. <view class="right-grid">
  25. <view
  26. v-for="child in currentChildren"
  27. :key="child.categoryId"
  28. class="right-item"
  29. @click="onLevel2Tap(child)"
  30. >
  31. <image-preview class="right-item__pic" :src="child.categoryPic" />
  32. <text v-if="child.isHot" class="right-item__badge">热</text>
  33. <text class="right-item__name">{{ child.categoryName }}</text>
  34. </view>
  35. </view>
  36. <view v-if="!currentChildren.length" class="right-empty">
  37. <text>该分类下暂无子类</text>
  38. </view>
  39. </scroll-view>
  40. </view>
  41. </view>
  42. </template>
  43. <script setup>
  44. import { ref, computed } from 'vue'
  45. import { onShow } from '@dcloudio/uni-app'
  46. import { getCategoryTree } from '@/api/category'
  47. import { mapCategoryTree } from '@/utils/categoryDisplay'
  48. import SearchEntry from '@/components/mall/SearchEntry.vue'
  49. import { PAGE_CATEGORY_GOODS_LIST } from '@/utils/pageRoute'
  50. const treeList = ref([])
  51. const selectedIndex = ref(0)
  52. const pageLoading = ref(true)
  53. const loadFailed = ref(false)
  54. const currentChildren = computed(() => {
  55. const cur = treeList.value[selectedIndex.value]
  56. return cur && cur.children ? cur.children : []
  57. })
  58. async function loadTree() {
  59. pageLoading.value = true
  60. loadFailed.value = false
  61. try {
  62. const res = await getCategoryTree()
  63. treeList.value = mapCategoryTree(res.data || [])
  64. if (treeList.value.length && selectedIndex.value >= treeList.value.length) {
  65. selectedIndex.value = 0
  66. }
  67. } catch (e) {
  68. loadFailed.value = true
  69. treeList.value = []
  70. } finally {
  71. pageLoading.value = false
  72. }
  73. }
  74. function onSelectLevel1(index) {
  75. selectedIndex.value = index
  76. }
  77. function onLevel2Tap(child) {
  78. const level1 = treeList.value[selectedIndex.value]
  79. if (!level1) return
  80. uni.navigateTo({
  81. url:
  82. `${PAGE_CATEGORY_GOODS_LIST}?categoryId=${child.categoryId}` +
  83. `&level1Id=${level1.categoryId}` +
  84. `&level1Name=${encodeURIComponent(level1.categoryName || '')}` +
  85. `&level2Name=${encodeURIComponent(child.categoryName || '')}`
  86. })
  87. }
  88. onShow(() => {
  89. loadTree()
  90. })
  91. </script>
  92. <style lang="scss" scoped>
  93. .page-a {
  94. display: flex;
  95. flex-direction: column;
  96. height: calc(100vh - 188rpx);
  97. background: #f5f6f8;
  98. }
  99. .page-a__loading,
  100. .page-a__empty {
  101. flex: 1;
  102. display: flex;
  103. align-items: center;
  104. justify-content: center;
  105. }
  106. .page-a__body {
  107. flex: 1;
  108. display: flex;
  109. min-height: 0;
  110. }
  111. .page-a__left {
  112. width: 200rpx;
  113. background: #f0f2f0;
  114. flex-shrink: 0;
  115. }
  116. .left-item {
  117. position: relative;
  118. padding: 28rpx 16rpx;
  119. text-align: center;
  120. }
  121. .left-item--active {
  122. background: #fff;
  123. }
  124. .left-item--active::before {
  125. content: '';
  126. position: absolute;
  127. left: 0;
  128. top: 24rpx;
  129. bottom: 24rpx;
  130. width: 6rpx;
  131. background: #2e7d32;
  132. border-radius: 0 4rpx 4rpx 0;
  133. }
  134. .left-item__name {
  135. font-size: 26rpx;
  136. color: #333;
  137. }
  138. .left-item__hot {
  139. display: inline-block;
  140. margin-left: 4rpx;
  141. padding: 0 6rpx;
  142. font-size: 18rpx;
  143. color: #fff;
  144. background: #ff9800;
  145. border-radius: 4rpx;
  146. vertical-align: middle;
  147. }
  148. .page-a__right {
  149. flex: 1;
  150. background: #fff;
  151. }
  152. .right-grid {
  153. display: flex;
  154. flex-wrap: wrap;
  155. padding: 16rpx;
  156. }
  157. .right-item {
  158. position: relative;
  159. width: 33.33%;
  160. padding: 16rpx 8rpx;
  161. box-sizing: border-box;
  162. display: flex;
  163. flex-direction: column;
  164. align-items: center;
  165. }
  166. .right-item__pic {
  167. width: 120rpx;
  168. height: 120rpx;
  169. border-radius: 16rpx;
  170. background: #f5f5f5;
  171. }
  172. .right-item__badge {
  173. position: absolute;
  174. top: 12rpx;
  175. right: 12rpx;
  176. padding: 2rpx 8rpx;
  177. font-size: 18rpx;
  178. color: #fff;
  179. background: #ff9800;
  180. border-radius: 6rpx;
  181. }
  182. .right-item__name {
  183. margin-top: 12rpx;
  184. font-size: 24rpx;
  185. color: #333;
  186. text-align: center;
  187. }
  188. .right-empty {
  189. padding: 80rpx 24rpx;
  190. text-align: center;
  191. color: #999;
  192. font-size: 26rpx;
  193. }
  194. </style>