cart-handle.js 4.99 KB

function AllGoodsSelect() {
  let isSelectAll = true;

  this.check = function(list) {
    (list || []).map(item => {
      if (!item.bLackStorage && item.goods_type !== 'gift' && item.selected !== 'Y') {
        isSelectAll = false;
      }
    });
  };

  this.val = function() {
    return isSelectAll;
  };

  return this;
}

function _processPromotion(promotionList, isSub) {
  if (!promotionList || !promotionList.length) {
    return false;
  }

  promotionList.map(promotion => {
    let statuString = '';

    switch (+promotion.status) {
      case 0:
        statuString = '去凑单';
        break;
      case 10:
        if (promotion.promotion_type == 'Gift') { //Cashreduce, Degressdiscount, Cheapestfree, Discount,Gift,Needpaygift,SpecifiedAmount
          statuString = '领赠品';
        } else if (promotion.promotion_type == 'Needpaygift') {
          statuString = '去换购';
        }
        break;
      case 20:
        statuString = '已抢光';
        promotion.soldOut = true;
        break;
      case 30:
        statuString = '更换';
        break;
      default:
        break;
    }

    promotion.statuString = statuString;

    if (isSub) {
      let typeString = '';

      switch (promotion.promotion_type) {
        case 'Gift':
          typeString = '赠品';
          break;
        case 'Needpaygift':
          typeString = '加价购';
          break;
        case 'Cashreduce':
          typeString = '满减';
          break;
        default:
          typeString = '折扣';
          break;
      }

      promotion.typeString = typeString;
    }
  });

  return promotionList;
}


function _processGiftList(list, isGift) {
  let result = {
    isGift,
    title: isGift ? '赠品' : '全场加价购',
    statusString: '已抢光'
  };

  if (!list || !list.length) {
    return false;
  }

  let statusCount10 = 0;//已满足
  let statusCount20 = 0;//已售罄
  let promotionArr = [];

  list.map(item => {
    switch (+item.status) {
      case 10:
        statusCount10++;
        promotionArr.push(item.promotion_id);
        break;
      case 20:
        statusCount20++;
        break;
      default:
        break;
    }
  });

  if (statusCount10 === 0 && statusCount20 !== list.length) {
    return false;
  }

  result.promotion_ids = promotionArr.join(',');

  if (promotionArr.length > 0) {
    result.statusString = isGift ? '领赠品' : '去换购';
    result.showArrow = true;
  }

  return result;
}

/**
 * 购物车数据
 * @param params
 * @returns {*}
 */
const formatOrdinaryCartData = (data) => {
  data = data || {};
  data = data.ordinary_cart_data || {};

  let ordinaryCart = {};
  let isEmptyCart = true;

  // 免邮提示
  if (data.shipping_cost_prompt) {
    ordinaryCart.shippingCostTips = data.shipping_cost_prompt.shipping_cost_tips || '';
  }

  // 降价提示
  if (data.price_down_prompt) {
    ordinaryCart.priceDownTips = data.price_down_prompt.price_down_tips || '';
  }

  const allGoodsSelect = new AllGoodsSelect();

  // 商品池
  let goodsPoolList = data.goods_pool_list;
  if (goodsPoolList && goodsPoolList.length) {
    goodsPoolList.map(item => {
      if (item.pool_batch_no && item.pool_id) {
        //套餐商品参数转换
        item.int_pool_buy_number = parseInt(item.pool_buy_number, 10);
        item.int_pool_storage_number = parseInt(item.pool_storage_number, 10);
      }

      item.isPromotionExpanded = false;
      item.goods_list && allGoodsSelect.check(item.goods_list);
      item.promotion_list = _processPromotion(item.promotion_list);

      if (item.sub_pool && item.sub_pool.length) {
        item.sub_pool.map(spItem =>  {
          spItem.promotion_list = _processPromotion(spItem.promotion_list, true);
          spItem.goods_list && allGoodsSelect.check(spItem.goods_list);
        })
      }
    });

    isEmptyCart = false;
    ordinaryCart.goodsPoolList = goodsPoolList;
  }

  // 商品列表
  let goodsList = data.goods_list;
  if (goodsList && goodsList.length) {
    isEmptyCart = false;
    allGoodsSelect.check(item.goods_list);
    ordinaryCart.goodsList = goodsList;
  }

  // 加价购&赠品
  let gGiftAndPriceGiftList = [];
  let giftItem = _processGiftList(data.g_gift_list, true);
  let priceGiftItem = _processGiftList(data.g_price_gift_list);

  giftItem && gGiftAndPriceGiftList.push(giftItem);
  priceGiftItem && gGiftAndPriceGiftList.push(priceGiftItem);

  if (gGiftAndPriceGiftList.length) {
    ordinaryCart.gGiftAndPriceGiftList = gGiftAndPriceGiftList;
  }

  //失效商品
  ordinaryCart.invalidGoodsList = [...(data.off_shelves_goods_list || []), ...(data.sold_out_goods_list || [])];
  if (ordinaryCart.invalidGoodsList.length) {
    isEmptyCart = false;
  }

  ordinaryCart.isValidGoodsSelectAll = allGoodsSelect.val();
  ordinaryCart.shoppingCartData = data.shopping_cart_data;

  if (data.promotion_info && data.promotion_info.length) {
    ordinaryCart.promotionInfo = data.promotion_info;
  }

console.log(ordinaryCart);
  return {
    isEmptyCart,
    ...ordinaryCart
  };
}

export default {
  formatOrdinaryCartData
};