New.php 4.6 KB
<?php

namespace Product;

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

/**
 * sale首页模板数据模型
 *
 */
class NewModel 
{
    /**
     * 搜索新品到着首页数据
     * @param $customCondition array(
     *      'new' => (string)Y (只展示新品)
     * )
     * @param $customOptions array(
     *      'reviewNum'  => int (底部浏览记录显示个数)
     *      'controller' => string (说明当前控制器名)
     *      'action' => string (说明当前方法名)
     * )   排序条件
     * @return array()  (处理后的list首页数据)
     */
    public static function getNewSearchData($customCondition, $customOptions) 
    {
        $urlList = array();
        $searchCondition = SearchModel::searchCondition($customCondition, $customOptions);
        if (USE_CACHE) {
            $key = WebCacheConfig::KEY_WEB_PRODUCT_LIST_NEW;
            if (!empty($searchCondition['userInput'])) {
                $key .= http_build_query($searchCondition['userInput'], null, '&');
            }
            $channel = HomeModel::getSwitchChannel();
            //key加上性别参数
            $key .= $channel;
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }
        // 组合搜索商品url
        $urlList['product'] = SearchData::getProductUrl($searchCondition['condition']);
        // 组合搜索分类url
        $sortCondition = array();
        if (isset($searchCondition['condition']['misort']) && !empty($searchCondition['condition']['misort'])) {
            $sortCondition['needSmallSort'] = 1;
        }
        $urlList['sort'] = SearchData::getClassesUrl($sortCondition);
        // 组合搜索最新上架url
        $urlList['recent'] = SearchData::getRecentShelveUrl();

        $data = Yohobuy::getMulti($urlList);

        // 组织模板数据
        $result = HelperSearch::getList($data, $searchCondition['options'], $searchCondition['userInput']);
        
        //new页面模拟数据
//            $data['newMain'] = array(
//                'banner' => 'http://img11.static.yhbimg.com/yhb-img01/2015/11/23/07/010a459d41b99a839cba9377532f1c19b2.jpg?imageView/3/w/970/h/200',
//                    'date' => '12月16日',
//                    'title' => '新品到着',
//                    'brands' => array(
//                        array(
//                            'href' => '',
//                            'logo' => 'http://img13.static.yhbimg.com/brandLogo/2012/02/28/15/02b2b5ded161ab31e2e097a327ed475052.jpg?imageView/2/w/170/h/120'
//                        ),
//                        array(
//                            'href' => '',
//                            'logo' => 'http://img13.static.yhbimg.com/brandLogo/2012/11/09/09/023721f44182f775d79904010af421331e.jpg?imageView/2/w/170/h/120'
//                        ),
//                        array(
//                            'href' => '',
//                            'logo' => 'http://img11.static.yhbimg.com/brandLogo/2012/12/13/17/01408fb72646c8f3fa59d870514f08a356.jpg?imageView/2/w/170/h/120'
//                        ),
//                        array(
//                            'href' => '',
//                            'logo' => 'http://img13.static.yhbimg.com/brandLogo/2012/12/24/13/0265b45e37af697c5ba12d5415fb341f27.jpg?imageView/2/w/170/h/120'
//                        ),
//                        array(
//                            'href' => '',
//                            'logo' => 'http://img11.static.yhbimg.com/brandLogo/2015/08/11/15/012d09a5cae187af1f6f3ed246b9b5a4fc.jpg?imageView/2/w/170/h/120'
//                        ),
//                        array(
//                            'href' => '',
//                            'logo' => 'http://img11.static.yhbimg.com/brandLogo/2015/08/20/16/01047ffb3ca182871821d551af31ac2378.jpg?imageView/2/w/170/h/120'
//                        )
//                    )
//            );
            
        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set($key, $result, 1800); // 缓存30分钟
            }
        }
        return $result;
    }
    
}