Brands.php 9.92 KB
<?php

namespace Product;

use Api\Yohobuy;
use WebPlugin\HelperSearch;
use LibModels\Web\Product\BrandData;
use WebPlugin\Helpers;
use LibModels\Web\Product\SearchData;
use Configs\WebCacheConfig;
use WebPlugin\Cache;
use Index\HomeModel;

/**
 * 品牌首页模板数据模型
 *
 * @author Administrator
 */
class BrandsModel
{
    /**
     * 搜索品牌数据
     * @param $condition array(
     *      'brand' => int (品牌id,必传)
     *      'folder_id' => string (搜索品牌系列参数)
     * )
     * @param $options array(
     *      'brandName' => string (品牌域名)
            'uid' => int (用户uid)
            'brandId' => int (品牌id)
            'node' => string (左侧广告node)
            'brandBanner' => string (品牌banner)
            'brandNameEn' => string (品牌英文名)
            'brandNameCn' => string (品牌中文名)
            'reviewNum'  => int (底部浏览记录显示个数)
            'controller' => string (说明当前控制器名)
            'action' => string (说明当前方法名)
     * )   排序条件
     * @return array()  (处理后的品牌首页数据)
     */
    public static function getBrandSearchData($condition, $options)
    {
        //获取$condition和$option 筛选条件和排序条件
        $searchCondition = SearchModel::searchCondition($condition, $options);

        // 并行调用品牌相关接口并封装数据
        $result = self::getBrandData($searchCondition, $options);

        //获取品牌系列数据
        $adNav = self::getAdNav($options['brandId']);
        $result['leftContent'][] = array('picLink' => $adNav);

        //获取静态内容
        if ($options['node']) {
            $nodeContent = HelperSearch::getNodeContent($options['node']);
            $result['leftContent'][]['picLink']['list'] = $nodeContent;
        }


        return $result;
    }

    /**
     * 品牌介绍页
     * @param $customOptions  array(
     *      'brandName' => string (品牌域名)
            'uid' => int (用户uid)
            'brandId' => int (品牌id)
            'brandAbout' => string (品牌简介)
            'brandBanner' => string (品牌banner)
            'brandNameEn' => string (品牌英文名)
            'brandNameCn' => string (品牌中文名)
     * )
     * @return array
     */
    public static function getBrandIntro($customOptions = array())
    {
        $urlList = array();
        if (USE_CACHE) {
            $key = WebCacheConfig::KEY_WEB_PRODUCT_INDEX_BRANDINTRO;
            if (!empty($customOptions['userInput'])) {
                $key .= http_build_query($customOptions['userInput'], null, '&');
            }
            $channel = HomeModel::getSwitchChannel();
            //key加上性别参数
            $key .= $channel;
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        //组合搜索品牌url
        $urlList['brand'] = SearchData::getBrandUrl($customOptions);
        //批量调接口
        $data = Yohobuy::getMulti($urlList, array(), true);
        $result = array();
        if (isset($data['brand']) && isset($customOptions['channel'])) {
            $banner = HelperSearch::getBannerFormat($data['brand'], $customOptions['brandBanner']);
            $channel = $customOptions['channel'];
            $brandNav = $customOptions['brandNameEn'] . ' ' . $customOptions['brandNameCn'];
            //根据频道判断home页地址
            switch ($channel) {
                case 'boys':
                    $home = Helpers::url('', '', 'default');
                    break;
                case 'girls':
                    $home = Helpers::url('/woman', '', 'new');
                    break;
                case 'lifestyle':
                    $home = Helpers::url('/lifestyle', '', 'new');
                    break;
                case 'kids':
                    $home = Helpers::url('/kids', '', 'new');
                    break;
                default:
                    $home = Helpers::url('', '', 'new');
                    break;
            }
            $result = array(
                'brandBanner' => $banner,
                'brandAbout' => $customOptions['brandAbout'],
                'pathNav' => array(
                    array(
                        'href' => $home,
                        'name' => strtoupper($channel) . '首页',
                        'title' => 'YOHO!有货'
                    ),
                    array(
                        'href' => Helpers::url('/brands'),
                        'name' => '品牌一览',
                        'title' => '品牌一览'
                    ),
                    array(
                        'name' => $brandNav
                    )
                )
            );
        }



        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set($key, $result, 1800); // 缓存30分钟
            }
        }

