会员修复
This commit is contained in:
@@ -12,13 +12,13 @@ import _ from "lodash";
|
||||
* @param {Object} shopInfo
|
||||
*/
|
||||
export function returnGoodsPrice(goods, user, shopInfo) {
|
||||
if(!goods){
|
||||
if (!goods) {
|
||||
return 0
|
||||
}
|
||||
if (goods.discount_sale_amount * 1 > 0) {
|
||||
return goods.discount_sale_amount;
|
||||
}
|
||||
if(shopInfo&&!shopInfo.isMemberPrice){
|
||||
if (shopInfo && !shopInfo.isMemberPrice) {
|
||||
return goods.salePrice;
|
||||
}
|
||||
if (user.isVip && goods.memberPrice * 1 <= goods.salePrice * 1 && goods.memberPrice * 1 > 0) {
|
||||
@@ -68,38 +68,37 @@ export function returnCoupType(coupon) {
|
||||
* @param user 用户信息
|
||||
*/
|
||||
export function returnCanDikouGoodsArr(canDikouGoodsArr, selCoupon, user) {
|
||||
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 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的商品
|
||||
|
||||
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;
|
||||
return arr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,25 +134,24 @@ export function returnCouponCanUse(args) {
|
||||
reason: "优惠券未启用"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// 计算门槛金额
|
||||
let fullAmount = goodsOrderPrice;
|
||||
canDikouGoodsArr = returnCanDikouGoodsArr(canDikouGoodsArr, selCoupon, user,shopInfo);
|
||||
|
||||
canDikouGoodsArr = returnCanDikouGoodsArr(canDikouGoodsArr, selCoupon, user, shopInfo);
|
||||
//优惠券指定门槛商品列表
|
||||
let canCalcGoodsArr=[...canDikouGoodsArr]
|
||||
let canCalcGoodsArr = [...canDikouGoodsArr]
|
||||
//部分商品参与门槛计算
|
||||
if(coupon.thresholdFoods.length){
|
||||
canCalcGoodsArr = canDikouGoodsArr.filter((v) => {
|
||||
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;
|
||||
return pre + returnGoodsPrice(cur, user, shopInfo) * cur.num;
|
||||
}, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 是否全部商品可用
|
||||
const isDikouAll = coupon.useFoods.length === 0;
|
||||
// 订单可用商品列表
|
||||
@@ -163,27 +161,27 @@ export function returnCouponCanUse(args) {
|
||||
return coupon.useFoods.find((food) => food.id == v.productId);
|
||||
});
|
||||
}
|
||||
if(user.isVip&&!coupon.vipPriceShare){
|
||||
if (user.isVip && !coupon.vipPriceShare) {
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "非会员可用"
|
||||
};
|
||||
}
|
||||
if(selCoupon.length>0&&!selCoupon[0].otherCouponShare){
|
||||
return {
|
||||
if (selCoupon.length > 0 && !selCoupon[0].otherCouponShare) {
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "当前选中的券不可与其他券同享"
|
||||
};
|
||||
}
|
||||
if(selCoupon.length>0&&!coupon.otherCouponShare){
|
||||
return {
|
||||
if (selCoupon.length > 0 && !coupon.otherCouponShare) {
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "当前选中的券不可与其他券同享"
|
||||
};
|
||||
}
|
||||
// 满减券和折扣券计算门槛金额是否满足
|
||||
if([1,3].includes(coupon.type)){
|
||||
if(canCalcGoodsArr.length<=0){
|
||||
if ([1, 3].includes(coupon.type)) {
|
||||
if (canCalcGoodsArr.length <= 0) {
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "没有可参与计算门槛的商品"
|
||||
@@ -198,9 +196,15 @@ export function returnCouponCanUse(args) {
|
||||
}
|
||||
}
|
||||
// 商品兑换券,第二件半价和买一送一判断是否有可用商品
|
||||
if([2,4,5].includes(coupon.type)){
|
||||
if ([2, 4, 5].includes(coupon.type)) {
|
||||
// 没有符合条件的商品
|
||||
if (!isDikouAll && canCalcGoodsArr.length === 0) {
|
||||
if (isDikouAll && canDikouGoodsArr.length === 0) {
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "没有符合条件的商品"
|
||||
};
|
||||
}
|
||||
if (!isDikouAll && canUseGoodsArr.length === 0) {
|
||||
return {
|
||||
canUse: false,
|
||||
reason: "没有符合条件的商品"
|
||||
@@ -208,7 +212,7 @@ export function returnCouponCanUse(args) {
|
||||
}
|
||||
}
|
||||
//商品兑换券是否达到门槛金额
|
||||
if(coupon.type==2&&goodsOrderPrice<coupon.fullAmount){
|
||||
if (coupon.type == 2 && goodsOrderPrice < coupon.fullAmount) {
|
||||
return {
|
||||
canUse: false,
|
||||
reason: `满${coupon.fullAmount}元可用,当前可参与金额${fullAmount}元`
|
||||
@@ -264,7 +268,7 @@ export function returnCouponCanUse(args) {
|
||||
* @param user 用户信息
|
||||
* @param {Object} shopInfo 店铺信息
|
||||
*/
|
||||
export function calcDiscountGoodsArrPrice(discountGoodsArr, discountNum, user,shopInfo) {
|
||||
export function calcDiscountGoodsArrPrice(discountGoodsArr, discountNum, user, shopInfo) {
|
||||
let hasCountNum = 0;
|
||||
let discountPrice = 0;
|
||||
let hasDiscountGoodsArr = [];
|
||||
@@ -275,7 +279,7 @@ export function calcDiscountGoodsArrPrice(discountGoodsArr, discountNum, user,sh
|
||||
const goods = discountGoodsArr[i];
|
||||
const shengyuNum = discountNum - hasCountNum;
|
||||
const num = Math.min(goods.num, shengyuNum);
|
||||
discountPrice += returnGoodsPrice(goods, user,shopInfo) * num;
|
||||
discountPrice += returnGoodsPrice(goods, user, shopInfo) * num;
|
||||
hasCountNum += num;
|
||||
hasDiscountGoodsArr.push({
|
||||
...goods,
|
||||
@@ -297,14 +301,14 @@ export function calcDiscountGoodsArrPrice(discountGoodsArr, discountNum, user,sh
|
||||
* @param selCoupon 已选择的优惠券列表
|
||||
* @param shopInfo 店铺信息
|
||||
*/
|
||||
export function returnCouponDiscount(arr, coupon, user, goodsOrderPrice, selCoupon,shopInfo) {
|
||||
export function returnCouponDiscount(arr, coupon, user, goodsOrderPrice, selCoupon, shopInfo) {
|
||||
const canDikouGoodsArr = returnCanDikouGoodsArr(arr, selCoupon, user);
|
||||
console.log('canDikouGoodsArr', canDikouGoodsArr);
|
||||
if (coupon.type == 2) {
|
||||
return returnCouponProductDiscount(canDikouGoodsArr, coupon, user, shopInfo);
|
||||
}
|
||||
if (coupon.type == 6) {
|
||||
return returnCouponBuyOneGiveOneDiscount(canDikouGoodsArr, coupon, user, shopInfo);
|
||||
const result = returnCouponBuyOneGiveOneDiscount(canDikouGoodsArr, coupon, user, shopInfo);
|
||||
return result
|
||||
}
|
||||
if (coupon.type == 4) {
|
||||
return returnSecoendDiscount(canDikouGoodsArr, coupon, user, shopInfo);
|
||||
@@ -341,19 +345,19 @@ export function returnCouponZhekouDiscount(
|
||||
.reduce((prve, cur) => {
|
||||
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;
|
||||
@@ -372,7 +376,7 @@ export function returnCouponZhekouDiscount(
|
||||
* @param user 用户信息
|
||||
* @param shopInfo 店铺信息
|
||||
*/
|
||||
export function returnCouponProductDiscount(canDikouGoodsArr, coupon, user,shopInfo) {
|
||||
export function returnCouponProductDiscount(canDikouGoodsArr, coupon, user, shopInfo) {
|
||||
const {
|
||||
useFoods,
|
||||
discountNum,
|
||||
@@ -402,7 +406,7 @@ export function returnCouponProductDiscount(canDikouGoodsArr, coupon, user,shopI
|
||||
discountGoodsArr = discountSelGoodsArr.slice(0, discountNum);
|
||||
}
|
||||
}
|
||||
const result = calcDiscountGoodsArrPrice(discountGoodsArr, discountNum, user,shopInfo);
|
||||
const result = calcDiscountGoodsArrPrice(discountGoodsArr, discountNum, user, shopInfo);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -413,7 +417,7 @@ export function returnCouponProductDiscount(canDikouGoodsArr, coupon, user,shopI
|
||||
* @param user 用户信息
|
||||
* @param shopInfo 店铺信息
|
||||
*/
|
||||
function returnCouponBuyOneGiveOneDiscount(canDikouGoodsArr, coupon, user,shopInfo) {
|
||||
function returnCouponBuyOneGiveOneDiscount(canDikouGoodsArr, coupon, user, shopInfo) {
|
||||
const {
|
||||
useFoods,
|
||||
useRule
|
||||
@@ -425,23 +429,28 @@ function returnCouponBuyOneGiveOneDiscount(canDikouGoodsArr, coupon, user,shopIn
|
||||
//抵扣全部商品
|
||||
if (useFoods.length === 0) {
|
||||
if (useRule == "price_asc") {
|
||||
discountGoods = canUseGoods[canUseGoods.length - 1];
|
||||
discountGoods = canUseGoods[canUseGoods.length - 1]
|
||||
} else {
|
||||
discountGoods = canUseGoods.slice(0, 1);
|
||||
discountGoods = canUseGoods[0];
|
||||
}
|
||||
} else {
|
||||
//符合抵扣条件的商品
|
||||
const canUseGoods1 = canUseGoods.filter((v) => useFoods.find((food) => food.id == v.productId));
|
||||
if (useRule == "price_asc") {
|
||||
discountGoods = canUseGoods1[canUseGoods1.length - 1];
|
||||
discountGoods = canUseGoods1[canUseGoods1.length - 1]
|
||||
} else {
|
||||
discountGoods = canUseGoods1.slice(0, 1);
|
||||
discountGoods = canUseGoods1[0]
|
||||
}
|
||||
}
|
||||
const discountPrice = returnGoodsPrice(discountGoods, user,shopInfo);
|
||||
const hasDiscountGoodsArr = [discountGoods];
|
||||
let discountPrice = 0;
|
||||
let hasDiscountGoodsArr = []
|
||||
console.log('returnCouponBuyOneGiveOneDiscount:discountGoods',discountGoods);
|
||||
if (discountGoods) {
|
||||
discountPrice = returnGoodsPrice(discountGoods, user, shopInfo);
|
||||
hasDiscountGoodsArr = [discountGoods];
|
||||
}
|
||||
return {
|
||||
discountPrice,
|
||||
discountPrice:discountPrice<=0?0:discountPrice,
|
||||
hasDiscountGoodsArr
|
||||
};
|
||||
}
|
||||
@@ -453,38 +462,40 @@ function returnCouponBuyOneGiveOneDiscount(canDikouGoodsArr, coupon, user,shopIn
|
||||
* @param user 用户信息
|
||||
* @param shopInfo 店铺信息
|
||||
*/
|
||||
function returnSecoendDiscount(canDikouGoodsArr, coupon, user,shopInfo) {
|
||||
function returnSecoendDiscount(canDikouGoodsArr, coupon, user, shopInfo) {
|
||||
const {
|
||||
useFoods,
|
||||
useRule
|
||||
} = coupon;
|
||||
//抵扣商品
|
||||
let discountGoods = undefined;
|
||||
//符合买一送一条件的商品
|
||||
//符合条件的商品
|
||||
const canUseGoods = canDikouGoodsArr.filter((v) => v.num >= 2);
|
||||
//抵扣全部商品
|
||||
if (useFoods.length === 0) {
|
||||
if (useRule == "price_asc") {
|
||||
discountGoods = canUseGoods[canUseGoods.length - 1];
|
||||
discountGoods = canUseGoods[canUseGoods.length - 1]
|
||||
} else {
|
||||
discountGoods = canUseGoods.slice(0, 1);
|
||||
discountGoods = canUseGoods[0]
|
||||
}
|
||||
} else {
|
||||
//符合抵扣条件的商品
|
||||
const canUseGoods1 = canUseGoods.filter((v) => useFoods.find((food) => food.id == v.productId));
|
||||
if (useRule == "price_asc") {
|
||||
discountGoods = canUseGoods1[canUseGoods1.length - 1];
|
||||
discountGoods = canUseGoods1[canUseGoods1.length - 1]
|
||||
} else {
|
||||
discountGoods = canUseGoods1.slice(0, 1);
|
||||
discountGoods = canUseGoods1[0]
|
||||
}
|
||||
}
|
||||
console.log('returnSecoendDiscount:discountGoods',discountGoods);
|
||||
const discountPrice = returnGoodsPrice(discountGoods[0], user,shopInfo);
|
||||
console.log('returnSecoendDiscount:discountPrice',discountPrice);
|
||||
const hasDiscountGoodsArr = [discountGoods];
|
||||
let discountPrice = 0;
|
||||
let hasDiscountGoodsArr = []
|
||||
if (discountGoods) {
|
||||
discountPrice = returnGoodsPrice(discountGoods, user, shopInfo);
|
||||
hasDiscountGoodsArr = [discountGoods];
|
||||
}
|
||||
//返回半价价格
|
||||
return {
|
||||
discountPrice: new BigNumber(discountPrice).dividedBy(2).toNumber(),
|
||||
discountPrice: discountPrice <= 0 ? 0 : new BigNumber(discountPrice).dividedBy(2).toNumber(),
|
||||
hasDiscountGoodsArr,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user