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

This commit is contained in:
Kroytz committed 2026-08-02 21:52:39 +08:00
1 parent 5b766619e3
commit 2b15925482
1 file changed
+43 -14
@@ -705,13 +705,13 @@ const getInfo = (id) => {
} }
oneFormValidate.value = [data.attr]; oneFormValidate.value = [data.attr];
formValidate.value.header = []; formValidate.value.header = [];
generate();
manyFormValidate.value = data.attrs;
console.log('data2:',formValidate.value.spec_type) console.log('data2:',formValidate.value.spec_type)
if(data.spec_type === 0){ if(data.spec_type === 0){
manyFormValidate.value = []; manyFormValidate.value = [];
}else { }else {
createBnt.value = true; createBnt.value = true;
// 先放入已有规格行,供 generate 合并,避免异步生成后丢失各规格图片
manyFormValidate.value = data.attrs || [];
oneFormValidate.value = [ oneFormValidate.value = [
{ {
imageArr: [], imageArr: [],
@@ -732,7 +732,7 @@ const getInfo = (id) => {
integral:0 integral:0
} }
] ]
await generate();
console.log('spec_type2:',formValidate.value.spec_type) console.log('spec_type2:',formValidate.value.spec_type)
} }
} }
@@ -745,11 +745,20 @@ const getInfo = (id) => {
}) })
} }
const route = useRoute()
// 立即生成 // 立即生成
const generate = () => { const generate = async () => {
const previousRows = new Map(manyFormValidate.value.map((item) => [JSON.stringify(item.detail), item])); const previousRows = new Map(
StoreProductApi.isFormatAttr(formValidate.value.id, { attrs: attrs.value }).then(res => { 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; manyFormValidate.value = res.value;
let headerdel = { let headerdel = {
title: '操作', title: '操作',
@@ -762,19 +771,39 @@ const generate = () => {
//this.formValidate.attrs = res.attr; //this.formValidate.attrs = res.attr;
let header = res.header; let header = res.header;
header.pop(); header.pop();
if (!route.params.id && formValidate.value.spec_type === 1) { // 仅新建商品时,用封面图填充尚未设置图片的规格行(编辑走弹窗,不能用 route.params.id 判断)
manyFormValidate.value.map((item) => { const isNewProduct = !formValidate.value.id
if (isNewProduct && formValidate.value.spec_type === 1) {
manyFormValidate.value.forEach((item) => {
if (!item.pic) {
item.pic = formValidate.value.image item.pic = formValidate.value.image
}
}); });
oneFormBatch.value[0].pic = formValidate.value.image; oneFormBatch.value[0].pic = formValidate.value.image;
} }
manyFormValidate.value = manyFormValidate.value.map((item) => ({ 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, ...item,
...(previousRows.get(JSON.stringify(item.detail)) || {}) ...previous,
})); detail: item.detail ?? previous.detail
}).catch(res => { }
console.log('err:'+res) // 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)
}
} }
/** 获得分类树 */ /** 获得分类树 */