business.helper.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // 获取已选商品列表
  2. export function totalAllCheckedProduct(shopList) {
  3. let productList = []
  4. shopList.forEach(shop => {
  5. productList.push(...shop.productList.filter(product => product.checked))
  6. })
  7. return productList
  8. }
  9. // 计算购物车商品价格
  10. export function computeTotalPrice(productList) {
  11. const { allPrice } = productList.reduce((prevPrice, product) => {
  12. // 已选商品总价
  13. prevPrice.originalPrice += product.price * product.num
  14. // 单品满减
  15. if (product.promotion && product.promotion.type == 1 && product.promotion.mode == 2) {
  16. // 是否满足 满减条件
  17. if (product.price * product.num >= product.promotion.touchPrice) {
  18. prevPrice.reducedPrice += product.promotion.reducedPrice
  19. }
  20. }
  21. prevPrice.allPrice = prevPrice.originalPrice - prevPrice.reducedPrice
  22. return prevPrice
  23. }, {
  24. originalPrice: 0, // 商品总价
  25. reducedPrice: 0, // 优惠金额
  26. allPrice: 0 // 组后后价格
  27. })
  28. return allPrice
  29. }
  30. // 处理优惠券列表: 排序,优惠券点击类型
  31. export function initFormatCouponList(couponList = [], controlType, sort) {
  32. couponList = couponList.map((coupon, index) => {
  33. coupon.controlType = controlType
  34. coupon.checked = false
  35. coupon.uniqueId = index + 1
  36. return coupon
  37. })
  38. if (sort) {
  39. return couponList.sort((a, b) => b.couponAmount - a.couponAmount)
  40. }
  41. return couponList
  42. }
  43. // 将优惠券列表拆分为满足条件和为满足条件列表
  44. export function splitCouponList(couponList = [], productList = []) {
  45. const result = {
  46. canUseCouponList: [],
  47. notUseCouponList: []
  48. }
  49. couponList.forEach(coupon => {
  50. if (
  51. coupon.noThresholdFlag === 1 ||
  52. (coupon.productType === 1 && allProdoctUseCheck(productList, coupon)) ||
  53. (coupon.productType === 2 && someProductUseCheck(productList, coupon))
  54. ) {
  55. result.canUseCouponList.push(coupon)
  56. } else {
  57. result.notUseCouponList.push(coupon)
  58. }
  59. })
  60. return result
  61. }
  62. // 判断全部商品可用 (全部商品价格总计 是否大于 当前优惠券的触发金额)
  63. export function allProdoctUseCheck(productList, coupon) {
  64. const countPrice = productList.reduce((countPrice, product) => countPrice + product.price * product.num, 0)
  65. console.log('all', countPrice)
  66. return countPrice >= coupon.touchPrice
  67. }
  68. // 判断指定商品可用 (当前优惠券可用的商品的价格总计 是否大于 当前优惠券的触发金额)
  69. export function someProductUseCheck(productList, coupon) {
  70. const countPrice = productList.reduce((countPrice, product) => {
  71. // 当前优惠券可用的商品总价
  72. const isIncludes = coupon.productIds.indexOf(product.productId.toString()) > -1
  73. return isIncludes ? countPrice + product.price * product.num : countPrice
  74. }, 0)
  75. console.log('some', countPrice)
  76. return countPrice >= coupon.touchPrice
  77. }
  78. // 优惠券使用提示
  79. export function makeCouponUseTip(currentCoupon, nextCoupon, allPrice) {
  80. // 两者都不存在
  81. if (!currentCoupon && !nextCoupon) return ''
  82. // 只有可使用券时
  83. if (currentCoupon && !nextCoupon) {
  84. if (currentCoupon.noThresholdFlag === 1) {
  85. return `已享“减${currentCoupon.couponAmount}元”优惠券`
  86. }
  87. return `已享“满${currentCoupon.touchPrice}元减${currentCoupon.couponAmount}元”优惠券`
  88. }
  89. // 只有下阶段券时
  90. if (!currentCoupon && nextCoupon) {
  91. const { couponAmount: nextCouponAmount, touchPrice: nextTouchPrice } = nextCoupon
  92. return `还差¥${(nextTouchPrice - allPrice).toFixed(2)}元可用“满${nextTouchPrice}元减${nextCouponAmount}元”优惠券`
  93. }
  94. // 全部同时有时
  95. if (currentCoupon && nextCoupon) {
  96. const { couponAmount: currentCouponAmount, touchPrice: currentTouchPrice } = currentCoupon
  97. const { couponAmount: nextCouponAmount, touchPrice: nextTouchPrice } = nextCoupon
  98. return `还差¥${(nextTouchPrice - allPrice).toFixed(2)}元可用“满${nextTouchPrice}元减${nextCouponAmount}元”优惠券`
  99. }
  100. }