export function isTui(item) { return item.status == "return" || item.status == "refund" || item.status == "refunding"; } //计算打包费 export function returnPackFee(arr, isOld = true) { if (isOld) { return arr.reduce((a, b) => { const bTotal = b.info .filter((v) => v.isGift !== "true" && v.status !== "return") .reduce((prve, cur) => { return prve + (cur.packFee || cur.packAmount || 0); }, 0); return a + bTotal; }, 0); } else { return arr .filter((v) => v.status !== "return" && v.isGift !== "true") .reduce((a, b) => { return a + (b.packFee || b.packAmount || 0); }, 0); } } //判断商品是否可以下单 export function isCanBuy(skuGoods, goods) { if (goods.typeEnum == "normal") { //单规格 return ( goods.isGrounding && goods.isPauseSale == 0 && (goods.isStock ? goods.stockNumber > 0 : true) ); } else { //多规格 return ( goods.isGrounding && goods.isPauseSale == 0 && skuGoods.isGrounding && skuGoods.isPauseSale == 0 && (goods.isStock ? goods.stockNumber > 0 : true) ); } } //字符匹配 export function $strMatch(matchStr, str) { return matchStr.toLowerCase().includes(str.toLowerCase()); } // 一个数组是否包含另外一个数组全部元素 export function arrayContainsAll(arr1, arr2) { for (let i = 0; i < arr2.length; i++) { if (!arr1.includes(arr2[i])) { return false; } } return true; } //n项 n-1项组合,生成全部结果 export function generateCombinations(arr, k) { console.log(arr); console.log(k); let result = []; function helper(index, current) { if (current.length === k) { result.push(current.slice()); // 使用slice()来避免直接修改原始数组 } else { for (let i = index; i < arr.length; i++) { current.push(arr[i]); // 将当前元素添加到组合中 helper(i + 1, current); // 递归调用,索引增加以避免重复选择相同的元素 current.pop(); // 回溯,移除当前元素以便尝试其他组合 } } } helper(0, []); // 从索引0开始,初始空数组作为起点 return result; } export function returnReverseVal(val, isReturnString = true) { const isBol = typeof val === "boolean"; const isString = typeof val === "string"; let reverseNewval = ""; if (isBol) { reverseNewval = !val; } if (isString) { reverseNewval = val === "true" ? "false" : "true"; } return reverseNewval; } export function returnGiftArr(arr) { let result = []; for (let i = 0; i < arr.length; i++) { const info = arr[i].info; for (let j = 0; j < info.length; j++) { if (info[j].isGift === "true") { result.push(info[j]); } } } return result; } export function formatOrderGoodsList(arr) { const goodsMap = {}; for (let i in arr) { const goods = arr[i]; if (goods.productName != "客座费") { if (goodsMap.hasOwnProperty(goods.placeNum)) { goodsMap[goods.placeNum || 1].push(goods); } else { goodsMap[goods.placeNum || 1] = [goods]; } } } return Object.entries(goodsMap).map(([key, value]) => ({ info: value, placeNum: key || 1, })); } export function returnIsSeatFee(item) { if (!item) { return false; } return item.productId == "-999" ? true : false; } /** * 计算购物车会员优惠价格 */ export function returnVipDiscountPrice() {} //计算商品券优惠价格 export function returnProductCouponPrice(coup, goodsArr, vipUser) { const item = goodsArr.find((v) => v.productId == coup.proId); if (!item) { return 0; } const memberPrice = item.memberPrice ? item.memberPrice : item.price; const price = item ? (vipUser.isVip ? memberPrice : item.price) : 0; return price * coup.num; } //返回新的商品列表,过滤掉退菜的,退单的商品 export function returnNewGoodsList(arr) { let goodsMap = {}; return arr.filter((v) => !isTui(v)); } //根据当前购物车商品以及数量,已选券对应商品数量,判断该商品券是否可用 export function returnCoupCanUse(goodsArr = [], coup, selCoupArr = []) { if (!coup.use) { return false; } const findGoods = goodsArr.filter((v) => v.productId == coup.proId); if (!findGoods.length) { return false; } const findGoodsTotalNumber = findGoods.reduce((prve, cur) => { return prve + cur.num * 1; }, 0); const selCoupNumber = selCoupArr .filter((v) => v.proId == coup.proId) .reduce((prve, cur) => { return prve + cur.num * 1; }, 0); if (selCoupNumber >= findGoodsTotalNumber) { return false; } return findGoodsTotalNumber < coup.num + selCoupNumber ? false : true; } //查找购物车商品根据购物车商品数据返回商品券信息(抵扣价格以及是否满足可用需求) export function returnProductCoupon(coup, goodsArr, vipUser, selCoupArr = []) { const newGoodsArr = returnNewGoodsList(goodsArr); const item = newGoodsArr.find((v) => v.productId == coup.proId); if (!item) { return { ...coup, discountAmount: 0, use: false }; } const memberPrice = item.memberPrice ? item.memberPrice : item.price; const price = item ? (vipUser.isVip ? memberPrice : item.price) : 0; const discountAmount = (price * coup.num).toFixed(2); console.log(discountAmount); const canUse = !coup.use ? false : discountAmount > 0 && returnCoupCanUse(goodsArr, coup, selCoupArr); // const canUse=discountAmount>0 return { ...coup, discountAmount: discountAmount, use: canUse }; } /** * 根据购物车商品计算商品券抵扣价格以及是否满足可用需求 * 1.商品券对应商品数量大于购物车对应商品数量不可用 * 2.未在购物车找到相关商品不可用 * @param {*} coupArr * @param {*} goodsArr * @param {*} vipUser * @returns */ export function returnProductAllCoup(coupArr, goodsArr, vipUser) { return coupArr.map((v) => { return returnProductCoupon(v, goodsArr, vipUser); }); } //计算商品券总优惠价格 export function returnProductCouponAllPrice(coupArr, goodsArr, vipUser) { if (coupArr.length == 0) { return 0; } return coupArr .reduce((a, b) => { const price = returnProductCouponPrice(b, goodsArr, vipUser); return a + price; }, 0) .toFixed(2); } //计算满减券总优惠价格 export function returnFullReductionCouponAllPrice(coupArr) { if (coupArr.length == 0) { return 0; } return coupArr .filter((v) => v.type == 1) .reduce((a, b) => { const price = b.discountAmount; return a + price; }, 0) .toFixed(2); } //计算优惠券总价格 export function returnCouponAllPrice(coupArr, goodsArr, vipUser) { const poductAllprice = returnProductCouponAllPrice(coupArr, goodsArr, vipUser); const pointAllPrice = returnFullReductionCouponAllPrice(coupArr); return (poductAllprice * 1 + pointAllPrice * 1).toFixed(2); } //返回购物车商品价格 export function returnCartPrice(goods, vipUser) { const price = goods.price || goods.salePrice; if (!vipUser || !vipUser.id || !vipUser.isVip) { return price; } const memberPrice = goods.memberPrice ? goods.memberPrice : price; return memberPrice; } // 价格保留两位小数不四舍五入 export function customTruncateToTwoDecimals(number) { let stringNumber = number.toString(); let dotIndex = stringNumber.indexOf("."); if (dotIndex === -1) { return number; // 如果没有小数点,直接返回原数 } else { let integerPart = stringNumber.substring(0, dotIndex); // 整数部分 let decimalPart = stringNumber.substring(dotIndex + 1, dotIndex + 3); // 小数部分,取两位 return parseFloat(integerPart + "." + decimalPart); // 重新组合并返回 } }