Search.php 20 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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
<?php

namespace Product;

use Category\ClassModel;
use Configs\CacheConfig;
use LibModels\Wap\Product\SearchData;
use LibModels\Wap\Category\BrandData;
use LibModels\Wap\Category\ClassData;
use LibModels\Wap\Home\UserData;
use Plugin\DataProcess\ListProcess;
use Plugin\Cache;
use Plugin\Helpers;

/**
 * 搜索相关的模板数据模型
 *
 * @name Search
 * @package Product
 * @copyright yoho.inc
 * @version 1.0 (2015-11-1 14:35:52)
 */
class SearchModel
{

    /**
     * 搜索列表页处理筛选参数
     * @return mixed
     */
    public static function conditionHandle()
    {
        // 过滤请求参数
        $condition = filter_input_array(INPUT_GET, array(
            'shop_id' => FILTER_DEFAULT,
            'categoryId' => FILTER_DEFAULT,
            'subCategoryId' => FILTER_DEFAULT,
            'title' => FILTER_DEFAULT,
            'query' => FILTER_DEFAULT,
            'brand' => FILTER_DEFAULT,
            'sort' => FILTER_DEFAULT,
            'msort' => FILTER_DEFAULT,
            'misort' => FILTER_DEFAULT,
            'color' => FILTER_DEFAULT,
            'size' => FILTER_DEFAULT,
            'style' => FILTER_DEFAULT,
            'price' => FILTER_DEFAULT,
            'discount' => FILTER_DEFAULT,
            'gender' => FILTER_DEFAULT,
            'channel' => FILTER_DEFAULT,
            'outlets' => FILTER_DEFAULT,
            'ageLevel' => FILTER_DEFAULT,
            'p_d' => FILTER_DEFAULT,
            'students' => FILTER_DEFAULT,
            'app_type' => FILTER_DEFAULT,
            'filter_poolId' => FILTER_DEFAULT), false);

        if (isset($condition['shop_id'])) {
            $condition['shopId'] = $condition['shop_id'];
        }

        if (isset($condition['discount'])) {
            $condition['p_d'] = rawurldecode($condition['discount']);
            // unset($condition['discount']); 为了兼容js中传参的discount
        }
        // 为了兼容现在运营在用的p_d
        if (isset($condition['p_d'])) {
            $condition['discount'] = rawurldecode($condition['p_d']);
        }
        if (isset($condition['query'])) {
            $condition['query'] = rawurldecode($condition['query']);
        }

        return $condition;
    }

    /**
     * 会员增加学生类型搜索的处理
     * @param $uid
     * @param $isApp
     * @param $appVersion
     * @return mixed
     */
    public static function studentsHandle($uid, $isApp, $appVersion)
    {
        // 获取用户信息判断是否为学生
        if ($uid) {
            $userInfo = UserData::userData($uid);
            if ($userInfo['code'] === 200 && isset($userInfo['data']['vip_info']['is_student']) && $userInfo['data']['vip_info']['is_student'] === 1) {
                $data['students'] = '';
            } else {
                $data['students'] = Helpers::url('/activity/student/register');
            }
        } else {
            if ($isApp) {
                $data['students'] = '/signin.html?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"http://m.yohobuy.com/activity/student/register","antarget":"1"}}}';
            } else {
                $data['students'] = Helpers::url('/signin.html?refer=//m.yohobuy.com/activity/student/register');
            }
        }
        $data['showDownloadApp'] = false;
        if ($isApp) {
            //判断app版本是否低于4.9.1,用于显示下方的提示
            if (intval(substr(str_replace('.', '', $appVersion), 0, 3)) < 491) {
                $data['oldApp'] = true;
            }
        }

        return $data;
    }

    /**
     * 搜索-品牌处理
     * @param $condition
     * @param $query
     */
    public static function brandHandle($condition, $query)
    {
        $domain = null;
        $brandNames = ListModel::getAllBrandNames();

        do {
            /* 精确查品牌域名 */
            if (isset($brandNames[$query])) {
                $domain = $query;
                break;
            }

            /* 精确查品牌名称 */
            $domains = array_keys($brandNames, $query, true);
            if (isset($domains[0])) {
                $domain = $domains[0];
                break;
            }

            /* 模糊查品牌域名 tar notice 使用模糊搜索会出现直接跳转店铺,搜不到大量商品*/
            // foreach ($brandNames as $key => $domains) {
            //     if (strpos($key, $query) !== false) {
            //         $domain = $key;
            //         break;
            //     }
            // }
        } while (false);

        // 清空变量做释放
        $brandNames = array();

        // 跳转到品牌商品列表页
        if ($domain !== null && empty($condition['shop_id'])) {
            $url = Helpers::url('', array(
                'from' => 'search',
                'query' => $query
            ), $domain);

            /* 跳转 */
            headers_sent() || header('Location: ' . $url);
            exit();
        }
    }

