couponUtil.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { isIntersect } from '@/common/util.js'
  2. // 优惠商品统计价格
  3. export let countPrice = 0
  4. // 获取选中商品
  5. export function fetchSelectedProducts(goodsList) {
  6. const productList = []
  7. goodsList.forEach(shop => {
  8. shop.productList.forEach(prod => {
  9. if (prod.productsChecked) {
  10. productList.push(prod)
  11. }
  12. })
  13. })
  14. console.log(productList)
  15. return productList
  16. }
  17. // 根据选中商品筛选可用优惠券并排序
  18. export function findCouponBySelected(ids, couponList) {
  19. return couponList.filter(coupon => coupon.productType === 1 || isIntersect(ids, coupon.productIds.split(',')))
  20. .sort((a, b) => b.couponAmount - a.couponAmount)
  21. }
  22. // 判断可用优惠券是否满足使用条件
  23. export function canUseCoupon(current, productIds, canUseCouponList, selectedPoducts) {
  24. if (productIds.length <= 0) return -1
  25. const len = canUseCouponList.length
  26. if (current >= len) return len
  27. const currentCoupon = canUseCouponList[current]
  28. // 无门槛
  29. if (currentCoupon.noThresholdFlag === 1) return current
  30. // 是否满足条件
  31. if (currentCoupon.noThresholdFlag === 0) {
  32. countPrice = selectedPoducts.reduce((countPrice, prod) => {
  33. // 全部商品可用
  34. if (currentCoupon.productType === 1) {
  35. countPrice += prod.price * prod.productCount
  36. } else {
  37. // 部分商品可用
  38. const couponProductIds = currentCoupon.productIds.split(',')
  39. if (couponProductIds.includes(prod.productId.toString())) {
  40. countPrice += prod.price * prod.productCount
  41. }
  42. }
  43. return countPrice
  44. }, 0)
  45. if (countPrice >= currentCoupon.touchPrice) return current
  46. }
  47. return canUseCoupon(++current, productIds, canUseCouponList, selectedPoducts)
  48. }
  49. export function getCountPrice(){
  50. return countPrice
  51. }