You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
zhishifufei_uniapp/pages/store/goodsCate.vue

288 lines
7.9 KiB

<template>
<BaseContainer class="cate-box">
<NavBar title="分类" />
<view class="cate-list flex">
<view v-if="selectCate.length > 0" class="select-cate">
<view v-for="(cate, index) in selectCate" class="cate" @click="backCate(cate, index)">{{ cate.name }} <text v-if="index < selectCate.length - 1">/</text></view>
</view>
<view v-if="nowCateList.length > 0" class="cate-list-right">
<!-- <view v-for="(item, index) in goodsCateList.filter(item => item.name === bigCate)[0].list" class="small-cate-content"> -->
<view class="small-cate-content">
<!-- <view class="small-cate-title">{{ bigCate }} + {{ item.title }}</view> -->
<view class="small-cate-list flex">
<view v-for="(con, k) in nowCateList" :key="k" class="small-cate-list-item" @click="toList(con)">
<view class="flex item-image">
<image :src="con.pic" mode="aspectFill"></image>
</view>
<view class="item-name">{{ con.name }}</view>
</view>
</view>
</view>
</view>
<!-- <view class="cate-list-left">
<view v-for="(item, index) in goodsCateList" :key="index" class="cate-item" :class="{'active': bigCate === item.name}" @click="bigCate = item.name">
{{ item.name }}
</view>
</view> -->
<!-- <view v-if="bigCate && goodsCateList.length > 0" class="cate-list-right"> -->
<!-- <view v-for="(item, index) in goodsCateList.filter(item => item.name === bigCate)[0].list" class="small-cate-content"> -->
<!-- <view class="small-cate-content"> -->
<!-- <view class="small-cate-title">{{ bigCate }} + {{ item.title }}</view> -->
<!-- <view class="small-cate-list flex">
<view v-for="(con, k) in goodsCateList.filter(item => item.name === bigCate)[0].children" :key="k" class="small-cate-list-item" @click="toList(con)">
<view class="flex item-image">
<image :src="con.pic" mode="aspectFill"></image>
</view>
<view class="item-name">{{ con.name }}</view>
</view>
</view>
</view>
</view> -->
</view>
</BaseContainer>
</template>
<script>
import { getGoodsCateList, getGoodsList, getIndexData } from "@/api/store";
import { getSpecialSubject } from '@/api/special';
import { changeCategory } from '@/api/learning';
export default {
data() {
return {
goodsCateList: [],
nowCateList: [],
selectCate: [],
// bigCate: '',
type: '',
cate:'',
};
},
onLoad({type, cate}) {
this.type = type;
this.cate = cate || '';
if (this.type === 'store') {
this.getGoodsCateList();
} else {
this.getSpecialSubject();
}
},
methods: {
// 获取分类
async getGoodsCateList() {
try {
const { data } = await getGoodsCateList();
console.log(data);
this.goodsCateList = [...data.category_list];
// this.goodsCateList = [
// {
// name: "特别推荐",
// id: 0,
// children: data.recommend,
// },
// ].concat(data.category_list);
// this.bigCate = this.bigCate || '特别推荐';
if (this.cate) {
this.selectCate = this.getCategoryArr(this.goodsCateList, data => data.name === this.cate);
this.nowCateList = this.selectCate[this.selectCate.length - 1].children;
} else {
this.nowCateList = [...data.category_list];
}
} catch (err) { }
},
getCategoryArr(tree, func, path = []) {
if (!tree) {
return [];
}
for (const data of tree) {
path.push(data);
if (func(data)) {
return path;
}
if (data.children) {
const findChildren = this.getCategoryArr(data.children, func, path);
if (findChildren.length) {
return findChildren;
}
}
path.pop();
}
return [];
},
async getSpecialSubject() {
try {
const { data } = await getSpecialSubject();
console.log(data);
// this.goodsCateList = [
// {
// name: "特别推荐",
// id: 0,
// children: data.recommend,
// },
// ].concat(data.category_list);
// this.bigCate = this.bigCate || '特别推荐';
this.goodsCateList = data.category_list;
if (this.cate) {
this.selectCate = this.getCategoryArr(this.goodsCateList, data => data.name === this.cate);
this.nowCateList = this.selectCate[this.selectCate.length - 1].children;
} else {
this.nowCateList = data.category_list;
}
} catch (err) {
console.log(err);
}
},
backCate(cate, index) {
console.log(cate);
if (cate.children && cate.children.length > 0) {
this.nowCateList = cate.children;
this.selectCate = this.selectCate.splice(0, index + 1);
}
},
async toList(con) {
if (con.children && con.children.length > 0) {
this.selectCate.push(con);
this.nowCateList = con.children || [];
} else {
switch (this.type){
case 'store':
uni.navigateTo({
url: `/pages/store/goodsList?categoryId=${con.id}`
});
break;
case 'special':
uni.navigateTo({
url: `/pages/special/specialCateCourse?name=${con.name}&gradeId=${con.grade_id}&subjectId=${con.id}&specialCate=0`
});
break;
case 'learning':
try {
const { data } = await changeCategory({ subject_id: con.id});
// let pages = getCurrentPages();
// let prevPage = pages[pages.length - 2];
// prevPage.$vm.learningData.exam.name = con.name;
// prevPage.$vm.learningData.exam.grade_id = con.grade_id;
// prevPage.$vm.learningData.exam.subject_id = con.id;
// uni.navigateBack();
uni.switchTab({
url: '/pages/learning/index',
});
} catch (err) {
console.log(err);
}
break;
default:
break;
}
}
},
},
};
</script>
<style lang="scss" scoped>
.cate-box {
background: #f6f6f6;
height: 100vh;
.cate-list {
padding-top: 20rpx;
display: flex;
flex-direction: column;
height: 100%;
.select-cate {
margin-top: 20rpx;
background: #fff;
padding: 20rpx 40rpx;
display: flex;
flex-wrap: wrap;
.cate {
line-height: 50rpx;
>text {
margin: 0 10rpx;
}
}
}
.cate-list-left {
width: 220rpx;
background: #fff;
margin-right: 20rpx;
padding-top: 12rpx;
height: 100%;
overflow-y: auto;
.cate-item {
width: 100%;
height: 80rpx;
line-height: 80rpx;
padding-left: 40rpx;
margin-bottom: 12rpx;
position: relative;
color: #333;
font-size: 26rpx;
&.active {
background: rgba(179, 219, 248, 0.1);
color: #0f74bb;
&:before {
content: '';
background: linear-gradient(0deg, #24a9e1, #0f74bb);
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
width: 8rpx;
height: 36rpx;
border-radius: 4rpx;
}
}
}
}
.cate-list-right {
flex: 1;
height: 100%;
overflow-y: auto;
.small-cate-content {
padding-top: 18rpx;
&:not(:nth-child(1)) {
padding-top: 62rpx;
}
.small-cate-title {
color: #333;
font-size: 28rpx;
font-weight: bold;
line-height: 28rpx;
}
.small-cate-list {
flex-wrap: wrap;
padding: 0 40rpx;
.small-cate-list-item {
width: 130rpx;
margin-bottom: 30rpx;
margin-right: 40rpx;
&:nth-child(4n) {
margin-right: 0;
}
.item-image {
width: 130rpx;
height: 130rpx;
align-items: center;
justify-content: center;
>image {
width: 110rpx;
height: 110rpx;
}
}
.item-name {
margin-top: 18rpx;
color: #333;
font-size: 24rpx;
text-align: center;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
}
}
}
}
}
</style>