NewSaleProcess.php 3.04 KB
<?php

namespace Plugin\DataProcess;
use Plugin\Helpers;

/**
 * 新品到着、折扣专区数据处理类
 */
class NewSaleProcess
{

    /**
     * 处理新品到着、折扣专区数据
     * 
     * @param  array $data  接口传回的数据
     * @return array       处理之后的数据
     */
    public static function newSaleData($focus, $products)
    {
        $result = array(
            'headerBanner' => self::bannerData($focus),
            'goodsContainer' => array()
        );

        foreach($products as $single)
        {
            // 处理Tabs
            $noTab = true;
            if(isset($single['tabs']) && $noTab)
            {
                $result['tabs'] = array();
                foreach ($single['tabs'] as $key => $one)
                {
                    $tabItem = array();
                    $tabItem['title'] = $one;
                    if($key === 1)
                    {
                        $tabItem['focus'] = true;
                    }
                    $result['tabs'][] = $tabItem;
                }
                $noTab = false;
            }

            // 处理商品
            $productsLi = array('goods'=>array());
            if(isset($single['product_list']))
            {
                foreach ($single['product_list'] as $value) 
                {
                    $oneProduct = array();
                    $oneProduct['id'] = $value['product_skn'];
                    $oneProduct['thumb'] = Helpers::getImageUrl($value['default_images'], 290, 388, 1);
                    $oneProduct['name'] = $value['product_name'];
                    $oneProduct['price'] = $value['market_price'];
                    $oneProduct['salePrice'] = $value['sales_price'];
                    $oneProduct['isSale'] = ($value['is_discount'] === 'N' ? false : true);
                    $oneProduct['isFew'] = ($value['is_soon_sold_out'] === 'N' ? false : true);
                    $oneProduct['isNew'] = ($value['is_new'] === 'N' ? false : true);
                    $oneProduct['url'] = $value['product_skn'];

                    $productsLi['goods'][] = $oneProduct;
                }
            }
            
            // 对于第一个productsLi添加show字段
            if(count($result['goodsContainer']) === 0)
            {
                $productsLi['show'] = true;
            }

            $result['goodsContainer'][] = $productsLi;
        }
        
        return $result;
    }

    /**
     * 处理页面顶部焦点图数据
     * 
     * @param  array $data 接口传回的焦点图数据
     * @return array       处理之后的数据
     */
    private static function bannerData($data)
    {
        $result = array('list'=>array());

        foreach ($data as $single)
        {
            foreach ($single['data'] as $one)
            {
                $banner = array();
                $banner['img'] = Helpers::getImageUrl($one['src'], 750, 364, 1);
                $banner['url'] = $one['url'];
                $result['list'][] = $banner;
            }
        }

        return $result;
    }
}