Search.php 16.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
<?php
namespace Product;

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

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

    /**
     * 搜索相关页面公共条件处理
     * 
     * @param $param    array(
     *      'brand' => int (品牌页必传)
     *      'promotion' => int (sale页必传)
     *      'new' => string (new页必传)
     *      'folder_id' => string (搜索品牌系列参数,品牌页选传)
     *      ...(用户输入的参数,如没有可不传)
     * )
     * @param $option   array(
     *      'reviewNum'  => int (底部浏览记录显示个数)
     *      'controller' => string (说明当前控制器名)
     *      'action' => string (说明当前方法名)
     *      ...(其他一些排序参数,如没有可不传)
     * )
     * @return array
     */
    public static function  searchCondition($param = array(), $option = array())
    {
        $data = array();
        /* 过滤请求参数 */
        $condition = filter_input_array(INPUT_GET, array(
            'navBar' => FILTER_VALIDATE_INT,
            'query' => FILTER_FLAG_NO_ENCODE_QUOTES ,
            'keyword' => FILTER_FLAG_NO_ENCODE_QUOTES ,
            'brand' => FILTER_SANITIZE_STRING,
            'sort' => FILTER_VALIDATE_INT,
            'category' => FILTER_VALIDATE_INT,
            'channel' => 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,
            'specialsale_id' => FILTER_VALIDATE_INT,
            'new' => FILTER_SANITIZE_STRING,
            'series_id' => FILTER_VALIDATE_INT,
            'series' => FILTER_VALIDATE_INT,
            'folder_id' => FILTER_VALIDATE_INT,
            'page' => FILTER_VALIDATE_INT), false);
        //接收高级选项
        foreach ($_GET as $key =>$val) {
            if (strpos($key, 'parameter_') !== false) {
                $condition[$key] = (int) $val;
            }
        }
        //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']);
        }
        $data['userInput'] = $condition;
        //品牌
        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'] = '';
        }
        //每页显示商品数
        $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 (!isset($price[1]) || empty($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'],
        );

        //每页记录数减1,下一页占位
        $condition['viewNum'] = $condition['viewNum'] - 1;
        $condition = array_merge($condition, $param);
        
        $data['condition'] = $condition;
        $data['options'] = array_merge($options, $option);
        //调用商品分类的参数
        if (isset($condition['msort'])) {
            unset($condition['msort']);
        }
        if (isset($condition['misort'])) {
            unset($condition['misort']);
        }
        $data['sortCondition'] = $condition;

        // 清空不再使用的变更
        $condition = array();
        
        return $data;
    }

    /**
     * 搜索list-sale页数据
     * @param $customCondition array(
     *      'p_d' => string (如果没有传入折扣,则默认展示0.01~0.99折商品)
     * )
     * @param $customOptions array(
     *      'reviewNum'  => int (底部浏览记录显示个数)
     *      'controller' => string (说明当前控制器名)
     *      'action' => string (说明当前方法名)
     * )   排序条件
     * @return array()  (处理后的list首页数据)
     */
    public static function getListData($customCondition = array(), $customOptions = array())
    {
        $searchCondition = self::searchCondition($customCondition, $customOptions);
        // 组合搜索商品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['discount'] = SearchData::getDiscountUrl();
        $data = Yohobuy::getMulti($urlList);
        // 组织模板数据
        $result = HelperSearch::getList($data, $searchCondition['options'], $searchCondition['userInput']);
        
        // 清空不使用的变量
        $searchCondition = array();
        $data = array();
        
        return $result;
    }

    /**
     * 搜索页面获取搜索数据
     * 
     * @param $customCondition array()  搜索条件
     * @param $customOptions array(
     *      'reviewNum'  => int (底部浏览记录显示个数)
     *      'controller' => string (说明当前控制器名)
     *      'action' => string (说明当前方法名)
     * )   排序条件
     * @return array
     */
    public static function searchData($customCondition, $customOptions)
    {
        $searchCondition = self::searchCondition($customCondition, $customOptions);
        $urlList = array();
        // 组合搜索商品url
        $urlList['product'] = SearchData::getProductUrl($searchCondition['condition']);
        // 组合搜索分类url
        $urlList['sort'] = SearchData::getClassesUrl(array_merge($searchCondition['sortCondition'],array('needSmallSort'=>1)));
        // 组合搜索店铺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'])) {
            $data['shopData'] = array();
            $shopsByBrands = SearchData::getQueryShopsByBrandId($data['shop']['id']);
            if (!empty($shopsByBrands['data']) && is_array($shopsByBrands['data'])) {
                //多品店 店铺入口
                foreach ($shopsByBrands['data'] as $val) {
                    if (isset($val['brand_id'])) {
                        $shopSort = SearchData::getClassesData(array('brand' => $val['brand_id']));
                    }
                    //用品牌域名访问
                    $val['brand_domain'] = $data['shop']['brand_domain'];
                    $data['shopData'][] = array(
                        'shop' => $val,
                        'shopSort' => empty($shopSort['data']['sort']) ? array() : $shopSort['data']['sort']
                    );
                }
            } else {
                //品牌入口
                $shopSort = SearchData::getClassesData(array('brand' => $data['shop']['id']));
                if (isset($shopSort['code']) && $shopSort['code'] == 200) {
                    $data['shopData'][] = array('shop' => $data['shop'], 'shopSort' => $shopSort['data']['sort']);
                }
            }
        }
        // 组织模板数据
        $result = HelperSearch::getList($data, $searchCondition['options'], $searchCondition['userInput']);
        
        // 清空不使用的变量
        $searchCondition = array();
        $data = array();
        
        return $result;
    }
    /**
     * 根据product_sn查询产品图片信息
     * 
     * @param $productSkn
     * @param $options array()
     * @author sefon 2015-12-28 18:09:28
     * @return array
     */
    public static function getProductPic($productSkn, $options)
    {
        $condition = array();
        $condition['query'] = $productSkn;
        //返回搜索条件
        $condition['needFilter'] = 0;
        //获取产品数据
        $data = SearchData::searchElasticByCondition($condition);
        if (isset($data['code']) && $data['code'] === 200 && !empty($data['data']['product_list'])) {
            //组织数据结构
            $data = HelperSearch::getProductPic($data['data']['product_list'], $options);
        }
        else {
            $data = array();
        }
        $res = array('pics' => $data);
        
        return $res;
    }

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

    /**
     * 获取分类的尺码
     */
    public static function getSortSize($condition)
    {
        //当前页面的搜索条件
        $old = array();
        $url = explode('?', $_SERVER['HTTP_REFERER']);
        if (isset($url[1])) {
            $old = explode('&', $url[1]);
        }
        if (!empty($old)) {
            foreach ($old as $val) {
                $con = explode('=', $val);
                if ($con[0] == 'channel' && in_array($con[1], array(1,2))) {
                    $condition['gender'] = urldecode($con[1]);
                } else {
                    $condition[$con[0]] = urldecode($con[1]);
                }
            }
        }
        //分类,把销售分类转为物理分类
        if (isset($condition['msort']) && !empty($condition['msort'])) {
            $category = self::getRelateSort();
            $condition['sort'] = $category[$condition['msort']]['sort'];
        }
        //返回搜索条件
        $condition['needFilter'] = 1;
        $condition['viewNum'] = 1;
        unset($condition['msort']);
        $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;
    }

    /**
     * 浏览记录(根据skn查询浏览记录商品信息)
     * @param string $skn (多个skn,','号隔开)
     * @return array
     * @return int $limit
     * @autor sefon 2016-4-18 16:06:06
     */
    public static function historyProduct($skn, $limit)
    {
        $result = array();
        $param['query'] = implode(',', $skn);
        $skn = array_flip($skn);
        $param['viewNum'] = $limit;
        $data = SearchData::searchElasticByCondition($param);
        if (!isset($data['code']) || $data['code'] != 200 || !isset($data['data']['product_list']) || empty($data['data']['product_list'])) {
            return $result;
        }
        foreach ($data['data']['product_list'] as $key => $productVal) {
            $product = array();
            $backUp = array();
            foreach ($productVal['goods_list'] as $goodsVal) {
                if ($goodsVal['is_default'] == 'Y') {
                    $product['default_goods'] =  $goodsVal['goods_id'];
                    $product['default_image'] =  $goodsVal['images_url'];
                    break;
                }
                $backUp['default_goods'] = $goodsVal['goods_id'];
                $backUp['default_image'] = $goodsVal['images_url'];
            }
            if (empty($product['default_goods'])) {
                $product = $backUp;
            }
            $product['url'] = Helpers::getUrlBySkc($productVal['product_id'], $product['default_goods'], $productVal['cn_alphabet']);
            $product['pic_url'] = Images::getImageUrl($product['default_image'], 150, 200);
            $product['market_price'] = $productVal['market_price'] != $productVal['sales_price'] ? '¥'.$productVal['market_price'] : '';
            $product['price'] = '¥'.$productVal['sales_price'];
            $product['product_name'] = $productVal['product_name'];
            unset($product['default_goods']);
            unset($product['default_image']);
            if (isset($skn[$productVal['product_skn']])) {
                $result[$skn[$productVal['product_skn']]] = $product;
            }
        }
        ksort($result);
        $result = array_values($result);
        return $result;
    }

}