couponUtils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * 取出优惠券列表中的最优优惠券和次优优惠券
  3. * */
  4. class CouponUtils {
  5. constructor(couponList, productList) {
  6. this.couponList = this.couponSort(this.getCanBeUseCouponList(couponList, productList))
  7. this.productList = productList
  8. this.bestCoupon = null
  9. this.secondCoupon = null
  10. this.currentIndex = this.couponList.length
  11. this.fetchBestCoupon()
  12. }
  13. // 获取最高可用金额的优惠券
  14. fetchBestCoupon() {
  15. if (this.couponList.length === 0 || this.productList.length === 0) {
  16. this.bestCoupon = null
  17. return
  18. }
  19. this.bestCoupon = this.couponList.find(coupon => this.couponCanBeUse(coupon, this.productList)) || null
  20. if (this.bestCoupon) {
  21. this.currentIndex = this.couponList.findIndex(coupon => coupon.uniqueId === this.bestCoupon.uniqueId)
  22. }
  23. this.fetchSecondCoupon()
  24. }
  25. // 获取下一阶段可用优惠券
  26. fetchSecondCoupon() {
  27. if (this.couponList.length === 0 || this.productList.length === 0) {
  28. this.secondCoupon = null
  29. return
  30. }
  31. this.secondCoupon = this.currentIndex > 0 ? this.couponList[this.currentIndex - 1] : null
  32. }
  33. // 勾选商品满足优惠券使用条件 && 满足使用条件
  34. couponCanBeUse(coupon, productList) {
  35. return (
  36. this.isNoThreshold(coupon) ||
  37. this.isSomeProductUse(coupon, productList) ||
  38. this.isAllProductUse(coupon, productList)
  39. )
  40. }
  41. // 获取当前选中商品都能使用的优惠券
  42. getCanBeUseCouponList(couponList, productList) {
  43. return couponList.filter(coupon => coupon.productType === 1 || this.isCanUseByAllProduct(coupon,
  44. productList))
  45. }
  46. // 全部商品可用
  47. isCanUseByAllProduct(coupon, productList) {
  48. return productList.every(product => coupon.productIds.indexOf(product.productId.toString()) > -1)
  49. }
  50. // 优惠券是否无门槛
  51. isNoThreshold(coupon) {
  52. return coupon.noThresholdFlag === 1
  53. }
  54. // 全部商品可用 && 满足使用条件
  55. isAllProductUse(coupon, productList) {
  56. if (coupon.productType !== 1) {
  57. return false
  58. }
  59. const countPrice = productList.reduce((countPrice, product) => {
  60. return countPrice + this.totalProductPrice(product)
  61. }, 0)
  62. return countPrice >= coupon.touchPrice
  63. }
  64. // 部分商品可用 && 满足使用条件
  65. isSomeProductUse(coupon, productList) {
  66. if (coupon.productType !== 2) {
  67. return false
  68. }
  69. const countPrice = productList.reduce((countPrice, product) => {
  70. const isIncludes = coupon.productIds.indexOf(product.productId.toString()) > -1
  71. return isIncludes ? countPrice + this.totalProductPrice(product) : countPrice
  72. }, 0)
  73. return countPrice >= coupon.touchPrice
  74. }
  75. // 统计商品价格
  76. totalProductPrice(product) {
  77. return product.price * product.num
  78. }
  79. // 排序
  80. couponSort(couponList) {
  81. return couponList.sort((a, b) => b.couponAmount - a.couponAmount)
  82. }
  83. }
  84. export default CouponUtils