巴青农资商城

level1.vue 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <template>
  2. <view class="page-b">
  3. <search-entry />
  4. <view v-if="level1Name" class="cat-panel">
  5. <!-- 手风琴标题:一级分类,点击展开/收起二级 -->
  6. <view class="l1-hero" @click="toggleAccordion">
  7. <image class="l1-hero__pic" :src="level1DisplayPic" mode="aspectFill" />
  8. <view class="l1-hero__info">
  9. <view class="l1-hero__title-row">
  10. <text class="l1-hero__name">{{ level1Name }}</text>
  11. <text v-if="level1IsHot" class="l1-hero__hot">热门</text>
  12. </view>
  13. <text class="l1-hero__sub">{{ level1SubText }}</text>
  14. </view>
  15. <view class="l1-hero__arrow" :class="{ 'l1-hero__arrow--open': accordionOpen }">
  16. <u-icon name="arrow-down" color="#689f38" size="14" />
  17. </view>
  18. </view>
  19. <!-- 手风琴内容:二级分类宫格 -->
  20. <view
  21. class="accordion-body"
  22. :class="{ 'accordion-body--open': accordionOpen }"
  23. >
  24. <view class="accordion-body__inner">
  25. <view v-if="tabsLoading" class="cat-panel__hint">
  26. <u-loading-icon mode="circle" size="20" />
  27. </view>
  28. <view v-else-if="!tabList.length" class="cat-panel__hint">
  29. <u-empty mode="list" :text="tabsFailed ? '分类加载失败' : '暂无子分类'" icon-size="64" />
  30. </view>
  31. <view v-else class="l2-grid">
  32. <view
  33. v-for="(tab, index) in tabList"
  34. :key="tab.categoryId"
  35. class="l2-item"
  36. :class="{ 'l2-item--active': activeTabIndex === index }"
  37. @click.stop="onTabChange(index)"
  38. >
  39. <view class="l2-item__pic-wrap">
  40. <image class="l2-item__pic" :src="tab.displayPic" mode="aspectFill" />
  41. <text v-if="tab.isHot" class="l2-item__hot">热</text>
  42. </view>
  43. <text class="l2-item__name">{{ tab.categoryName }}</text>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. </view>
  49. <goods-list-block
  50. v-if="activeCategoryId"
  51. :category-id="activeCategoryId"
  52. :sort-by="sortBy"
  53. :scroll-top-key="scrollTopKey"
  54. :scroll-height="goodsScrollHeight"
  55. @update:sort-by="sortBy = $event"
  56. @goods-click="onGoodsClick"
  57. />
  58. </view>
  59. </template>
  60. <script setup>
  61. import { ref, computed } from 'vue'
  62. import { onLoad, onShow } from '@dcloudio/uni-app'
  63. import { getCategoryTree, getLevel2Tabs } from '@/api/category'
  64. import { mapCategoryTree, mapLevel2Tabs } from '@/utils/categoryDisplay'
  65. import { resolveFileUrl } from '@/utils/image'
  66. import { DEFAULT_SORT_BY } from '@/constants/categorySort'
  67. import SearchEntry from '@/components/mall/SearchEntry.vue'
  68. import GoodsListBlock from '@/components/category/GoodsListBlock.vue'
  69. import { goGoodsDetail } from '@/utils/goodsDetail'
  70. const CATEGORY_PLACEHOLDER = '/static/logo.png'
  71. const level1Id = ref('')
  72. const level1Name = ref('')
  73. const level1Pic = ref('')
  74. const level1IsHot = ref(false)
  75. const tabList = ref([])
  76. const activeTabIndex = ref(0)
  77. const tabsLoading = ref(true)
  78. const tabsFailed = ref(false)
  79. const sortBy = ref(DEFAULT_SORT_BY)
  80. const scrollTopKey = ref(0)
  81. const goodsScrollHeight = ref('500px')
  82. /** 手风琴默认展开 */
  83. const accordionOpen = ref(true)
  84. const activeCategoryId = computed(() => {
  85. const tab = tabList.value[activeTabIndex.value]
  86. return tab ? tab.categoryId : ''
  87. })
  88. const level1DisplayPic = computed(
  89. () => resolveFileUrl(level1Pic.value) || CATEGORY_PLACEHOLDER
  90. )
  91. const activeTabName = computed(() => {
  92. const tab = tabList.value[activeTabIndex.value]
  93. return tab ? tab.categoryName : ''
  94. })
  95. const level1SubText = computed(() => {
  96. if (tabsLoading.value) return '子分类加载中…'
  97. if (tabsFailed.value) return '子分类加载失败'
  98. if (!tabList.value.length) return '暂无子分类'
  99. if (!accordionOpen.value && activeTabName.value) {
  100. return `当前:${activeTabName.value} · 点击展开子分类`
  101. }
  102. const n = tabList.value.length
  103. return `共 ${n} 个子分类,点击选择`
  104. })
  105. function calcScrollHeight() {
  106. try {
  107. const sys = uni.getSystemInfoSync()
  108. const h = sys.windowHeight || 600
  109. // 搜索 + 手风琴面板(展开更高)+ 排序栏
  110. const panelPx = accordionOpen.value ? 300 : 170
  111. goodsScrollHeight.value = `${h - panelPx}px`
  112. } catch (e) {
  113. goodsScrollHeight.value = '500px'
  114. }
  115. }
  116. function toggleAccordion() {
  117. accordionOpen.value = !accordionOpen.value
  118. calcScrollHeight()
  119. }
  120. /** 补全一级分类图标(路由未带图时从分类树读取) */
  121. async function loadLevel1Meta() {
  122. if (!level1Id.value) return
  123. if (level1Pic.value && level1Name.value) return
  124. try {
  125. const res = await getCategoryTree()
  126. const tree = mapCategoryTree(res.data || [])
  127. const node = tree.find((n) => String(n.categoryId) === String(level1Id.value))
  128. if (!node) return
  129. if (!level1Name.value) level1Name.value = node.categoryName
  130. if (!level1Pic.value) level1Pic.value = node.categoryPic || ''
  131. if (!level1IsHot.value) level1IsHot.value = node.isHot
  132. } catch (e) {
  133. // 静默失败,保留路由传入的名称
  134. }
  135. }
  136. async function loadTabs() {
  137. if (!level1Id.value) return
  138. tabsLoading.value = true
  139. tabsFailed.value = false
  140. try {
  141. const res = await getLevel2Tabs(level1Id.value)
  142. tabList.value = mapLevel2Tabs(res.data || [])
  143. if (activeTabIndex.value >= tabList.value.length) {
  144. activeTabIndex.value = 0
  145. }
  146. } catch (e) {
  147. tabsFailed.value = true
  148. tabList.value = []
  149. } finally {
  150. tabsLoading.value = false
  151. }
  152. }
  153. function onTabChange(index) {
  154. if (activeTabIndex.value === index) return
  155. activeTabIndex.value = index
  156. // C1:列表回顶并重新加载(由 scrollTopKey 触发子组件 watch)
  157. scrollTopKey.value += 1
  158. }
  159. function onGoodsClick(item) {
  160. goGoodsDetail(item.goodsId)
  161. }
  162. onLoad((options) => {
  163. level1Id.value = options.level1Id || ''
  164. level1Name.value = decodeURIComponent(options.level1Name || '')
  165. level1Pic.value = options.level1Pic ? decodeURIComponent(options.level1Pic) : ''
  166. level1IsHot.value = options.level1Hot === '1'
  167. if (level1Name.value) {
  168. uni.setNavigationBarTitle({ title: level1Name.value })
  169. }
  170. calcScrollHeight()
  171. })
  172. onShow(() => {
  173. loadLevel1Meta()
  174. loadTabs()
  175. })
  176. </script>
  177. <style lang="scss" scoped>
  178. .page-b {
  179. display: flex;
  180. flex-direction: column;
  181. height: calc(100vh - 88rpx);
  182. background: #f5f6f8;
  183. }
  184. /* 一二级分类合并面板 */
  185. .cat-panel {
  186. margin: 16rpx 24rpx 0;
  187. background: #fff;
  188. border-radius: 16rpx;
  189. overflow: hidden;
  190. box-shadow: 0 4rpx 20rpx rgba(46, 125, 50, 0.08);
  191. }
  192. .l1-hero {
  193. display: flex;
  194. align-items: center;
  195. padding: 20rpx 24rpx;
  196. background: linear-gradient(135deg, #e8f5e9 0%, #f9fdf9 55%, #fff 100%);
  197. }
  198. .l1-hero__pic {
  199. width: 80rpx;
  200. height: 80rpx;
  201. border-radius: 18rpx;
  202. background: #fff;
  203. flex-shrink: 0;
  204. box-shadow: 0 4rpx 12rpx rgba(46, 125, 50, 0.12);
  205. }
  206. .l1-hero__info {
  207. flex: 1;
  208. min-width: 0;
  209. margin-left: 24rpx;
  210. }
  211. .l1-hero__title-row {
  212. display: flex;
  213. align-items: center;
  214. gap: 12rpx;
  215. }
  216. .l1-hero__name {
  217. flex: 1;
  218. min-width: 0;
  219. font-size: 32rpx;
  220. font-weight: 700;
  221. color: #1b5e20;
  222. white-space: nowrap;
  223. overflow: hidden;
  224. text-overflow: ellipsis;
  225. }
  226. .l1-hero__arrow {
  227. flex-shrink: 0;
  228. margin-left: 12rpx;
  229. width: 40rpx;
  230. height: 40rpx;
  231. display: flex;
  232. align-items: center;
  233. justify-content: center;
  234. border-radius: 50%;
  235. background: rgba(104, 159, 56, 0.12);
  236. transition: transform 0.25s ease;
  237. }
  238. .l1-hero__arrow--open {
  239. transform: rotate(180deg);
  240. }
  241. .l1-hero__hot {
  242. flex-shrink: 0;
  243. padding: 4rpx 14rpx;
  244. font-size: 20rpx;
  245. color: #fff;
  246. background: linear-gradient(90deg, #ff9800, #f57c00);
  247. border-radius: 20rpx;
  248. }
  249. .l1-hero__sub {
  250. display: block;
  251. margin-top: 10rpx;
  252. font-size: 24rpx;
  253. color: #689f38;
  254. line-height: 1.4;
  255. }
  256. .cat-panel__hint {
  257. padding: 24rpx;
  258. display: flex;
  259. justify-content: center;
  260. }
  261. /* 手风琴展开区 */
  262. .accordion-body {
  263. max-height: 0;
  264. overflow: hidden;
  265. transition: max-height 0.28s ease;
  266. border-top: 0 solid #f0f0f0;
  267. }
  268. .accordion-body--open {
  269. max-height: 1200rpx;
  270. border-top-width: 1rpx;
  271. }
  272. .accordion-body__inner {
  273. background: #fafbfa;
  274. }
  275. /* 二级分类:手风琴内四列小图标宫格 */
  276. .l2-grid {
  277. display: flex;
  278. flex-wrap: wrap;
  279. padding: 16rpx 8rpx 12rpx;
  280. }
  281. .l2-item {
  282. width: 25%;
  283. display: flex;
  284. flex-direction: column;
  285. align-items: center;
  286. padding: 10rpx 6rpx 14rpx;
  287. box-sizing: border-box;
  288. }
  289. .l2-item__pic-wrap {
  290. position: relative;
  291. width: 72rpx;
  292. height: 72rpx;
  293. border-radius: 16rpx;
  294. overflow: hidden;
  295. background: #f0f2f0;
  296. border: 2rpx solid transparent;
  297. box-sizing: border-box;
  298. }
  299. .l2-item--active .l2-item__pic-wrap {
  300. border-color: #2e7d32;
  301. background: #e8f5e9;
  302. }
  303. .l2-item--active .l2-item__name {
  304. color: #2e7d32;
  305. font-weight: 600;
  306. }
  307. .l2-item__pic {
  308. width: 100%;
  309. height: 100%;
  310. }
  311. .l2-item__hot {
  312. position: absolute;
  313. top: 0;
  314. right: 0;
  315. padding: 0 6rpx;
  316. font-size: 16rpx;
  317. color: #fff;
  318. background: #ff9800;
  319. border-radius: 0 0 0 6rpx;
  320. line-height: 1.3;
  321. }
  322. .l2-item__name {
  323. margin-top: 8rpx;
  324. width: 100%;
  325. font-size: 22rpx;
  326. color: #333;
  327. text-align: center;
  328. line-height: 1.3;
  329. white-space: nowrap;
  330. overflow: hidden;
  331. text-overflow: ellipsis;
  332. }
  333. </style>