// 获取已选商品列表 export function totalAllCheckedProduct(shopList) { let productList = [] shopList.forEach(shop => { productList.push(...shop.productList.filter(product => product.checked)) }) return productList } // 计算购物车商品价格 export function computeTotalPrice(productList) { const { allPrice } = productList.reduce((prevPrice, product) => { // 已选商品总价 prevPrice.originalPrice += product.price * product.num // 单品满减 if (product.promotion && product.promotion.type == 1 && product.promotion.mode == 2) { // 是否满足 满减条件 if (product.price * product.num >= product.promotion.touchPrice) { prevPrice.reducedPrice += product.promotion.reducedPrice } } prevPrice.allPrice = prevPrice.originalPrice - prevPrice.reducedPrice return prevPrice }, { originalPrice: 0, // 商品总价 reducedPrice: 0, // 优惠金额 allPrice: 0 // 组后后价格 }) return allPrice } // 处理优惠券列表: 排序,优惠券点击类型 export function initFormatCouponList(couponList = [], controlType, sort) { couponList = couponList.map((coupon, index) => { coupon.controlType = controlType coupon.checked = false coupon.uniqueId = index + 1 return coupon }) if (sort) { return couponList.sort((a, b) => b.couponAmount - a.couponAmount) } return couponList } // 将优惠券列表拆分为满足条件和为满足条件列表 export function splitCouponList(couponList = [], productList = []) { const result = { canUseCouponList: [], notUseCouponList: [] } couponList.forEach(coupon => { if ( coupon.noThresholdFlag === 1 || (coupon.productType === 1 && allProdoctUseCheck(productList, coupon)) || (coupon.productType === 2 && someProductUseCheck(productList, coupon)) ) { result.canUseCouponList.push(coupon) } else { result.notUseCouponList.push(coupon) } }) return result } // 判断全部商品可用 (全部商品价格总计 是否大于 当前优惠券的触发金额) export function allProdoctUseCheck(productList, coupon) { const countPrice = productList.reduce((countPrice, product) => countPrice + product.price * product.num, 0) console.log('all', countPrice) return countPrice >= coupon.touchPrice } // 判断指定商品可用 (当前优惠券可用的商品的价格总计 是否大于 当前优惠券的触发金额) export function someProductUseCheck(productList, coupon) { const countPrice = productList.reduce((countPrice, product) => { // 当前优惠券可用的商品总价 const isIncludes = coupon.productIds.indexOf(product.productId.toString()) > -1 return isIncludes ? countPrice + product.price * product.num : countPrice }, 0) console.log('some', countPrice) return countPrice >= coupon.touchPrice } // 优惠券使用提示 export function makeCouponUseTip(currentCoupon, nextCoupon, allPrice) { // 两者都不存在 if (!currentCoupon && !nextCoupon) return '' // 只有可使用券时 if (currentCoupon && !nextCoupon) { if (currentCoupon.noThresholdFlag === 1) { return `已享“减${currentCoupon.couponAmount}元”优惠券` } return `已享“满${currentCoupon.touchPrice}元减${currentCoupon.couponAmount}元”优惠券` } // 只有下阶段券时 if (!currentCoupon && nextCoupon) { const { couponAmount: nextCouponAmount, touchPrice: nextTouchPrice } = nextCoupon return `还差¥${(nextTouchPrice - allPrice).toFixed(2)}元可用“满${nextTouchPrice}元减${nextCouponAmount}元”优惠券` } // 全部同时有时 if (currentCoupon && nextCoupon) { const { couponAmount: currentCouponAmount, touchPrice: currentTouchPrice } = currentCoupon const { couponAmount: nextCouponAmount, touchPrice: nextTouchPrice } = nextCoupon return `还差¥${(nextTouchPrice - allPrice).toFixed(2)}元可用“满${nextTouchPrice}元减${nextCouponAmount}元”优惠券` } }