CouponFloorProcess.php 10.3 KB
<?php
/**
 * Created by PhpStorm.
 * User: Targaryen
 * Date: 2016/4/15
 * Time: 13:17
 */

namespace Plugin\DataProcess;

use WebPlugin\Cache;
use Plugin\Images;
use Plugin\Helpers;
use WebPlugin\HelperSearch;

class CouponFloorProcess
{
    // 兼容驼峰规则命名的 template_name,此处是以 templateName 命名的数据
    private static $TEMPLATE_LIST = array(
        'single_image',
        'focus'
    );

    /**
     * 获取楼层数据
     *
     * @param $data
     * @param $isApp
     * @return array
     */
    public static function getContent($data, $isApp = false)
    {
        $result = array();

        if (empty($data)) {
            return array();
        }

        $build = array();
        foreach ($data as $k => &$v) {
            $fun = '';
            if (empty($v) || !is_array($v) || !array_key_exists('template_name', $v)) {
                if (isset($v['templateName'])) {
                    $fun = $v['templateName'];
                } else {
                    continue;
                }
            }
            if (!in_array($fun, self::$TEMPLATE_LIST)) {
                $fun = $v['template_name'];
            }
            if (empty($v['data']) || !is_callable("self::$fun")) {
                continue;
            }
            // tar note 处理楼层标题
            if ($fun === 'getCoupon' && array_key_exists('template_name', $data[$k - 1]) && $data[$k - 1]['template_name'] === 'text') {
                $v['data']['floorTitle'] = $data[$k - 1]['data'];
            }
            $build = self::$fun($v['data'], $isApp);
            if (empty($build)) {
                continue;
            }
            $result[] = $build;
        }
        $build = array();
        return $result;
    }

    /**
     * 焦点图处理
     *
     * @param $data
     * @param $isApp
     * @return mixed
     */
    private static function carousel_banner($data, $isApp = false)
    {
        if (empty($data['list'])) {
            return array();
        }
        foreach ($data['list'] as &$item) {
            $item['img'] = Images::getImageUrl($item['src'], 0, 0);
            if (!is_string($item['url'])) {
                $item['url'] = '';
            }
        }
        $data['isCarouselBanner'] = true;
        return $data;
    }

    /**
     *
     * 领券楼层数据处理
     *
     * @param $data
     * @param bool $isApp
     * @return array
     * @internal param $type
     */
    private static function getCoupon($data, $isApp = false)
    {
        $result = array();
        if (empty($data)) {
            return array();
        }
        $floorTitle = '';
        foreach ($data as $key => &$item) {
            if ($key === 'floorTitle') {
                $floorTitle = (key_exists('text', $item) && ($item['text'] !== '')) ? $item['text'] : '';
                continue;
            }
            $imageSrc = Images::getImageUrl($item['image']['src'], 0, 0);
            $item['image']['src'] = $imageSrc;
            $item['image']['url'] = $isApp ? $item['image']['url'] : Helpers::getFilterUrl($item['image']['url']);
            switch ($item['status']) {
                case 1:
                    $item['isGet'] = true;
                    break;
                case 2:
                    $item['isZero'] = true;
                    break;
                case 3:
                    $item['isGeted'] = true;
                    break;
                default:
                    break;
            }
        }
        if ($floorTitle !== '') {
            $data[0]['floorTitle'] = $floorTitle;
            $data[0]['showFloorTitle'] = true;
        }
        $result = $data[0];
        $result['isCoupon'] = true;
        return $result;
    }

    /**
     * 处理标题数据
     *
     * @param $data
     * @return bool
     */
    private static function text($data, $isApp = false)
    {
        $data['isTitle'] = true;
        if ($data['text']) {
            $data['isShow'] = true;
        }
        return $data;
    }

    /**
     *
     * 单张图片的处理方法
     *
     * @param $data
     * @param bool $isApp
     * @return mixed
     */
    private static function single_image($data, $isApp = false)
    {
        $data[0]['src'] = Images::getImageUrl($data[0]['src'], 0, 0);
        $data[0]['url'] = $isApp ? $data[0]['url'] : Helpers::getFilterUrl($data[0]['url']);
        $data[0]['isSingleImage'] = true;
        return $data[0];
    }

    /**
     * 焦点图
     *
     * @param $data
     * @param bool $isApp
     * @return mixed
     */
    public static function focus($data, $isApp = false)
    {
        $result = array();
        $result['isFocus'] = true;
        foreach ($data as &$item) {
            $item['img'] = Images::getImageUrl($item['src'], 0, 0);
            $item['url'] = $isApp ? $item['url'] : Helpers::getFilterUrl($item['url']);
        }
        $result['list'] = $data;
        return $result;
    }

