Search.php 7.19 KB
<?php
namespace Product;

use Api\Yohobuy;
use Plugin\Helpers;
use Plugin\HelperSearch;
use \LibModels\Web\Product\SearchData;

/**
 * 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, $option)
    {
        /* 过滤请求参数 */
        $condition = filter_input_array(INPUT_GET, array(
            'query' => FILTER_SANITIZE_STRING,
            'brand' => FILTER_VALIDATE_INT,
            'sort' => FILTER_VALIDATE_INT,
            'msort' => FILTER_VALIDATE_INT,
            'misort' => FILTER_VALIDATE_INT,
            '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,
            'isNew' => FILTER_DEFAULT,
            'specialoffer' => FILTER_DEFAULT,
            'limited' => FILTER_DEFAULT,
            'order' => FILTER_DEFAULT,
            'viewNum' => FILTER_VALIDATE_INT,
            'rowNum' => FILTER_VALIDATE_INT,
            'page' => FILTER_VALIDATE_INT), false);
        //关键字
        if (isset($condition['query']) && !empty($condition['query'])) {
            $condition['query'] = rawurldecode($condition['query']);
        }
        //性别
        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'])) {
            $condition['order'] = 's_n_desc';
        }
        if (!isset($condition['p_d']) || empty($condition['p_d'])) {
            $condition['p_d'] = '0,0.9';
        }
        $options = array(
            'imgSize' => $imgSize,
            'minImgSize' => $minImgSize,
            'rowNum' =>$condition['rowNum'],
            'viewNum' =>$condition['viewNum'],
        );
        //接收高级选项
        foreach ($_GET as $key =>$val) {
            if (strpos('parameter_',$key) !== false) {
                $condition[$key] = (int) $val;
            }
        }
        //每页记录数减1,下一页占位
        $condition['viewNum'] = $condition['viewNum'] - 1;
        $condition = array_merge($condition, $param);
        $options = array_merge($options, $option);
        $data['condition'] = $condition;
        $data['options'] = $options;
        return $data;
    }

    /**
     * 根据条件获取搜索数据
     * @param array $customCondition
     * @param array $customOptions
     * @return array
     */
    public static function getSearchData($customCondition = array(), $customOptions = array())
    {
        $data = array();
        $searchCondition = self::searchCondition($customCondition, $customOptions);
        // 调用接口查询商品数据
        $urlList['productUrl'] = HelperSearch::getProductUrl($searchCondition['condition']);
        // 调用分类信息
        $urlList['sortUrl'] = HelperSearch::getSortUrl($searchCondition['condition']);
//        $result['data']['filter']['group_sort'] = $category['data']['sort'];

        // 调用折扣区间
        $urlList['discountUrl'] = HelperSearch::getDiscountUrl($searchCondition['condition']);
//        $result['data']['filter']['discount'] = $discount['data']['discount'];
        // 调用最新上架
        $urlList['recentUrl'] = HelperSearch::getRecentUrl($searchCondition['condition']);
//        $result['data']['filter']['recent'] = $recent['data']['recent'];

        //用户浏览记录
        //$urlList['reviewUrl'] = HelperSearch::getReviewUrl($searchCondition['condition']);

        $result = Yohobuy::getMulti($urlList, array(), true);



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

    /**
     * 搜索页面获取搜索数据
     * @param $condition
     * @param $options
     * @return array
     */
    public static function searchData($condition, $options)
    {
        $data = self::getSearchData($condition, $options);
        //获取品牌店铺信息
        if (isset($condition['query'])) {
            $param['keyword'] = $condition['query'];
            $result = SearchData::getShop($param);
            if (isset($result['code']) && $result['code'] === 200 && isset($result['data']) && !empty($result['data'])) {
                //调用品牌分类
                $class['brand'] = $result['data']['id'];
                $brandSort = SearchData::getClassesData($class);
                $shop = $result['data'];
                if (isset($brandSort['data']['sort']) && !empty($brandSort['data']['sort']))
                $shop['sort'] = $brandSort['data']['sort'];
                $shopEntry = HelperSearch::shop($shop);
            }
        }
        $data['shopEntry'] = isset($shopEntry) ? $shopEntry : array();
        return $data;

    }
    /**
     * 根据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);
        }
        echo json_encode($data);
    }
}