index.vue 17 KB

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