index.vue 12 KB

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