From c5d675b6d24c773ecc79bced2dda543155bb71c8 Mon Sep 17 00:00:00 2001 From: Ryan Ran <1410277647@qq.com> Date: Tue, 23 Sep 2025 19:17:24 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E5=95=86=E5=93=81=E8=A7=84=E6=A0=BC):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=A7=84=E6=A0=BC=E5=80=BC=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E7=A1=AE=E4=BF=9D=E5=9C=A8=E8=A7=84?= =?UTF-8?q?=E6=A0=BC=E5=80=BC=E4=B8=BA=E7=A9=BA=E6=97=B6=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=AF=B9=E5=BA=94=E8=A1=8C=E5=B9=B6=E6=9B=B4=E6=96=B0=E8=A1=A8?= =?UTF-8?q?=E6=A0=BC=E6=95=B0=E6=8D=AE=EF=BC=8C=E5=A2=9E=E5=BC=BA=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../goods/goods-seller/goodsOperationSec.vue | 208 +++++++++++++----- 1 file changed, 158 insertions(+), 50 deletions(-) diff --git a/seller/src/views/goods/goods-seller/goodsOperationSec.vue b/seller/src/views/goods/goods-seller/goodsOperationSec.vue index 58547be9..2cf9f509 100644 --- a/seller/src/views/goods/goods-seller/goodsOperationSec.vue +++ b/seller/src/views/goods/goods-seller/goodsOperationSec.vue @@ -1370,6 +1370,17 @@ export default { if (val.value === "") { // 内联错误提示,不使用弹窗 this.$set(val, '_error', '规格值不能为空!'); + + // 如果规格项和规格名称存在,从表格数据中移除包含该空规格值的行 + if (spec && spec.name && this.skuInfo[groupIndex]) { + // 从表格数据中过滤掉包含该空规格值的行 + this.skuTableData = this.skuTableData.filter(item => { + return item[spec.name] !== val.value && item[spec.name] !== ""; + }); + + // 重新渲染表格数据 + this.renderTableData(this.skuTableData); + } } else if (val._error) { // 清除错误 this.$delete(val, '_error'); @@ -1507,46 +1518,36 @@ export default { itemValue.images = this.baseInfoForm.goodsGalleryFiles } this.$set(item.spec_values, item.spec_values.length, itemValue); - if (item.spec_values.length > 1) { - let index = beforeLength; - let filterSkuInfo = this.skuInfo.filter((i) => i.name !== item.name); - let totalLength = 1; - filterSkuInfo.forEach((skuInfo) => { - totalLength *= skuInfo.spec_values.length; + + // 生成新的规格组合 + const newCombinations = this.generateSkuCombinations(this.skuInfo); + + // 保留原有组合的属性值 + this.skuTableData = newCombinations.map(combination => { + // 查找匹配的原有组合 + const existingCombination = this.skuTableData.find(item => { + // 检查所有规格项是否匹配 + return this.skuInfo.every(info => { + return item[info.name] === combination[info.name]; + }); }); - if ($index === 0) { - index = 1; - for (let i = 0; i < totalLength; i++) { - let find = cloneObj(this.skuTableData[index - 1]); - find[item.name] = this.newSkuValues[$index]; - find.id = ""; - find.price && (find.price = ""); - find.sn && (find.sn = ""); - // find.cost && (find.cost = ""); - find.quantity && (find.quantity = ""); - // find.alertQuantity && (find.alertQuantity = ""); - find.weight && (find.weight = ""); - this.skuTableData.splice(this.skuTableData.length, 0, find); - index++; - } + if (existingCombination) { + // 保留原有组合的属性值 + return { + ...combination, + id: existingCombination.id || "", + sn: existingCombination.sn || "", + quantity: existingCombination.quantity || "", + cost: existingCombination.cost || "", + price: existingCombination.price || "", + weight: existingCombination.weight || "" + }; } else { - for (let i = 0; i < totalLength; i++) { - let find = cloneObj(this.skuTableData[index - 1]); - find[item.name] = this.newSkuValues[$index]; - find.id = ""; - find.price && (find.price = ""); - find.sn && (find.sn = ""); - // find.cost && (find.cost = ""); - find.quantity && (find.quantity = ""); - // find.alertQuantity && (find.alertQuantity = ""); - find.weight && (find.weight = ""); - - this.skuTableData.splice(index, 0, find); - index += $index === 0 ? beforeLength : beforeLength + 1; - } + // 新组合使用默认值 + return combination; } - } + }); this.baseInfoForm.regeneratorSkuFlag = true; this.newSkuValues[$index] = ""; @@ -1565,14 +1566,44 @@ export default { this.$Message.error("至少保留一个规格值!"); return; } + + // 从规格项中移除规格值 this.skuInfo.forEach((i) => { if (i.name === spec.name) { i.spec_values.splice(index, 1); } }); - this.skuTableData = this.skuTableData.filter( - (e) => e[spec.name] !== item.value - ); + + // 生成新的规格组合 + const newCombinations = this.generateSkuCombinations(this.skuInfo); + + // 保留原有组合的属性值 + this.skuTableData = newCombinations.map(combination => { + // 查找匹配的原有组合 + const existingCombination = this.skuTableData.find(item => { + // 检查所有规格项是否匹配 + return this.skuInfo.every(info => { + return item[info.name] === combination[info.name]; + }); + }); + + if (existingCombination) { + // 保留原有组合的属性值 + return { + ...combination, + id: existingCombination.id || "", + sn: existingCombination.sn || "", + quantity: existingCombination.quantity || "", + cost: existingCombination.cost || "", + price: existingCombination.price || "", + weight: existingCombination.weight || "" + }; + } else { + // 新组合使用默认值 + return combination; + } + }); + this.baseInfoForm.regeneratorSkuFlag = true; }, @@ -1640,17 +1671,37 @@ export default { ); this.skuTableColumn = pushData; - //克隆所有渲染的数据 - if (this.skuInfo.length > 0) { - //存放最终结果 - let result = []; - this.skuIndex = 0; - this.skuTableData = this.specIterator( - result, - this.skuInfo, - this.skuTableData - ); + // 生成规格组合 + if (this.skuInfo.length > 0) { + const newCombinations = this.generateSkuCombinations(this.skuInfo); + + // 保留原有组合的属性值 + this.skuTableData = newCombinations.map(combination => { + // 查找匹配的原有组合 + const existingCombination = skus.find(item => { + // 检查所有规格项是否匹配 + return this.skuInfo.every(info => { + return item[info.name] === combination[info.name]; + }); + }); + + if (existingCombination) { + // 保留原有组合的属性值 + return { + ...combination, + id: existingCombination.id || "", + sn: existingCombination.sn || "", + quantity: existingCombination.quantity || "", + cost: existingCombination.cost || "", + price: existingCombination.price || "", + weight: existingCombination.weight || "" + }; + } else { + // 新组合使用默认值 + return combination; + } + }); } }, @@ -1689,7 +1740,7 @@ export default { obj.weight = skus[index].weight; } table.push(obj); - } else { + } else if (specItem.value !== "") { table.push({ [spec[0].name]: specItem.value, images: this.baseInfoForm.goodsGalleryFiles || [], @@ -1703,6 +1754,63 @@ export default { } return table; }, + /** + * 生成所有可能的规格组合 + * @param {Array} skuInfo 规格信息 + * @returns {Array} 所有可能的规格组合 + */ + generateSkuCombinations(skuInfo) { + if (!skuInfo || skuInfo.length === 0) { + return []; + } + + // 提取每个规格项的规格值 + const specValues = skuInfo.map(item => { + return item.spec_values + .filter(val => val.value !== "") // 过滤掉空值 + .map(val => ({ + name: item.name, + value: val.value, + images: val.images || this.baseInfoForm.goodsGalleryFiles || [] + })); + }); + + // 如果有规格项没有有效的规格值,返回空数组 + if (specValues.some(values => values.length === 0)) { + return []; + } + + // 递归生成所有组合 + const generateCombinations = (index, current) => { + if (index === specValues.length) { + // 基本属性 + const combination = { + images: this.baseInfoForm.goodsGalleryFiles || [] + }; + + // 添加规格属性 + current.forEach(spec => { + combination[spec.name] = spec.value; + + // 如果是第一个规格项且有图片,使用其图片 + if (spec.name === skuInfo[0].name) { + combination.images = spec.images; + } + }); + + return [combination]; + } + + const results = []; + for (const value of specValues[index]) { + results.push(...generateCombinations(index + 1, [...current, value])); + } + return results; + }; + + return generateCombinations(0, []); + }, + /** 根据分类id获取系统设置规格信息*/ Get_SkuInfoByCategory(categoryId) { if (categoryId) {