    /**
     * 搜索-品类处理
     * @param $query
     * @return array
     */
    public static function categoryHandle($query)
    {
        $classNames = ClassModel::getClassNames();
        $queryClassType = array(
            'isQueryFirstClass' => false,
            'isQuerySecondClass' => false
        );

        do {
            // 品类名称为空时跳出
            if (empty($classNames)) {
                break;
            }

            /* 精确查一级品类 */
            $sorts = array_keys($classNames['first'], $query, true);
            if (isset($sorts[0])) {
                $queryClassType['isQueryFirstClass'] = true;
                break;
            }

            /* 精确查二级品类 */
            $sorts = array_keys($classNames['second'], $query, true);
            if (isset($sorts[0])) {
                $queryClassType['isQuerySecondClass'] = true;
                break;
            }
        } while (false);

        $classNames = array();

        return $queryClassType;
    }

    /**
     * 获取搜索的数据
     *
     * @param array $condition 查询条件
     * @return array
     */
    public static function getSearchData($condition, $showTag = true, $tagNew = true, $tagSale = true)
    {
        $result = array();

        if (USE_CACHE) {
            $key = CacheConfig::KEY_ACTION_SEARCH_SEARCH;
            if (!empty($condition)) {
                $key .= http_build_query($condition, null, '&');
            }
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        //频道参数
        $coverChannel = empty($condition['coverChannel']) ? '' : $condition['coverChannel'];

        // 调用接口查询数据 (使用新的 Elastic Engine)
        $listData = SearchData::searchElasticByCondition($condition);
        // 调用接口查询数据 (使用老的 Sphinx Engine)
        // $listData = SearchData::searchByCondition($condition);
        // 处理返回的数据
        if (isset($listData['code']) && $listData['code'] === 200) {
            $result = ListProcess::getListData($listData['data'], false, $showTag, $tagNew, $tagSale, $coverChannel);
        }

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            } // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set($key, $result, 300); // 缓存5分钟
            }
        }

        return $result;
    }

    /**
     * 获取筛选的数据
     *
     * @param array $condition 查询条件
     * @return array
     */
    public static function getFilterData($condition)
    {
        $result = array();

        if (USE_CACHE) {
            $key = CacheConfig::KEY_ACTION_SEARCH_FILTER;
            if (!empty($condition)) {
                $key .= http_build_query($condition, null, '&');
            }
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        // 设置选定的gender
        $gender = '1,2,3';
        if (isset($condition['gender']) && !empty($condition['gender'])) {
            $gender = $condition['gender'];
        }

        // 区别各种列表页面的筛选数据
        if (isset($condition['shop_id'])) {
            $listData = SearchData::searchByCondition($condition);
            $exclude = 'null';
        } elseif (isset($condition['brand'])) {
            $listData = BrandData::filterBrandData($condition);
            $exclude = 'brand';
        } else if (isset($condition['sort'])) {
            $listData = ClassData::filterClassData($condition);
            $exclude = 'group_sort';
        } else if (isset($condition['filter_poolId'])) {
            $listData = SearchData::getFilter($condition);
            $exclude = null;
        } else if (isset($condition['students'])) {
            $listData = ClassData::filterStudentsData($condition);
            $exclude = 'students';
        } else {
            $listData = SearchData::searchByCondition($condition);
            $exclude = null;
        }

        if (isset($listData['data']['filter'])) {
            $result['filter'] = ListProcess::getFilterData($listData['data']['filter'], $gender, $exclude);
        }

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            } // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set($key, $result, 300); // 缓存5分钟
            }
        }

        return $result;
    }

    /**
     * @param array $params 直接传筛选参数
     * @return array
     */
    public static function filter($params = array())
    {
        $data = array();

        if (empty($params)) {
            $condition = filter_input_array(INPUT_GET, array(
                'query' => FILTER_DEFAULT,
                'brand' => FILTER_DEFAULT,
                'shop_id' => FILTER_DEFAULT,
                'sort' => FILTER_DEFAULT,
                'msort' => FILTER_DEFAULT,
                'misort' => FILTER_DEFAULT,
                'color' => FILTER_DEFAULT,
                'size' => FILTER_DEFAULT,
                'style' => FILTER_DEFAULT,
                'price' => FILTER_DEFAULT,
                'discount' => FILTER_DEFAULT,
                'gender' => FILTER_DEFAULT,
                'channel' => FILTER_DEFAULT,
                'ageLevel' => FILTER_DEFAULT,
                'p_d' => FILTER_DEFAULT,
                'students' => FILTER_DEFAULT,
                'filter_poolId' => FILTER_DEFAULT
            ), false);

            // 转义分类
            if (isset($condition['sort'])) {
                $condition['sort'] = rawurldecode($condition['sort']);
            }
            // 转义分类
            if (isset($condition['msort'])) {
                $condition['msort'] = rawurldecode($condition['msort']);
            }
            // 转义分类
            if (isset($condition['misort'])) {
                $condition['misort'] = rawurldecode($condition['misort']);
            }
            // 转义颜色
            if (isset($condition['color'])) {
                $condition['color'] = rawurldecode($condition['color']);
            }
            //
            if (isset($condition['ageLevel'])) {
                $condition['age_level'] = rawurldecode($condition['ageLevel']);
                unset($condition['ageLevel']);
            }
            // 为了兼容现在运营在用的p_d
            if (isset($condition['p_d'])) {
                $condition['p_d'] = rawurldecode($condition['p_d']);
            } // 转换折扣
            elseif (isset($condition['discount'])) {
                $condition['p_d'] = rawurldecode($condition['discount']);
                unset($condition['discount']);
            }
            // 转义性别
            if (isset($condition['gender'])) {
                $condition['gender'] = rawurldecode($condition['gender']);
            }
            // 转义价格
            if (isset($condition['price'])) {
                $condition['price'] = rawurldecode($condition['price']);
            }
            // 转义风格
            if (isset($condition['style'])) {
                $condition['style'] = rawurldecode($condition['style']);
            }

        } else {
            $condition = $params;
        }

        // 区别各种列表页面的筛选数据
        $data = self::getFilterData($condition);

        return $data;
    }

    /**
     * @param array $params
     * @return array
     */
    public static function search($params = array())
    {
        do {

            /* 如果参数为空是 Ajax 调用 */
            if (empty($params)) {
                /* Ajax 调用过滤请求参数 */
                $condition = filter_input_array(INPUT_GET, array(
                    'shop_id' => FILTER_DEFAULT,
                    'query' => FILTER_DEFAULT,
                    'specialsale_id' => FILTER_DEFAULT,
                    'promotion' => FILTER_DEFAULT,
                    'brand' => FILTER_DEFAULT,
                    'sort' => FILTER_DEFAULT,
                    'msort' => FILTER_DEFAULT,
                    'misort' => FILTER_DEFAULT,
                    'color' => FILTER_DEFAULT,
                    'size' => FILTER_DEFAULT,
                    'style' => FILTER_DEFAULT,
                    'price' => FILTER_DEFAULT,
                    'discount' => FILTER_DEFAULT,
                    'gender' => FILTER_DEFAULT,
                    'channel' => FILTER_DEFAULT,
                    'p_d' => FILTER_DEFAULT,
                    'outlets' => FILTER_DEFAULT,
                    'age_level' => FILTER_DEFAULT,
                    'page' => FILTER_VALIDATE_INT,
                    'filter_poolId' => FILTER_DEFAULT,
                    'showTag' => FILTER_DEFAULT,
                    'tagNew' => FILTER_DEFAULT,
                    'students' => FILTER_DEFAULT,
                    'coverChannel' => FILTER_DEFAULT,
                    'tagSale' => FILTER_DEFAULT), false);


                if (!empty($condition['shop_id'])) {
                    $condition['shop'] = $condition['shop_id'];
                }
                // 转义分类
                if (isset($condition['sort'])) {
                    $condition['sort'] = rawurldecode($condition['sort']);
                }
                // 转义分类
                if (isset($condition['msort'])) {
                    $condition['msort'] = rawurldecode($condition['msort']);
                }
                // 转义分类
                if (isset($condition['misort'])) {
                    $condition['misort'] = rawurldecode($condition['misort']);
                }
                // 转义活动ID
                if (isset($condition['specialsale_id'])) {
                    $condition['specialsale_id'] = rawurldecode($condition['specialsale_id']);
                }
                if (isset($condition['promotion'])) {
                    $condition['promotion'] = rawurldecode($condition['promotion']);
                }
                // 转义颜色
                if (isset($condition['color'])) {
                    $condition['color'] = rawurldecode($condition['color']);
                }
                // 转义价格
                if (isset($condition['price'])) {
                    $condition['price'] = rawurldecode($condition['price']);
                }
                // 转义风格
                if (isset($condition['style'])) {
                    $condition['style'] = rawurldecode($condition['style']);
                }
                // 为了兼容现在运营在用的p_d
                if (isset($condition['p_d'])) {
                    $condition['p_d'] = rawurldecode($condition['p_d']);
                } // 转换折扣
                elseif (isset($condition['discount'])) {
                    $condition['p_d'] = rawurldecode($condition['discount']);
                    unset($condition['discount']);
                }
                // 转义性别
                if (isset($condition['gender'])) {
                    $condition['gender'] = rawurldecode($condition['gender']);
                }
                // 转义性别
                if (isset($condition['age_level'])) {
                    $condition['age_level'] = rawurldecode($condition['age_level']);
                }
                if (isset($condition['query'])) {
                    $condition['query'] = rawurldecode($condition['query']);
                }

                // 转义奥莱
                if (isset($condition['outlets'])) {
                    $condition['outlets'] = rawurldecode($condition['outlets']);
                }

                // 用户自定义商品类目
                if (isset($condition['filter_poolId'])) {
                    $condition['filter_poolId'] = rawurldecode($condition['filter_poolId']);
                }

                // 封面频道参数
                if (isset($condition['coverChannel'])) {
                    $condition['coverChannel'] = rawurldecode($condition['coverChannel']);
                }

                // 转换排序方式
//            $type = $this->get('type', '');
//            $order = $this->get('order', 0);
                $order = empty($_GET['order']) ? 0 : $_GET['order'];
                $condition['order'] = Helpers::transOrder($order, $_GET['type']);

                /* 如果参数不为空是直接调用 */
            } else {
                $condition = $params;
            }

            //标签显示控制
//            $showTag = $this->get('showTag', 0);
            $showTag = empty($condition['showTag']);
//            $showTag = $showTag === 0;
            //显示标签new
//            $tagNew = $this->get('tagNew', 0);
            $tagNew = empty($condition['tagNew']);
//            $tagNew = $tagNew === 0;
            //显示标签sale
            $tagSale = empty($condition['tagSale']);
//            $tagSale = $tagSale === 0;

            // 过滤掉值为空的条件
            $condition = array_filter($condition);

//            /* 模糊搜索关键词 */
//            if (isset($condition['query'])) {
//                $data = Product\SearchModel::getSearchData($condition);
//                break;
//            }
//
//            /* 精确搜索品牌 */
//            if (isset($condition['brand'])) {
//                $title = '';
//                $data = Product\ListModel::getBrandData($condition, $title);
//                break;
//            }
//
//            /* 精确搜索品类 */
//            $data = Product\ListModel::getClassData($condition);

            // 产品吴婷要求这么做,目的是保持性别中的所有性别和当前频道性别一致
            if (isset($condition['gender']) && isset($condition['channel']) && $condition['gender'] === '1,2,3') {
                switch ($condition['channel']) {
                    case 1:
                        $condition['gender'] = '1,3';
                        break;
                    case 2:
                        $condition['gender'] = '2,3';
                        break;
                    default:
                        break;
                }
            }

            $data = self::getSearchData($condition, $showTag, $tagNew, $tagSale);
        } while (false);

        //奥莱商品过滤新品标签
        if (isset($condition['outlets']) && $condition['outlets'] === '1') {
            foreach ($data['new'] as $outletskey => $outletsval) {
                unset($data['new'][$outletskey]['tags']);
            }
        }

        // 是否显示学生价
        if (isset($condition['students'])) {
            $data['students'] = true;
        }

        return $data;
    }

}