From c9da2424801f59e81b93211f6d51387995e1a6f6 Mon Sep 17 00:00:00 2001 From: huliyong <2783385703@qq.com> Date: Tue, 2 Sep 2025 14:24:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E8=AE=A2=E5=8D=95=E7=A1=AE=E8=AE=A4):?= =?UTF-8?q?=20=E9=87=8D=E6=9E=84=E5=AE=A0=E7=89=A9=E8=B4=B9=E7=94=A8?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E9=80=BB=E8=BE=91=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=80=BC=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用新的 freePetConfig 配置替代旧的 freeQuota 规则计算宠物费用 - 为 price_config 相关字段添加默认值处理,提高代码健壮性 - 优化费用计算逻辑,只对超过免费数量的宠物进行收费 --- pages/newOrder/confirmOrder.vue | 166 ++++++++++++++------------ pages/newOrder/serviceNew2.vue | 250 ++++++++++++++++++++++++---------------- 2 files changed, 241 insertions(+), 175 deletions(-) diff --git a/pages/newOrder/confirmOrder.vue b/pages/newOrder/confirmOrder.vue index d0bc594..a55a550 100644 --- a/pages/newOrder/confirmOrder.vue +++ b/pages/newOrder/confirmOrder.vue @@ -155,9 +155,9 @@ - 提前熟悉 - ¥{{ price_config.preFamiliarize.price }} - + 提前熟悉 + ¥{{ (price_config.preFamiliarize && price_config.preFamiliarize.price) || 40 }} + 费用总计 @@ -358,6 +358,13 @@ //折扣 discountMemberText : '', companionLevelTitle : '', + + freePetConfig : { + cat: { freeCount: 1 }, + smallDog: { freeCount: 0 }, + mediumDog: { freeCount: 0 }, + largeDog: { freeCount: 0 } + }, } }, onLoad() { @@ -484,83 +491,83 @@ }, 0); - //免费规则 - // 如果总宠物费用>30 - const freeQuota = this.price_config.freeQuota - if (totalPetCost > Number(freeQuota.threshold)) { - freeQuota.rules.forEach(rule => { - if (rule.type === 'cat' && catCount >= rule.count) { - additionalCost = totalPetCost - Number(rule.freeAmount) - catCount = catCount - rule.count - }else if(rule.type === 'smallDog' && smallDogCount >= rule.count){ - additionalCost = totalPetCost - Number(rule.freeAmount) - smallDogCount = smallDogCount - rule.count - }else if(rule.type === 'mediumDog' && mediumDogCount >= rule.count){ - additionalCost = totalPetCost - Number(rule.freeAmount) - mediumDogCount = mediumDogCount - rule.count - }else{ - additionalCost = totalPetCost - Number(rule.freeAmount) - } + // 根据 freePetConfig 配置计算免费宠物数量 + const freePetConfig = this.price_config.freePetConfig || this.freePetConfig + + // 计算收费的宠物数量(扣除免费数量) + const chargeableMediumDogCount = Math.max(0, mediumDogCount - freePetConfig.mediumDog.freeCount) + const chargeableSmallDogCount = Math.max(0, smallDogCount - freePetConfig.smallDog.freeCount) + const chargeableCatCount = Math.max(0, catCount - freePetConfig.cat.freeCount) + + // 计算额外宠物费用 + if (chargeableMediumDogCount > 0) { + additionalCost += (this.calculatePetCost({ + petType: 'dog', + bodyType: '中型', + }) * chargeableMediumDogCount) + additionalCostItem.push({ + itemName: '中型犬', + price: this.calculatePetCost({ + petType: 'dog', + bodyType: '中型', + }), + quantity: chargeableMediumDogCount, + unit: '只' }) - - - if (mediumDogCount > 0) { - additionalCostItem.push({ - itemName: '中型犬', - price: this.calculatePetCost({ - petType: 'dog', - bodyType: '中型', - }), - quantity: mediumDogCount, - unit: '只' - }) - } - if (smallDogCount > 0) { - additionalCostItem.push({ - itemName: '小型犬', - price: this.calculatePetCost({ - petType: 'dog', - bodyType: '小型', - }), - quantity: smallDogCount, - unit: '只' - }) - } - if (catCount > 0) { - additionalCostItem.push({ - itemName: '猫猫', - price: this.calculatePetCost({ - petType: 'cat', - }), - quantity: catCount, - unit: '只' - }) - } - - } - // 如果有大型犬,额外费用为40元/只 - if (largeDogCount > 0) { + } + if (chargeableSmallDogCount > 0) { + additionalCost += (this.calculatePetCost({ + petType: 'dog', + bodyType: '小型', + }) * chargeableSmallDogCount) + additionalCostItem.push({ + itemName: '小型犬', + price: this.calculatePetCost({ + petType: 'dog', + bodyType: '小型', + }), + quantity: chargeableSmallDogCount, + unit: '只' + }) + } + if (chargeableCatCount > 0) { + additionalCost += (this.calculatePetCost({ + petType: 'cat', + }) * chargeableCatCount) + additionalCostItem.push({ + itemName: '猫猫', + price: this.calculatePetCost({ + petType: 'cat', + }), + quantity: chargeableCatCount, + unit: '只' + }) + } + // 计算收费的大型犬数量(扣除免费数量) + const chargeableLargeDogCount = Math.max(0, largeDogCount - freePetConfig.largeDog.freeCount) + if (chargeableLargeDogCount > 0) { additionalCost += (this.calculatePetCost({ petType: 'dog', bodyType: '大型', - }) * largeDogCount) + }) * chargeableLargeDogCount) additionalCostItem.push({ itemName: '大型犬', price: this.calculatePetCost({ petType: 'dog', bodyType: '大型', }), - quantity: largeDogCount, + quantity: chargeableLargeDogCount, unit: '只' }) } // 当日多次服务次数 - 基础价格乘以倍数 let multServicesTotalCost = 0 const maxFeedCount = Math.max(...pets.map(pet => pet.feedCount)); + const multiService = this.price_config.multiService || { two: { price: 1 }, three: { price: 1 } } if (maxFeedCount === 2) { - multServicesTotalCost += baseServiceCost * this.price_config.multiService.two.price; // 基础价格 × 2次倍数 + multServicesTotalCost += baseServiceCost * (multiService.two.price || 1); // 基础价格 × 2次倍数 } else if (maxFeedCount === 3) { - multServicesTotalCost += baseServiceCost * this.price_config.multiService.three.price; // 基础价格 × 3次倍数 + multServicesTotalCost += baseServiceCost * (multiService.three.price || 1); // 基础价格 × 3次倍数 } priceDetails.push({ name: '专业喂养', @@ -575,13 +582,13 @@ name: '上门次数', item: [{ itemName: `1天2次 (${this.isHoliday(date) ? '节假日' : '非节假日'})`, - price: baseServiceCost * this.price_config.multiService.two.price, + price: baseServiceCost * (multiService.two.price || 1), quantity: maxFeedCount === 2 ? 1 : 0, unit: '天' }, { itemName: `1天3次 (${this.isHoliday(date) ? '节假日' : '非节假日'})`, - price: baseServiceCost * this.price_config.multiService.three.price, + price: baseServiceCost * (multiService.three.price || 1), quantity: maxFeedCount === 3 ? 1 : 0, unit: '天' }, @@ -626,14 +633,16 @@ }, calculatePetCost(pet) { // 宠物额外费用 不计算大型犬 - let petExtra = this.price_config.petExtra + let petExtra = this.price_config.petExtra || {} let petCost = 0; - if (pet.petType === 'cat') { - petCost += Number(petExtra.cat); // 猫额外费用 - } else if (pet.petType === 'dog' && pet.bodyType.includes('小型')) { - petCost += Number(petExtra.smallDog); // 小型犬额外费用 - } else if (pet.petType === 'dog' && pet.bodyType.includes('中型')) { - petCost += Number(petExtra.mediumDog); // 中型犬额外费用 + if (pet.petType === 'cat' && petExtra.cat) { + petCost += Number(petExtra.cat.price || 0); // 猫额外费用 + } else if (pet.petType === 'dog' && pet.bodyType.includes('小型') && petExtra.smallDog) { + petCost += Number(petExtra.smallDog.price || 0); // 小型犬额外费用 + } else if (pet.petType === 'dog' && pet.bodyType.includes('中型') && petExtra.mediumDog) { + petCost += Number(petExtra.mediumDog.price || 0); // 中型犬额外费用 + } else if (pet.petType === 'dog' && pet.bodyType.includes('大型') && petExtra.largeDog) { + petCost += Number(petExtra.largeDog.price || 0); // 大型犬额外费用 } return petCost; }, @@ -675,6 +684,9 @@ let priceConfig = this.$store.state.price_config console.log('价格配置:', priceConfig) + // 将价格配置赋值给组件 + this.price_config = priceConfig || {} + // 获取伴宠师等级价格和城市倍率 let companionPrice = this.$globalData.newOrderData.companionLevelPrice || 0 let cityPriceRate = this.$globalData.newOrderData.cityPriceRate || 1 @@ -821,7 +833,7 @@ //费用总计 oldPrice : this.originalTotalPrice, //提前熟悉的费用 - preFamiliarizePrice: this.price_config.preFamiliarize.price, + preFamiliarizePrice: (this.price_config.preFamiliarize && this.price_config.preFamiliarize.price) || 40, //伴宠师等级名称 companionLevelTitle : this.companionLevelTitle, @@ -876,10 +888,11 @@ // 当日多次服务次数 - 基础价格乘以倍数 let feedCountPrice = 0; + const multiService = this.price_config.multiService || { two: { price: 1 }, three: { price: 1 } } if (pet.feedCount == 2) { - feedCountPrice += price * this.price_config.multiService.two.price; // 基础价格 × 2次倍数 + feedCountPrice += price * (multiService.two.price || 1); // 基础价格 × 2次倍数 } else if (pet.feedCount == 3) { - feedCountPrice += price * this.price_config.multiService.three.price; // 基础价格 × 3次倍数 + feedCountPrice += price * (multiService.three.price || 1); // 基础价格 × 3次倍数 } return { petId: pet.petId, @@ -932,12 +945,13 @@ }) }, changePreFamiliarize(name) { + const preFamiliarizePrice = (this.price_config.preFamiliarize && this.price_config.preFamiliarize.price) || 40 if (name && name.length > 0) { this.needPreFamiliarize = name - this.originalTotalPrice = this.originalTotalPrice + this.price_config.preFamiliarize.price + this.originalTotalPrice = this.originalTotalPrice + preFamiliarizePrice } else { this.needPreFamiliarize = [] - this.originalTotalPrice = this.originalTotalPrice - this.price_config.preFamiliarize.price + this.originalTotalPrice = this.originalTotalPrice - preFamiliarizePrice } // 重新计算最优惠的优惠券 this.autoSelectBestCoupon() diff --git a/pages/newOrder/serviceNew2.vue b/pages/newOrder/serviceNew2.vue index 49e2990..3e62d42 100644 --- a/pages/newOrder/serviceNew2.vue +++ b/pages/newOrder/serviceNew2.vue @@ -377,6 +377,13 @@ export default { customServiceItemCount: [], // 当日价格 currentDayPrice: 0, + + freePetConfig : { + cat: { freeCount: 1 }, + smallDog: { freeCount: 0 }, + mediumDog: { freeCount: 0 }, + largeDog: { freeCount: 0 } + }, } }, @@ -806,52 +813,84 @@ export default { return acc + this.calculatePetCost(pet); }, 0); // 如果总宠物费用>30 - const freeQuota = this.price_config.freeQuota - if (totalPetCost > Number(freeQuota.threshold)) { - freeQuota.rules.forEach(rule => { - if (rule.type === 'cat' && catCount >= rule.count) { - additionalCost = totalPetCost - Number(rule.freeAmount) - catCount = catCount - rule.count - }else if(rule.type === 'smallDog' && smallDogCount >= rule.count){ - additionalCost = totalPetCost - Number(rule.freeAmount) - smallDogCount = smallDogCount - rule.count - }else if(rule.type === 'mediumDog' && mediumDogCount >= rule.count){ - additionalCost = totalPetCost - Number(rule.freeAmount) - mediumDogCount = mediumDogCount - rule.count - }else{ - additionalCost = totalPetCost - Number(rule.freeAmount) - } - }) - // if (totalPetCost > 30) { - // // 如果猫数量>=3 - // if (catCount >= 3) { - // additionalCost = totalPetCost - 30 - // catCount = catCount - 3 - // } else if (smallDogCount >= 2) { - // additionalCost = totalPetCost - 30 - // smallDogCount = smallDogCount - 2 - // } else if (mediumDogCount >= 1) { - // additionalCost = totalPetCost - 30 - // mediumDogCount = mediumDogCount - 1 - // } else { - // additionalCost = totalPetCost - 25 - // catCount = catCount - 1 - // smallDogCount = smallDogCount - 1 - // } - petTypeCounts.push({ - date, - largeDogCount, - mediumDogCount, - smallDogCount, - catCount - }) - } + // const freeQuota = this.price_config.freeQuota + // if (totalPetCost > Number(freeQuota.threshold)) { + // freeQuota.rules.forEach(rule => { + // if (rule.type === 'cat' && catCount >= rule.count) { + // additionalCost = totalPetCost - Number(rule.freeAmount) + // catCount = catCount - rule.count + // }else if(rule.type === 'smallDog' && smallDogCount >= rule.count){ + // additionalCost = totalPetCost - Number(rule.freeAmount) + // smallDogCount = smallDogCount - rule.count + // }else if(rule.type === 'mediumDog' && mediumDogCount >= rule.count){ + // additionalCost = totalPetCost - Number(rule.freeAmount) + // mediumDogCount = mediumDogCount - rule.count + // }else{ + // additionalCost = totalPetCost - Number(rule.freeAmount) + // } + // }) + // // if (totalPetCost > 30) { + // // // 如果猫数量>=3 + // // if (catCount >= 3) { + // // additionalCost = totalPetCost - 30 + // // catCount = catCount - 3 + // // } else if (smallDogCount >= 2) { + // // additionalCost = totalPetCost - 30 + // // smallDogCount = smallDogCount - 2 + // // } else if (mediumDogCount >= 1) { + // // additionalCost = totalPetCost - 30 + // // mediumDogCount = mediumDogCount - 1 + // // } else { + // // additionalCost = totalPetCost - 25 + // // catCount = catCount - 1 + // // smallDogCount = smallDogCount - 1 + // // } + // } + petTypeCounts.push({ + date, + largeDogCount, + mediumDogCount, + smallDogCount, + catCount + }) - // 如果有大型犬,额外费用为40元/只 - if (largeDogCount > 0) { - additionalCost += (40 * largeDogCount) + // 根据 freePetConfig 配置计算免费宠物数量 + const freePetConfig = this.price_config.freePetConfig || this.freePetConfig + + // 如果有大型犬,超过免费数量的收费 + if (largeDogCount > freePetConfig.largeDog.freeCount) { + const chargeableCount = largeDogCount - freePetConfig.largeDog.freeCount + additionalCost += (this.calculatePetCost({ + petType: 'dog', + bodyType: '大型', + }) * chargeableCount) + } + // 如果有中型犬,超过免费数量的收费 + if (mediumDogCount > freePetConfig.mediumDog.freeCount) { + const chargeableCount = mediumDogCount - freePetConfig.mediumDog.freeCount + additionalCost += (this.calculatePetCost({ + petType: 'dog', + bodyType: '中型', + }) * chargeableCount) } + // 如果有小型犬,超过免费数量的收费 + if (smallDogCount > freePetConfig.smallDog.freeCount) { + const chargeableCount = smallDogCount - freePetConfig.smallDog.freeCount + additionalCost += (this.calculatePetCost({ + petType: 'dog', + bodyType: '小型', + }) * chargeableCount) + } + // 如果有猫,超过免费数量的收费 + if (catCount > freePetConfig.cat.freeCount) { + const chargeableCount = catCount - freePetConfig.cat.freeCount + additionalCost += (this.calculatePetCost({ + petType: 'cat', + bodyType: '小型', + }) * chargeableCount) + } + // 如果有额外费用,添加到总额外费用 if (additionalCost > 0) { additionalTotalCost += additionalCost; @@ -917,11 +956,11 @@ export default { let petExtra = this.price_config.petExtra let petCost = 0; if (pet.petType === 'cat') { - petCost += Number(petExtra.cat); // 猫额外费用 + petCost += Number(petExtra.cat.price); // 猫额外费用 } else if (pet.petType === 'dog' && pet.bodyType.includes('小型')) { - petCost += Number(petExtra.smallDog); // 小型犬额外费用 + petCost += Number(petExtra.smallDog.price); // 小型犬额外费用 } else if (pet.petType === 'dog' && pet.bodyType.includes('中型')) { - petCost += Number(petExtra.mediumDog); // 中型犬额外费用 + petCost += Number(petExtra.mediumDog.price); // 中型犬额外费用 } return petCost; }, @@ -1044,50 +1083,60 @@ export default { const petTypeArr = this.petTypeCounts.filter(item => item.largeDogCount > 0 || item.mediumDogCount > 0 || item.smallDogCount > 0 || item.catCount > 0) // 假设largeDogCount=1 mediumDogCount=2 smallDogCount=3 catCount=4 则生成4条数据 + petTypeArr.forEach(item => { - if (item.largeDogCount > 0) { + // 获取免费宠物配置 + const freePetConfig = this.price_config.freePetConfig || this.freePetConfig + + // 只显示收费的宠物数量(扣除免费数量) + const chargeableLargeDogCount = Math.max(0, item.largeDogCount - freePetConfig.largeDog.freeCount) + const chargeableMediumDogCount = Math.max(0, item.mediumDogCount - freePetConfig.mediumDog.freeCount) + const chargeableSmallDogCount = Math.max(0, item.smallDogCount - freePetConfig.smallDog.freeCount) + const chargeableCatCount = Math.max(0, item.catCount - freePetConfig.cat.freeCount) + + if (chargeableLargeDogCount > 0) { additionalCostItem.push({ itemName: '大型犬', price: this.calculatePetCost({ petType: 'dog', bodyType: '大型', }), - quantity: item.largeDogCount, + quantity: chargeableLargeDogCount, unit: '只', date: item.date }) } - if (item.mediumDogCount > 0) { + if (chargeableMediumDogCount > 0) { additionalCostItem.push({ itemName: '中型犬', price: this.calculatePetCost({ petType: 'dog', bodyType: '中型', }), - quantity: item.mediumDogCount, + quantity: chargeableMediumDogCount, unit: '只', date: item.date }) } - if (item.smallDogCount > 0) { + if (chargeableSmallDogCount > 0) { additionalCostItem.push({ itemName: '小型犬', price: this.calculatePetCost({ petType: 'dog', bodyType: '小型', }), - quantity: item.smallDogCount, + quantity: chargeableSmallDogCount, unit: '只', date: item.date }) } - if (item.catCount > 0) { + if (chargeableCatCount > 0) { additionalCostItem.push({ itemName: '猫猫', price: this.calculatePetCost({ petType: 'cat', }), - quantity: item.catCount, + quantity: chargeableCatCount, unit: '只', date: item.date }) @@ -1249,55 +1298,58 @@ export default { let totalPetCost = currentDayPets.reduce((acc, pet) => { return acc + this.calculatePetCost(pet); }, 0); - // 如果总宠物费用>30 - const freeQuota = this.price_config.freeQuota - if (totalPetCost > Number(freeQuota.threshold)) { - freeQuota.rules.forEach(rule => { - if (rule.type === 'cat' && catCount >= rule.count) { - additionalCost = totalPetCost - Number(rule.freeAmount) - catCount = catCount - rule.count - }else if(rule.type === 'smallDog' && smallDogCount >= rule.count){ - additionalCost = totalPetCost - Number(rule.freeAmount) - smallDogCount = smallDogCount - rule.count - }else if(rule.type === 'mediumDog' && mediumDogCount >= rule.count){ - additionalCost = totalPetCost - Number(rule.freeAmount) - mediumDogCount = mediumDogCount - rule.count - }else{ - additionalCost = totalPetCost - Number(rule.freeAmount) - } - }) - - if (largeDogCount > 0) { - additionalCostItem.push({ itemName: '大型犬', price: this.calculatePetCost({ - petType: 'dog', - bodyType: '大型', - }), quantity: largeDogCount, unit: '只' }) - } - if (mediumDogCount > 0) { - additionalCostItem.push({ itemName: '中型犬', price: this.calculatePetCost({ - petType: 'dog', - bodyType: '中型', - }), quantity: mediumDogCount, unit: '只' }) - } - if (smallDogCount > 0) { - additionalCostItem.push({ itemName: '小型犬', price: this.calculatePetCost({ - petType: 'dog', - bodyType: '小型', - }), quantity: smallDogCount, unit: '只' }) - } - if (catCount > 0) { - additionalCostItem.push({ itemName: '猫猫', price: this.calculatePetCost({ - petType: 'cat', - }), quantity: catCount, unit: '只' }) - } - + // 根据 freePetConfig 配置计算免费宠物数量 + const freePetConfig = this.price_config.freePetConfig || { + cat: { freeCount: 1 }, + smallDog: { freeCount: 0 }, + mediumDog: { freeCount: 0 }, + largeDog: { freeCount: 0 } } - // 如果有大型犬,额外费用为40元/只 - if (largeDogCount > 0) { + + // 计算收费的宠物数量(扣除免费数量) + const chargeableLargeDogCount = Math.max(0, largeDogCount - freePetConfig.largeDog.freeCount) + const chargeableMediumDogCount = Math.max(0, mediumDogCount - freePetConfig.mediumDog.freeCount) + const chargeableSmallDogCount = Math.max(0, smallDogCount - freePetConfig.smallDog.freeCount) + const chargeableCatCount = Math.max(0, catCount - freePetConfig.cat.freeCount) + + // 计算额外宠物费用 + if (chargeableLargeDogCount > 0) { additionalCost += (this.calculatePetCost({ petType: 'dog', bodyType: '大型', - }) * largeDogCount) + }) * chargeableLargeDogCount) + additionalCostItem.push({ itemName: '大型犬', price: this.calculatePetCost({ + petType: 'dog', + bodyType: '大型', + }), quantity: chargeableLargeDogCount, unit: '只' }) + } + if (chargeableMediumDogCount > 0) { + additionalCost += (this.calculatePetCost({ + petType: 'dog', + bodyType: '中型', + }) * chargeableMediumDogCount) + additionalCostItem.push({ itemName: '中型犬', price: this.calculatePetCost({ + petType: 'dog', + bodyType: '中型', + }), quantity: chargeableMediumDogCount, unit: '只' }) + } + if (chargeableSmallDogCount > 0) { + additionalCost += (this.calculatePetCost({ + petType: 'dog', + bodyType: '小型', + }) * chargeableSmallDogCount) + additionalCostItem.push({ itemName: '小型犬', price: this.calculatePetCost({ + petType: 'dog', + bodyType: '小型', + }), quantity: chargeableSmallDogCount, unit: '只' }) + } + if (chargeableCatCount > 0) { + additionalCost += (this.calculatePetCost({ + petType: 'cat', + }) * chargeableCatCount) + additionalCostItem.push({ itemName: '猫猫', price: this.calculatePetCost({ + petType: 'cat', + }), quantity: chargeableCatCount, unit: '只' }) } // 当日多次服务次数 - 基础价格乘以倍数 let multServicesTotalCost = 0