index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. this._onLoadFired = true
  172. },
  173. onShow() {
  174. if (this._onLoadFired) {
  175. this._onLoadFired = false
  176. } else {
  177. this.checkMemberStatus()
  178. this.isLoggedIn = !!uni.getStorageSync('token')
  179. this.loadCartCount()
  180. }
  181. },
  182. methods: {
  183. loadCategories() {
  184. goodsCategory('0').then(res => {
  185. if (res.code === 200 && res.data) {
  186. this.categories = res.data || []
  187. }
  188. }).catch(() => {})
  189. },
  190. onCat1Select(cat) {
  191. if (this.currentCat1 && this.currentCat1.id === cat.id) return
  192. this.currentCat1 = cat
  193. this.currentCat2 = null
  194. this.page = 1
  195. this.products = []
  196. this.noMore = false
  197. this.loadProducts()
  198. },
  199. onCat2Select(cat) {
  200. this.currentCat2 = cat
  201. this.page = 1
  202. this.products = []
  203. this.noMore = false
  204. this.loadProducts()
  205. },
  206. loadProducts() {
  207. if (this.loading) return
  208. this.loading = true
  209. const params = { page: this.page, size: this.size }
  210. if (this.currentCat2) {
  211. params.categoryId = this.currentCat2.id
  212. } else if (this.currentCat1) {
  213. params.categoryId = this.currentCat1.id
  214. }
  215. productList(params).then(res => {
  216. this.loading = false
  217. this.refreshing = false
  218. if (res.code === 200 && res.data) {
  219. const records = res.data.records || []
  220. if (this.page === 1) {
  221. this.products = records
  222. } else {
  223. this.products = this.products.concat(records)
  224. }
  225. const total = res.data.total || 0
  226. this.noMore = this.products.length >= total
  227. }
  228. }).catch(() => {
  229. this.loading = false
  230. this.refreshing = false
  231. })
  232. },
  233. onRefresh() {
  234. this.refreshing = true
  235. this.page = 1
  236. this.noMore = false
  237. this.loadProducts()
  238. },
  239. onLoadMore() {
  240. if (this.noMore || this.loadingMore) return
  241. this.page++
  242. this.loadingMore = true
  243. const params = { page: this.page, size: this.size }
  244. if (this.currentCat2) {
  245. params.categoryId = this.currentCat2.id
  246. } else if (this.currentCat1) {
  247. params.categoryId = this.currentCat1.id
  248. }
  249. productList(params).then(res => {
  250. this.loadingMore = false
  251. if (res.code === 200 && res.data) {
  252. const records = res.data.records || []
  253. this.products = this.products.concat(records)
  254. const total = res.data.total || 0
  255. this.noMore = this.products.length >= total
  256. }
  257. }).catch(() => {
  258. this.loadingMore = false
  259. })
  260. },
  261. checkMemberStatus() {
  262. if (!uni.getStorageSync('token')) {
  263. this.isMember = false
  264. return
  265. }
  266. var that = this
  267. getMyMembership().then(function(res) {
  268. if (res.data && res.data.memberLevel) {
  269. that.isMember = res.data.memberLevel !== 'FREE'
  270. }
  271. }).catch(function() {
  272. that.isMember = false
  273. })
  274. },
  275. goUpgrade() {
  276. uni.navigateTo({ url: '/pages/membership/upgrade' })
  277. },
  278. goDetail(id) {
  279. uni.navigateTo({ url: '/pages/shop/detail/detail?id=' + id })
  280. },
  281. goSearch() {
  282. uni.navigateTo({ url: '/pages/shop/search/search' })
  283. },
  284. goCart() {
  285. uni.navigateTo({ url: '/pages/shop/cart/cart' })
  286. },
  287. loadCartCount() {
  288. if (!uni.getStorageSync('token')) {
  289. this.cartCount = 0
  290. return
  291. }
  292. cartCount().then(res => {
  293. if (res.code === 200) {
  294. this.cartCount = res.data || 0
  295. }
  296. }).catch(() => {})
  297. },
  298. loadRecommended() {
  299. productList({ page: 1, size: 6, sort: 'sales' }).then(res => {
  300. if (res.code === 200 && res.data) {
  301. this.recommended = res.data.records || []
  302. }
  303. }).catch(() => {})
  304. },
  305. formatPriceWithSymbol(price) {
  306. if (price === null || price === undefined) return ''
  307. return '¥' + (Number(price) / 100).toFixed(2)
  308. },
  309. getImageUrl: function(path) {
  310. return config.API_BASE_URL + path
  311. }
  312. }
  313. }
  314. </script>
  315. <style scoped>
  316. .container {
  317. display: flex;
  318. flex-direction: column;
  319. height: 100vh;
  320. background: #f5f5f5;
  321. }
  322. /* ==================== 分类布局 ==================== */
  323. /* Level 1 分类:2列横排网格 */
  324. .cat-level1 {
  325. display: flex;
  326. flex-wrap: wrap;
  327. padding: 16rpx 16rpx 8rpx;
  328. background: #fff;
  329. border-bottom: 1rpx solid #eee;
  330. }
  331. .cat1-item {
  332. width: 25%;
  333. box-sizing: border-box;
  334. padding: 16rpx 8rpx;
  335. display: flex;
  336. flex-direction: column;
  337. align-items: center;
  338. text-align: center;
  339. border-radius: 16rpx;
  340. margin-bottom: 8rpx;
  341. }
  342. .cat1-item.active {
  343. background: rgba(249, 115, 22, 0.08);
  344. }
  345. .cat1-icon {
  346. width: 72rpx;
  347. height: 72rpx;
  348. border-radius: 50%;
  349. margin-bottom: 6rpx;
  350. background: #f5f5f5;
  351. }
  352. .cat1-icon-placeholder {
  353. width: 72rpx;
  354. height: 72rpx;
  355. border-radius: 50%;
  356. margin-bottom: 6rpx;
  357. background: linear-gradient(135deg, #667eea, #764ba2);
  358. display: flex;
  359. align-items: center;
  360. justify-content: center;
  361. }
  362. .cat1-icon-text {
  363. font-size: 32rpx;
  364. font-weight: bold;
  365. color: #fff;
  366. }
  367. .cat1-name {
  368. font-size: 24rpx;
  369. color: #333;
  370. max-width: 140rpx;
  371. overflow: hidden;
  372. text-overflow: ellipsis;
  373. white-space: nowrap;
  374. }
  375. .cat1-item.active .cat1-name {
  376. color: #F97316;
  377. font-weight: bold;
  378. }
  379. /* 搜索栏 + 购物车 */
  380. .search-row {
  381. display: flex;
  382. align-items: center;
  383. margin: 16rpx 16rpx 0;
  384. gap: 16rpx;
  385. }
  386. .search-bar {
  387. flex: 1;
  388. display: flex;
  389. align-items: center;
  390. background: #fff;
  391. padding: 16rpx 24rpx;
  392. border-radius: 50rpx;
  393. border: 1rpx solid #eee;
  394. }
  395. .search-icon {
  396. font-size: 32rpx;
  397. margin-right: 12rpx;
  398. }
  399. .search-placeholder {
  400. font-size: 26rpx;
  401. color: #bbb;
  402. }
  403. .cart-btn {
  404. position: relative;
  405. width: 72rpx;
  406. height: 72rpx;
  407. border-radius: 50%;
  408. background: #fff;
  409. border: 1rpx solid #eee;
  410. display: flex;
  411. align-items: center;
  412. justify-content: center;
  413. flex-shrink: 0;
  414. }
  415. .cart-icon {
  416. font-size: 36rpx;
  417. }
  418. .cart-badge {
  419. position: absolute;
  420. top: -6rpx;
  421. right: -6rpx;
  422. min-width: 32rpx;
  423. height: 32rpx;
  424. line-height: 32rpx;
  425. padding: 0 8rpx;
  426. border-radius: 16rpx;
  427. background: #FF4D4F;
  428. color: #fff;
  429. font-size: 20rpx;
  430. text-align: center;
  431. box-sizing: border-box;
  432. }
  433. /* Member Upgrade Banner */
  434. .member-banner {
  435. display: flex;
  436. align-items: center;
  437. justify-content: space-between;
  438. background: linear-gradient(135deg, #FFF7ED, #FFEDD5);
  439. margin: 16rpx;
  440. padding: 20rpx 24rpx;
  441. border-radius: 16rpx;
  442. border: 2rpx solid #FED7AA;
  443. }
  444. .member-banner-content {
  445. display: flex;
  446. align-items: center;
  447. }
  448. .member-banner-icon {
  449. font-size: 40rpx;
  450. margin-right: 16rpx;
  451. }
  452. .member-banner-texts {
  453. display: flex;
  454. flex-direction: column;
  455. }
  456. .member-banner-title {
  457. font-size: 28rpx;
  458. font-weight: bold;
  459. color: #C2410C;
  460. }
  461. .member-banner-desc {
  462. font-size: 22rpx;
  463. color: #F97316;
  464. margin-top: 4rpx;
  465. }
  466. .member-banner-action {
  467. font-size: 26rpx;
  468. color: #F97316;
  469. font-weight: 500;
  470. }
  471. /* 推荐商品 */
  472. .recommend-section {
  473. background: #fff;
  474. border-radius: 16rpx;
  475. margin: 16rpx;
  476. padding: 20rpx;
  477. }
  478. .recommend-header {
  479. margin-bottom: 16rpx;
  480. }
  481. .recommend-title {
  482. font-size: 28rpx;
  483. font-weight: bold;
  484. color: #333;
  485. }
  486. .recommend-scroll {
  487. white-space: nowrap;
  488. padding-bottom: 8rpx;
  489. }
  490. .recommend-card {
  491. display: inline-flex;
  492. flex-direction: column;
  493. width: 160rpx;
  494. margin-right: 12rpx;
  495. flex-shrink: 0;
  496. }
  497. .recommend-img {
  498. width: 160rpx;
  499. height: 160rpx;
  500. border-radius: 10rpx;
  501. background: #f0f0f0;
  502. margin-bottom: 6rpx;
  503. }
  504. .recommend-name {
  505. font-size: 22rpx;
  506. color: #333;
  507. lines: 1;
  508. overflow: hidden;
  509. text-overflow: ellipsis;
  510. white-space: nowrap;
  511. margin-bottom: 4rpx;
  512. }
  513. .recommend-price {
  514. font-size: 24rpx;
  515. color: #F97316;
  516. font-weight: bold;
  517. }
  518. /* Level 2 子分类:横向滚动chips */
  519. .cat-level2-scroll {
  520. background: #fff;
  521. white-space: nowrap;
  522. padding: 12rpx 16rpx;
  523. border-bottom: 1rpx solid #f0f0f0;
  524. }
  525. .cat2-chips {
  526. display: inline-flex;
  527. gap: 12rpx;
  528. padding: 0 4rpx;
  529. }
  530. .cat2-chip {
  531. display: inline-block;
  532. padding: 8rpx 24rpx;
  533. font-size: 24rpx;
  534. color: #666;
  535. background: #f0f0f0;
  536. border-radius: 30rpx;
  537. flex-shrink: 0;
  538. }
  539. .cat2-chip.active {
  540. background: #F97316;
  541. color: #fff;
  542. }
  543. /* ==================== 商品列表 ==================== */
  544. .product-list {
  545. flex: 1;
  546. height: calc(100vh - 220rpx);
  547. }
  548. .login-hint-bar {
  549. text-align: center;
  550. padding: 20rpx;
  551. background: #fef9e7;
  552. border-top: 1rpx solid #f0e6c0;
  553. }
  554. .login-hint-text {
  555. font-size: 24rpx;
  556. color: #b8860b;
  557. }
  558. .bottom-spacer {
  559. height: 120rpx;
  560. }
  561. .grid {
  562. display: flex;
  563. flex-wrap: wrap;
  564. padding: 20rpx;
  565. justify-content: space-between;
  566. }
  567. .product-card {
  568. width: 49%;
  569. background: #fff;
  570. border-radius: 16rpx;
  571. overflow: hidden;
  572. margin-bottom: 20rpx;
  573. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  574. }
  575. .cover {
  576. width: 100%;
  577. height: 320rpx;
  578. background: #f0f0f0;
  579. }
  580. .card-body {
  581. padding: 16rpx;
  582. }
  583. .product-name {
  584. display: block;
  585. font-size: 28rpx;
  586. color: #333;
  587. lines: 2;
  588. overflow: hidden;
  589. text-overflow: ellipsis;
  590. display: -webkit-box;
  591. -webkit-line-clamp: 2;
  592. -webkit-box-orient: vertical;
  593. margin-bottom: 12rpx;
  594. }
  595. .price-row {
  596. display: flex;
  597. align-items: baseline;
  598. margin-bottom: 8rpx;
  599. }
  600. .price {
  601. font-size: 32rpx;
  602. color: #F97316;
  603. font-weight: bold;
  604. margin-right: 12rpx;
  605. }
  606. .member-price {
  607. font-size: 22rpx;
  608. color: #999;
  609. }
  610. .vendor-name {
  611. font-size: 22rpx;
  612. color: #999;
  613. }
  614. .loading-wrap,
  615. .empty-wrap {
  616. display: flex;
  617. flex-direction: column;
  618. align-items: center;
  619. padding-top: 200rpx;
  620. }
  621. .loading-text {
  622. font-size: 26rpx;
  623. color: #999;
  624. }
  625. .empty-icon {
  626. font-size: 100rpx;
  627. margin-bottom: 30rpx;
  628. }
  629. .empty-text {
  630. font-size: 28rpx;
  631. color: #999;
  632. }
  633. .loading-more,
  634. .no-more {
  635. text-align: center;
  636. padding: 30rpx;
  637. }
  638. .loading-text {
  639. font-size: 24rpx;
  640. color: #999;
  641. }
  642. .no-more-text {
  643. font-size: 24rpx;
  644. color: #ccc;
  645. }
  646. /* Loading skeleton grid */
  647. .skeleton-grid {
  648. display: flex;
  649. flex-wrap: wrap;
  650. padding: 20rpx;
  651. justify-content: space-between;
  652. }
  653. .skeleton-card {
  654. width: 49%;
  655. background: #fff;
  656. border-radius: 16rpx;
  657. overflow: hidden;
  658. margin-bottom: 20rpx;
  659. }
  660. .skeleton-img {
  661. width: 100%;
  662. height: 320rpx;
  663. background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  664. background-size: 200% 100%;
  665. animation: shimmer 1.5s infinite;
  666. }
  667. .skeleton-body {
  668. padding: 16rpx;
  669. }
  670. .skeleton-line {
  671. height: 24rpx;
  672. border-radius: 6rpx;
  673. background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
  674. background-size: 200% 100%;
  675. animation: shimmer 1.5s infinite;
  676. }
  677. .skeleton-title { width: 70%; margin-bottom: 12rpx; }
  678. .skeleton-price { width: 40%; margin-bottom: 8rpx; }
  679. .skeleton-vendor { width: 50%; }
  680. @keyframes shimmer {
  681. 0% { background-position: 200% 0; }
  682. 100% { background-position: -200% 0; }
  683. }
  684. /* Product grid fade-in */
  685. .grid.fade-in {
  686. animation: gridFadeIn 0.3s ease-out;
  687. }
  688. @keyframes gridFadeIn {
  689. from { opacity: 0; transform: translateY(10rpx); }
  690. to { opacity: 1; transform: translateY(0); }
  691. }
  692. </style>