        return $result;
    }


    /**
     * 根据品牌域名处理相关品牌参数
     * @param $domain (品牌域名)
     * @param $type   (根据type调取同一接口不同的数据)
     * @return array|bool
     */
    public static function getBrandByDomain($domain, $type)
    {
        //根据传来的type值,选择请求需要的数据
        switch ($type) {
            case 1:
                $fields = 'id,brand_name,brand_name_cn,brand_name_en,brand_domain,brand_alif,brand_banner,brand_ico,static_content_code';
                break;
            case 2:
                $fields = 'id,brand_name,brand_name_cn,brand_name_en,brand_banner,brand_ico,brand_intro';
            default:
                break;
        }
        //调用接口获得数据
        $brandInfo = BrandData::getBrandLogoByDomain($domain, $fields);
        $result = array();
        //组装品牌页顶部banner条需要的数据
        if (!empty($brandInfo['data']) && $brandInfo['code'] == 200) {
            $result['brandId'] = isset($brandInfo['data']['id']) ? $brandInfo['data']['id'] : '';
            $result['node'] = isset($brandInfo['data']['static_content_code']) ? $brandInfo['data']['static_content_code'] : false;
            $result['brandBanner'] = isset($brandInfo['data']['brand_banner']) ? $brandInfo['data']['brand_banner'] : '';
            $result['brandNameEn'] = isset($brandInfo['data']['brand_name_en']) ? $brandInfo['data']['brand_name_en'] : '';
            $result['brandNameCn'] = isset($brandInfo['data']['brand_name_cn']) ? $brandInfo['data']['brand_name_cn'] : '';
            $result['brandAbout'] = isset($brandInfo['data']['brand_intro']) ? $brandInfo['data']['brand_intro'] : '';
        }
        else {
            return false;
        }
        return $result;
    }


    /**
     * 获取品牌系列数据
     * @param $brandId  (品牌id)
     * @param int $status   (状态码)
     * @return array
     */
    public static function getAdNav($brandId, $status = 1)
    {
        //调用接口获得品牌系列数据
        $advNav = BrandData::getFolderByBrand($brandId, $status);
        $result = array();
        if (isset($advNav['data']) && $advNav['code'] === 200) {
            foreach ($advNav['data'] as $key => $value) {
                $result['list'][$key]['href'] = '?folder_id=' . $value['id'];
                $result['list'][$key]['src'] = $value['brand_sort_ico'];
            }
            //title设置为经典系列
            $result['picTitle'] = '经典系列';
        }

        return $result;
    }

    /**
     * 根据条件获取搜索品牌数据
     * @param $searchCondition array()    (经过过滤的、用户输入的一些数据)
     * @param $customOptions array(
     *       'brandName' => string (品牌域名)
     *       'uid' => int (用户uid)
     *       'brandId' => int (品牌id)
     *       'node' => string (左侧广告node)
     *       'brandBanner' => string (品牌banner)
     *       'brandNameEn' => string (品牌英文名)
     *       'brandNameCn' => string (品牌中文名)
     *       'reviewNum'  => int (底部浏览记录显示个数)
     *       'controller' => string (说明当前控制器名)
     *       'action' => string (说明当前方法名)
     * )
     * @return array()  (品牌首页数据,不包括系列数据、广告数据、banner数据)
     */
    public static function getBrandData($searchCondition = array(), $customOptions = array())
    {
        // 组合搜索商品url
        $urlList['product'] = SearchData::getProductUrl($searchCondition['condition']);

        // 组合搜索分类url
        $sortCondition = array();
        if (isset($searchCondition['condition']['misort']) && !empty($searchCondition['condition']['misort'])) {
            $sortCondition['needSmallSort'] = 1;
        }
        $sortCondition= array(
            'attribute_not' => $searchCondition['sortCondition']['attribute_not'],
            'brand' => $searchCondition['sortCondition']['brand']
        );
        $urlList['sort'] = SearchData::getClassesUrl($sortCondition);

        //组合搜索品牌url
        $urlList['brand'] = SearchData::getBrandUrl($customOptions);

        //批量调接口
        $data = Yohobuy::getMulti($urlList);

        //组织模板数据格式
        $result = HelperSearch::getList($data, $searchCondition['options'], $searchCondition['userInput']);

        return $result;
    }

}