From 46ab724837f9df2e69c71b1912b03addc61ed324 Mon Sep 17 00:00:00 2001 From: wangdong <1551135706@qq.com> Date: Sun, 21 Apr 2024 19:50:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=B1=BB=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/store/goodsCate.vue | 194 ++++++++++++++++++++++++++------------ 1 file changed, 136 insertions(+), 58 deletions(-) diff --git a/pages/store/goodsCate.vue b/pages/store/goodsCate.vue index 1599a75..6e3415d 100644 --- a/pages/store/goodsCate.vue +++ b/pages/store/goodsCate.vue @@ -2,17 +2,15 @@ - - - {{ item.name }} - + + {{ cate.name }} / - + - + @@ -21,6 +19,25 @@ + + + + + + @@ -34,14 +51,16 @@ data() { return { goodsCateList: [], - bigCate: '', + nowCateList: [], + selectCate: [], + // bigCate: '', type: '', + cate:'', }; }, onLoad({type, cate}) { this.type = type; - this.bigCate = cate || ''; - console.log(type, cate); + this.cate = cate || ''; if (this.type === 'store') { this.getGoodsCateList(); } else { @@ -54,64 +73,107 @@ try { const { data } = await getGoodsCateList(); console.log(data); - this.goodsCateList = [ - { - name: "特别推荐", - id: 0, - children: data.recommend, - }, - ].concat(data.category_list); - this.bigCate = this.bigCate || '特别推荐'; - console.log(this.goodsCateList); + 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 || '特别推荐'; - console.log(this.goodsCateList); + // 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) { - 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', + 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}` }); - } catch (err) { - console.log(err); - } - break; - default: - break; + 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; + } } }, }, @@ -125,7 +187,21 @@ .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; @@ -176,11 +252,13 @@ } .small-cate-list { flex-wrap: wrap; + padding: 0 40rpx; .small-cate-list-item { width: 130rpx; - margin-top: 30rpx; - &:nth-child(3n + 2) { - margin: 30rpx 40rpx 0; + margin-bottom: 30rpx; + margin-right: 40rpx; + &:nth-child(4n) { + margin-right: 0; } .item-image { width: 130rpx;