index.vue 17 KB

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