积分问题修复
This commit is contained in:
@@ -135,22 +135,34 @@ export function returnCouponCanUse(args) {
|
||||
reason: "优惠券未启用"
|
||||
};
|
||||
}
|
||||
|
||||
canDikouGoodsArr = returnCanDikouGoodsArr(canDikouGoodsArr, selCoupon, user,shopInfo);
|
||||
|
||||
// 计算门槛金额
|
||||
let fullAmount = goodsOrderPrice;
|
||||
// 是否抵扣全部商品
|
||||
const isDikouAll = coupon.useFoods.length === 0;
|
||||
let canCalcGoodsArr = [];
|
||||
// 订单里参与门槛计算的商品
|
||||
if (!isDikouAll) {
|
||||
canCalcGoodsArr = canDikouGoodsArr.filter((v) => {
|
||||
return coupon.useFoods.find((food) => food.id == v.productId);
|
||||
canDikouGoodsArr = returnCanDikouGoodsArr(canDikouGoodsArr, selCoupon, user,shopInfo);
|
||||
|
||||
//优惠券指定门槛商品列表
|
||||
let canCalcGoodsArr=[...canDikouGoodsArr]
|
||||
//部分商品参与门槛计算
|
||||
if(coupon.thresholdFoods.length){
|
||||
canCalcGoodsArr = canDikouGoodsArr.filter((v) => {
|
||||
return coupon.thresholdFoods.find((food) => food.id == v.productId);
|
||||
});
|
||||
fullAmount = canCalcGoodsArr.reduce((pre, cur) => {
|
||||
return pre + returnGoodsPrice(cur, user,shopInfo) * cur.num;
|
||||
}, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 是否全部商品可用
|
||||
const isDikouAll = coupon.useFoods.length === 0;
|
||||
// 订单可用商品列表
|
||||
let canUseGoodsArr = [];
|
||||
if (!isDikouAll) {
|
||||
canUseGoodsArr = canDikouGoodsArr.filter((v) => {
|
||||
return coupon.useFoods.find((food) => food.id == v.productId);
|
||||
});
|
||||
}
|
||||
if(user.isVip&&!coupon.vipPriceShare){
|
||||
return {
|
||||
canUse: false,
|
||||
@@ -169,39 +181,40 @@ export function returnCouponCanUse(args) {
|
||||
reason: "当前选中的券不可与其他券同享"
|
||||
};
|
||||
}
|
||||
|
||||
// 没有符合条件的商品
|
||||
if (!isDikouAll && canCalcGoodsArr.length === 0) {
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "没有符合条件的商品"
|
||||
};
|
||||
// 满减券和折扣券计算门槛金额是否满足
|
||||
if([1,3].includes(coupon.type)){
|
||||
if(canCalcGoodsArr.length<=0){
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "没有可参与计算门槛的商品"
|
||||
};
|
||||
}
|
||||
// 不满足门槛金额
|
||||
if (fullAmount < coupon.fullAmount) {
|
||||
return {
|
||||
canUse: false,
|
||||
reason: `满${coupon.fullAmount}元可用,当前可参与金额${fullAmount}元`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 不满足门槛金额
|
||||
if (fullAmount < coupon.fullAmount) {
|
||||
// 商品兑换券,第二件半价和买一送一判断是否有可用商品
|
||||
if([2,4,5].includes(coupon.type)){
|
||||
// 没有符合条件的商品
|
||||
if (!isDikouAll && canCalcGoodsArr.length === 0) {
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "没有符合条件的商品"
|
||||
};
|
||||
}
|
||||
}
|
||||
//商品兑换券是否达到门槛金额
|
||||
if(coupon.type==2&&goodsOrderPrice<coupon.fullAmount){
|
||||
return {
|
||||
canUse: false,
|
||||
reason: `满${coupon.fullAmount}元可用,当前可参与金额${fullAmount}元`
|
||||
};
|
||||
}
|
||||
|
||||
// 商品券特殊验证
|
||||
if (coupon.type === 2) {
|
||||
if (!(isDikouAll || canCalcGoodsArr.length === 0)) {
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "没有符合条件的商品可抵扣"
|
||||
};
|
||||
}
|
||||
if(canDikouGoodsArr.length<=0){
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "没有符合条件的商品可抵扣"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 买一送一券特殊验证
|
||||
if (coupon.type === 6) {
|
||||
let canUse = false;
|
||||
@@ -322,20 +335,27 @@ export function returnCouponZhekouDiscount(
|
||||
maxDiscountAmount
|
||||
} = coupon;
|
||||
|
||||
// 计算商品优惠券折扣总和,使用BigNumber避免精度问题
|
||||
const goodsCouponDiscount = selCoupon
|
||||
.filter((v) => v.type == 2)
|
||||
.reduce((prve, cur) => {
|
||||
return prve + cur.discount.discountPrice;
|
||||
}, 0);
|
||||
goodsOrderPrice -= goodsCouponDiscount;
|
||||
// 使用bignumber处理高精度计算
|
||||
// 1. 计算折扣率(百分比转小数):discountRate / 100
|
||||
const discountRatio = new BigNumber(discountRate).dividedBy(100);
|
||||
// 2. 计算优惠比例:1 - 折扣率(例如:8折的优惠比例是 1 - 0.8 = 0.2)
|
||||
const discountAmountRatio = new BigNumber(1).minus(discountRatio);
|
||||
// 3. 计算折扣金额:商品订单金额 × 优惠比例
|
||||
let discountPrice = new BigNumber(goodsOrderPrice).times(discountAmountRatio).decimalPlaces(2, BigNumber.ROUND_FLOOR).toNumber();
|
||||
if (maxDiscountAmount != 0) {
|
||||
return new BigNumber(prve).plus(new BigNumber(cur.discount.discountPrice));
|
||||
}, new BigNumber(0));
|
||||
|
||||
// 将商品订单价格转换为BigNumber并减去优惠券折扣
|
||||
const adjustedGoodsOrderPrice = new BigNumber(goodsOrderPrice).minus(goodsCouponDiscount);
|
||||
console.log('adjustedGoodsOrderPrice', adjustedGoodsOrderPrice.toNumber());
|
||||
|
||||
// 计算优惠比例:(100 - 折扣率) / 100
|
||||
const discountAmountRatio = new BigNumber(100).minus(discountRate).dividedBy(100);
|
||||
|
||||
// 计算折扣金额:调整后的商品订单金额 × 优惠比例
|
||||
let discountPrice = adjustedGoodsOrderPrice.times(discountAmountRatio)
|
||||
.decimalPlaces(2, BigNumber.ROUND_FLOOR)
|
||||
.toNumber();
|
||||
|
||||
// 应用最大折扣金额限制
|
||||
if (maxDiscountAmount !== 0) {
|
||||
discountPrice = discountPrice >= maxDiscountAmount ? maxDiscountAmount : discountPrice;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user