Sale.php 3.82 KB
<?php

namespace Product;

use Api\Yohobuy;
use WebPlugin\HelperSearch;
use WebPlugin\Images;
use LibModels\Web\Product\SearchData;
use Configs\WebCacheConfig;
use WebPlugin\Cache;
use Index\HomeModel;
/**
 * sale首页模板数据模型
 *
 */
class SaleModel 
{
    /**
     * 搜索sale首页数据
     * @param $customCondition array(
     *      'brand' => int (活动涉及的品牌id)
     *      'promotion' => int (sale页促销id、专区id)
     * )
     * @param $customOptions array(
     *      'reviewNum'  => int (底部浏览记录显示个数)
     *      'controller' => string (说明当前控制器名)
     *      'action' => string (说明当前方法名)
     * )   排序条件
     * @return array()  (处理后的list首页数据)
     */
    public static function getSaleSearchData($customCondition, $customOptions, $specialInfo) 
    {
        $urlList = array();
        
        //获取$condition和$option 筛选条件和排序条件
        $searchCondition = SearchModel::searchCondition($customCondition, $customOptions);
        
        if (USE_CACHE) {
            $key = WebCacheConfig::KEY_WEB_PRODUCT_SALE_INDEX;
            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);
        
        //批量调接口
        $data = Yohobuy::getMulti($urlList);
        
        //组织模板数据格式
        $result = HelperSearch::getList($data, $searchCondition['options'], $searchCondition['userInput']);
        
        //组织sale数据
        $special = array();
        if (isset($specialInfo['data']) && !empty($specialInfo['data']['banner_img'])) {
            $banner = json_decode($specialInfo['data']['banner_img'], true);
            foreach ($banner as $k => $v) {
                $v['img'] = Images::getSourceUrl($v['img'], 'couponImg');
                $specialInfo['data']['banner'][] = $v;
            }
            $special = $specialInfo['data'];
            
            //Sale首页 banner数据
            $result['saleBanner']['bannerHeight'] = $special['banner'][0]['height'];
            $result['saleBanner']['img'] = $special['banner'][0]['img'];
            $result['saleTitle']['name'] = '全部商品';
            $result['saleTitle']['count'] = isset($result['totalCount']) ? $result['totalCount'] : 0;
        }

         //获取广告位数据
        if (isset($special['left_ad_code'])) {
            $nodeContent = HelperSearch::getNodeContent($special['left_ad_code']);
            $result['leftContent'][]['picLink']['list'] = $nodeContent;
        }
        
        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set($key, $result, 1800); // 缓存30分钟
            }
        }
        
        return $result;
    }

}