index.vue 15 KB

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