index.vue 9.9 KB

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