baimacms/pages/category/category.vue

496 lines
11 KiB
Vue

<template>
<view class="category-container">
<!-- 头部搜索与导航 (1:1 还原首页样式) -->
<view class="header">
<view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
<view class="search-section">
<view class="search-bar-wrap">
<text class="search-icon">🔍</text>
<input class="search-input" placeholder="输入关键词或粘贴商品标题" placeholder-class="placeholder-style" />
<view class="search-btn-red">搜索</view>
</view>
<image class="search-right-ad" src="https://img.bc.haodanku.com/cms_web/1651735687" mode="aspectFit" @click="goHome"></image>
</view>
<scroll-view scroll-x class="nav-scroll" :show-scrollbar="false">
<view class="nav-item" :class="{ 'active': currentCatId == item.cat_id }" v-for="(item, index) in navList" :key="index" @click="switchCategory(item)">
<text class="nav-text">{{ item.name }}</text>
<view class="nav-line" v-if="currentCatId == item.cat_id"></view>
</view>
</scroll-view>
</view>
<scroll-view scroll-y class="main-content" @scrolltolower="loadMore">
<!-- 占位符适配固定头部 -->
<view class="header-placeholder" :style="{ height: (statusBarHeight + 90) + 'px' }"></view>
<!-- 子分类金刚区 -->
<view class="sub-grid-wrap" v-if="subCategories.length > 0">
<view class="sub-grid">
<view class="sub-item" v-for="(sub, idx) in subCategories" :key="idx" @click="selectSub(sub.son_category)">
<image class="sub-icon" :src="sub.img" mode="aspectFit"></image>
<text class="sub-text">{{ sub.son_name }}</text>
</view>
</view>
</view>
<!-- 排序筛选栏 -->
<view class="filter-bar">
<view class="filter-item" :class="{ 'active': sortType === 1 }" @click="changeSort(1)">综合</view>
<view class="filter-item" :class="{ 'active': sortType === 2 }" @click="changeSort(2)">销量</view>
<view class="filter-item" :class="{ 'active': sortType === 3 }" @click="changeSort(3)">
价格
<view class="price-arrows">
<text class="up" :class="{ 'hl': sortType === 3 && priceOrder === 'asc' }">▲</text>
<text class="down" :class="{ 'hl': sortType === 3 && priceOrder === 'desc' }">▼</text>
</view>
</view>
</view>
<!-- 商品列表 (1:1 还原首页风格) -->
<view class="goods-list">
<view class="goods-item" v-for="(goods, idx) in goodsList" :key="idx" @click="goToDetail(goods.id)">
<view class="g-img-wrap">
<image class="goods-img" :src="goods.image" mode="aspectFill"></image>
</view>
<view class="goods-info">
<view class="goods-title">
<text :class="goods.shopType === '天猫' ? 'tag-tmall' : 'tag-taobao'">{{ goods.shopType }}</text>
<text class="title-text">{{ goods.title }}</text>
</view>
<view class="goods-price-row">
<view class="g-price-left">
<text class="g-price-tip">券后¥</text>
<text class="g-price-val">{{ goods.finalPrice }}</text>
</view>
<view class="g-coupon-tag">券 {{ goods.couponValue }}元</view>
</view>
<view class="goods-sales">已售 {{ goods.sales }} 件</view>
</view>
</view>
</view>
<view class="loading-more" v-if="loading">加载中...</view>
<view class="loading-more" v-else-if="goodsList.length === 0">该分类暂无商品</view>
<view class="loading-more" v-else>-- 到底啦 --</view>
<view class="footer-placeholder"></view>
</scroll-view>
</view>
</template>
<script>
export default {
data() {
return {
statusBarHeight: 44,
currentCatId: 0,
currentSonId: 0,
navList: [],
subCategories: [],
goodsList: [],
page: 1,
sortType: 1,
priceOrder: 'desc',
loading: false,
finished: false
}
},
onLoad(options) {
this.currentCatId = options.cat_id || 1;
const sysInfo = uni.getSystemInfoSync();
this.statusBarHeight = sysInfo.statusBarHeight || 44;
this.getCategoryTabs();
this.getProducts(true);
},
methods: {
goHome() {
uni.reLaunch({
url: '/pages/index/index'
});
},
getCategoryTabs() {
uni.request({
url: 'https://api.cmspro.haodanku.com/index/category?cid=qOstW90',
success: (res) => {
if (res.data && res.data.code === 200) {
// 与首页 navList 保持一致的结构
this.navList = [{ name: '首页', cat_id: 0 }, { name: '推荐', cat_id: -1 }, ...res.data.data.category];
const currentCat = this.navList.find(c => c.cat_id == this.currentCatId);
if (currentCat && currentCat.son) {
this.subCategories = currentCat.son;
}
}
}
});
},
getProducts(refresh = false) {
if (refresh) {
this.page = 1;
this.finished = false;
this.goodsList = [];
}
if (this.loading || this.finished) return;
this.loading = true;
let sortParam = this.sortType;
if (this.sortType === 3) {
sortParam = this.priceOrder === 'asc' ? 3 : 4;
}
uni.request({
url: `https://api.cmspro.haodanku.com/category/category_items?cat_id=${this.currentCatId}&son_category=${this.currentSonId}&page=${this.page}&sort=${sortParam}&cid=qOstW90`,
success: (res) => {
if (res.data && res.data.code === 200) {
const list = res.data.data.map(item => ({
id: item.id,
image: item.itempic,
title: item.itemshorttitle,
finalPrice: item.itemendprice,
couponValue: item.couponmoney,
sales: item.itemsale >= 10000 ? (item.itemsale / 10000).toFixed(1) + '万' : item.itemsale,
shopType: item.shoptype === 'B' ? '天猫' : '淘宝'
}));
if (list.length === 0) {
this.finished = true;
} else {
this.goodsList = [...this.goodsList, ...list];
this.page++;
}
} else {
this.finished = true;
}
},
complete: () => {
this.loading = false;
}
});
},
switchCategory(item) {
if (item.cat_id <= 0) {
this.goHome();
return;
}
if (this.currentCatId == item.cat_id) return;
this.currentCatId = item.cat_id;
this.currentSonId = 0;
this.subCategories = item.son || [];
this.getProducts(true);
},
selectSub(sonId) {
this.currentSonId = sonId;
this.getProducts(true);
},
changeSort(type) {
if (type === 3 && this.sortType === 3) {
this.priceOrder = this.priceOrder === 'asc' ? 'desc' : 'asc';
} else {
this.sortType = type;
if (type === 3) this.priceOrder = 'desc';
}
this.getProducts(true);
},
loadMore() {
this.getProducts();
},
goToDetail(id) {
uni.navigateTo({
url: `/pages/detail/detail?id=${id}`
});
}
}
}
</script>
<style scoped>
.category-container {
width: 100%;
height: 100vh;
background-color: #f5f6f8;
display: flex;
flex-direction: column;
}
/* 头部区域 (与首页 1:1) */
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
background: #ffffff;
z-index: 100;
padding-bottom: 10rpx;
}
.search-section {
display: flex;
align-items: center;
padding: 20rpx 30rpx;
justify-content: space-between;
}
.search-bar-wrap {
flex: 1;
height: 72rpx;
background: #ffffff;
border: 2rpx solid #ff416c;
border-radius: 36rpx;
display: flex;
align-items: center;
padding-left: 30rpx;
padding-right: 4rpx;
position: relative;
}
.search-icon {
font-size: 30rpx;
color: #ff416c;
margin-right: 16rpx;
}
.search-input {
flex: 1;
font-size: 26rpx;
color: #333;
}
.search-btn-red {
width: 130rpx;
height: 64rpx;
background: linear-gradient(to right, #ff715a, #ff416c);
color: #ffffff;
font-size: 28rpx;
font-weight: bold;
border-radius: 32rpx;
display: flex;
align-items: center;
justify-content: center;
}
.search-right-ad {
width: 100rpx;
height: 80rpx;
margin-left: 20rpx;
}
.nav-scroll {
white-space: nowrap;
padding: 0 20rpx;
}
.nav-item {
display: inline-block;
padding: 10rpx 30rpx;
position: relative;
}
.nav-text {
font-size: 30rpx;
color: #333;
transition: all 0.3s;
}
.nav-item.active .nav-text {
color: #ff416c;
font-size: 34rpx;
font-weight: bold;
}
.nav-line {
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 40rpx;
height: 6rpx;
background-color: #ff416c;
border-radius: 6rpx;
}
.main-content {
flex: 1;
height: 100%;
}
.header-placeholder {
width: 100%;
}
/* 子分类金刚区 */
.sub-grid-wrap {
background: #ffffff;
margin: 20rpx;
border-radius: 16rpx;
padding: 20rpx 10rpx;
}
.sub-grid {
display: flex;
flex-wrap: wrap;
}
.sub-item {
width: 20%;
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 20rpx;
}
.sub-icon {
width: 80rpx;
height: 80rpx;
margin-bottom: 10rpx;
}
.sub-text {
font-size: 22rpx;
color: #333;
}
/* 筛选栏 */
.filter-bar {
display: flex;
background: #ffffff;
height: 88rpx;
align-items: center;
border-bottom: 1rpx solid #f1f1f1;
position: sticky;
top: 0;
z-index: 90;
}
.filter-item {
flex: 1;
text-align: center;
font-size: 28rpx;
color: #666;
display: flex;
align-items: center;
justify-content: center;
}
.filter-item.active {
color: #ff416c;
font-weight: bold;
}
.price-arrows {
display: flex;
flex-direction: column;
margin-left: 6rpx;
line-height: 1;
}
.price-arrows text {
font-size: 16rpx;
color: #ccc;
}
.price-arrows text.hl {
color: #ff416c;
}
/* 商品列表 */
.goods-list {
padding: 20rpx;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.goods-item {
width: 345rpx;
background-color: #ffffff;
border-radius: 16rpx;
overflow: hidden;
margin-bottom: 20rpx;
box-shadow: 0 4rpx 16rpx rgba(0,0,0,0.05);
}
.goods-img {
width: 345rpx;
height: 345rpx;
}
.goods-info {
padding: 16rpx;
}
.goods-title {
font-size: 26rpx;
color: #333;
line-height: 1.4;
height: 72rpx;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.tag-tmall {
background-color: #ff0036;
color: #ffffff;
font-size: 20rpx;
padding: 0 4rpx;
border-radius: 4rpx;
margin-right: 8rpx;
}
.tag-taobao {
background-color: #ff5000;
color: #ffffff;
font-size: 20rpx;
padding: 0 4rpx;
border-radius: 4rpx;
margin-right: 8rpx;
}
.goods-price-row {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 16rpx;
}
.g-price-tip {
font-size: 20rpx;
color: #ff416c;
}
.g-price-val {
font-size: 32rpx;
color: #ff416c;
font-weight: bold;
}
.g-coupon-tag {
font-size: 20rpx;
color: #ff416c;
border: 1rpx solid #ff416c;
padding: 0 8rpx;
border-radius: 4rpx;
}
.goods-sales {
font-size: 22rpx;
color: #999;
margin-top: 10rpx;
}
.loading-more {
text-align: center;
padding: 40rpx 0;
font-size: 24rpx;
color: #999;
}
.footer-placeholder {
height: 40rpx;
}
</style>