Search.php 11.1 KB
<?php
namespace Product;

use Api\Yohobuy;
use WebPlugin\Helpers;
use WebPlugin\HelperSearch;
use \LibModels\Web\Product\SearchData;
use Configs\CacheConfig;
use WebPlugin\Cache;

/**
 * sale首页模板数据模型
 *
 * @name SaleModel
 * @package models
 * @copyright yoho.inc
 * @version 1.0 (2015-12-17 13:54:56)
 * @author sefon
 */
class SearchModel
{

    /**
     * 搜索相关页面公共条件处理
     * @param $param
     * @param $option
     * @return mixed
     */
    public static function  searchCondition($param = array(), $option = array())
    {
        /* 过滤请求参数 */
        $condition = filter_input_array(INPUT_GET, array(
            'query' => FILTER_SANITIZE_STRING,
            'keyword' => FILTER_SANITIZE_STRING,
            'brand' => FILTER_SANITIZE_STRING,
            'sort' => FILTER_VALIDATE_INT,
            'msort' => FILTER_SANITIZE_STRING,
            'misort' => FILTER_SANITIZE_STRING,
            'color' => FILTER_VALIDATE_INT,
            'size' => FILTER_DEFAULT,
            'style' => FILTER_DEFAULT,
            'price' => FILTER_DEFAULT,
            'gender' => FILTER_SANITIZE_STRING,
            'p_d' => FILTER_DEFAULT,
            'shelve_time' => FILTER_DEFAULT,
            'new' => FILTER_DEFAULT,
            'specialoffer' => FILTER_DEFAULT,
            'limited' => FILTER_DEFAULT,
            'order' => FILTER_DEFAULT,
            'viewNum' => FILTER_VALIDATE_INT,
            'rowNum' => FILTER_VALIDATE_INT,
            'page' => FILTER_VALIDATE_INT), false);
        //keyword=>query关键字
        if (isset($condition['keyword']) && !empty($condition['keyword'])) {
            $condition['query'] = rawurldecode($condition['keyword']);
        }
        //关键字
        if (isset($condition['query']) && !empty($condition['query'])) {
            $condition['query'] = rawurldecode($condition['query']);
            $condition['query'] = HelperSearch::stripTags($condition['query']);
        }
        //品牌
        if (isset($condition['brand']) && !empty($condition['brand'])) {
            $condition['brand'] = rawurldecode($condition['brand']);
        }
        //性别
        if (isset($condition['gender']) && !empty($condition['gender'])) {
            $condition['gender'] = rawurldecode($condition['gender']);
        }
        else {
            $condition['gender'] = Helpers::getGenderByCookie();
        }
        //每页显示商品数
        $viewNumArray = array(60, 100, 200);
        if(!isset($condition['viewNum']) || empty($condition['viewNum'])){
            $condition['viewNum'] =60;
        }
        if (!in_array($condition['viewNum'], $viewNumArray)) {
            $condition['viewNum'] = 60;
        }
        //每行显示的商品数量
        if(!isset($condition['rowNum']) || empty($condition['rowNum'])){
            $condition['rowNum'] =5;
        }
        if ($condition['rowNum'] == 6) {
            $imgSize = array(195, 260);
            $minImgSize = array(50, 67);
        }
        else {
            $condition['rowNum'] = 5;
            $imgSize = array(235, 314);
            $minImgSize = array(60, 80);
        }
        //自定义搜索价格
        if (isset($condition['price']) && !empty($condition['price'])) {
            $price = explode(',', $condition['price']);
            if (!$price[0]) {
                $price[0] = 0;
            }
            if (!$price[1]) {
                $price[1] = 99999;
            }
            $condition['price'] = implode(',', $price);
        }
        //返回搜索条件
        $condition['needFilter'] = 1;
        //过滤赠品
        $condition['attribute_not'] = 2;
        //默认排序
        if (!isset($condition['order']) || empty($condition['order'])) {
            //如果新品到着没传排序方式,默认按s_t_desc(新品)顺序排序
            if ($option['controller'] == 'List' && $option['action'] == 'new') {
                $condition['order'] = 's_t_desc';
            }else{
                $condition['order'] = 's_n_desc';
            }
        }
        $options = array(
            'imgSize' => $imgSize,
            'minImgSize' => $minImgSize,
            'rowNum' =>$condition['rowNum'],
            'viewNum' =>$condition['viewNum'],
        );
        //接收高级选项
        foreach ($_GET as $key =>$val) {
            if (strpos($key, 'parameter_') !== false) {
                $condition[$key] = (int) $val;
            }
        }
        //每页记录数减1,下一页占位
        $condition['viewNum'] = $condition['viewNum'] - 1;
        $condition = array_merge($condition, $param);
        $options = array_merge($options, $option);
        //调用商品分类的参数
        $sortCondition = $condition;
        if (isset($sortCondition['msort'])) {
            unset($sortCondition['msort']);
        }
        if (isset($sortCondition['misort'])) {
            unset($sortCondition['misort']);
        }
        $data['condition'] = $condition;
        $data['sortCondition'] = $sortCondition;
        $data['options'] = $options;
        return $data;
    }

