index.vue 9.6 KB

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