chore:切换左侧一级分类时,右侧会自动选中第一个二级分类按钮(如"离型纸")

如果用户之前点击过某个二级分类(如"离型膜"),切换一级分类后会自动选中同名的二级分类
This commit is contained in:
2026-07-13 18:05:36 +08:00
parent 9915fb9bb1
commit f18577a46c
2 changed files with 39 additions and 9 deletions

View File

@@ -323,6 +323,10 @@ export default {
isList: { isList: {
type: Boolean, type: Boolean,
default: false default: false
},
lastSecondCategoryName: {
type: String,
default: ''
} }
}, },
mixins: [DiyMinx], mixins: [DiyMinx],
@@ -353,9 +357,16 @@ export default {
if (this.index == this.select && this.pageIndex == 0) this.getGoodsList(); if (this.index == this.select && this.pageIndex == 0) this.getGoodsList();
this.loadType = this.value.goodsLevel == 1 && this.value.loadType == 'all' ? 'all' : 'part'; this.loadType = this.value.goodsLevel == 1 && this.value.loadType == 'all' ? 'all' : 'part';
if (this.index == this.select && parseInt(this.value.level) == 2 && this.category.child_list && this.category.child_list.length > 0) { if (this.index == this.select && parseInt(this.value.level) == 2 && this.category.child_list && this.category.child_list.length > 0) {
this.selectedSecondCategory = this.category.child_list[0].category_id; let targetCategory = null;
if (this.lastSecondCategoryName) {
targetCategory = this.category.child_list.find(item => item.category_name === this.lastSecondCategoryName);
}
if (!targetCategory) {
targetCategory = this.category.child_list[0];
}
this.selectedSecondCategory = targetCategory.category_id;
this.secondCategoryGoodsList = []; this.secondCategoryGoodsList = [];
this.getSecondCategoryGoods(this.category.child_list[0].category_id); this.getSecondCategoryGoods(targetCategory.category_id);
} }
}, },
mounted() { mounted() {
@@ -367,9 +378,16 @@ export default {
}).exec(); }).exec();
}, 1000); }, 1000);
if (parseInt(this.value.level) == 2 && this.category.child_list && this.category.child_list.length > 0) { if (parseInt(this.value.level) == 2 && this.category.child_list && this.category.child_list.length > 0) {
this.selectedSecondCategory = this.category.child_list[0].category_id; let targetCategory = null;
if (this.lastSecondCategoryName) {
targetCategory = this.category.child_list.find(item => item.category_name === this.lastSecondCategoryName);
}
if (!targetCategory) {
targetCategory = this.category.child_list[0];
}
this.selectedSecondCategory = targetCategory.category_id;
this.secondCategoryGoodsList = []; this.secondCategoryGoodsList = [];
this.getSecondCategoryGoods(this.category.child_list[0].category_id); this.getSecondCategoryGoods(targetCategory.category_id);
} }
}, },
watch: { watch: {
@@ -400,9 +418,16 @@ export default {
this.scrollTop = -1; this.scrollTop = -1;
} }
if (parseInt(this.value.level) == 2 && this.category.child_list && this.category.child_list.length > 0) { if (parseInt(this.value.level) == 2 && this.category.child_list && this.category.child_list.length > 0) {
this.selectedSecondCategory = this.category.child_list[0].category_id; let targetCategory = null;
if (this.lastSecondCategoryName) {
targetCategory = this.category.child_list.find(item => item.category_name === this.lastSecondCategoryName);
}
if (!targetCategory) {
targetCategory = this.category.child_list[0];
}
this.selectedSecondCategory = targetCategory.category_id;
this.secondCategoryGoodsList = []; this.secondCategoryGoodsList = [];
this.getSecondCategoryGoods(this.category.child_list[0].category_id); this.getSecondCategoryGoods(targetCategory.category_id);
} }
}, },
'globalStoreInfo.store_id': { 'globalStoreInfo.store_id': {
@@ -791,6 +816,7 @@ export default {
this.selectedSecondCategory = category.category_id; this.selectedSecondCategory = category.category_id;
this.secondCategoryGoodsList = []; this.secondCategoryGoodsList = [];
this.getSecondCategoryGoods(category.category_id); this.getSecondCategoryGoods(category.category_id);
this.$emit('saveSecondCategory', category.category_name);
} }
}, },
getSecondCategoryGoods(categoryId) { getSecondCategoryGoods(categoryId) {

View File

@@ -101,8 +101,9 @@
:id="'category-' + index" v-if="select == index"> :id="'category-' + index" v-if="select == index">
<diy-category-item :category="item" :value="value" ref="categoryItem" :index="index" <diy-category-item :category="item" :value="value" ref="categoryItem" :index="index"
:select="select" :oneCategorySelect="oneCategorySelect" :isList="isList" :select="select" :oneCategorySelect="oneCategorySelect" :isList="isList"
:lastSecondCategoryName="lastSecondCategoryName"
@tologin="toLogin" @selectsku="selectSku($event, index)" @addCart="addCartPoint" @tologin="toLogin" @selectsku="selectSku($event, index)" @addCart="addCartPoint"
@loadfinish="getHeightArea"></diy-category-item> @loadfinish="getHeightArea" @saveSecondCategory="saveSecondCategoryName"></diy-category-item>
</view> </view>
<view class="end-tips" ref="endTips" :style="{ opacity: endTips }">{{ $lang('components-diy.diy-category.reachedBottom') }}</view> <view class="end-tips" ref="endTips" :style="{ opacity: endTips }">{{ $lang('components-diy.diy-category.reachedBottom') }}</view>
</scroll-view> </scroll-view>
@@ -194,7 +195,8 @@ export default {
loadType: '', loadType: '',
templateFourData: [], templateFourData: [],
isList: false, isList: false,
lang: uni.getStorageSync("lang")//en-us 英文 lang: uni.getStorageSync("lang"),
lastSecondCategoryName: ''
}; };
}, },
created() { created() {
@@ -373,9 +375,11 @@ export default {
if (index >= this.categoryTree.length) return; if (index >= this.categoryTree.length) return;
this.select = index; this.select = index;
this.categoryId = 'category-' + index; this.categoryId = 'category-' + index;
// 阻止切换分类之后滚动事件也立即执行
this.scrollLock = true; this.scrollLock = true;
}, },
saveSecondCategoryName(name) {
this.lastSecondCategoryName = name;
},
touchStart() { touchStart() {
this.scrollLock = false; this.scrollLock = false;
}, },