From 7a6495fa33a968df32bb6e2ba981a6fb106686c5 Mon Sep 17 00:00:00 2001 From: jinhhanhan <1683105490@qq.com> Date: Thu, 16 Jul 2026 14:31:14 +0800 Subject: [PATCH] =?UTF-8?q?chore:=E6=8C=89=E9=92=AE=20=3D=20=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E6=89=80=E6=9C=89=E6=9C=89=E5=95=86=E5=93=81=E7=9A=84?= =?UTF-8?q?=E4=BA=8C=E7=BA=A7=E5=88=86=E7=B1=BB=EF=BC=88=E5=8E=BB=E9=87=8D?= =?UTF-8?q?=EF=BC=8C=E4=B8=80=E7=9B=B4=E6=98=BE=E7=A4=BA=EF=BC=89=EF=BC=8C?= =?UTF-8?q?=E9=80=89=E4=B8=AD=E7=8A=B6=E6=80=81=E5=9C=A8=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E4=B8=80=E7=BA=A7=E5=88=86=E7=B1=BB=E6=97=B6=E4=BF=9D=E6=8C=81?= =?UTF-8?q?=20=E5=95=86=E5=93=81=20=3D=20=E5=88=87=E6=8D=A2=E4=B8=80?= =?UTF-8?q?=E7=BA=A7=E5=88=86=E7=B1=BB=E6=97=B6=EF=BC=8C=E7=94=A8=E9=80=89?= =?UTF-8?q?=E4=B8=AD=E6=8C=89=E9=92=AE=E7=9A=84=E5=90=8D=E7=A7=B0=E5=9C=A8?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E4=B8=80=E7=BA=A7=E5=88=86=E7=B1=BB=E4=B8=8B?= =?UTF-8?q?=E6=89=BE=E5=90=8C=E5=90=8D=20category=EF=BC=8C=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=8A=A0=E8=BD=BD=E8=AF=A5=20category=20=E7=9A=84?= =?UTF-8?q?=E5=95=86=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components-diy/diy-category-item.vue | 50 ++++++++++++++++- components-diy/diy-category.vue | 84 +++++++++++++++++++++++++++- 2 files changed, 129 insertions(+), 5 deletions(-) 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