    /**
     * 根据条件获取搜索数据
     * @param array $customCondition
     * @param array $customOptions
     * @return array
     */
    public static function getListData($customCondition = array(), $customOptions = array())
    {
        $urlList = array();
        $searchCondition = self::searchCondition($customCondition, $customOptions);
        // 组合搜索商品url
        $urlList['product'] = SearchData::getProductUrl($searchCondition['condition']);
        // 组合搜索分类url
        $urlList['sort'] = SearchData::getClassesUrl();
        // 组合搜索折扣区间url
        $urlList['discount'] = SearchData::getDiscountUrl();
        $result = Yohobuy::getMulti($urlList);
        // 组织模板数据
        $data = HelperSearch::getList($result, $searchCondition['options']);
        return $data;
    }

    /**
     * 搜索页面获取搜索数据
     * @param $customCondition
     * @param $customOptions
     * @return array
     */
    public static function searchData($customCondition, $customOptions)
    {
        $urlList = array();
        $searchCondition = self::searchCondition($customCondition, $customOptions);
        if (USE_CACHE) {
            $key = CacheConfig::KEY_WEB_PRODUCT_SEARCH_DATA;
            if (!empty($searchCondition['condition'])) {
                $key .= http_build_query($searchCondition['condition'], null, '&');
            }
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }
        // 组合搜索商品url
        $urlList['product'] = SearchData::getProductUrl($searchCondition['condition']);
        // 组合搜索分类url
        $urlList['sort'] = SearchData::getClassesUrl($searchCondition['sortCondition']);
        // 组合搜索店铺url
        if (isset($searchCondition['condition']['query'])) {
            $param['keyword'] = $searchCondition['condition']['query'];
            $urlList['shop'] = SearchData::getShopUrl($param);
        }
        $data = Yohobuy::getMulti($urlList);
        //获取品牌商品分类
        if (isset($data['shop']) && !empty($data['shop'])) {
            $shopSortParam['brand'] = $data['shop']['id'];
            $shopSort = SearchData::getClassesData($shopSortParam);
            if (isset($shopSort['code']) && $shopSort['code'] == 200) {
                $data['shopSort'] = $shopSort['data']['sort'];
            }
        }
        // 组织模板数据
        $result = HelperSearch::getList($data, $searchCondition['options']);

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set($key, $result, 1800); // 缓存30分钟
            }
        }
        return $result;
    }
    /**
     * 根据product_sn查询产品图片信息
     * @param $product_sn
     * @param $options
     * @author sefon 2015-12-28 18:09:28
     * @return array
     */
    public static function getProductPic($product_sn, $options)
    {
        $condition['query'] = $product_sn;
        //返回搜索条件
        $condition['needFilter'] = 0;
        //获取产品数据
        $data = SearchData::searchElasticByCondition($condition);
        if (isset($data['code']) && $data['code'] === 200 && isset($data['data']['product_list']) && !empty($data['data']['product_list'])) {
            //组织数据结构
            $data = HelperSearch::getProductPic($data['data']['product_list'], $options);
        }
        $res = array(
            'pics'=>$data,
            'isFavorite'=>$options['isFavorite']
        );
        return $res;
    }

    /**
     * 查询搜索提示
     * @param $query
     * @return array
     */
    public static function getSuggest($query)
    {
        $data = '';
        $result = SearchData::getSuggest($query);
        if (isset($result['code']) && $result['code'] == 200 && isset($result['suggest']['items']) && !empty($result['suggest']['items'])) {
            foreach ($result['suggest']['items'] as $v) {
                $data []= '<li><a style="display: block;" href="http://search.yohobuy.com/?query='.$v['item'].'" class="clearfix clear search-item" title="'.$v['item'].'" act="http://search.yohobuy.com/?query='.$v['item'].'"><span class="searchvalue" >'.$v['item'].'</span><span class="valuenum">约'.$v['frequency'].'个商品</span></a></li>';
            }
        }
        return $data;
    }

    /**
     * 获取分类的尺码
     */
    public static function getSortSize($condition)
    {
        //返回搜索条件
        $condition['needFilter'] = 1;
        $condition['viewNum'] = 1;
        $result = SearchData::searchElasticByCondition($condition);
        if (isset($result['code']) && $result['code'] == 200 && isset($result['data']['filter']) && !empty($result['data']['filter'])) {
            return HelperSearch::size($result['data']['filter'], true);
        }
    }

    /**
     * @param $keyword
     * @return array
     */
    public static function searchError($keyword) {
        $keyword = HelperSearch::stripTags($keyword);
        $keyword = HelperSearch::mbSubstr($keyword, 300);
        $data = array(
            //初始化js
            'searchListPage' => true,
            'search' => array(
                'keyWord' => $keyword,
                'searchActionUrl' => 'http://search.yohobuy.com',
                'latestWalk' => 7,
                'pathNav' => Array(
                    '0'=>array(
                        'href' => '/',
                        'name' => '首页'
                    ),
                    '1'=>array(
                        'name' => '搜索“<span id="nav_keyword">'.$keyword.'</span>”共<span id="nav_keyword_count">0</span>个结果'
                    )
                )
            )
        );
        return $data;
    }
}