12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import { isIntersect } from '@/common/util.js'
- // 优惠商品统计价格
- export let countPrice = 0
- // 获取选中商品
- export function fetchSelectedProducts(goodsList) {
- const productList = []
- goodsList.forEach(shop => {
- shop.productList.forEach(prod => {
- if (prod.productsChecked) {
- productList.push(prod)
- }
- })
- })
- console.log(productList)
- return productList
- }
- // 根据选中商品筛选可用优惠券并排序
- export function findCouponBySelected(ids, couponList) {
- return couponList.filter(coupon => coupon.productType === 1 || isIntersect(ids, coupon.productIds.split(',')))
- .sort((a, b) => b.couponAmount - a.couponAmount)
- }
- // 判断可用优惠券是否满足使用条件
- export function canUseCoupon(current, productIds, canUseCouponList, selectedPoducts) {
- if (productIds.length <= 0) return -1
- const len = canUseCouponList.length
- if (current >= len) return len
- const currentCoupon = canUseCouponList[current]
- // 无门槛
- if (currentCoupon.noThresholdFlag === 1) return current
- // 是否满足条件
- if (currentCoupon.noThresholdFlag === 0) {
- countPrice = selectedPoducts.reduce((countPrice, prod) => {
- // 全部商品可用
- if (currentCoupon.productType === 1) {
- countPrice += prod.price * prod.productCount
- } else {
- // 部分商品可用
- const couponProductIds = currentCoupon.productIds.split(',')
- if (couponProductIds.includes(prod.productId.toString())) {
- countPrice += prod.price * prod.productCount
- }
- }
- return countPrice
- }, 0)
- if (countPrice >= currentCoupon.touchPrice) return current
- }
- return canUseCoupon(++current, productIds, canUseCouponList, selectedPoducts)
- }
- export function getCountPrice(){
- return countPrice
- }
|