Browse Source

refactor(订单确认): 重构宠物费用计算逻辑并添加默认值处理

- 使用新的 freePetConfig 配置替代旧的 freeQuota 规则计算宠物费用
- 为 price_config 相关字段添加默认值处理,提高代码健壮性
- 优化费用计算逻辑,只对超过免费数量的宠物进行收费
master
前端-胡立永 1 week ago
parent
commit
c9da242480
2 changed files with 241 additions and 175 deletions
  1. +90
    -76
      pages/newOrder/confirmOrder.vue
  2. +151
    -99
      pages/newOrder/serviceNew2.vue

+ 90
- 76
pages/newOrder/confirmOrder.vue View File

@ -155,9 +155,9 @@
<view class="service-new-address">
<view v-if="needPreFamiliarize.length>0" class="total-cost">
<view>提前熟悉 </view>
<view>¥{{ price_config.preFamiliarize.price }}</view>
</view>
<view>提前熟悉 </view>
<view>¥{{ (price_config.preFamiliarize && price_config.preFamiliarize.price) || 40 }}</view>
</view>
<!-- 伴宠师价格已合并到基础服务价格中不再单独显示 -->
<view class="total-cost">
<view>费用总计 </view>
@ -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()


+ 151
- 99
pages/newOrder/serviceNew2.vue View File

@ -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


Loading…
Cancel
Save