Search.php 5.85 KB
<?php

use Action\AbstractAction;
use LibModels\Wap\Product\SearchData;
/**
 * 搜索页
 */
class SearchController extends AbstractAction
{
    /**
     * 搜索首页
     */
    public function indexAction()
    {
        $data = array(
            array(
                'hot' => array(
                    array(
                        'name' => '夹克',
                        'url' => 'm.yohobuy.com'
                    ),
                    array(
                        'name' => '休闲运动鞋',
                        'url' => 'm.yohobuy.com'
                    ),
                    array(
                        'name' => 'Into the Rainbow',
                        'url' => 'm.yohobuy.com'
                    )
                ),
                'history' => array(
                    array(
                        'name' => 'what',
                        'url' => 'm.yohobuy.com'
                    ),
                    array(
                        'name' => 'the',
                        'url' => 'm.yohobuy.com'
                    ),
                    array(
                        'name' => 'fuck',
                        'url' => 'm.yohobuy.com'
                    )
                )
            )
        );
        $this->_view->display('index', array(
            'search' => $data, 
            'searchPage' => true, 
            'pageFooter' => true
        ));
    }
    
    /**
     * 搜索列表页
     */
    public function listAction()
    {
        $query = $this->get('query', null);
        $brand = $this->get('brand', null);
        $gender = $this->getCookie('_Channel', 'boys');
        $p_d = $this->get('p_d', null);
        $misort = $this->get('misort', null);
        $msort = $this->get('msort', null);

        $data = array(
            'pageHeader' => array(
                'navBack' => true,
                'navTitle' => '搜索',
                'navHome' => '/'
            ),
            'goodListPage' => true,
            'goodList' => array(
                'brand' => 0,
                'msort' => 0,
                'gender' => $gender,
                'price' => 0,
                'size' => 0,
                'discount' => ''
            )
        );

        // 首先查询是否属于内置品类
        $classes = ClassModel::getClassesArr();
        $classFlag = array_search($query, $classes);
        if($classFlag !== false)// 属于内部品类
        {
             $data['pageHeader']['navTitle'] = '所有'.$query;
        }

        // 如果存在搜索字符串就显示搜索栏
        if(!is_null($query) && $classFlag === false)
        {
            $data['search'] = array(
                'default' => $query
            );
        }

        // 转换性别
        $this->genderTrans($gender);

        // 查询数据
        $listData = SearchData::searchLiDatas($query, $brand, $gender, $p_d, $misort, $msort);
        // 处理返回的数据
        if (isset($listData['code']) && $listData['code'] === 200) {
            $tmpData = $listData['data'];

            // 如果存在品牌信息就显示品牌字段
            if(isset($tmpData['brand']) && !empty($tmpData['brand']))
            {
                $brandData = $tmpData['brand'];
                $data['brandWay'] = array(
                    'url' => '/product/list/brand?brand='.$brandData['id'],
                    'thumb' => Helpers::getImageUrl($brandData['brand_ico'], 75, 40),
                    'name' => $brandData['brand_name']
                );

                // 设置品牌默认值
                $data['goodList']['brand'] = $brandData['id'];
            }

            $data['goodList'] += ListProcess::getListData($tmpData);
        }

        $this->_view->display('list', $data);
    }
    
        /**
     * Ajax异步筛选请求
     */
    public function searchAction()
    {
        if($this->isAjax())
        {
            $query = $this->get('query', null);
            $brand = $this->get('brand', null);
            $gender = $this->get('gender', null);
            $color = $this->get('color', null);
            $size = $this->get('size', null);
            $price = $this->get('price', null);
            $p_d = $this->get('discount', null);
            $sort = $this->get('msort', null);

            // 转换性别
            $this->genderTrans($gender);

            // 转换排序方式
            $order = $this->get('order', null);
            $type = $this->get('type', '');
            switch ($type) {
                case 'price':
                    $order = ($order == 0) ? 's_p_desc' : 's_p_asc';
                    break;
                case 'discount':
                    $order = ($order == 0) ? 'p_d_desc' : 'p_d_asc';
                    break;
                case 'newest':
                default:
                    $order = ($order == 0) ? 's_t_desc' : 's_t_asc';
                    break;
            }

            $data = array();
            // 查询数据
            $listData = SearchData::searchLiDatas($query, $brand, $gender, $color, $size, $price, $p_d, $sort, $order);
            // 处理返回的数据
            if (isset($listData['code']) && $listData['code'] === 200) {
                $tmpData = $listData['data'];

                unset($tmpData['filter']);// 不要筛选条件的数据
                $data = ListProcess::getListData($tmpData);
            }

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

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

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

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