index.vue 11 KB

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