170 lines
4.2 KiB
JavaScript
170 lines
4.2 KiB
JavaScript
import BigNumber from "bignumber.js";
|
|
|
|
//判断商品是否可以使用限时折扣
|
|
export function canUseLimitTimeDiscount(
|
|
goods,
|
|
limitTimeDiscountRes,
|
|
shopInfo,
|
|
shopUserInfo,
|
|
idKey = "id"
|
|
) {
|
|
shopInfo = shopInfo || {};
|
|
shopUserInfo = shopUserInfo || {};
|
|
if (!limitTimeDiscountRes || !limitTimeDiscountRes.id) {
|
|
return false;
|
|
}
|
|
|
|
const canUseFoods = (limitTimeDiscountRes.foods || "").split(",");
|
|
const goodsCanUse =
|
|
limitTimeDiscountRes.foodType == 1 ||
|
|
canUseFoods.includes("" + goods[idKey]);
|
|
if (!goodsCanUse) {
|
|
return false;
|
|
}
|
|
if (limitTimeDiscountRes.discountPriority == "limit-time") {
|
|
return true;
|
|
}
|
|
if (limitTimeDiscountRes.discountPriority == "vip-price") {
|
|
if (shopUserInfo.isVip != 1 || shopUserInfo.isMemberPrice != 1) {
|
|
return true;
|
|
}
|
|
|
|
if (
|
|
shopUserInfo.isVip == 1 &&
|
|
shopUserInfo.isMemberPrice == 1 &&
|
|
goods.memberPrice * 1 <= 0
|
|
) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 返回商品显示价格
|
|
* @params {*} args 参数对象
|
|
* @params {*} args.goods 商品对象
|
|
* @params {*} args.shopInfo 店铺信息
|
|
* @params {*} args.limitTimeDiscountRes 限时折扣信息
|
|
* @params {*} args.shopUserInfo 店铺用户信息
|
|
* @returns
|
|
*/
|
|
export function returnPrice(args) {
|
|
let {
|
|
goods,
|
|
shopInfo,
|
|
limitTimeDiscountRes,
|
|
shopUserInfo,
|
|
idKey = "product_id",
|
|
} = args;
|
|
const canUseFoods = (limitTimeDiscountRes.foods || "").split(",");
|
|
shopInfo = shopInfo || {};
|
|
shopUserInfo = shopUserInfo || {};
|
|
|
|
if (shopUserInfo.isMemberPrice == 1 && shopUserInfo.isVip == 1) {
|
|
const memberPrice = goods.memberPrice || goods.salePrice;
|
|
|
|
//是会员而且启用会员价
|
|
if (limitTimeDiscountRes) {
|
|
//使用限时折扣
|
|
//限时折扣优先
|
|
if (limitTimeDiscountRes.discountPriority == "limit-time") {
|
|
if (
|
|
limitTimeDiscountRes.foodType == 1 ||
|
|
canUseFoods.includes("" + goods[idKey])
|
|
) {
|
|
return returnLimitPrice({
|
|
price: goods.salePrice,
|
|
limitTimeDiscountRes,
|
|
});
|
|
} else {
|
|
return memberPrice;
|
|
}
|
|
}
|
|
if (limitTimeDiscountRes.discountPriority == "vip-price") {
|
|
if (goods.memberPrice * 1 > 0) {
|
|
//会员优先
|
|
return memberPrice;
|
|
} else {
|
|
const price = returnLimitPrice({
|
|
price: goods.salePrice,
|
|
limitTimeDiscountRes,
|
|
goods: goods,
|
|
});
|
|
|
|
return price;
|
|
}
|
|
}
|
|
} else {
|
|
//是会员没有限时折扣
|
|
return memberPrice;
|
|
}
|
|
} else {
|
|
// console.log('不是会员或者没有启用会员价',goods,limitTimeDiscountRes);
|
|
//不是会员或者没有启用会员价
|
|
if (
|
|
limitTimeDiscountRes &&
|
|
limitTimeDiscountRes.id &&
|
|
(limitTimeDiscountRes.foodType == 1 ||
|
|
canUseFoods.includes("" + goods[idKey]))
|
|
) {
|
|
const price = returnLimitPrice({
|
|
price: goods.salePrice,
|
|
limitTimeDiscountRes,
|
|
goods: goods,
|
|
});
|
|
|
|
return price;
|
|
} else {
|
|
return goods.salePrice;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 返回限时折扣价格
|
|
* @params {*} args 参数对象
|
|
* @params {*} args.limitTimeDiscountRes 限时折扣信息
|
|
* @params {*} args.price 商品价格
|
|
* @param {*} args.goods 商品对象
|
|
* @returns
|
|
*/
|
|
export function returnLimitPrice(args) {
|
|
const { limitTimeDiscountRes, price, goods } = args;
|
|
const discountRate = new BigNumber(
|
|
limitTimeDiscountRes.discountRate
|
|
).dividedBy(100);
|
|
|
|
const result = BigNumber(price)
|
|
.times(discountRate)
|
|
.decimalPlaces(2, BigNumber.ROUND_UP)
|
|
.toNumber();
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 判断是否返回会员价
|
|
* @param {*} args 参数对象
|
|
* @param {*} args.shopInfo 店铺信息
|
|
* @param {*} args.shopUserInfo 店铺用户信息
|
|
* @returns
|
|
*/
|
|
export function canReturnMemberPrice(args) {
|
|
const { shopInfo, shopUserInfo } = args;
|
|
if (shopUserInfo.isMemberPrice == 1 && shopUserInfo.isVip == 1) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 返回会员价格
|
|
* @param {*} goods
|
|
* @returns
|
|
*/
|
|
export function returnMemberPrice(goods) {
|
|
return goods.memberPrice || goods.salePrice;
|
|
}
|