index.vue 17 KB

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