Browse Source

完成合并操作

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

82
pages/goods-update/goods-update.js

@ -809,21 +809,59 @@ Page({
const spec = goodsDetail.spec || ''; const spec = goodsDetail.spec || '';
let specArray = []; let specArray = [];
let priceArray = []; let priceArray = [];
let costpriceArray = [];
// 分割规格字符串,支持多种逗号分隔符:英文逗号、中文逗号、全角逗号 // 分割规格字符串,支持多种逗号分隔符:英文逗号、中文逗号、全角逗号
if (spec) { if (spec) {
specArray = spec.split(/[,,、]/).map(item => item.trim()).filter(item => item); specArray = spec.split(/[,,、]/).map(item => item.trim()).filter(item => item);
} }
// 处理价格字段 // 从weightQuantityData中获取每个规格对应的价格和采购价格
const price = goodsDetail.price || ''; if (goodsDetail.weightQuantityData && goodsDetail.weightQuantityData.length > 0) {
if (price) { // 遍历weightQuantityData,获取每个规格对应的价格和采购价格
if (typeof price === 'string' && price.includes(',')) { for (let i = 0; i < specArray.length; i++) {
// 如果价格是字符串且包含逗号,分割成数组 // 查找当前规格对应的weightQuantityData项
priceArray = price.split(',').map(item => item.trim()).filter(item => item); const currentSpec = specArray[i];
} else { const matchingItem = goodsDetail.weightQuantityData.find(item =>
// 如果只有一个价格,为所有规格设置相同的价格 item.weightSpec === currentSpec ||
priceArray = Array(specArray.length).fill(String(price)); 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, spec: spec,
specArray: specArray, // 新增:规格数组 specArray: specArray, // 新增:规格数组
priceArray: priceArray, // 新增:价格数组 priceArray: priceArray, // 新增:价格数组
costpriceArray: costpriceArray, // 新增:采购价格数组
minOrder: goodsDetail.minOrder || '', minOrder: goodsDetail.minOrder || '',
yolk: goodsDetail.yolk || '', yolk: goodsDetail.yolk || '',
region: goodsDetail.region || '', region: goodsDetail.region || '',
@ -864,21 +903,20 @@ Page({
// 处理价格字段,将价格数组合并成字符串 // 处理价格字段,将价格数组合并成字符串
let priceStr = ''; let priceStr = '';
if (editSupply.priceArray && editSupply.priceArray.length > 0) { if (editSupply.priceArray && editSupply.priceArray.length > 0) {
// 移除空值 // 验证所有价格输入框都必须填写
const validPrices = editSupply.priceArray.filter(price => price && price.trim() !== ''); for (const price of editSupply.priceArray) {
if (!price || price.trim() === '') {
// 验证必填字段 wx.showToast({
if (validPrices.length === 0) { title: '请填写所有规格的销售价格',
wx.showToast({ icon: 'none',
title: '请填写销售价格', duration: 2000
icon: 'none', });
duration: 2000 return;
}); }
return;
} }
// 验证价格是否为有效数字 // 验证价格是否为有效数字
for (const price of validPrices) { for (const price of editSupply.priceArray) {
const priceNum = parseFloat(price); const priceNum = parseFloat(price);
if (isNaN(priceNum) || priceNum < 0) { if (isNaN(priceNum) || priceNum < 0) {
wx.showToast({ wx.showToast({
@ -891,7 +929,7 @@ Page({
} }
// 合并价格数组为字符串 // 合并价格数组为字符串
priceStr = validPrices.join(','); priceStr = editSupply.priceArray.join(',');
} else if (editSupply.price) { } else if (editSupply.price) {
// 兼容旧的单个价格字段 // 兼容旧的单个价格字段
const priceNum = parseFloat(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}}"> <block wx:if="{{editSupply.specArray && editSupply.specArray.length > 1}}">
<view class="dynamic-price-container"> <view class="dynamic-price-container">
<view class="price-item" wx:for="{{editSupply.specArray}}" wx:key="index"> <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 <input
class="edit-form-input price-input" class="edit-form-input price-input"
type="text" type="text"

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

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

Loading…
Cancel
Save