From 2b159254826846a7d866b01a6a536a56f505d73c Mon Sep 17 00:00:00 2001 From: Kroytz Date: Sun, 2 Aug 2026 21:52:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=9A=E8=A7=84=E6=A0=BC=E5=95=86?= =?UTF-8?q?=E5=93=81=E7=BC=96=E8=BE=91=E5=9B=BE=E7=89=87=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/storeProduct/StoreProductForm.vue | 61 ++++++++++++++----- 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/yshop-drink-vue3/src/views/mall/product/storeProduct/StoreProductForm.vue b/yshop-drink-vue3/src/views/mall/product/storeProduct/StoreProductForm.vue index 1a548fc..f1b4f50 100644 --- a/yshop-drink-vue3/src/views/mall/product/storeProduct/StoreProductForm.vue +++ b/yshop-drink-vue3/src/views/mall/product/storeProduct/StoreProductForm.vue @@ -705,13 +705,13 @@ const getInfo = (id) => { } oneFormValidate.value = [data.attr]; formValidate.value.header = []; - generate(); - manyFormValidate.value = data.attrs; console.log('data2:',formValidate.value.spec_type) if(data.spec_type === 0){ manyFormValidate.value = []; }else { createBnt.value = true; + // 先放入已有规格行,供 generate 合并,避免异步生成后丢失各规格图片 + manyFormValidate.value = data.attrs || []; oneFormValidate.value = [ { imageArr: [], @@ -732,7 +732,7 @@ const getInfo = (id) => { integral:0 } ] - + await generate(); console.log('spec_type2:',formValidate.value.spec_type) } } @@ -745,11 +745,20 @@ const getInfo = (id) => { }) } -const route = useRoute() // 立即生成 -const generate = () => { - const previousRows = new Map(manyFormValidate.value.map((item) => [JSON.stringify(item.detail), item])); - StoreProductApi.isFormatAttr(formValidate.value.id, { attrs: attrs.value }).then(res => { +const generate = async () => { + const previousRows = new Map( + manyFormValidate.value + .filter((item) => item?.detail) + .map((item) => [JSON.stringify(item.detail), item]) + ) + const previousBySku = new Map( + manyFormValidate.value + .filter((item) => item?.sku) + .map((item) => [item.sku, item]) + ) + try { + const res = await StoreProductApi.isFormatAttr(formValidate.value.id, { attrs: attrs.value }) manyFormValidate.value = res.value; let headerdel = { title: '操作', @@ -762,19 +771,39 @@ const generate = () => { //this.formValidate.attrs = res.attr; let header = res.header; header.pop(); - if (!route.params.id && formValidate.value.spec_type === 1) { - manyFormValidate.value.map((item) => { - item.pic = formValidate.value.image + // 仅新建商品时,用封面图填充尚未设置图片的规格行(编辑走弹窗,不能用 route.params.id 判断) + const isNewProduct = !formValidate.value.id + if (isNewProduct && formValidate.value.spec_type === 1) { + manyFormValidate.value.forEach((item) => { + if (!item.pic) { + item.pic = formValidate.value.image + } }); oneFormBatch.value[0].pic = formValidate.value.image; } - manyFormValidate.value = manyFormValidate.value.map((item) => ({ - ...item, - ...(previousRows.get(JSON.stringify(item.detail)) || {}) - })); - }).catch(res => { + manyFormValidate.value = manyFormValidate.value.map((item) => { + const previous = + (item.detail && previousRows.get(JSON.stringify(item.detail))) || + (item.sku && previousBySku.get(item.sku)) || + {} + const merged = { + ...item, + ...previous, + detail: item.detail ?? previous.detail + } + // previous 可能来自不含 valueN 的详情数据,规格展示列以生成结果为准 + Object.keys(item).forEach((key) => { + if (key.startsWith('value')) { + merged[key] = item[key] + } + }) + // 已有规格图优先,避免编辑/重新生成时被封面图覆盖 + merged.pic = previous.pic || item.pic || '' + return merged + }); + } catch (res) { console.log('err:'+res) - }) + } } /** 获得分类树 */