巴青农资商城

index.vue 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <template>
  2. <view class="home-page">
  3. <!-- ① 顶部:品牌 + 搜索(吸顶) -->
  4. <view class="home-header" :style="{ paddingTop: statusBarHeight + 'px' }">
  5. <view class="header-inner">
  6. <view class="brand-row">
  7. <text class="brand-name">巴青农资商城</text>
  8. </view>
  9. <view class="search-bar" @click="onSearchTap">
  10. <u-icon name="search" color="#999" size="18" />
  11. <text class="search-placeholder">搜索兽药、饲料、店铺</text>
  12. </view>
  13. </view>
  14. </view>
  15. <view class="header-placeholder" :style="{ height: headerTotalHeight + 'px' }" />
  16. <scroll-view
  17. class="home-scroll"
  18. scroll-y
  19. :style="{ height: scrollHeight + 'px' }"
  20. :scroll-top="scrollTop"
  21. @scroll="onScroll"
  22. >
  23. <!-- ② Banner 轮播 -->
  24. <view v-if="bannerList.length" class="section banner-section">
  25. <swiper
  26. class="banner-swiper"
  27. :indicator-dots="bannerList.length > 1"
  28. indicator-color="rgba(255,255,255,0.5)"
  29. indicator-active-color="#ffffff"
  30. :autoplay="bannerList.length > 1"
  31. :circular="bannerList.length > 1"
  32. interval="4000"
  33. duration="500"
  34. >
  35. <swiper-item v-for="(item, index) in bannerList" :key="item.bannerId || index">
  36. <image
  37. class="banner-img"
  38. :src="item.bannerImage"
  39. mode="aspectFill"
  40. @click="onBannerPreview(index)"
  41. />
  42. </swiper-item>
  43. </swiper>
  44. </view>
  45. <!-- ③ 平台一级类目 + 更多 -->
  46. <view class="section category-section">
  47. <scroll-view class="category-scroll" scroll-x enable-flex show-scrollbar="false">
  48. <view class="category-list">
  49. <view
  50. v-for="item in categoryList"
  51. :key="item.categoryId"
  52. class="category-item"
  53. @click="onCategoryTap(item)"
  54. >
  55. <image
  56. class="category-icon"
  57. :src="item.displayPic"
  58. mode="aspectFill"
  59. @error="onCategoryImgError(item)"
  60. />
  61. <text class="category-name">{{ item.categoryName }}</text>
  62. </view>
  63. <view class="category-item" @click="onMoreCategoryTap">
  64. <view class="category-icon category-more-icon">
  65. <u-icon name="grid" color="#2E7D32" size="28" />
  66. </view>
  67. <text class="category-name">更多</text>
  68. </view>
  69. </view>
  70. </scroll-view>
  71. <view v-if="categoryLoadFailed && !categoryList.length" class="module-hint">
  72. <text>分类加载失败</text>
  73. </view>
  74. </view>
  75. <!-- ④ 热销商品 -->
  76. <view class="section hot-section">
  77. <view class="section-head">
  78. <view class="section-title-wrap">
  79. <view class="title-bar" />
  80. <text class="section-title">热销商品</text>
  81. </view>
  82. <text class="section-sub">全平台精选</text>
  83. </view>
  84. <view v-if="hotLoading" class="hot-skeleton">
  85. <view v-for="n in 4" :key="n" class="skeleton-card" />
  86. </view>
  87. <view v-else-if="hotGoodsList.length" class="hot-grid">
  88. <view
  89. v-for="item in hotGoodsList"
  90. :key="item.goodsId"
  91. class="hot-card"
  92. @click="onGoodsTap(item)"
  93. >
  94. <image class="hot-pic" :src="item.displayPic" mode="aspectFill" />
  95. <view class="hot-info">
  96. <text class="hot-name">{{ item.goodsName }}</text>
  97. <view class="hot-meta">
  98. <text class="hot-price">
  99. <text class="price-symbol">¥</text>
  100. <text class="price-num">{{ item.priceText }}</text>
  101. </text>
  102. </view>
  103. <text class="hot-shop">{{ item.shopName || '—' }}</text>
  104. </view>
  105. </view>
  106. </view>
  107. <view v-else class="empty-block">
  108. <u-empty mode="list" text="暂无热销商品" icon-size="80" />
  109. </view>
  110. </view>
  111. <view class="safe-bottom" />
  112. </scroll-view>
  113. </view>
  114. </template>
  115. <script setup>
  116. import { ref, computed, onMounted } from 'vue'
  117. import { onShow } from '@dcloudio/uni-app'
  118. import { listHomeBanners, listHomeCategories, listHomeHotGoods } from '@/api/home'
  119. import { resolveFileUrl } from '@/utils/image'
  120. import { formatPrice } from '@/utils/format'
  121. import { goGoodsDetail } from '@/utils/goodsDetail'
  122. import { PAGE_SEARCH_INDEX, PAGE_CATEGORY_LEVEL1, PAGE_CATEGORY_TAB } from '@/utils/pageRoute'
  123. const CATEGORY_PLACEHOLDER = '/static/logo.png'
  124. const GOODS_PLACEHOLDER = '/static/logo.png'
  125. const statusBarHeight = ref(20)
  126. const headerContentHeight = 100
  127. const scrollHeight = ref(600)
  128. const scrollTop = ref(0)
  129. const savedScrollTop = ref(0)
  130. const hotLoading = ref(true)
  131. const bannerList = ref([])
  132. const categoryList = ref([])
  133. const hotGoodsList = ref([])
  134. const categoryLoadFailed = ref(false)
  135. const headerTotalHeight = computed(() => statusBarHeight.value + headerContentHeight)
  136. /** 并行拉取三接口,各模块独立渲染(HM14) */
  137. function loadHomeData() {
  138. hotLoading.value = true
  139. Promise.all([
  140. fetchModule(listHomeBanners, mapBanners).then((res) => {
  141. bannerList.value = res.data
  142. }),
  143. fetchModule(listHomeCategories, mapCategories).then((res) => {
  144. categoryLoadFailed.value = !res.ok
  145. categoryList.value = res.data
  146. }),
  147. fetchModule(listHomeHotGoods, mapHotGoods)
  148. .then((res) => {
  149. hotGoodsList.value = res.data
  150. })
  151. .finally(() => {
  152. hotLoading.value = false
  153. })
  154. ])
  155. }
  156. async function fetchModule(apiFn, mapper) {
  157. try {
  158. const res = await apiFn()
  159. const list = Array.isArray(res.data) ? res.data : []
  160. return { ok: true, data: mapper(list) }
  161. } catch (e) {
  162. return { ok: false, data: [] }
  163. }
  164. }
  165. /** Banner:过滤无效图(BN13) */
  166. function mapBanners(list) {
  167. return list
  168. .map((row) => {
  169. const url = resolveFileUrl(row.bannerImage)
  170. return {
  171. bannerId: row.bannerId,
  172. bannerImage: url,
  173. sortNo: row.sortNo
  174. }
  175. })
  176. .filter((item) => !!item.bannerImage)
  177. }
  178. function mapCategories(list) {
  179. return list.map((row) => ({
  180. categoryId: row.categoryId,
  181. categoryName: row.categoryName,
  182. categoryPic: row.categoryPic,
  183. displayPic: resolveFileUrl(row.categoryPic) || CATEGORY_PLACEHOLDER,
  184. sortNo: row.sortNo
  185. }))
  186. }
  187. function mapHotGoods(list) {
  188. return list.map((row) => ({
  189. goodsId: row.goodsId,
  190. goodsSn: row.goodsSn,
  191. goodsName: row.goodsName,
  192. mainPic: row.mainPic,
  193. displayPic: resolveFileUrl(row.mainPic) || GOODS_PLACEHOLDER,
  194. salePrice: row.salePrice,
  195. priceText: formatPrice(row.salePrice),
  196. shopId: row.shopId,
  197. shopName: row.shopName
  198. }))
  199. }
  200. function initLayout() {
  201. try {
  202. const sys = uni.getSystemInfoSync()
  203. statusBarHeight.value = sys.statusBarHeight || 20
  204. const windowH = sys.windowHeight || sys.screenHeight || 600
  205. // 减去自定义顶栏与底部 tabBar(约 50px)
  206. const tabBarH = 20
  207. scrollHeight.value = windowH - statusBarHeight.value - headerContentHeight - tabBarH
  208. } catch (e) {
  209. statusBarHeight.value = 20
  210. scrollHeight.value = 500
  211. }
  212. }
  213. function onScroll(e) {
  214. savedScrollTop.value = e.detail.scrollTop || 0
  215. }
  216. function onSearchTap() {
  217. uni.navigateTo({ url: PAGE_SEARCH_INDEX })
  218. }
  219. function onBannerPreview(index) {
  220. const urls = bannerList.value.map((b) => b.bannerImage).filter(Boolean)
  221. if (!urls.length) return
  222. uni.previewImage({ urls, current: urls[index] || urls[0] })
  223. }
  224. function onCategoryTap(item) {
  225. uni.navigateTo({
  226. url:
  227. `${PAGE_CATEGORY_LEVEL1}?level1Id=${item.categoryId}` +
  228. `&level1Name=${encodeURIComponent(item.categoryName || '')}` +
  229. `&level1Pic=${encodeURIComponent(item.categoryPic || '')}`
  230. })
  231. }
  232. function onMoreCategoryTap() {
  233. uni.switchTab({ url: PAGE_CATEGORY_TAB })
  234. }
  235. function onCategoryImgError(item) {
  236. item.displayPic = CATEGORY_PLACEHOLDER
  237. }
  238. function onGoodsTap(item) {
  239. goGoodsDetail(item.goodsId)
  240. }
  241. onMounted(() => {
  242. initLayout()
  243. })
  244. /** 进入页 / 切回 Tab 重新拉数;从子页返回时恢复滚动(§7.2、§7.3) */
  245. onShow(() => {
  246. const pages = getCurrentPages()
  247. const fromSubPage = pages.length > 1
  248. loadHomeData()
  249. if (fromSubPage && savedScrollTop.value > 0) {
  250. setTimeout(() => {
  251. scrollTop.value = savedScrollTop.value
  252. }, 80)
  253. }
  254. })
  255. </script>
  256. <style lang="scss" scoped>
  257. $primary: #2e7d32;
  258. $primary-light: #4caf50;
  259. .home-page {
  260. min-height: 100vh;
  261. background: #f5f6f8;
  262. }
  263. .home-header {
  264. position: fixed;
  265. left: 0;
  266. right: 0;
  267. top: 0;
  268. z-index: 100;
  269. background: linear-gradient(135deg, $primary 0%, $primary-light 100%);
  270. box-shadow: 0 4rpx 16rpx rgba(46, 125, 50, 0.25);
  271. }
  272. .header-inner {
  273. padding: 12rpx 24rpx 20rpx;
  274. }
  275. .brand-row {
  276. margin-bottom: 16rpx;
  277. }
  278. .brand-name {
  279. font-size: 34rpx;
  280. font-weight: 700;
  281. color: #fff;
  282. letter-spacing: 2rpx;
  283. }
  284. .search-bar {
  285. display: flex;
  286. align-items: center;
  287. height: 72rpx;
  288. padding: 0 24rpx;
  289. background: #fff;
  290. border-radius: 36rpx;
  291. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  292. }
  293. .search-placeholder {
  294. margin-left: 12rpx;
  295. font-size: 28rpx;
  296. color: #999;
  297. }
  298. .home-scroll {
  299. box-sizing: border-box;
  300. }
  301. .section {
  302. margin: 0 24rpx 24rpx;
  303. background: #fff;
  304. border-radius: 16rpx;
  305. overflow: hidden;
  306. }
  307. .banner-section {
  308. margin-top: 16rpx;
  309. padding: 0;
  310. background: transparent;
  311. overflow: visible;
  312. }
  313. .banner-swiper {
  314. height: 300rpx;
  315. border-radius: 16rpx;
  316. overflow: hidden;
  317. }
  318. .banner-img {
  319. width: 100%;
  320. height: 300rpx;
  321. display: block;
  322. }
  323. .category-section {
  324. padding: 24rpx 0 16rpx;
  325. }
  326. .category-scroll {
  327. width: 100%;
  328. white-space: nowrap;
  329. }
  330. .category-list {
  331. display: inline-flex;
  332. flex-direction: row;
  333. padding: 0 16rpx;
  334. }
  335. .category-item {
  336. display: inline-flex;
  337. flex-direction: column;
  338. align-items: center;
  339. width: 128rpx;
  340. margin: 0 8rpx;
  341. flex-shrink: 0;
  342. }
  343. .category-icon {
  344. width: 96rpx;
  345. height: 96rpx;
  346. border-radius: 50%;
  347. background: #f0f4f0;
  348. }
  349. .category-more-icon {
  350. display: flex;
  351. align-items: center;
  352. justify-content: center;
  353. border: 2rpx dashed #c8e6c9;
  354. background: #f1f8f1;
  355. }
  356. .category-name {
  357. margin-top: 12rpx;
  358. font-size: 24rpx;
  359. color: #333;
  360. text-align: center;
  361. width: 100%;
  362. overflow: hidden;
  363. text-overflow: ellipsis;
  364. white-space: nowrap;
  365. }
  366. .module-hint {
  367. padding: 8rpx 24rpx 0;
  368. font-size: 24rpx;
  369. color: #999;
  370. text-align: center;
  371. }
  372. .hot-section {
  373. padding: 24rpx;
  374. }
  375. .section-head {
  376. display: flex;
  377. align-items: center;
  378. justify-content: space-between;
  379. margin-bottom: 24rpx;
  380. }
  381. .section-title-wrap {
  382. display: flex;
  383. align-items: center;
  384. }
  385. .title-bar {
  386. width: 8rpx;
  387. height: 32rpx;
  388. margin-right: 12rpx;
  389. border-radius: 4rpx;
  390. background: $primary;
  391. }
  392. .section-title {
  393. font-size: 32rpx;
  394. font-weight: 700;
  395. color: #222;
  396. }
  397. .section-sub {
  398. font-size: 24rpx;
  399. color: #999;
  400. }
  401. .hot-grid {
  402. display: flex;
  403. flex-wrap: wrap;
  404. justify-content: space-between;
  405. }
  406. .hot-card {
  407. width: 48%;
  408. margin-bottom: 20rpx;
  409. background: #fafafa;
  410. border-radius: 12rpx;
  411. overflow: hidden;
  412. }
  413. .hot-pic {
  414. width: 100%;
  415. height: 320rpx;
  416. background: #eee;
  417. display: block;
  418. }
  419. .hot-info {
  420. padding: 16rpx;
  421. }
  422. .hot-name {
  423. font-size: 26rpx;
  424. color: #333;
  425. line-height: 1.4;
  426. height: 72rpx;
  427. overflow: hidden;
  428. display: -webkit-box;
  429. -webkit-line-clamp: 2;
  430. -webkit-box-orient: vertical;
  431. }
  432. .hot-meta {
  433. margin-top: 8rpx;
  434. }
  435. .hot-price {
  436. color: #e53935;
  437. font-weight: 600;
  438. }
  439. .price-symbol {
  440. font-size: 22rpx;
  441. }
  442. .price-num {
  443. font-size: 34rpx;
  444. }
  445. .hot-shop {
  446. display: block;
  447. margin-top: 8rpx;
  448. font-size: 22rpx;
  449. color: #999;
  450. overflow: hidden;
  451. text-overflow: ellipsis;
  452. white-space: nowrap;
  453. }
  454. .hot-skeleton {
  455. display: flex;
  456. flex-wrap: wrap;
  457. justify-content: space-between;
  458. }
  459. .skeleton-card {
  460. width: 48%;
  461. height: 420rpx;
  462. margin-bottom: 20rpx;
  463. border-radius: 12rpx;
  464. background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  465. background-size: 200% 100%;
  466. animation: shimmer 1.2s infinite;
  467. }
  468. @keyframes shimmer {
  469. 0% {
  470. background-position: 100% 0;
  471. }
  472. 100% {
  473. background-position: -100% 0;
  474. }
  475. }
  476. .empty-block {
  477. padding: 40rpx 0;
  478. }
  479. .safe-bottom {
  480. height: 24rpx;
  481. }
  482. </style>