Commit 0ed2a431 by cen

fix: 更新价格计算逻辑以支持订单类型和数量变化

- 在 OrderTypePopup.vue 中修正当前订单类型的引用,确保使用 orderTypeCode
- 在 CreateOrder.vue、Package.vue 和 SkuGoods.vue 中调整价格计算逻辑,确保在数量变化时重新计算 finalPrice
- 更新 OrderStrategy.vue 中的折扣和抵扣金额的验证逻辑,确保最小值为 0.01
- 优化多个组件中的基础价格计算,确保不再乘以数量
1 parent 227c81c1
<template>
<view class="merchant-list">
<template v-for="(i, index) in list">
<view :key="index" @click="choose(i)" :class="['item flex', { active: currentId ? i.value == currentId : i.value == data[params.idKey] }]">
<view :key="index" @click="choose(i)" :class="['item flex', { active: currentId ? i.value == currentId : i.value == data.orderTypeCode }]">
<view class="sprite-icon icon-tag-checked"></view>
{{ i.name }}
</view>
......@@ -27,11 +27,14 @@ export default {
this.updateParams(params, data || [])
},
initData (list) {
const { params: { idKey }, data } = this
if (data[idKey]) {
this.currentId = data[idKey]
const { data } = this
// 使用 orderTypeCode 而不是 djOrderType,因为 orderTypeCode 存储的是代码(如 "1"),而 djOrderType 存储的是名称(如 "正常订单")
if (data.orderTypeCode) {
this.currentId = data.orderTypeCode
let item = list.find(i => i.value == this.currentId)
this.$emit('choose', item)
if (item) {
this.$emit('choose', item)
}
}
this.list = list
this.$emit('onComplete')
......
<template>
<view class="list">
<view
@click="currentId = ''"
:class="['item flex',{active:!currentId}]"
>
<view @click="currentId = ''" :class="['item flex', { active: !currentId }]">
<view class="sprite-icon icon-tag-checked"></view>
全部
</view>
<template v-for="(i, index) in list">
<view
:key="index"
@click="currentId = i.id"
:class="['item flex',{active:currentId == i.id}]"
>
<view :key="index" @click="currentId = i.id" :class="['item flex', { active: currentId == i.id }]">
<view class="sprite-icon icon-tag-checked"></view>
{{i.name}}
{{ i.name }}
</view>
</template>
</view>
</template>
<script type='text/ecmascript-6'>
<script type="text/ecmascript-6">
export default {
name: 'OrderStatusList',
inject: ['userInfo'],
......@@ -81,7 +74,12 @@ export default {
name: '已退款',
value: [5, 12],
id: 11
}
},
{
name: '审批中',
value: [14],
id: 12
},
]),
currentId: '',
}
......@@ -120,7 +118,7 @@ export default {
}
</script>
<style scoped lang='scss'>
<style scoped lang="scss">
.list {
display: grid;
grid-template-columns: repeat(6, 1fr);
......@@ -156,4 +154,4 @@ export default {
}
}
}
</style>
\ No newline at end of file
</style>
const screenList = [
{
title: '转化人员',
......@@ -27,18 +26,16 @@ const screenList = [
componentName: 'TimeList',
params: {
idKey: ['placeOrderStartDateStr', 'placeOrderEndDateStr'],
currentIdKey: 'orderTimeId',
},
currentIdKey: 'orderTimeId'
}
},
{
title: '支付时间',
componentName: 'TimeList',
params: {
idKey: ['installUpgradeBeginTimeStr', 'installUpgradeEndTimeStr'],
currentIdKey: 'payTimeId',
},
currentIdKey: 'payTimeId'
}
},
{
title: '订单状态',
......@@ -48,13 +45,19 @@ const screenList = [
}
},
{
title: '异常标记',
componentName: 'OrderStatusList',
params: {
idKey: 'statusList'
}
},
{
title: '支付方式',
componentName: 'PayList',
params: {
idKey: 'payType'
}
},
}
]
export default { screenList }
\ No newline at end of file
export default { screenList }
......@@ -242,10 +242,13 @@ export default {
this.initData()
},
methods: {
// 计算单个商品的最终价格
calculateFinalPrice(goods, basePrice, orderTypeCode) {
if (!basePrice || !orderTypeCode) {
return Math.round(basePrice * 100) / 100
// 计算单个商品的最终价格(单价优惠后的价格 * 数量)
calculateFinalPrice(goods, sellPrice, number, orderTypeCode) {
const price = parseFloat(sellPrice || 0)
const qty = parseInt(number || 1)
if (!price || !orderTypeCode) {
return Math.round(price * qty * 100) / 100
}
const orderType = String(orderTypeCode || '')
......@@ -255,16 +258,18 @@ export default {
if (orderType === '2') {
if (strategyConfig.mode === 'ratio' && strategyConfig.discountRatio) {
const ratio = parseFloat(strategyConfig.discountRatio)
if (ratio >= 1 && ratio <= 10) {
return Math.round(basePrice * (ratio / 10) * 100) / 100
if (ratio >= 0.1 && ratio <= 10) {
// 单价 * 折扣比例 * 数量
return Math.round(price * (ratio / 10) * qty * 100) / 100
}
} else if (strategyConfig.mode === 'amount' && strategyConfig.discountAmount) {
const discountAmount = parseFloat(strategyConfig.discountAmount)
if (discountAmount >= 0 && discountAmount <= basePrice) {
return Math.round((basePrice - discountAmount) * 100) / 100
if (discountAmount >= 0.01 && discountAmount <= price) {
// (单价 - 折扣金额) * 数量
return Math.round((price - discountAmount) * qty * 100) / 100
}
}
return Math.round(basePrice * 100) / 100
return Math.round(price * qty * 100) / 100
}
// '3' 买赠策略
......@@ -272,21 +277,22 @@ export default {
if (strategyConfig.isGift === '1') {
return 0
}
return Math.round(basePrice * 100) / 100
return Math.round(price * qty * 100) / 100
}
// '5' 组合付款
if (orderType === '5') {
if (strategyConfig.mode === 'deduct' && strategyConfig.deductAmount) {
const deductAmount = parseFloat(strategyConfig.deductAmount)
if (deductAmount >= 0 && deductAmount <= basePrice) {
return Math.round((basePrice - deductAmount) * 100) / 100
if (deductAmount >= 0.01 && deductAmount <= price) {
// (单价 - 抵扣金额) * 数量
return Math.round((price - deductAmount) * qty * 100) / 100
}
}
return Math.round(basePrice * 100) / 100
return Math.round(price * qty * 100) / 100
}
return Math.round(basePrice * 100) / 100
return Math.round(price * qty * 100) / 100
},
// 计算主订单的总 finalPrice
calculateTotalFinalPrice(subGoodsList) {
......@@ -297,7 +303,7 @@ export default {
return Math.round(total * 100) / 100
},
// 计算优惠相关字段(discountType, discountValue, discountAmount)
calculateDiscountFields(goods, basePrice, orderTypeCode) {
calculateDiscountFields(goods, sellPrice, number, orderTypeCode) {
const result = {
discountType: null,
discountValue: null,
......@@ -308,6 +314,9 @@ export default {
return result
}
const price = parseFloat(sellPrice || 0)
const qty = parseInt(number || 1)
const totalPrice = price * qty
const orderType = String(orderTypeCode || '')
const strategyConfig = goods.strategyConfig || {}
......@@ -317,12 +326,14 @@ export default {
// 比例折扣
result.discountType = 1
result.discountValue = parseFloat(strategyConfig.discountRatio)
result.discountAmount = Math.round((basePrice - basePrice * (result.discountValue / 10)) * 100) / 100
// 优惠金额 = 总价 - 折扣后总价
result.discountAmount = Math.round((totalPrice - totalPrice * (result.discountValue / 10)) * 100) / 100
} else if (strategyConfig.mode === 'amount' && strategyConfig.discountAmount) {
// 折扣金额
// 折扣金额(单价折扣)
result.discountType = 2
result.discountValue = parseFloat(strategyConfig.discountAmount)
result.discountAmount = result.discountValue
// 优惠金额 = 单价折扣 * 数量
result.discountAmount = Math.round(result.discountValue * qty * 100) / 100
} else {
// 无折扣
result.discountType = 3
......@@ -334,7 +345,7 @@ export default {
if (strategyConfig.isGift === '1') {
// 赠品
result.discountType = 4
result.discountAmount = basePrice
result.discountAmount = totalPrice
} else {
// 非赠品
result.discountType = 5
......@@ -344,10 +355,11 @@ export default {
// '5' 组合付款
else if (orderType === '5') {
if (strategyConfig.mode === 'deduct' && strategyConfig.deductAmount) {
// 抵扣金额(类似折扣金额)
// 抵扣金额(单价抵扣)
result.discountType = 2
result.discountValue = parseFloat(strategyConfig.deductAmount)
result.discountAmount = result.discountValue
// 优惠金额 = 单价抵扣 * 数量
result.discountAmount = Math.round(result.discountValue * qty * 100) / 100
} else {
// 无抵扣
result.discountType = 3
......@@ -651,9 +663,10 @@ export default {
goodsList.map(e => {
if (e.goodsFlag == 1) {
// 单商品
const basePrice = parseFloat(e.goods.sellPrice || 0) * parseInt(e.goods.number || 1)
const finalPrice = e.goods.finalPrice !== undefined ? parseFloat(e.goods.finalPrice) : this.calculateFinalPrice(e.goods, basePrice, obj.orderTypeCode)
const discountFields = this.calculateDiscountFields(e.goods, basePrice, obj.orderTypeCode)
const sellPrice = parseFloat(e.goods.sellPrice || 0)
const number = parseInt(e.goods.number || 1)
const finalPrice = e.goods.finalPrice !== undefined ? parseFloat(e.goods.finalPrice) : this.calculateFinalPrice(e.goods, sellPrice, number, obj.orderTypeCode)
const discountFields = this.calculateDiscountFields(e.goods, sellPrice, number, obj.orderTypeCode)
subGoodsList.push({
totalPrice: e.goods.sellPrice,
number: e.goods.number,
......@@ -667,9 +680,10 @@ export default {
// 套餐
if (e.goodsFlag == 3) {
e.packageGoods.goodsList.forEach(g => {
const basePrice = parseFloat(g.sellPrice || 0) * parseInt(e.packageGoods.number || 1)
const finalPrice = g.finalPrice !== undefined ? parseFloat(g.finalPrice) : this.calculateFinalPrice(g, basePrice, obj.orderTypeCode)
const discountFields = this.calculateDiscountFields(g, basePrice, obj.orderTypeCode)
const sellPrice = parseFloat(g.sellPrice || 0)
const number = parseInt(e.packageGoods.number || 1)
const finalPrice = g.finalPrice !== undefined ? parseFloat(g.finalPrice) : this.calculateFinalPrice(g, sellPrice, number, obj.orderTypeCode)
const discountFields = this.calculateDiscountFields(g, sellPrice, number, obj.orderTypeCode)
subGoodsList.push({
packageCode: e.packageGoods.packageCode,
packageName: e.packageGoods.packageName,
......@@ -687,9 +701,10 @@ export default {
// 多规格
if (e.goodsFlag == 2) {
const goods = e.multipleSpecificationsGoods.goods
const basePrice = parseFloat(goods.sellPrice || 0) * parseInt(e.multipleSpecificationsGoods.number || 1)
const finalPrice = goods.finalPrice !== undefined ? parseFloat(goods.finalPrice) : this.calculateFinalPrice(goods, basePrice, obj.orderTypeCode)
const discountFields = this.calculateDiscountFields(goods, basePrice, obj.orderTypeCode)
const sellPrice = parseFloat(goods.sellPrice || 0)
const number = parseInt(e.multipleSpecificationsGoods.number || 1)
const finalPrice = goods.finalPrice !== undefined ? parseFloat(goods.finalPrice) : this.calculateFinalPrice(goods, sellPrice, number, obj.orderTypeCode)
const discountFields = this.calculateDiscountFields(goods, sellPrice, number, obj.orderTypeCode)
subGoodsList.push({
baseGoodsId: goods.baseGoodsId,
totalPrice: Number(goods.sellPrice),
......@@ -753,7 +768,7 @@ export default {
if (item.goods && item.goods.sellPrice && item.goods.number) {
console.log(item.goods, '---单商品')
if (useFinalPrice && item.goods.finalPrice !== undefined) {
// 使用优惠后的价格
// 使用优惠后的价格(已经是总价:单价优惠后 × 数量)
total += parseFloat(item.goods.finalPrice || 0)
} else {
// 使用原始价格
......
......@@ -124,15 +124,15 @@ export default {
if (this.strategyType === 'discount') {
if (this.localState.mode === 'ratio' && this.localState.discountRatio) {
const ratio = parseFloat(this.localState.discountRatio)
// 折扣比例:1-10,表示 1 折到 10 折(即 0.1 到 1.0)
if (ratio >= 1 && ratio <= 10) {
// 折扣比例:0.1-10,表示 0.1 折到 10 折(即 0.01 到 1.0)
if (ratio >= 0.1 && ratio <= 10) {
const finalPrice = basePrice * (ratio / 10)
return '¥' + (Math.round(finalPrice * 100) / 100).toFixed(2)
}
} else if (this.localState.mode === 'amount' && this.localState.discountAmount) {
const discountAmount = parseFloat(this.localState.discountAmount)
// 折扣金额:0 到不能大于基础价格
if (discountAmount >= 0 && discountAmount <= basePrice) {
// 折扣金额:0.01 到不能大于基础价格
if (discountAmount >= 0.01 && discountAmount <= basePrice) {
const finalPrice = basePrice - discountAmount
return '¥' + (Math.round(finalPrice * 100) / 100).toFixed(2)
}
......@@ -152,8 +152,8 @@ export default {
if (this.strategyType === 'combined') {
if (this.localState.mode === 'deduct' && this.localState.deductAmount) {
const deductAmount = parseFloat(this.localState.deductAmount)
// 抵扣金额:0 到不能大于基础价格
if (deductAmount >= 0 && deductAmount <= basePrice) {
// 抵扣金额:0.01 到不能大于基础价格
if (deductAmount >= 0.01 && deductAmount <= basePrice) {
const finalPrice = basePrice - deductAmount
return '¥' + (Math.round(finalPrice * 100) / 100).toFixed(2)
}
......@@ -214,12 +214,12 @@ export default {
if (this.strategyType === 'discount') {
if (this.localState.mode === 'ratio' && this.localState.discountRatio) {
const ratio = parseFloat(this.localState.discountRatio)
if (ratio >= 1 && ratio <= 10) {
if (ratio >= 0.1 && ratio <= 10) {
return Math.round(bp * (ratio / 10) * 100) / 100
}
} else if (this.localState.mode === 'amount' && this.localState.discountAmount) {
const discountAmount = parseFloat(this.localState.discountAmount)
if (discountAmount >= 0 && discountAmount <= bp) {
if (discountAmount >= 0.01 && discountAmount <= bp) {
return Math.round((bp - discountAmount) * 100) / 100
}
}
......@@ -238,7 +238,7 @@ export default {
if (this.strategyType === 'combined') {
if (this.localState.mode === 'deduct' && this.localState.deductAmount) {
const deductAmount = parseFloat(this.localState.deductAmount)
if (deductAmount >= 0 && deductAmount <= bp) {
if (deductAmount >= 0.01 && deductAmount <= bp) {
return Math.round((bp - deductAmount) * 100) / 100
}
}
......@@ -252,9 +252,9 @@ export default {
let v = this.localState[key]
if (key === 'discountAmount' || key === 'deductAmount') {
// 折扣金额/抵扣金额:跟价格输入框一样的验证逻辑(直接截断,不四舍五入)
// 折扣金额/抵扣金额:限制 0.01 到商品原价(直接截断,不四舍五入)
if (!v || (typeof v === 'string' && v.trim() === '')) {
v = '0'
v = '0.01'
}
// 限制只能输入数字和小数点
v = String(v).replace(/[^\d.]/g, '')
......@@ -274,9 +274,9 @@ export default {
const basePrice = parseFloat(this.basePrice || 0)
let amount = parseFloat(v)
// 限制最小值为0
if (isNaN(amount) || amount < 0) {
v = '0'
// 限制最小值为 0.01
if (isNaN(amount) || amount < 0.01) {
v = '0.01'
}
// 限制最大值为基础价格
else if (basePrice > 0 && amount > basePrice) {
......@@ -286,7 +286,7 @@ export default {
this.$set(this.localState, key, v)
} else if (key === 'discountRatio') {
// 折扣比例:限制在 1-10 之间(直接截断,不四舍五入)
// 折扣比例:限制在 0.1-10 之间,限制 1 位小数(直接截断,不四舍五入)
if (!v || (typeof v === 'string' && v.trim() === '')) {
v = ''
} else {
......@@ -296,19 +296,19 @@ export default {
if (v.split('.').length > 2) {
v = v.slice(0, v.lastIndexOf('.')) + v.slice(v.lastIndexOf('.') + 1)
}
// 限制最多两位小数(直接截断,不四舍五入)
// 限制最多 1 位小数(直接截断,不四舍五入)
if (v.includes('.')) {
const parts = v.split('.')
if (parts[1] && parts[1].length > 2) {
parts[1] = parts[1].substring(0, 2)
if (parts[1] && parts[1].length > 1) {
parts[1] = parts[1].substring(0, 1)
v = parts.join('.')
}
}
const ratio = parseFloat(v)
if (!isNaN(ratio)) {
if (ratio < 1) {
v = '1'
if (ratio < 0.1) {
v = '0.1'
} else if (ratio > 10) {
v = '10'
}
......
......@@ -27,7 +27,7 @@
</view>
</view>
<!-- 折扣 / 买赠 / 组合付款策略组件 -->
<OrderStrategy v-if="getStrategyType(goods)" :strategyType="getStrategyType(goods)" v-model="goods.strategyConfig" :basePrice="getBasePrice(goods, item)" :displayPrice="getDisplayPrice(goods, item)" @priceChange="onPriceChange(goods, $event)" />
<OrderStrategy v-if="getStrategyType(goods)" :strategyType="getStrategyType(goods)" v-model="goods.strategyConfig" :basePrice="getBasePrice(goods, item)" :displayPrice="getDisplayPrice(goods, item)" @priceChange="onPriceChange(goods, $event, item)" />
</view>
</view>
</view>
......@@ -112,6 +112,8 @@ export default {
}
// 更新值
this.$set(item.packageGoods, 'number', value)
// 数量改变时,需要重新计算所有包内商品的 finalPrice
this.recalculateFinalPriceForPackage(item)
this.$emit('changeTotalPrice')
},
// 增加数量
......@@ -124,6 +126,8 @@ export default {
const newValue = currentValue >= 999 ? 999 : currentValue + 1
this.$set(item.packageGoods, 'number', newValue.toString())
}
// 数量改变时,需要重新计算所有包内商品的 finalPrice
this.recalculateFinalPriceForPackage(item)
this.$emit('changeTotalPrice')
},
// 减少数量
......@@ -137,6 +141,8 @@ export default {
// 确保减少后的值不小于1
const newValue = Math.max(1, currentValue - 1)
this.$set(item.packageGoods, 'number', newValue.toString())
// 数量改变时,需要重新计算所有包内商品的 finalPrice
this.recalculateFinalPriceForPackage(item)
this.$emit('changeTotalPrice')
},
validatePrice(event, goods, ind, index) {
......@@ -185,13 +191,59 @@ export default {
this.list.splice(index, 1)
this.$emit('changeTotalPrice')
},
// 计算套餐的总价
// 计算套餐的总价(显示优惠后的单价相加,不乘以数量)
changeTotal(item) {
let total = 0
item.packageGoods.goodsList.forEach(i => {
// total += parseFloat(i.sellPrice) * parseInt(item.packageGoods.number)
total += parseFloat(i.sellPrice)
const orderType = String(this.orderTypeCode || '')
item.packageGoods.goodsList.forEach(goods => {
const sellPrice = parseFloat(goods.sellPrice || 0)
// 正常订单('1')和冲量订单('4'):显示原价相加
if (orderType === '1' || orderType === '4') {
total += sellPrice
}
// 其他类型(打折/买赠/组合付款):显示优惠后的单价相加
else {
const strategyConfig = goods.strategyConfig || {}
let finalPricePerUnit = sellPrice // 默认使用原价
// '2' 打折策略
if (orderType === '2') {
if (strategyConfig.mode === 'ratio' && strategyConfig.discountRatio) {
const ratio = parseFloat(strategyConfig.discountRatio)
if (ratio >= 0.1 && ratio <= 10) {
finalPricePerUnit = Math.round(sellPrice * (ratio / 10) * 100) / 100
}
} else if (strategyConfig.mode === 'amount' && strategyConfig.discountAmount) {
const discountAmount = parseFloat(strategyConfig.discountAmount)
if (discountAmount >= 0.01 && discountAmount <= sellPrice) {
finalPricePerUnit = Math.round((sellPrice - discountAmount) * 100) / 100
}
}
}
// '3' 买赠策略
if (orderType === '3') {
if (strategyConfig.isGift === '1') {
finalPricePerUnit = 0
}
}
// '5' 组合付款
if (orderType === '5') {
if (strategyConfig.mode === 'deduct' && strategyConfig.deductAmount) {
const deductAmount = parseFloat(strategyConfig.deductAmount)
if (deductAmount >= 0.01 && deductAmount <= sellPrice) {
finalPricePerUnit = Math.round((sellPrice - deductAmount) * 100) / 100
}
}
}
total += finalPricePerUnit
}
})
item.packageGoods.upgradePrice = total
return total.toFixed(2)
},
......@@ -216,14 +268,12 @@ export default {
}
return null
},
// 计算基础价格(单价 * 数量)
// 计算基础价格(单价,不乘以数量)
getBasePrice(goods, item) {
if (!goods) return 0
const sellPrice = parseFloat(goods.sellPrice || 0)
const number = parseInt(item.packageGoods.number || 1)
const basePrice = sellPrice * number
// 四舍五入保留两位小数
return Math.round(basePrice * 100) / 100
return Math.round(sellPrice * 100) / 100
},
// 计算最终显示价格
getDisplayPrice(goods, item) {
......@@ -237,13 +287,13 @@ export default {
if (orderType === '2') {
if (strategyConfig.mode === 'ratio' && strategyConfig.discountRatio) {
const ratio = parseFloat(strategyConfig.discountRatio)
if (ratio >= 1 && ratio <= 10) {
if (ratio >= 0.1 && ratio <= 10) {
const finalPrice = basePrice * (ratio / 10)
return '¥' + (Math.round(finalPrice * 100) / 100).toFixed(2)
}
} else if (strategyConfig.mode === 'amount' && strategyConfig.discountAmount) {
const discountAmount = parseFloat(strategyConfig.discountAmount)
if (discountAmount >= 0 && discountAmount <= basePrice) {
if (discountAmount >= 0.01 && discountAmount <= basePrice) {
const finalPrice = basePrice - discountAmount
return '¥' + (Math.round(finalPrice * 100) / 100).toFixed(2)
}
......@@ -263,7 +313,7 @@ export default {
if (orderType === '5') {
if (strategyConfig.mode === 'deduct' && strategyConfig.deductAmount) {
const deductAmount = parseFloat(strategyConfig.deductAmount)
if (deductAmount >= 0 && deductAmount <= basePrice) {
if (deductAmount >= 0.01 && deductAmount <= basePrice) {
const finalPrice = basePrice - deductAmount
return '¥' + (Math.round(finalPrice * 100) / 100).toFixed(2)
}
......@@ -273,12 +323,34 @@ export default {
return '¥' + basePrice.toFixed(2)
},
// 价格变化回调
onPriceChange(goods, finalPrice) {
// 可以在这里更新商品的其他价格相关字段
this.$set(goods, 'finalPrice', finalPrice)
// 价格变化回调(finalPrice 是单件商品优惠后的价格,需要乘以数量得到总价)
onPriceChange(goods, finalPricePerUnit, item) {
// 从父组件的 item 中获取数量
const packageItem = this.list.find(pkg => pkg.packageGoods.goodsList.includes(goods))
if (packageItem) {
const number = parseInt(packageItem.packageGoods.number || 1)
const totalFinalPrice = Math.round(finalPricePerUnit * number * 100) / 100
// 存储总价和单价优惠后的价格
this.$set(goods, 'finalPrice', totalFinalPrice)
this.$set(goods, 'finalPricePerUnit', finalPricePerUnit)
} else {
this.$set(goods, 'finalPrice', finalPricePerUnit)
this.$set(goods, 'finalPricePerUnit', finalPricePerUnit)
}
this.$emit('changeTotalPrice')
},
// 重新计算套餐内所有商品的 finalPrice(当数量改变时调用)
recalculateFinalPriceForPackage(item) {
const number = parseInt(item.packageGoods.number || 1)
if (item.packageGoods && item.packageGoods.goodsList) {
item.packageGoods.goodsList.forEach(goods => {
if (goods.strategyConfig && goods.finalPricePerUnit !== undefined) {
const totalFinalPrice = Math.round(goods.finalPricePerUnit * number * 100) / 100
this.$set(goods, 'finalPrice', totalFinalPrice)
}
})
}
},
// 初始化策略配置
initStrategyConfig() {
if (!this.list || this.list.length === 0) return
......
......@@ -106,14 +106,12 @@ export default {
}
return null
},
// 计算基础价格(单价 * 数量)
// 计算基础价格(单价,不乘以数量)
getBasePrice(item) {
if (!item || !item.goods) return 0
const sellPrice = parseFloat(item.goods.sellPrice || 0)
const number = parseInt(item.goods.number || 1)
const basePrice = sellPrice * number
// 四舍五入保留两位小数
return Math.round(basePrice * 100) / 100
return Math.round(sellPrice * 100) / 100
},
// 计算最终显示价格
getDisplayPrice(item) {
......@@ -126,16 +124,16 @@ export default {
// '2' 打折策略
if (orderType === '2') {
if (strategyConfig.mode === 'ratio' && strategyConfig.discountRatio) {
// 折扣比例:1-10,表示 1 折到 10 折(即 0.1 到 1.0)
// 折扣比例:0.1-10,表示 0.1 折到 10 折(即 0.01 到 1.0)
const ratio = parseFloat(strategyConfig.discountRatio)
if (ratio >= 1 && ratio <= 10) {
if (ratio >= 0.1 && ratio <= 10) {
const finalPrice = basePrice * (ratio / 10)
return '¥' + (Math.round(finalPrice * 100) / 100).toFixed(2)
}
} else if (strategyConfig.mode === 'amount' && strategyConfig.discountAmount) {
// 折扣金额:0 到不能大于基础价格
// 折扣金额:0.01 到不能大于基础价格
const discountAmount = parseFloat(strategyConfig.discountAmount)
if (discountAmount >= 0 && discountAmount <= basePrice) {
if (discountAmount >= 0.01 && discountAmount <= basePrice) {
const finalPrice = basePrice - discountAmount
return '¥' + (Math.round(finalPrice * 100) / 100).toFixed(2)
}
......@@ -154,9 +152,9 @@ export default {
// '5' 组合付款
if (orderType === '5') {
if (strategyConfig.mode === 'deduct' && strategyConfig.deductAmount) {
// 抵扣金额:0 到不能大于基础价格
// 抵扣金额:0.01 到不能大于基础价格
const deductAmount = parseFloat(strategyConfig.deductAmount)
if (deductAmount >= 0 && deductAmount <= basePrice) {
if (deductAmount >= 0.01 && deductAmount <= basePrice) {
const finalPrice = basePrice - deductAmount
return '¥' + (Math.round(finalPrice * 100) / 100).toFixed(2)
}
......@@ -166,12 +164,26 @@ export default {
return '¥' + basePrice.toFixed(2)
},
// 价格变化回调
onPriceChange(item, finalPrice) {
// 可以在这里更新商品的其他价格相关字段
this.$set(item.goods, 'finalPrice', finalPrice)
// 价格变化回调(finalPrice 是单件商品优惠后的价格,需要乘以数量得到总价)
onPriceChange(item, finalPricePerUnit) {
// 计算总价:单件优惠价 * 数量
const number = parseInt(item.goods.number || 1)
const totalFinalPrice = Math.round(finalPricePerUnit * number * 100) / 100
// 存储总价
this.$set(item.goods, 'finalPrice', totalFinalPrice)
// 同时存储单价优惠后的价格,用于数量改变时重新计算
this.$set(item.goods, 'finalPricePerUnit', finalPricePerUnit)
this.$emit('changeTotalPrice')
},
// 重新计算 finalPrice(当数量改变时调用)
recalculateFinalPrice(item) {
// 如果有优惠策略配置
if (item.goods.strategyConfig && item.goods.finalPricePerUnit !== undefined) {
const number = parseInt(item.goods.number || 1)
const totalFinalPrice = Math.round(item.goods.finalPricePerUnit * number * 100) / 100
this.$set(item.goods, 'finalPrice', totalFinalPrice)
}
},
// 初始化策略配置
initStrategyConfig() {
if (!this.list || this.list.length === 0) return
......@@ -281,6 +293,8 @@ export default {
}
// 更新值
this.$set(item.goods, 'number', value)
// 数量改变时,需要重新计算 finalPrice
this.recalculateFinalPrice(item)
// 数量变化时,如果已选择订单类型,触发策略组件重新计算价格
this.$forceUpdate()
this.$emit('changeTotalPrice')
......@@ -295,6 +309,8 @@ export default {
const newValue = currentValue >= 999 ? 999 : currentValue + 1
this.$set(item.goods, 'number', newValue.toString())
}
// 数量改变时,需要重新计算 finalPrice
this.recalculateFinalPrice(item)
this.$emit('changeTotalPrice')
},
// 减少数量
......@@ -308,6 +324,8 @@ export default {
// 确保减少后的值不小于1
const newValue = Math.max(1, currentValue - 1)
this.$set(item.goods, 'number', newValue.toString())
// 数量改变时,需要重新计算 finalPrice
this.recalculateFinalPrice(item)
this.$emit('changeTotalPrice')
},
// 删除商品
......
......@@ -216,6 +216,8 @@ export default {
this.$set(item.multipleSpecificationsGoods, 'number', value)
}
console.log(item, '2')
// 数量改变时,需要重新计算 finalPrice
this.recalculateFinalPrice(item)
this.$emit('changeTotalPrice')
},
// 增加数量
......@@ -241,6 +243,8 @@ export default {
this.$set(item.multipleSpecificationsGoods, 'number', newValue.toString())
}
}
// 数量改变时,需要重新计算 finalPrice
this.recalculateFinalPrice(item)
this.$emit('changeTotalPrice')
},
// 减少数量
......@@ -268,6 +272,8 @@ export default {
const newValue = Math.max(1, currentValue - 1)
this.$set(item.multipleSpecificationsGoods, 'number', newValue.toString())
}
// 数量改变时,需要重新计算 finalPrice
this.recalculateFinalPrice(item)
this.$emit('changeTotalPrice')
},
// 删除商品
......@@ -311,22 +317,18 @@ export default {
this.$set(item.goods, 'strategyConfig', config)
}
},
// 计算基础价格(单价 * 数量)
// 计算基础价格(单价,不乘以数量)
getBasePrice(item) {
let sellPrice = 0
let number = 1
if (item.multipleSpecificationsGoods && item.multipleSpecificationsGoods.goods) {
sellPrice = parseFloat(item.multipleSpecificationsGoods.goods.sellPrice || 0)
number = parseInt(item.multipleSpecificationsGoods.number || 1)
} else if (item.goods) {
sellPrice = parseFloat(item.goods.sellPrice || 0)
number = parseInt(item.goods.number || 1)
}
const basePrice = sellPrice * number
// 四舍五入保留两位小数
return Math.round(basePrice * 100) / 100
return Math.round(sellPrice * 100) / 100
},
// 计算最终显示价格
getDisplayPrice(item) {
......@@ -340,13 +342,13 @@ export default {
if (orderType === '2') {
if (strategyConfig.mode === 'ratio' && strategyConfig.discountRatio) {
const ratio = parseFloat(strategyConfig.discountRatio)
if (ratio >= 1 && ratio <= 10) {
if (ratio >= 0.1 && ratio <= 10) {
const finalPrice = basePrice * (ratio / 10)
return '¥' + (Math.round(finalPrice * 100) / 100).toFixed(2)
}
} else if (strategyConfig.mode === 'amount' && strategyConfig.discountAmount) {
const discountAmount = parseFloat(strategyConfig.discountAmount)
if (discountAmount >= 0 && discountAmount <= basePrice) {
if (discountAmount >= 0.01 && discountAmount <= basePrice) {
const finalPrice = basePrice - discountAmount
return '¥' + (Math.round(finalPrice * 100) / 100).toFixed(2)
}
......@@ -366,7 +368,7 @@ export default {
if (orderType === '5') {
if (strategyConfig.mode === 'deduct' && strategyConfig.deductAmount) {
const deductAmount = parseFloat(strategyConfig.deductAmount)
if (deductAmount >= 0 && deductAmount <= basePrice) {
if (deductAmount >= 0.01 && deductAmount <= basePrice) {
const finalPrice = basePrice - deductAmount
return '¥' + (Math.round(finalPrice * 100) / 100).toFixed(2)
}
......@@ -376,16 +378,40 @@ export default {
return '¥' + basePrice.toFixed(2)
},
// 价格变化回调
onPriceChange(item, finalPrice) {
// 可以在这里更新商品的其他价格相关字段
// 价格变化回调(finalPrice 是单件商品优惠后的价格,需要乘以数量得到总价)
onPriceChange(item, finalPricePerUnit) {
let number = 1
if (item.multipleSpecificationsGoods && item.multipleSpecificationsGoods.goods) {
this.$set(item.multipleSpecificationsGoods.goods, 'finalPrice', finalPrice)
number = parseInt(item.multipleSpecificationsGoods.number || 1)
const totalFinalPrice = Math.round(finalPricePerUnit * number * 100) / 100
this.$set(item.multipleSpecificationsGoods.goods, 'finalPrice', totalFinalPrice)
this.$set(item.multipleSpecificationsGoods.goods, 'finalPricePerUnit', finalPricePerUnit)
} else if (item.goods) {
this.$set(item.goods, 'finalPrice', finalPrice)
number = parseInt(item.goods.number || 1)
const totalFinalPrice = Math.round(finalPricePerUnit * number * 100) / 100
this.$set(item.goods, 'finalPrice', totalFinalPrice)
this.$set(item.goods, 'finalPricePerUnit', finalPricePerUnit)
}
this.$emit('changeTotalPrice')
},
// 重新计算 finalPrice(当数量改变时调用)
recalculateFinalPrice(item) {
let goods, number
if (item.multipleSpecificationsGoods && item.multipleSpecificationsGoods.goods) {
goods = item.multipleSpecificationsGoods.goods
number = parseInt(item.multipleSpecificationsGoods.number || 1)
} else if (item.goods) {
goods = item.goods
number = parseInt(item.goods.number || 1)
}
if (goods && goods.strategyConfig && goods.finalPricePerUnit !== undefined) {
const totalFinalPrice = Math.round(goods.finalPricePerUnit * number * 100) / 100
this.$set(goods, 'finalPrice', totalFinalPrice)
}
},
// 初始化策略配置
initStrategyConfig() {
if (!this.list || this.list.length === 0) return
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!