diff --git a/pages/goods-update/goods-update.js b/pages/goods-update/goods-update.js index c3cfe9e..ae92231 100644 --- a/pages/goods-update/goods-update.js +++ b/pages/goods-update/goods-update.js @@ -809,21 +809,59 @@ Page({ const spec = goodsDetail.spec || ''; let specArray = []; let priceArray = []; + let costpriceArray = []; // 分割规格字符串,支持多种逗号分隔符:英文逗号、中文逗号、全角逗号 if (spec) { specArray = spec.split(/[,,、]/).map(item => item.trim()).filter(item => item); } - // 处理价格字段 - const price = goodsDetail.price || ''; - if (price) { - if (typeof price === 'string' && price.includes(',')) { - // 如果价格是字符串且包含逗号,分割成数组 - priceArray = price.split(',').map(item => item.trim()).filter(item => item); - } else { - // 如果只有一个价格,为所有规格设置相同的价格 - priceArray = Array(specArray.length).fill(String(price)); + // 从weightQuantityData中获取每个规格对应的价格和采购价格 + if (goodsDetail.weightQuantityData && goodsDetail.weightQuantityData.length > 0) { + // 遍历weightQuantityData,获取每个规格对应的价格和采购价格 + for (let i = 0; i < specArray.length; i++) { + // 查找当前规格对应的weightQuantityData项 + const currentSpec = specArray[i]; + const matchingItem = goodsDetail.weightQuantityData.find(item => + item.weightSpec === currentSpec || + item.weightSpec.includes(currentSpec) || + currentSpec.includes(item.weightSpec) + ); + + if (matchingItem) { + // 如果找到匹配项,使用匹配项的价格和采购价格 + priceArray[i] = matchingItem.price || ''; + costpriceArray[i] = matchingItem.costprice || ''; + } else { + // 如果没有找到匹配项,使用空字符串或默认值 + priceArray[i] = ''; + costpriceArray[i] = ''; + } + } + } else { + // 如果没有weightQuantityData,使用原有的处理方式 + // 处理销售价格字段 + const price = goodsDetail.price || ''; + if (price) { + if (typeof price === 'string' && price.includes(',')) { + // 如果价格是字符串且包含逗号,分割成数组 + priceArray = price.split(/[,,、]/).map(item => item.trim()).filter(item => item); + } else { + // 如果只有一个价格,为所有规格设置相同的价格 + priceArray = Array(specArray.length).fill(String(price)); + } + } + + // 处理采购价格字段 + const costprice = goodsDetail.costprice || ''; + if (costprice) { + if (typeof costprice === 'string' && costprice.includes(',')) { + // 如果采购价格是字符串且包含逗号,分割成数组 + costpriceArray = costprice.split(/[,,、]/).map(item => item.trim()).filter(item => item); + } else { + // 如果只有一个采购价格,为所有规格设置相同的采购价格 + costpriceArray = Array(specArray.length).fill(String(costprice)); + } } } @@ -835,6 +873,7 @@ Page({ spec: spec, specArray: specArray, // 新增:规格数组 priceArray: priceArray, // 新增:价格数组 + costpriceArray: costpriceArray, // 新增:采购价格数组 minOrder: goodsDetail.minOrder || '', yolk: goodsDetail.yolk || '', region: goodsDetail.region || '', @@ -864,21 +903,20 @@ Page({ // 处理价格字段,将价格数组合并成字符串 let priceStr = ''; if (editSupply.priceArray && editSupply.priceArray.length > 0) { - // 移除空值 - const validPrices = editSupply.priceArray.filter(price => price && price.trim() !== ''); - - // 验证必填字段 - if (validPrices.length === 0) { - wx.showToast({ - title: '请填写销售价格', - icon: 'none', - duration: 2000 - }); - return; + // 验证所有价格输入框都必须填写 + for (const price of editSupply.priceArray) { + if (!price || price.trim() === '') { + wx.showToast({ + title: '请填写所有规格的销售价格', + icon: 'none', + duration: 2000 + }); + return; + } } // 验证价格是否为有效数字 - for (const price of validPrices) { + for (const price of editSupply.priceArray) { const priceNum = parseFloat(price); if (isNaN(priceNum) || priceNum < 0) { wx.showToast({ @@ -891,7 +929,7 @@ Page({ } // 合并价格数组为字符串 - priceStr = validPrices.join(','); + priceStr = editSupply.priceArray.join(','); } else if (editSupply.price) { // 兼容旧的单个价格字段 const priceNum = parseFloat(editSupply.price); diff --git a/pages/goods-update/goods-update.wxml b/pages/goods-update/goods-update.wxml index a428f46..d46b629 100644 --- a/pages/goods-update/goods-update.wxml +++ b/pages/goods-update/goods-update.wxml @@ -216,7 +216,10 @@ - {{item}} + + {{item}} + (采购价:¥{{editSupply.costpriceArray[index]}}) +