index.vue 15 KB

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