问题修复,下单修复
This commit is contained in:
45
utils/coupon.js
Normal file
45
utils/coupon.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import {
|
||||
findCoupon
|
||||
} from '@/common/api/market/coupon.js';
|
||||
import * as UTILS from '@/utils/goods-utils.js'
|
||||
export async function returnHasCouponCanUse(args) {
|
||||
const {
|
||||
goodsOrderPrice,
|
||||
dinnerType,
|
||||
shopId,
|
||||
shopInfo,
|
||||
shopUserId,
|
||||
allGoods,
|
||||
user
|
||||
} = args
|
||||
const res = await findCoupon({
|
||||
shopUserId,
|
||||
});
|
||||
if(!res||res.length<=0){
|
||||
return false
|
||||
}
|
||||
|
||||
const canDikouGoodsArr = UTILS.returnCanDikouGoods(allGoods, [], user);
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
const coupon = res[i]
|
||||
const canuseResult = UTILS.returnCouponCanUse({
|
||||
canDikouGoodsArr,
|
||||
coupon,
|
||||
goodsOrderPrice,
|
||||
user,
|
||||
selCoupon: [],
|
||||
shopInfo
|
||||
})
|
||||
const {
|
||||
canUse,
|
||||
reason
|
||||
} = canuseResult
|
||||
if (canUse) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
|
||||
|
||||
}
|
||||
@@ -12,6 +12,9 @@ import _ from "lodash";
|
||||
* @param {Object} shopInfo
|
||||
*/
|
||||
export function returnGoodsPrice(goods, user, shopInfo) {
|
||||
if(!goods){
|
||||
return 0
|
||||
}
|
||||
if (goods.discount_sale_amount * 1 > 0) {
|
||||
return goods.discount_sale_amount;
|
||||
}
|
||||
@@ -65,22 +68,38 @@ export function returnCoupType(coupon) {
|
||||
* @param user 用户信息
|
||||
*/
|
||||
export function returnCanDikouGoodsArr(canDikouGoodsArr, selCoupon, user) {
|
||||
const goodsCouponGoods = selCoupon
|
||||
.filter((v) => v.type == 2)
|
||||
.reduce((prve, cur) => {
|
||||
prve.push(...cur.discount.hasDiscountGoodsArr);
|
||||
return prve;
|
||||
}, []);
|
||||
const arr = _.cloneDeep(canDikouGoodsArr)
|
||||
.map((v) => {
|
||||
const findCart = goodsCouponGoods.find((carts) => carts.id == v.id);
|
||||
if (findCart) {
|
||||
v.num -= findCart.num;
|
||||
}
|
||||
return v;
|
||||
})
|
||||
.filter((v) => v.num > 0);
|
||||
return arr;
|
||||
const types = [2, 4, 6];
|
||||
// 收集已抵扣商品并关联对应的优惠券类型
|
||||
const goodsCouponGoods = selCoupon
|
||||
.filter((v) => types.includes(v.type))
|
||||
.reduce((prev, cur) => {
|
||||
// 给每个抵扣商品添加所属优惠券类型
|
||||
const goodsWithType = cur.discount.hasDiscountGoodsArr.map(goods => ({
|
||||
...goods,
|
||||
couponType: cur.type // 记录该商品是被哪种类型的优惠券抵扣的
|
||||
}));
|
||||
prev.push(...goodsWithType);
|
||||
return prev;
|
||||
}, []);
|
||||
|
||||
const arr = _.cloneDeep(canDikouGoodsArr)
|
||||
.map((v) => {
|
||||
const findCart = goodsCouponGoods.find((carts) => carts.id == v.id);
|
||||
if (findCart) {
|
||||
// 根据优惠券类型判断扣减数量
|
||||
if ([4, 6].includes(findCart.couponType)) {
|
||||
// 类型4(第二件半价)或6(买一送一),数量减2
|
||||
v.num -= 2;
|
||||
} else {
|
||||
// 其他类型(如类型2商品券),按原逻辑扣减对应数量
|
||||
v.num -= findCart.num;
|
||||
}
|
||||
}
|
||||
return v;
|
||||
})
|
||||
.filter((v) => v.num > 0); // 过滤掉数量<=0的商品
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -151,12 +170,18 @@ export function returnCouponCanUse(args) {
|
||||
|
||||
// 商品券特殊验证
|
||||
if (coupon.type === 2) {
|
||||
if (!(isDikouAll || canCalcGoodsArr.length > 0)) {
|
||||
if (!(isDikouAll || canCalcGoodsArr.length === 0)) {
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "没有符合条件的商品可抵扣"
|
||||
};
|
||||
}
|
||||
if(canDikouGoodsArr.length<=0){
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "没有符合条件的商品可抵扣"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 买一送一券特殊验证
|
||||
@@ -241,7 +266,6 @@ export function calcDiscountGoodsArrPrice(discountGoodsArr, discountNum, user,sh
|
||||
* @param selCoupon 已选择的优惠券列表
|
||||
*/
|
||||
export function returnCouponDiscount(arr, coupon, user, goodsOrderPrice, selCoupon) {
|
||||
console.log('arr', arr);
|
||||
const canDikouGoodsArr = returnCanDikouGoodsArr(arr, selCoupon, user);
|
||||
console.log('canDikouGoodsArr', canDikouGoodsArr);
|
||||
if (coupon.type == 2) {
|
||||
@@ -291,7 +315,7 @@ export function returnCouponZhekouDiscount(
|
||||
// 2. 计算优惠比例:1 - 折扣率(例如:8折的优惠比例是 1 - 0.8 = 0.2)
|
||||
const discountAmountRatio = new BigNumber(1).minus(discountRatio);
|
||||
// 3. 计算折扣金额:商品订单金额 × 优惠比例
|
||||
let discountPrice = new BigNumber(goodsOrderPrice).times(discountAmountRatio).toNumber();
|
||||
let discountPrice = new BigNumber(goodsOrderPrice).times(discountAmountRatio).decimalPlaces(2, BigNumber.ROUND_FLOOR).toNumber();
|
||||
if (maxDiscountAmount != 0) {
|
||||
discountPrice = discountPrice >= maxDiscountAmount ? maxDiscountAmount : discountPrice;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user