diff --git a/components-diy/diy-category-item.vue b/components-diy/diy-category-item.vue index c0734c1..bf600cc 100644 --- a/components-diy/diy-category-item.vue +++ b/components-diy/diy-category-item.vue @@ -340,6 +340,10 @@ export default { lastSecondCategoryName: { type: String, default: '' + }, + currentOneCategory: { + type: Object, + default: null } }, mixins: [DiyMinx], @@ -379,7 +383,8 @@ export default { } this.selectedSecondCategory = targetCategory.category_id; this.secondCategoryGoodsList = []; - this.getSecondCategoryGoods(targetCategory.category_id); + // 用当前一级分类下同名二级分类的 id 查商品(而非全局按钮的 id) + this.refreshGoodsByCurrentCategory(); } }, mounted() { @@ -440,9 +445,18 @@ export default { } this.selectedSecondCategory = targetCategory.category_id; this.secondCategoryGoodsList = []; - this.getSecondCategoryGoods(targetCategory.category_id); + this.refreshGoodsByCurrentCategory(); } }, + // 切换一级分类时, 根据当前一级分类下同名二级分类重新加载商品 + currentOneCategory: { + handler() { + if (this.value.template == 99 && parseInt(this.value.level) == 2) { + this.refreshGoodsByCurrentCategory(); + } + }, + deep: true + }, 'globalStoreInfo.store_id': { handler(nval, oval) { if (nval != oval) { @@ -821,6 +835,35 @@ export default { if (!link) return; this.$util.diyRedirectTo(JSON.parse(link)); }, + /** + * 根据当前一级分类(currentOneCategory)下与选中按钮同名的二级分类, 加载对应商品 + * 按钮是全局去重的(选中状态用全局 category_id), 但商品只显示当前一级分类下的 + * 当前一级分类下若没有同名二级分类, 则商品清空(按钮保持选中) + */ + refreshGoodsByCurrentCategory() { + if (this.selectedSecondCategory == null) { + this.secondCategoryGoodsList = []; + return; + } + // 取当前选中按钮的名称 + const cur = (this.category.child_list || []).find(c => c.category_id === this.selectedSecondCategory); + if (!cur) { + this.secondCategoryGoodsList = []; + return; + } + const one = this.currentOneCategory; + if (!one || !one.child_list || one.child_list.length === 0) { + this.secondCategoryGoodsList = []; + return; + } + const match = one.child_list.find(c => c.category_name === cur.category_name); + if (match) { + this.getSecondCategoryGoods(match.category_id); + } else { + // 当前一级分类下没有该二级分类, 商品清空(按钮仍保持选中) + this.secondCategoryGoodsList = []; + } + }, selectSecondCategory(category) { if (this.selectedSecondCategory === category.category_id) { this.selectedSecondCategory = null; @@ -828,8 +871,9 @@ export default { } else { this.selectedSecondCategory = category.category_id; this.secondCategoryGoodsList = []; - this.getSecondCategoryGoods(category.category_id); this.$emit('saveSecondCategory', category.category_name); + // 用当前一级分类下同名 category_id 查商品 + this.refreshGoodsByCurrentCategory(); } }, getSecondCategoryGoods(categoryId) { diff --git a/components-diy/diy-category.vue b/components-diy/diy-category.vue index cb4727d..e85b231 100644 --- a/components-diy/diy-category.vue +++ b/components-diy/diy-category.vue @@ -92,7 +92,19 @@ - + + + + + { + if (one.child_list && one.child_list.length) { + one.child_list.forEach(two => { + allSecond.push(Object.assign({ _origIndex: allSecond.length }, two)); + }); + } + }); + if (!allSecond.length) { + this.template99Category = { category_name: '', child_list: [] }; + return; + } + let pending = allSecond.length; + const results = []; + const finishOne = () => { + pending--; + if (pending <= 0) { + results.sort((a, b) => a._origIndex - b._origIndex); + // 按名称去重, 同名只保留第一个有商品的(只要任一同名有商品就显示) + const deduped = []; + const seenNames = {}; + results.forEach(item => { + if (!seenNames[item.category_name]) { + seenNames[item.category_name] = true; + deduped.push(item); + } + }); + this.template99Category = { + category_name: '', + child_list: deduped + }; + } + }; + allSecond.forEach(two => { + this.$api.sendRequest({ + url: '/api/goodssku/pageByCategory', + data: { + category_id: two.category_id, + page: 1, + page_size: 1 + }, + success: res => { + if (res && res.code == 0 && res.data && res.data.list && res.data.list.length > 0) { + results.push(two); + } + }, + complete: () => { + finishOne(); + } + }); + }); + }, /** * 切换一级分类 * @param {Object} index