index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. <template>
  2. <view class="container">
  3. <!-- Member Upgrade Banner -->
  4. <view v-if="!isMember" class="member-banner" @click="goUpgrade">
  5. <view class="member-banner-content">
  6. <text class="member-banner-icon">👑</text>
  7. <view class="member-banner-texts">
  8. <text class="member-banner-title">开通会员</text>
  9. <text class="member-banner-desc">享专属折扣与成长服务</text>
  10. </view>
  11. </view>
  12. <text class="member-banner-action">立即开通 ›</text>
  13. </view>
  14. <!-- 搜索栏 + 购物车 -->
  15. <view class="search-row">
  16. <view class="search-bar" @click="goSearch">
  17. <text class="search-icon">🔍</text>
  18. <text class="search-placeholder">搜索商品</text>
  19. </view>
  20. <view class="cart-btn" @click="goCart">
  21. <text class="cart-icon">🛒</text>
  22. <text v-if="cartCount > 0" class="cart-badge">{{ cartCount > 99 ? '99+' : cartCount }}</text>
  23. </view>
  24. </view>
  25. <!-- Level 1 分类:2列横排网格 -->
  26. <view class="cat-level1">
  27. <view
  28. v-for="cat in categories"
  29. :key="cat.id"
  30. :class="['cat1-item', currentCat1 && currentCat1.id === cat.id ? 'active' : '']"
  31. @click="onCat1Select(cat)"
  32. >
  33. <image v-if="cat.image" class="cat1-icon" :src="cat.image" mode="aspectFit" />
  34. <view v-else class="cat1-icon-placeholder">
  35. <text class="cat1-icon-text">{{ cat.name.substr(0, 1) }}</text>
  36. </view>
  37. <text class="cat1-name">{{ cat.name }}</text>
  38. </view>
  39. </view>
  40. <!-- Level 2 子分类:横向滚动chips -->
  41. <scroll-view
  42. v-if="currentCat1 && currentCat1.children && currentCat1.children.length"
  43. scroll-x
  44. enable-flex
  45. show-scrollbar="false"
  46. class="cat-level2-scroll"
  47. >
  48. <view class="cat2-chips">
  49. <view
  50. :class="['cat2-chip', !currentCat2 ? 'active' : '']"
  51. @click="onCat2Select(null)"
  52. >全部</view>
  53. <view
  54. v-for="child in currentCat1.children"
  55. :key="child.id"
  56. :class="['cat2-chip', currentCat2 && currentCat2.id === child.id ? 'active' : '']"
  57. @click="onCat2Select(child)"
  58. >{{ child.name }}</view>
  59. </view>
  60. </scroll-view>
  61. <!-- 商品列表 -->
  62. <scroll-view
  63. scroll-y
  64. class="product-list"
  65. @scrolltolower="onLoadMore"
  66. refresher-enabled
  67. :refresher-triggered="refreshing"
  68. @refresherrefresh="onRefresh"
  69. >
  70. <!-- 推荐商品 -->
  71. <view class="recommend-section" v-if="recommended.length > 0">
  72. <view class="recommend-header">
  73. <text class="recommend-title">🔥 推荐商品</text>
  74. </view>
  75. <scroll-view scroll-x enable-flex show-scrollbar="false" class="recommend-scroll">
  76. <view
  77. v-for="item in recommended"
  78. :key="item.id"
  79. class="recommend-card"
  80. @click="goDetail(item.id)"
  81. >
  82. <image
  83. class="recommend-img"
  84. :src="getImageUrl(item.image || item.coverImage) || '/static/default-product.png'"
  85. mode="aspectFill"
  86. />
  87. <text class="recommend-name">{{ item.name }}</text>
  88. <text class="recommend-price">{{ formatPriceWithSymbol(item.price) }}</text>
  89. </view>
  90. </scroll-view>
  91. </view>
  92. <!-- Loading skeleton -->
  93. <view v-if="loading && products.length === 0" class="skeleton-grid">
  94. <view class="skeleton-card" v-for="n in 6" :key="n">
  95. <view class="skeleton-img"></view>
  96. <view class="skeleton-body">
  97. <view class="skeleton-line skeleton-title"></view>
  98. <view class="skeleton-line skeleton-price"></view>
  99. <view class="skeleton-line skeleton-vendor"></view>
  100. </view>
  101. </view>
  102. </view>
  103. <view v-else-if="products.length === 0" class="empty-wrap">
  104. <text class="empty-icon">📦</text>
  105. <text class="empty-text">暂无商品</text>
  106. </view>
  107. <view v-else class="grid fade-in">
  108. <view
  109. v-for="item in products"
  110. :key="item.id"
  111. class="product-card"
  112. @click="goDetail(item.id)"
  113. >
  114. <image
  115. class="cover"
  116. :src="getImageUrl(item.image || item.coverImage) || '/static/default-product.png'"
  117. mode="aspectFill"
  118. />
  119. <view class="card-body">
  120. <text class="product-name">{{ item.name }}</text>
  121. <view class="price-row">
  122. <text class="price">{{ formatPriceWithSymbol(item.price) }}</text>
  123. <text v-if="item.memberPrice" class="member-price">会员{{ formatPriceWithSymbol(item.memberPrice) }}</text>
  124. </view>
  125. <text class="vendor-name">{{ item.brandName || '平台官方' }}</text>
  126. </view>
  127. </view>
  128. </view>
  129. <view v-if="loadingMore" class="loading-more">
  130. <text class="loading-text">加载中...</text>
  131. </view>
  132. <view v-if="noMore && products.length > 0" class="no-more">
  133. <text class="no-more-text">— 没有更多了 —</text>
  134. </view>
  135. <view v-if="!isLoggedIn" class="login-hint-bar">
  136. <text class="login-hint-text">🔒 登录查看全部商品和会员价</text>
  137. </view>
  138. <view class="bottom-spacer"></view>
  139. </scroll-view>
  140. </view>
  141. </template>
  142. <script>
  143. import { goodsCategory, productList, getMyMembership, cartCount } from '@/utils/api.js'
  144. import config from '@/config.js'
  145. export default {
  146. data() {
  147. return {
  148. categories: [],
  149. currentCat1: null,
  150. currentCat2: null,
  151. products: [],
  152. page: 1,
  153. size: 20,
  154. loading: false,
  155. loadingMore: false,
  156. refreshing: false,
  157. noMore: false,
  158. isLoggedIn: false,
  159. isMember: false,
  160. recommended: [],
  161. cartCount: 0
  162. }
  163. },
  164. onLoad() {
  165. this.checkMemberStatus()
  166. this.isLoggedIn = !!uni.getStorageSync('token')
  167. this.loadCategories()
  168. this.loadProducts()
  169. this.loadRecommended()
  170. this.loadCartCount()
  171. },
  172. onShow() {
  173. this.checkMemberStatus()
  174. this.isLoggedIn = !!uni.getStorageSync('token')
  175. this.loadCartCount()
  176. },
  177. methods: {
  178. loadCategories() {
  179. goodsCategory('0').then(res => {
  180. if (res.code === 200 && res.data) {
  181. this.categories = res.data || []
  182. }
  183. }).catch(() => {})
  184. },
  185. onCat1Select(cat) {
  186. if (this.currentCat1 && this.currentCat1.id === cat.id) return
  187. this.currentCat1 = cat
  188. this.currentCat2 = null
  189. this.page = 1
  190. this.products = []
  191. this.noMore = false
  192. this.loadProducts()
  193. },
  194. onCat2Select(cat) {
  195. this.currentCat2 = cat
  196. this.page = 1
  197. this.products = []
  198. this.noMore = false
  199. this.loadProducts()
  200. },
  201. loadProducts() {
  202. if (this.loading) return
  203. this.loading = true
  204. const params = { page: this.page, size: this.size }
  205. if (this.currentCat2) {
  206. params.categoryId = this.currentCat2.id
  207. } else if (this.currentCat1) {
  208. params.categoryId = this.currentCat1.id
  209. }
  210. productList(params).then(res => {
  211. this.loading = false
  212. this.refreshing = false
  213. if (res.code === 200 && res.data) {
  214. const records = res.data.records || []
  215. if (this.page === 1) {
  216. this.products = records
  217. } else {
  218. this.products = this.products.concat(records)
  219. }
  220. const total = res.data.total || 0
  221. this.noMore = this.products.length >= total
  222. }
  223. }).catch(() => {
  224. this.loading = false
  225. this.refreshing = false
  226. })
  227. },
  228. onRefresh() {
  229. this.refreshing = true
  230. this.page = 1
  231. this.noMore = false
  232. this.loadProducts()
  233. },
  234. onLoadMore() {
  235. if (this.noMore || this.loadingMore) return
  236. this.page++
  237. this.loadingMore = true
  238. const params = { page: this.page, size: this.size }
  239. if (this.currentCat2) {
  240. params.categoryId = this.currentCat2.id
  241. } else if (this.currentCat1) {
  242. params.categoryId = this.currentCat1.id
  243. }
  244. productList(params).then(res => {
  245. this.loadingMore = false
  246. if (res.code === 200 && res.data) {
  247. const records = res.data.records || []
  248. this.products = this.products.concat(records)
  249. const total = res.data.total || 0
  250. this.noMore = this.products.length >= total
  251. }
  252. }).catch(() => {
  253. this.loadingMore = false
  254. })
  255. },
  256. checkMemberStatus() {
  257. if (!uni.getStorageSync('token')) {
  258. this.isMember = false
  259. return
  260. }
  261. var that = this
  262. getMyMembership().then(function(res) {
  263. if (res.data && res.data.memberLevel) {
  264. that.isMember = res.data.memberLevel !== 'FREE'
  265. }
  266. }).catch(function() {
  267. that.isMember = false
  268. })
  269. },
  270. goUpgrade() {
  271. uni.navigateTo({ url: '/pages/membership/upgrade' })
  272. },
  273. goDetail(id) {
  274. uni.navigateTo({ url: '/pages/shop/detail/detail?id=' + id })
  275. },
  276. goSearch() {
  277. uni.navigateTo({ url: '/pages/shop/search/search' })
  278. },
  279. goCart() {
  280. uni.navigateTo({ url: '/pages/shop/cart/cart' })
  281. },
  282. loadCartCount() {
  283. if (!uni.getStorageSync('token')) {
  284. this.cartCount = 0
  285. return
  286. }
  287. cartCount().then(res => {
  288. if (res.code === 200) {
  289. this.cartCount = res.data || 0
  290. }
  291. }).catch(() => {})
  292. },
  293. loadRecommended() {
  294. productList({ page: 1, size: 6, sort: 'sales' }).then(res => {
  295. if (res.code === 200 && res.data) {
  296. this.recommended = res.data.records || []
  297. }
  298. }).catch(() => {})
  299. },
  300. formatPriceWithSymbol(price) {
  301. if (price === null || price === undefined) return ''
  302. return '¥' + (Number(price) / 100).toFixed(2)
  303. },
  304. getImageUrl: function(path) {
  305. return config.API_BASE_URL + path
  306. }
  307. }
  308. }
  309. </script>
  310. <style scoped>
  311. .container {
  312. display: flex;
  313. flex-direction: column;
  314. height: 100vh;
  315. background: #f5f5f5;
  316. }
  317. /* ==================== 分类布局 ==================== */
  318. /* Level 1 分类:2列横排网格 */
  319. .cat-level1 {
  320. display: flex;
  321. flex-wrap: wrap;
  322. padding: 16rpx 16rpx 8rpx;
  323. background: #fff;
  324. border-bottom: 1rpx solid #eee;
  325. }
  326. .cat1-item {
  327. width: 25%;
  328. box-sizing: border-box;
  329. padding: 16rpx 8rpx;
  330. display: flex;
  331. flex-direction: column;
  332. align-items: center;
  333. text-align: center;
  334. border-radius: 16rpx;
  335. margin-bottom: 8rpx;
  336. }
  337. .cat1-item.active {
  338. background: rgba(249, 115, 22, 0.08);
  339. }
  340. .cat1-icon {
  341. width: 72rpx;
  342. height: 72rpx;
  343. border-radius: 50%;
  344. margin-bottom: 6rpx;
  345. background: #f5f5f5;
  346. }
  347. .cat1-icon-placeholder {
  348. width: 72rpx;
  349. height: 72rpx;
  350. border-radius: 50%;
  351. margin-bottom: 6rpx;
  352. background: linear-gradient(135deg, #667eea, #764ba2);
  353. display: flex;
  354. align-items: center;
  355. justify-content: center;
  356. }
  357. .cat1-icon-text {
  358. font-size: 32rpx;
  359. font-weight: bold;
  360. color: #fff;
  361. }
  362. .cat1-name {
  363. font-size: 24rpx;
  364. color: #333;
  365. max-width: 140rpx;
  366. overflow: hidden;
  367. text-overflow: ellipsis;
  368. white-space: nowrap;
  369. }
  370. .cat1-item.active .cat1-name {
  371. color: #F97316;
  372. font-weight: bold;
  373. }
  374. /* 搜索栏 + 购物车 */
  375. .search-row {
  376. display: flex;
  377. align-items: center;
  378. margin: 16rpx 16rpx 0;
  379. gap: 16rpx;
  380. }
  381. .search-bar {
  382. flex: 1;
  383. display: flex;
  384. align-items: center;
  385. background: #fff;
  386. padding: 16rpx 24rpx;
  387. border-radius: 50rpx;
  388. border: 1rpx solid #eee;
  389. }
  390. .search-icon {
  391. font-size: 32rpx;
  392. margin-right: 12rpx;
  393. }
  394. .search-placeholder {
  395. font-size: 26rpx;
  396. color: #bbb;
  397. }
  398. .cart-btn {
  399. position: relative;
  400. width: 72rpx;
  401. height: 72rpx;
  402. border-radius: 50%;
  403. background: #fff;
  404. border: 1rpx solid #eee;
  405. display: flex;
  406. align-items: center;
  407. justify-content: center;
  408. flex-shrink: 0;
  409. }
  410. .cart-icon {
  411. font-size: 36rpx;
  412. }
  413. .cart-badge {
  414. position: absolute;
  415. top: -6rpx;
  416. right: -6rpx;
  417. min-width: 32rpx;
  418. height: 32rpx;
  419. line-height: 32rpx;
  420. padding: 0 8rpx;
  421. border-radius: 16rpx;
  422. background: #FF4D4F;
  423. color: #fff;
  424. font-size: 20rpx;
  425. text-align: center;
  426. box-sizing: border-box;
  427. }
  428. /* Member Upgrade Banner */
  429. .member-banner {
  430. display: flex;
  431. align-items: center;
  432. justify-content: space-between;
  433. background: linear-gradient(135deg, #FFF7ED, #FFEDD5);
  434. margin: 16rpx;
  435. padding: 20rpx 24rpx;
  436. border-radius: 16rpx;
  437. border: 2rpx solid #FED7AA;
  438. }
  439. .member-banner-content {
  440. display: flex;
  441. align-items: center;
  442. }
  443. .member-banner-icon {
  444. font-size: 40rpx;
  445. margin-right: 16rpx;
  446. }
  447. .member-banner-texts {
  448. display: flex;
  449. flex-direction: column;
  450. }
  451. .member-banner-title {
  452. font-size: 28rpx;
  453. font-weight: bold;
  454. color: #C2410C;
  455. }
  456. .member-banner-desc {
  457. font-size: 22rpx;
  458. color: #F97316;
  459. margin-top: 4rpx;
  460. }
  461. .member-banner-action {
  462. font-size: 26rpx;
  463. color: #F97316;
  464. font-weight: 500;
  465. }
  466. /* 推荐商品 */
  467. .recommend-section {
  468. background: #fff;
  469. border-radius: 16rpx;
  470. margin: 16rpx;
  471. padding: 20rpx;
  472. }
  473. .recommend-header {
  474. margin-bottom: 16rpx;
  475. }
  476. .recommend-title {
  477. font-size: 28rpx;
  478. font-weight: bold;
  479. color: #333;
  480. }
  481. .recommend-scroll {
  482. white-space: nowrap;
  483. padding-bottom: 8rpx;
  484. }
  485. .recommend-card {
  486. display: inline-flex;
  487. flex-direction: column;
  488. width: 160rpx;
  489. margin-right: 12rpx;
  490. flex-shrink: 0;
  491. }
  492. .recommend-img {
  493. width: 160rpx;
  494. height: 160rpx;
  495. border-radius: 10rpx;
  496. background: #f0f0f0;
  497. margin-bottom: 6rpx;
  498. }
  499. .recommend-name {
  500. font-size: 22rpx;
  501. color: #333;
  502. lines: 1;
  503. overflow: hidden;
  504. text-overflow: ellipsis;
  505. white-space: nowrap;
  506. margin-bottom: 4rpx;
  507. }
  508. .recommend-price {
  509. font-size: 24rpx;
  510. color: #F97316;
  511. font-weight: bold;
  512. }
  513. /* Level 2 子分类:横向滚动chips */
  514. .cat-level2-scroll {
  515. background: #fff;
  516. white-space: nowrap;
  517. padding: 12rpx 16rpx;
  518. border-bottom: 1rpx solid #f0f0f0;
  519. }
  520. .cat2-chips {
  521. display: inline-flex;
  522. gap: 12rpx;
  523. padding: 0 4rpx;
  524. }
  525. .cat2-chip {
  526. display: inline-block;
  527. padding: 8rpx 24rpx;
  528. font-size: 24rpx;
  529. color: #666;
  530. background: #f0f0f0;
  531. border-radius: 30rpx;
  532. flex-shrink: 0;
  533. }
  534. .cat2-chip.active {
  535. background: #F97316;
  536. color: #fff;
  537. }
  538. /* ==================== 商品列表 ==================== */
  539. .product-list {
  540. flex: 1;
  541. height: calc(100vh - 220rpx);
  542. }
  543. .login-hint-bar {
  544. text-align: center;
  545. padding: 20rpx;
  546. background: #fef9e7;
  547. border-top: 1rpx solid #f0e6c0;
  548. }
  549. .login-hint-text {
  550. font-size: 24rpx;
  551. color: #b8860b;
  552. }
  553. .bottom-spacer {
  554. height: 120rpx;
  555. }
  556. .grid {
  557. display: flex;
  558. flex-wrap: wrap;
  559. padding: 20rpx;
  560. justify-content: space-between;
  561. }
  562. .product-card {
  563. width: 49%;
  564. background: #fff;
  565. border-radius: 16rpx;
  566. overflow: hidden;
  567. margin-bottom: 20rpx;
  568. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  569. }
  570. .cover {
  571. width: 100%;
  572. height: 320rpx;
  573. background: #f0f0f0;
  574. }
  575. .card-body {
  576. padding: 16rpx;
  577. }
  578. .product-name {
  579. display: block;
  580. font-size: 28rpx;
  581. color: #333;
  582. lines: 2;
  583. overflow: hidden;
  584. text-overflow: ellipsis;
  585. display: -webkit-box;
  586. -webkit-line-clamp: 2;
  587. -webkit-box-orient: vertical;
  588. margin-bottom: 12rpx;
  589. }
  590. .price-row {
  591. display: flex;
  592. align-items: baseline;
  593. margin-bottom: 8rpx;
  594. }
  595. .price {
  596. font-size: 32rpx;
  597. color: #F97316;
  598. font-weight: bold;
  599. margin-right: 12rpx;
  600. }
  601. .member-price {
  602. font-size: 22rpx;
  603. color: #999;
  604. }
  605. .vendor-name {
  606. font-size: 22rpx;
  607. color: #999;
  608. }
  609. .loading-wrap,
  610. .empty-wrap {
  611. display: flex;
  612. flex-direction: column;
  613. align-items: center;
  614. padding-top: 200rpx;
  615. }
  616. .loading-text {
  617. font-size: 26rpx;
  618. color: #999;
  619. }
  620. .empty-icon {
  621. font-size: 100rpx;
  622. margin-bottom: 30rpx;
  623. }
  624. .empty-text {
  625. font-size: 28rpx;
  626. color: #999;
  627. }
  628. .loading-more,
  629. .no-more {
  630. text-align: center;
  631. padding: 30rpx;
  632. }
  633. .loading-text {
  634. font-size: 24rpx;
  635. color: #999;
  636. }
  637. .no-more-text {
  638. font-size: 24rpx;
  639. color: #ccc;
  640. }
  641. /* Loading skeleton grid */
  642. .skeleton-grid {
  643. display: flex;
  644. flex-wrap: wrap;
  645. padding: 20rpx;
  646. justify-content: space-between;
  647. }
  648. .skeleton-card {
  649. width: 49%;
  650. background: #fff;
  651. border-radius: 16rpx;
  652. overflow: hidden;
  653. margin-bottom: 20rpx;
  654. }
  655. .skeleton-img {
  656. width: 100%;
  657. height: 320rpx;
  658. background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  659. background-size: 200% 100%;
  660. animation: shimmer 1.5s infinite;
  661. }
  662. .skeleton-body {
  663. padding: 16rpx;
  664. }
  665. .skeleton-line {
  666. height: 24rpx;
  667. border-radius: 6rpx;
  668. background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  669. background-size: 200% 100%;
  670. animation: shimmer 1.5s infinite;
  671. }
  672. .skeleton-title { width: 70%; margin-bottom: 12rpx; }
  673. .skeleton-price { width: 40%; margin-bottom: 8rpx; }
  674. .skeleton-vendor { width: 50%; }
  675. @keyframes shimmer {
  676. 0% { background-position: 200% 0; }
  677. 100% { background-position: -200% 0; }
  678. }
  679. /* Product grid fade-in */
  680. .grid.fade-in {
  681. animation: gridFadeIn 0.3s ease-out;
  682. }
  683. @keyframes gridFadeIn {
  684. from { opacity: 0; transform: translateY(10rpx); }
  685. to { opacity: 1; transform: translateY(0); }
  686. }
  687. </style>