Search.php 10.4 KB
<?php

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

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

    /**
     * 搜索首页
     */
    public function indexAction()
    {
        // 设置客户端浏览器1小时内不改变
        $this->setLastModified(mktime(date('H'), 0, 0, date('n'), date('j'), date('Y')));
        
        $this->setNavHeader('搜索', true, SITE_MAIN);

        //$this->_view->html('search');
        $this->_view->display('index', array(
            'search' => array('url' => Helpers::url('', null, 'search')),
            'showDownloadApp' => true,
            'searchPage' => true,
            'pageFooter' => true
        ));
    }

    /**
     * 搜索列表页
     */
    public function listAction()
    {
        // 过滤请求参数
        $condition = filter_input_array(INPUT_GET, array(
            'query' => FILTER_DEFAULT,
            'brand' => FILTER_VALIDATE_INT,
            'sort' => FILTER_DEFAULT,
            'msort' => FILTER_VALIDATE_INT,
            'misort' => FILTER_VALIDATE_INT,
            'color' => FILTER_VALIDATE_INT,
            'size' => FILTER_VALIDATE_INT,
            'price' => FILTER_VALIDATE_INT,
            'discount' => FILTER_DEFAULT,
            'gender' => FILTER_DEFAULT,
            'p_d' => FILTER_DEFAULT,), false);

        $query = empty($condition['query']) ? null : strtolower(trim($condition['query']));
        if (isset($condition['discount'])) {
            $condition['p_d'] = rawurldecode($condition['discount']);
            unset($condition['discount']);
        }

        // 标识用户是否有输入搜索内容
        $haveQuery = $query !== null;
        // 标识用户搜的是不是一级品类
        $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;
                }

                /* 模糊查品牌域名 */
                foreach ($brandNames as $key => $domains) {
                    if (strpos($key, $query) !== false) {
                        $domain = $key;
                        break;
                    }
                }
            } while (false);

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

            // 跳转到品牌商品列表页
            if ($domain !== null) {
                $url = Helpers::url('', array(
                            'from' => 'search',
                            'query' => $query
                                ), $domain);
                $this->go($url);
            }
        }

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

            do {
                /* 精确查一级品类 */
                $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 ($isQueryFirstClass) {
            $this->setTitle('全部' . $query);
            $this->setNavHeader('全部' . $query, true, SITE_MAIN);
        }
        // 搜索是二级品类
        elseif ($isQuerySecondClass) {
            $this->setTitle($query);
            $this->setNavHeader($query, true, SITE_MAIN);
        }
        // 搜索其它内容
        else {
            if ($haveQuery) {
                $data['goodList']['search']['default'] = $query;
                $data['goodList']['search']['url'] = Helpers::url('', null, 'search');
            }
            $this->setTitle('搜索');
            $this->setNavHeader('搜索', true, SITE_MAIN);
        }

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

        // 显示底部悬浮下载
        $data['showDownloadApp'] = 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();
//        }

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

    /**
     * Ajax异步筛选请求
     */
    public function searchAction()
    {
        $data = array();

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            /* 过滤请求参数 */
            $condition = filter_input_array(INPUT_GET, array(
                'query' => FILTER_DEFAULT,
                'brand' => FILTER_VALIDATE_INT,
                'sort' => FILTER_DEFAULT,
                'msort' => FILTER_VALIDATE_INT,
                'misort' => FILTER_VALIDATE_INT,
                'color' => FILTER_VALIDATE_INT,
                'size' => FILTER_VALIDATE_INT,
                'price' => FILTER_VALIDATE_INT,
                'discount' => FILTER_DEFAULT,
                'gender' => FILTER_DEFAULT,
                'p_d' => FILTER_DEFAULT,
                'page' => FILTER_VALIDATE_INT,), false);
            // 转义分类
            if (isset($condition['sort'])) {
                $condition['sort'] = rawurldecode($condition['sort']);
            }
            // 转换折扣
            if (isset($condition['discount'])) {
                $condition['p_d'] = rawurldecode($condition['discount']);
                unset($condition['discount']);
            }
            // 转义性别
            if (isset($condition['gender'])) {
                $condition['gender'] = rawurldecode($condition['gender']);
            }
            // 转换排序方式
            $type = $this->get('type', '');
            $order = $this->get('order', 0);
            $condition['order'] = Helpers::transOrder($order, $type);

            // 过滤掉值为空的条件
            $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);

            $data = Product\SearchModel::getSearchData($condition);
        } while (false);

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

    /**
     * Ajax异步获取筛选数据
     * @return array 筛选数据
     */
    public function filterAction()
    {
        $data = array();

        if ($this->isAjax()) {
            // 过滤请求参数
            $condition = filter_input_array(INPUT_GET, array(
                'query' => FILTER_DEFAULT,
                'brand' => FILTER_VALIDATE_INT,
                'sort' => FILTER_DEFAULT,
                'msort' => FILTER_VALIDATE_INT,
                'misort' => FILTER_VALIDATE_INT,
                'color' => FILTER_VALIDATE_INT,
                'size' => FILTER_VALIDATE_INT,
                'price' => FILTER_VALIDATE_INT,
                'discount' => FILTER_DEFAULT,
                'gender' => FILTER_DEFAULT,
                'p_d' => FILTER_DEFAULT,), false);

            // 转义分类
            if (isset($condition['sort'])) {
                $condition['sort'] = rawurldecode($condition['sort']);
            }
            // 转换折扣
            if (isset($condition['discount'])) {
                $condition['p_d'] = rawurldecode($condition['discount']);
                unset($condition['discount']);
            }
            // 转义性别
            if (isset($condition['gender'])) {
                $condition['gender'] = rawurldecode($condition['gender']);
            }

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

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

    /**
     * 模糊搜索指定字符
     * 
     * @return array 模糊搜索的结果
     */
    /* public function fuzzysearch()
      {
      if ($this->isAjax()) {
      $keyword = $this->post('keyword', '');

      $result = SearchData::searchFuzzyDatas($keyword);

      $this->echoJson($result);
      }
      } */
}