巴青农资商城

index.vue 11KB

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