| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701 |
- <template>
- <view class="container">
- <!-- Member Upgrade Banner -->
- <view v-if="!isMember" class="member-banner" @click="goUpgrade">
- <view class="member-banner-content">
- <text class="member-banner-icon">👑</text>
- <view class="member-banner-texts">
- <text class="member-banner-title">开通会员</text>
- <text class="member-banner-desc">享专属折扣与成长服务</text>
- </view>
- </view>
- <text class="member-banner-action">立即开通 ›</text>
- </view>
- <!-- 搜索栏 + 购物车 -->
- <view class="search-row">
- <view class="search-bar" @click="goSearch">
- <text class="search-icon">🔍</text>
- <text class="search-placeholder">搜索商品</text>
- </view>
- <view class="cart-btn" @click="goCart">
- <text class="cart-icon">🛒</text>
- <text v-if="cartCount > 0" class="cart-badge">{{ cartCount > 99 ? '99+' : cartCount }}</text>
- </view>
- </view>
- <!-- Level 1 分类:2列横排网格 -->
- <view class="cat-level1">
- <view
- v-for="cat in categories"
- :key="cat.id"
- :class="['cat1-item', currentCat1 && currentCat1.id === cat.id ? 'active' : '']"
- @click="onCat1Select(cat)"
- >
- <image v-if="cat.image" class="cat1-icon" :src="cat.image" mode="aspectFit" />
- <view v-else class="cat1-icon-placeholder">
- <text class="cat1-icon-text">{{ cat.name.substr(0, 1) }}</text>
- </view>
- <text class="cat1-name">{{ cat.name }}</text>
- </view>
- </view>
- <!-- Level 2 子分类:横向滚动chips -->
- <scroll-view
- v-if="currentCat1 && currentCat1.children && currentCat1.children.length"
- scroll-x
- enable-flex
- show-scrollbar="false"
- class="cat-level2-scroll"
- >
- <view class="cat2-chips">
- <view
- :class="['cat2-chip', !currentCat2 ? 'active' : '']"
- @click="onCat2Select(null)"
- >全部</view>
- <view
- v-for="child in currentCat1.children"
- :key="child.id"
- :class="['cat2-chip', currentCat2 && currentCat2.id === child.id ? 'active' : '']"
- @click="onCat2Select(child)"
- >{{ child.name }}</view>
- </view>
- </scroll-view>
- <!-- 商品列表 -->
- <scroll-view
- scroll-y
- class="product-list"
- @scrolltolower="onLoadMore"
- refresher-enabled
- :refresher-triggered="refreshing"
- @refresherrefresh="onRefresh"
- >
- <!-- 推荐商品 -->
- <view class="recommend-section" v-if="recommended.length > 0">
- <view class="recommend-header">
- <text class="recommend-title">🔥 推荐商品</text>
- </view>
- <scroll-view scroll-x enable-flex show-scrollbar="false" class="recommend-scroll">
- <view
- v-for="item in recommended"
- :key="item.id"
- class="recommend-card"
- @click="goDetail(item.id)"
- >
- <image
- class="recommend-img"
- :src="getImageUrl(item.image || item.coverImage) || '/static/default-product.png'"
- mode="aspectFill"
- />
- <text class="recommend-name">{{ item.name }}</text>
- <text class="recommend-price">{{ formatPriceWithSymbol(item.price) }}</text>
- </view>
- </scroll-view>
- </view>
- <!-- Loading skeleton -->
- <view v-if="loading && products.length === 0" class="skeleton-grid">
- <view class="skeleton-card" v-for="n in 6" :key="n">
- <view class="skeleton-img"></view>
- <view class="skeleton-body">
- <view class="skeleton-line skeleton-title"></view>
- <view class="skeleton-line skeleton-price"></view>
- <view class="skeleton-line skeleton-vendor"></view>
- </view>
- </view>
- </view>
- <view v-else-if="products.length === 0" class="empty-wrap">
- <text class="empty-icon">📦</text>
- <text class="empty-text">暂无商品</text>
- </view>
- <view v-else class="grid fade-in">
- <view
- v-for="item in products"
- :key="item.id"
- class="product-card"
- @click="goDetail(item.id)"
- >
- <image
- class="cover"
- :src="getImageUrl(item.image || item.coverImage) || '/static/default-product.png'"
- mode="aspectFill"
- />
- <view class="card-body">
- <text class="product-name">{{ item.name }}</text>
- <view class="price-row">
- <text class="price">{{ formatPriceWithSymbol(item.price) }}</text>
- <text v-if="item.memberPrice" class="member-price">会员{{ formatPriceWithSymbol(item.memberPrice) }}</text>
- </view>
- <text class="vendor-name">{{ item.brandName || '平台官方' }}</text>
- </view>
- </view>
- </view>
- <view v-if="loadingMore" class="loading-more">
- <text class="loading-text">加载中...</text>
- </view>
- <view v-if="noMore && products.length > 0" class="no-more">
- <text class="no-more-text">— 没有更多了 —</text>
- </view>
- <view v-if="!isLoggedIn" class="login-hint-bar">
- <text class="login-hint-text">🔒 登录查看全部商品和会员价</text>
- </view>
- <view class="bottom-spacer"></view>
- </scroll-view>
- </view>
- </template>
- <script>
- import { goodsCategory, productList, getMyMembership, cartCount } from '@/utils/api.js'
- import config from '@/config.js'
- export default {
- data() {
- return {
- categories: [],
- currentCat1: null,
- currentCat2: null,
- products: [],
- page: 1,
- size: 20,
- loading: false,
- loadingMore: false,
- refreshing: false,
- noMore: false,
- isLoggedIn: false,
- isMember: false,
- recommended: [],
- cartCount: 0
- }
- },
- onLoad() {
- this.checkMemberStatus()
- this.isLoggedIn = !!uni.getStorageSync('token')
- this.loadCategories()
- this.loadProducts()
- this.loadRecommended()
- this.loadCartCount()
- },
- onShow() {
- this.checkMemberStatus()
- this.isLoggedIn = !!uni.getStorageSync('token')
- this.loadCartCount()
- },
- methods: {
- loadCategories() {
- goodsCategory('0').then(res => {
- if (res.code === 200 && res.data) {
- this.categories = res.data || []
- }
- }).catch(() => {})
- },
- onCat1Select(cat) {
- if (this.currentCat1 && this.currentCat1.id === cat.id) return
- this.currentCat1 = cat
- this.currentCat2 = null
- this.page = 1
- this.products = []
- this.noMore = false
- this.loadProducts()
- },
- onCat2Select(cat) {
- this.currentCat2 = cat
- this.page = 1
- this.products = []
- this.noMore = false
- this.loadProducts()
- },
- loadProducts() {
- if (this.loading) return
- this.loading = true
- const params = { page: this.page, size: this.size }
- if (this.currentCat2) {
- params.categoryId = this.currentCat2.id
- } else if (this.currentCat1) {
- params.categoryId = this.currentCat1.id
- }
- productList(params).then(res => {
- this.loading = false
- this.refreshing = false
- if (res.code === 200 && res.data) {
- const records = res.data.records || []
- if (this.page === 1) {
- this.products = records
- } else {
- this.products = this.products.concat(records)
- }
- const total = res.data.total || 0
- this.noMore = this.products.length >= total
- }
- }).catch(() => {
- this.loading = false
- this.refreshing = false
- })
- },
- onRefresh() {
- this.refreshing = true
- this.page = 1
- this.noMore = false
- this.loadProducts()
- },
- onLoadMore() {
- if (this.noMore || this.loadingMore) return
- this.page++
- this.loadingMore = true
- const params = { page: this.page, size: this.size }
- if (this.currentCat2) {
- params.categoryId = this.currentCat2.id
- } else if (this.currentCat1) {
- params.categoryId = this.currentCat1.id
- }
- productList(params).then(res => {
- this.loadingMore = false
- if (res.code === 200 && res.data) {
- const records = res.data.records || []
- this.products = this.products.concat(records)
- const total = res.data.total || 0
- this.noMore = this.products.length >= total
- }
- }).catch(() => {
- this.loadingMore = false
- })
- },
- checkMemberStatus() {
- if (!uni.getStorageSync('token')) {
- this.isMember = false
- return
- }
- var that = this
- getMyMembership().then(function(res) {
- if (res.data && res.data.memberLevel) {
- that.isMember = res.data.memberLevel !== 'FREE'
- }
- }).catch(function() {
- that.isMember = false
- })
- },
- goUpgrade() {
- uni.navigateTo({ url: '/pages/membership/upgrade' })
- },
- goDetail(id) {
- uni.navigateTo({ url: '/pages/shop/detail/detail?id=' + id })
- },
- goSearch() {
- uni.navigateTo({ url: '/pages/shop/search/search' })
- },
- goCart() {
- uni.navigateTo({ url: '/pages/shop/cart/cart' })
- },
- loadCartCount() {
- if (!uni.getStorageSync('token')) {
- this.cartCount = 0
- return
- }
- cartCount().then(res => {
- if (res.code === 200) {
- this.cartCount = res.data || 0
- }
- }).catch(() => {})
- },
- loadRecommended() {
- productList({ page: 1, size: 6, sort: 'sales' }).then(res => {
- if (res.code === 200 && res.data) {
- this.recommended = res.data.records || []
- }
- }).catch(() => {})
- },
- formatPriceWithSymbol(price) {
- if (price === null || price === undefined) return ''
- return '¥' + (Number(price) / 100).toFixed(2)
- },
- getImageUrl: function(path) {
- return config.API_BASE_URL + path
- }
- }
- }
- </script>
- <style scoped>
- .container {
- display: flex;
- flex-direction: column;
- height: 100vh;
- background: #f5f5f5;
- }
- /* ==================== 分类布局 ==================== */
- /* Level 1 分类:2列横排网格 */
- .cat-level1 {
- display: flex;
- flex-wrap: wrap;
- padding: 16rpx 16rpx 8rpx;
- background: #fff;
- border-bottom: 1rpx solid #eee;
- }
- .cat1-item {
- width: 25%;
- box-sizing: border-box;
- padding: 16rpx 8rpx;
- display: flex;
- flex-direction: column;
- align-items: center;
- text-align: center;
- border-radius: 16rpx;
- margin-bottom: 8rpx;
- }
- .cat1-item.active {
- background: rgba(249, 115, 22, 0.08);
- }
- .cat1-icon {
- width: 72rpx;
- height: 72rpx;
- border-radius: 50%;
- margin-bottom: 6rpx;
- background: #f5f5f5;
- }
- .cat1-icon-placeholder {
- width: 72rpx;
- height: 72rpx;
- border-radius: 50%;
- margin-bottom: 6rpx;
- background: linear-gradient(135deg, #667eea, #764ba2);
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .cat1-icon-text {
- font-size: 32rpx;
- font-weight: bold;
- color: #fff;
- }
- .cat1-name {
- font-size: 24rpx;
- color: #333;
- max-width: 140rpx;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .cat1-item.active .cat1-name {
- color: #F97316;
- font-weight: bold;
- }
- /* 搜索栏 + 购物车 */
- .search-row {
- display: flex;
- align-items: center;
- margin: 16rpx 16rpx 0;
- gap: 16rpx;
- }
- .search-bar {
- flex: 1;
- display: flex;
- align-items: center;
- background: #fff;
- padding: 16rpx 24rpx;
- border-radius: 50rpx;
- border: 1rpx solid #eee;
- }
- .search-icon {
- font-size: 32rpx;
- margin-right: 12rpx;
- }
- .search-placeholder {
- font-size: 26rpx;
- color: #bbb;
- }
- .cart-btn {
- position: relative;
- width: 72rpx;
- height: 72rpx;
- border-radius: 50%;
- background: #fff;
- border: 1rpx solid #eee;
- display: flex;
- align-items: center;
- justify-content: center;
- flex-shrink: 0;
- }
- .cart-icon {
- font-size: 36rpx;
- }
- .cart-badge {
- position: absolute;
- top: -6rpx;
- right: -6rpx;
- min-width: 32rpx;
- height: 32rpx;
- line-height: 32rpx;
- padding: 0 8rpx;
- border-radius: 16rpx;
- background: #FF4D4F;
- color: #fff;
- font-size: 20rpx;
- text-align: center;
- box-sizing: border-box;
- }
- /* Member Upgrade Banner */
- .member-banner {
- display: flex;
- align-items: center;
- justify-content: space-between;
- background: linear-gradient(135deg, #FFF7ED, #FFEDD5);
- margin: 16rpx;
- padding: 20rpx 24rpx;
- border-radius: 16rpx;
- border: 2rpx solid #FED7AA;
- }
- .member-banner-content {
- display: flex;
- align-items: center;
- }
- .member-banner-icon {
- font-size: 40rpx;
- margin-right: 16rpx;
- }
- .member-banner-texts {
- display: flex;
- flex-direction: column;
- }
- .member-banner-title {
- font-size: 28rpx;
- font-weight: bold;
- color: #C2410C;
- }
- .member-banner-desc {
- font-size: 22rpx;
- color: #F97316;
- margin-top: 4rpx;
- }
- .member-banner-action {
- font-size: 26rpx;
- color: #F97316;
- font-weight: 500;
- }
- /* 推荐商品 */
- .recommend-section {
- background: #fff;
- border-radius: 16rpx;
- margin: 16rpx;
- padding: 20rpx;
- }
- .recommend-header {
- margin-bottom: 16rpx;
- }
- .recommend-title {
- font-size: 28rpx;
- font-weight: bold;
- color: #333;
- }
- .recommend-scroll {
- white-space: nowrap;
- padding-bottom: 8rpx;
- }
- .recommend-card {
- display: inline-flex;
- flex-direction: column;
- width: 160rpx;
- margin-right: 12rpx;
- flex-shrink: 0;
- }
- .recommend-img {
- width: 160rpx;
- height: 160rpx;
- border-radius: 10rpx;
- background: #f0f0f0;
- margin-bottom: 6rpx;
- }
- .recommend-name {
- font-size: 22rpx;
- color: #333;
- lines: 1;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- margin-bottom: 4rpx;
- }
- .recommend-price {
- font-size: 24rpx;
- color: #F97316;
- font-weight: bold;
- }
- /* Level 2 子分类:横向滚动chips */
- .cat-level2-scroll {
- background: #fff;
- white-space: nowrap;
- padding: 12rpx 16rpx;
- border-bottom: 1rpx solid #f0f0f0;
- }
- .cat2-chips {
- display: inline-flex;
- gap: 12rpx;
- padding: 0 4rpx;
- }
- .cat2-chip {
- display: inline-block;
- padding: 8rpx 24rpx;
- font-size: 24rpx;
- color: #666;
- background: #f0f0f0;
- border-radius: 30rpx;
- flex-shrink: 0;
- }
- .cat2-chip.active {
- background: #F97316;
- color: #fff;
- }
- /* ==================== 商品列表 ==================== */
- .product-list {
- flex: 1;
- height: calc(100vh - 220rpx);
- }
- .login-hint-bar {
- text-align: center;
- padding: 20rpx;
- background: #fef9e7;
- border-top: 1rpx solid #f0e6c0;
- }
- .login-hint-text {
- font-size: 24rpx;
- color: #b8860b;
- }
- .bottom-spacer {
- height: 120rpx;
- }
- .grid {
- display: flex;
- flex-wrap: wrap;
- padding: 20rpx;
- justify-content: space-between;
- }
- .product-card {
- width: 49%;
- background: #fff;
- border-radius: 16rpx;
- overflow: hidden;
- margin-bottom: 20rpx;
- box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
- }
- .cover {
- width: 100%;
- height: 320rpx;
- background: #f0f0f0;
- }
- .card-body {
- padding: 16rpx;
- }
- .product-name {
- display: block;
- font-size: 28rpx;
- color: #333;
- lines: 2;
- overflow: hidden;
- text-overflow: ellipsis;
- display: -webkit-box;
- -webkit-line-clamp: 2;
- -webkit-box-orient: vertical;
- margin-bottom: 12rpx;
- }
- .price-row {
- display: flex;
- align-items: baseline;
- margin-bottom: 8rpx;
- }
- .price {
- font-size: 32rpx;
- color: #F97316;
- font-weight: bold;
- margin-right: 12rpx;
- }
- .member-price {
- font-size: 22rpx;
- color: #999;
- }
- .vendor-name {
- font-size: 22rpx;
- color: #999;
- }
- .loading-wrap,
- .empty-wrap {
- display: flex;
- flex-direction: column;
- align-items: center;
- padding-top: 200rpx;
- }
- .loading-text {
- font-size: 26rpx;
- color: #999;
- }
- .empty-icon {
- font-size: 100rpx;
- margin-bottom: 30rpx;
- }
- .empty-text {
- font-size: 28rpx;
- color: #999;
- }
- .loading-more,
- .no-more {
- text-align: center;
- padding: 30rpx;
- }
- .loading-text {
- font-size: 24rpx;
- color: #999;
- }
- .no-more-text {
- font-size: 24rpx;
- color: #ccc;
- }
- /* Loading skeleton grid */
- .skeleton-grid {
- display: flex;
- flex-wrap: wrap;
- padding: 20rpx;
- justify-content: space-between;
- }
- .skeleton-card {
- width: 49%;
- background: #fff;
- border-radius: 16rpx;
- overflow: hidden;
- margin-bottom: 20rpx;
- }
- .skeleton-img {
- width: 100%;
- height: 320rpx;
- background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
- background-size: 200% 100%;
- animation: shimmer 1.5s infinite;
- }
- .skeleton-body {
- padding: 16rpx;
- }
- .skeleton-line {
- height: 24rpx;
- border-radius: 6rpx;
- background: linear-gradient(90deg, #f0f0f0 25%, #e8e8e8 50%, #f0f0f0 75%);
- background-size: 200% 100%;
- animation: shimmer 1.5s infinite;
- }
- .skeleton-title { width: 70%; margin-bottom: 12rpx; }
- .skeleton-price { width: 40%; margin-bottom: 8rpx; }
- .skeleton-vendor { width: 50%; }
- @keyframes shimmer {
- 0% { background-position: 200% 0; }
- 100% { background-position: -200% 0; }
- }
- /* Product grid fade-in */
- .grid.fade-in {
- animation: gridFadeIn 0.3s ease-out;
- }
- @keyframes gridFadeIn {
- from { opacity: 0; transform: translateY(10rpx); }
- to { opacity: 1; transform: translateY(0); }
- }
- </style>
|