fix: 多规格商品编辑图片错误

This commit is contained in:
Kroytz committed 2026-08-02 21:52:39 +08:00
1 parent 5b766619e3
commit 2b15925482
1 file changed
+45 -16
@@ -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)
})
}
}
/** 获得分类树 */