    /**
     * 这个是图片列表
     * 重构
     *
     * @param $data
     * @param bool $isApp
     * @return mixed
     */
    public static function image_list($data, $isApp = false)
    {
        $result = array();
        foreach ($data['list'] as &$image) {
            $num = $data['title']['column_num'];
            if ($num == 0) {
                $image['src'] = str_replace('?imageView/{mode}/w/{width}/h/{height}', '', $image['src']);
            } else {
                if (640 % $num == 0) {
                    $width = (int)(640 / $num);
                    $width = empty($width) ? 640 : $width;
                    $image['src'] = str_replace('?imageView/{mode}/w/{width}/h/{height}', '?imageView2/2/w/' . $width, $image['src']);
                    $image['src'] = Images::getImageUrl($image['src'], $width, 0);
                } else {
                    $image['src'] = str_replace('?imageView/{mode}/w/{width}/h/{height}', '', $image['src']);
                }
            }
            if($isApp){
                $image['url'] = empty($image['url']) ? 'javascript:void(0);' : $image['url'];
            }else{
                $image['url'] = empty($image['url']) ? 'javascript:void(0);' : Helpers::getFilterUrl($image['url']);
            }
        }
        $result['isImageList'] = true;
        $result['imageList'] = array(
            'col' => $data['title']['column_num'],
            'title' => $data['title']['title'],
            'list' => $data['list']
        );
        return $result;
    }

    /**
     * 促銷
     * 重构
     *
     * @param $data
     * @param bool $isApp
     * @return mixed
     */
    public static function promotion($data, $isApp = false)
    {
        $result = array();
        $result['newArrival'] = array(
            'goods' => self::getRecomProduct(1, 50, $data['promotionId'])
        );
        $result['isNewArrival'] = true;
        return $result;
    }

    /**
     * 推荐商品
     * 重构
     *
     * @param int $page
     * @param int $limit
     * @param string $promotion
     * @return array
     */
    public static function getRecomProduct($page = 1, $limit = 50, $promotion = '')
    {
        $receiveData = filter_input_array(INPUT_GET, array(
            'gender' => FILTER_DEFAULT,
            'promotion' => FILTER_DEFAULT,
        ));
        $gender = empty($receiveData['gender']) ? '1,2,3' : $receiveData['gender'];
        $promotion = empty($receiveData['promotion']) ? $promotion : $receiveData['promotion'];
        $genders = array('1,3', '2,3', '1,2,3');
        if (!in_array($gender, $genders)) {
            $gender = '1,2,3';
        }
        if (empty($promotion)) {
            return array();
        }
        $params = array('status' => 1, 'order' => 's_t_desc', 'page' => $page, 'promotion' => $promotion, 'viewNum' => $limit, 'gender' => $gender, 'p_d' => '1,1', 'stocknumber' => 3);

        $key = Cache::makeKey($params);
        $data = Cache::get($key);
        if (empty($data)) {
            $productList = HelperSearch::getProductList($params, array('action' => 'new'));
            $data = array();
            foreach ($productList['data']['product_list'] as $product) {
                $is_yohood = false;
                $is_sale = false;
                if (isset($product['is_yohood'])) {
                    $is_yohood == 'Y' ? true : false;
                }
                $goods_id = $product['goods_list'][0]['goods_id'];

                $data[] = array('id' => $product['product_id'], 'isLike' => false, 'isFew' => false,//即将售罄
                    'src' => str_replace('?imageView/{mode}/w/{width}/h/{height}', '?imageView2/1/w/320/h/426', $product['default_images']),
                    'name' => $product['product_name'],
                    'price' => $product['market_price'] == $product['sales_price'] ? 0 : $product['market_price'],
                    'salePrice' => $product['sales_price'],
                    'tags' => array('isNew' => $product['is_new'] == 'Y' ? true : false,
                        'isLimit' => $product['is_limited'] == 'Y' ? true : false,
                        'isYohood' => $is_yohood,
                        'isSale' => $is_sale,//促销
                    ),
                    'url' => Helpers::getUrlBySkc($product['product_id'], $goods_id, $product['cn_alphabet']),
                );
            }
            Cache::set($key, $data, 300);
        }
        return $data;
    }

    /**
     * 获取分享
     *
     * @param $code 资源码
     * @param string $shareTitle 分享标题
     * @param string $shareDesc 分享描述
     * @param string $shareImg 分享图片
     * @return array
     * @internal param int $share_id
     */
    public static function getShare($code, $shareTitle = '', $shareDesc = '', $shareImg = '')
    {
        return array(
            'share' => array(
                'shareLink' => Helpers::url('/coupon/floor', array('code' => $code), ''),
                'shareTitle' => $shareTitle,
                'shareDesc' => $shareDesc,
                'shareImg' => $shareImg,
                'hasWxShare' => true,
            ),
        );
    }

    /**
     *
     * 辅助方法 Debug 数据输出
     *
     * @param $data
     * @param bool $is_exit 输出后是否中断执行
     */
    public static function debugOut($data, $is_exit = true)
    {
        header("Content-type:text/html;charset=utf-8");
        echo '<pre>';
        print_r($data);
        echo '</pre>';
        if ($is_exit) {
            exit;
        }
    }
}