Browse Source

完成合并操作

pull/11/head
徐飞洋 2 months ago
parent
commit
afcfe2318f
  1. 58
      pages/goods-update/goods-update.js
  2. 5
      pages/goods-update/goods-update.wxml
  3. 8
      pages/goods-update/goods-update.wxss

58
pages/goods-update/goods-update.js

@ -809,24 +809,62 @@ Page({
const spec = goodsDetail.spec || '';
let specArray = [];
let priceArray = [];
let costpriceArray = [];
// 分割规格字符串,支持多种逗号分隔符:英文逗号、中文逗号、全角逗号
if (spec) {
specArray = spec.split(/[,,、]/).map(item => item.trim()).filter(item => item);
}
// 处理价格字段
// 从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);
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));
}
}
}
// 创建editSupply对象
const editSupply = {
id: goodsDetail.id || goodsDetail.productId,
@ -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) {
// 验证所有价格输入框都必须填写
for (const price of editSupply.priceArray) {
if (!price || price.trim() === '') {
wx.showToast({
title: '请填写销售价格',
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);

5
pages/goods-update/goods-update.wxml

@ -216,7 +216,10 @@
<block wx:if="{{editSupply.specArray && editSupply.specArray.length > 1}}">
<view class="dynamic-price-container">
<view class="price-item" wx:for="{{editSupply.specArray}}" wx:key="index">
<view class="spec-label">{{item}}</view>
<view class="spec-label">
{{item}}
<text class="costprice-info" wx:if="{{editSupply.costpriceArray[index]}}">(采购价:¥{{editSupply.costpriceArray[index]}})</text>
</view>
<input
class="edit-form-input price-input"
type="text"

8
pages/goods-update/goods-update.wxss

@ -294,6 +294,14 @@ video.slider-media .wx-video-volume-icon {
word-break: break-all;
}
/* 采购价格信息样式 */
.costprice-info {
font-size: 24rpx;
color: #999;
margin-left: 10rpx;
font-weight: normal;
}
.price-input {
width: 100%;
}

Loading…
Cancel
Save