Search.php 10.5 KB
<?php

use Action\AbstractAction;
use LibModels\Wap\Product\SearchData;
use LibModels\Wap\Home\UserData;
use Plugin\DataProcess\ListProcess;
use Plugin\Helpers;

/**
 * 搜索页
 */
class SearchController extends AbstractAction
{

    /**
     * 搜索首页
     */
    public function indexAction()
    {
        $this->setNavHeader('搜索', true, SITE_MAIN);
        //热门搜索和默认搜索
        $termsData = SearchData::getTerms();
        //默认搜索
        $defaultTerms = empty($termsData['defaultTerms'][0]['content']) ? '' : $termsData['defaultTerms'][0]['content'];
        //热门搜索
        $hotTerms = empty($termsData['hotTerms']) ? array() : $termsData['hotTerms'];
        
        $data = array(
            'search' => array('url' => Helpers::url('', null, 'search')),
            'showDownloadApp' => true,
            'defaultTerms' => $defaultTerms,
            'hotTerms' => $hotTerms,
            'searchPage' => true,
            'pageFooter' => true
        );

        $this->_view->display('index', $data);
    }

    /**
     * 搜索列表页
     *
     */
    public function listAction()
    {
        $uid = $this->getUid() ? $this->getUid() : $this->get('uid');
        $isApp = $this->isApp();
        // 过滤请求参数
        $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'];
        }
        $query = empty($condition['query']) ? null : strtolower(trim($condition['query']));
        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'] = rawurlencode($condition['query']);
        }



        // 标识用户是否有输入搜索内容
        $haveQuery = !empty($query);

        // 标识用户搜的是不是一级品类
        $isQueryFirstClass = false;
        // 标识用户搜的是不是二级品类
        $isQuerySecondClass = false;

        /* 判断是不是品牌, 是品牌跳到品牌列表页(显示搜索框) */
        if ($haveQuery) {
            $domain = null;
            $brandNames = Product\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);
                $this->go($url);
            }
        }

        /* 判断是不是品类, 是品类加导航标题(不显示搜索框) */
        if ($haveQuery) {
            $classNames = Category\ClassModel::getClassNames();

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

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

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

            $classNames = array();
        } else {
            $condition['query'] = '';
        }

        $data = array();
        $data['goodListPage'] = true;
        $data['goodList'] = $condition;
        $data['query'] = $query;

        // 用户自定义商品类目
        if(isset($condition['filter_poolId'])){
            $data['filter_poolId'] = rawurldecode($condition['filter_poolId']);
        }
        
        do {
            if ($isApp) {
                break;
            }
            // 搜索是一级品类
            if ($isQueryFirstClass) {
                $this->setTitle('全部' . $query);
                $this->setNavHeader('全部' . $query, true, SITE_MAIN);
            } // 搜索是二级品类
            elseif ($isQuerySecondClass) {
                $this->setTitle($query);
                $this->setNavHeader($query, true, SITE_MAIN);
            } // 搜索其它内容
            else {
                $from = $this->get('from');
                if ($haveQuery || $from) {
                    $data['goodList']['search']['default'] = empty($query) ? false : $query;
                    $data['goodList']['search']['url'] = Helpers::url('', null, 'search');
                }
                $this->setTitle('搜索');
                $this->setNavHeader('搜索', true, SITE_MAIN);
            }
        }while(false);

        // 右下角的购物车链接
        $data['goodList']['cartUrl'] = Helpers::url('/cart/index/index', null);

        // 显示底部悬浮下载
        $data['showDownloadApp'] =  $this->isApp() ? false : true;

        // 显示页面底部登录信息
        $data['pageFooter'] =true;

        // 查询数据
        if (!isset($condition['query'])) {
            $data['goodList'] += Product\ListModel::getClassData($condition);
        } else {
            $listData = SearchData::searchByCondition($condition);
            // 处理返回的数据
            if (!empty($listData['data']['brand'])) {
                $brandData = $listData['data']['brand'];
                $data['brandWay'] = array(
                    'url' => 'http://' . $brandData['brand_domain'] . SUB_DOMAIN,
                    'thumb' => Helpers::getImageUrl($brandData['brand_ico'], 75, 40),
                    'name' => $brandData['brand_name']
                );
                // 设置品牌默认值
                $data['goodList']['brand'] = $brandData['id'];
                $data['goodList'] += ListProcess::getListData($listData['data']);
            }
            $listData = array();
        }


        if (isset($condition['title'])) {
            if ($isApp) {
                $this->setTitle($condition['title'], false, '');
            } else {
                $this->setNavHeader($condition['title'], true, SITE_MAIN);
            }
        }

        // 搜索列表将最新改成默认的标识
        $data['isSearch'] = true;

        if (isset($condition['students'])) {
            // 获取用户信息判断是否为学生
            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,用于显示下方的提示
                $appVersion = str_replace('.', '', $this->get('app_version'));
                if (intval(substr($appVersion, 0, 3)) < 491) {
                    $data['oldApp'] = true;
                }
            }
        }


        $this->_view->display('list', $data);
    }

    /**
     * Ajax异步筛选请求
     */
    public function searchAction()
    {
        header('Access-Control-Allow-Origin:*');

        $data = \Product\SearchModel::search();

        if (empty($data['new'])) {
            echo ' ';
        } else {
            $this->_view->display('page', $data);
        }
    }

    /**
     * Ajax异步获取筛选数据
     * @return array 筛选数据
     */
    public function filterAction()
    {
        header('Access-Control-Allow-Origin:*');

        $data = Product\SearchModel::filter();

        if (empty($data)) {
            echo ' ';
        } else {
            $this->_view->display('filter', $data);
        }
    }

    /**
     * 模糊搜索提供的关键词
     */
    public function fuzzyDatasAction()
    {
        $keyword = $this->get('keyword', '');
        $data = SearchData::searchFuzzyDatas($keyword);
        $this->echoJson($data);
    }

}