cart-handle.js 6.24 KB

function AllGoodsSelect() {
  let index = 0;
  let isSelectAll = true;
  let lowStorage = false;
  let goodsList = [];

  this.check = function(list) {
    (list || []).map(item => {
      goodsList.push(item);

      // 购物车商品唯一key
      item.indexKey = `${item.product_sku}_${index++}`;

      if (item.buy_number - item.storage_number > 0) {
        lowStorage = true;
      } else if (item.goods_type !== 'gift' && item.selected !== 'Y') {
        isSelectAll = false;
      }
    });
  };

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

  this.hasLowStorage = function() {
    return lowStorage;
  };

  this.getAllGoodsList = function() {
    return goodsList;
  };

  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:
        // Cashreduce, Degressdiscount, Cheapestfree, Discount,Gift,Needpaygift,SpecifiedAmount
        if (promotion.promotion_type === 'Gift') {
          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, needAllGoods) => {
  data = data || {};
  data = data.ordinary_cart_data || {};

  let ordinaryCart = {
    shippingCostTips: '',
    priceDownTips: ''
  };
  let isEmptyCart = true;
  let pid = 10;

  // 免邮提示
  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);
      item.promotionMapId = pid++;

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

    isEmptyCart = false;
  }
  ordinaryCart.goodsPoolList = goodsPoolList;

  // 商品列表
  let goodsList = data.goods_list;

  if (goodsList && goodsList.length) {
    isEmptyCart = false;
    allGoodsSelect.check(goodsList);
  }
  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);
  ordinaryCart.gGiftAndPriceGiftList = gGiftAndPriceGiftList;

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

  ordinaryCart.shoppingCartData = data.shopping_cart_data;
  ordinaryCart.matchGiftIds = data.match_gift_ids;
  ordinaryCart.isSelectAll = allGoodsSelect.isSelectAll();
  ordinaryCart.hasLowStorage = allGoodsSelect.hasLowStorage();
  ordinaryCart.promotionMoreStatus = {};

  needAllGoods && (ordinaryCart.allGoodsList = allGoodsSelect.getAllGoodsList());

  ordinaryCart.promotionInfo = data.promotion_info || [];

  return {
    isEmptyCart,
    ...ordinaryCart
  };
};

const changeGoodsListData = (list, selected) => {
  let glist = [];

  if (['Y', 'N'].indexOf(selected) < 0) {
    selected = false;
  }

  (list || []).map(item => {
    if (!item.lowStorage && item.goods_type !== 'gift') {
      glist.push({
        buy_number: item.buy_number || 0,
        goods_type: item.goods_type || '',
        product_sku: item.product_sku || '',
        promotion_id: item.promotion_id || 0,
        selected: selected || item.selected,
        activity_id: item.bundle_activity_id || 0,
        batch_no: item.batch_no || 0,
      });
    }
  });

  return glist;
};

export default {
  formatOrdinaryCartData,
  changeGoodsListData
};