Index.php 8.34 KB
<?php

use Action\WebAction;
use LibModels\Web\Product\FavoriteData;
use Product\BrandsModel;
use Product\HotrankModel;
use Index\HomeModel;
use WebPlugin\Helpers;
use LibModels\Web\Product\BrandData;

class IndexController extends WebAction
{

    /**
     * 品牌首页
     */
    public function brandAction()
    {
        //品牌域名,没有获取到品牌域名的跳转首页
        $domain = $this->param('named');
        if (empty($domain)) {
            $this->go(SITE_MAIN);
        }
        //根据品牌域名获取品牌id(同时判断品牌域名是否有效),无效跳转首页
        $type = 1;
        $result = BrandsModel::getBrandByDomain($domain, $type);
        if (!$result) {
            $this->go(SITE_MAIN);
        }

        //获取uid
        $uid = $this->getUid();

        //传品牌ID参数
        $condition = array();
        $condition['brand'] = isset($result['brandId']) ? $result['brandId'] : '';
        $order = $this->get('order');
        if (empty($order)) {
            $condition['order'] = 'brand_desc';
        }

        //品牌系列参数
        $condition['folder_id'] = $this->get('folder_id');
        // 品牌型类
        $condition['series_id'] = $this->get('series');

        //$options参数数组
        $options = array();
        $options['brandName'] = $domain;//品牌域名
        $options['uid'] = $uid;
        $options['brandId'] = isset($result['brandId']) ? $result['brandId'] : '';
        $options['node'] = isset($result['node']) ? $result['node'] : '';//左侧广告node
        $options['brandBanner'] = isset($result['brandBanner']) ? $result['brandBanner'] : '';//品牌banner
        $options['brandNameEn'] = isset($result['brandNameEn']) ? $result['brandNameEn'] : '';//品牌英文名
        $options['brandNameCn'] = isset($result['brandNameCn']) ? $result['brandNameCn'] : '';//品牌中文名
        $options['reviewNum'] = 6;//底部浏览记录显示个数
        $options['controller'] = 'Index';//说明当前控制器名
        $options['action'] = 'brand';//说明当前方法名

        //调用模型获得品牌页数据
        $data = BrandsModel::getBrandSearchData($condition, $options);

        //统计前三个商品
        $skn=array();
        $arr=array();
        if (isset($data['goods']) && !empty($data['goods'])) {
            $arr = array_slice($data['goods'],0,3);
            foreach ($arr as $key=>$value) {
                $skn[] = $value['skn'];
            }
            $arr = array('skn'=>$skn);
        }

        $seo = $data['seo'];
        $data = array(
            //初始化js
            'brandPage' => true,
            'list' => $data,
            'criteo' => $arr
        );
        $this->setBrandSeo($seo);
        $this->setWebNavHeader();
        //渲染模板
        $this->_view->display('list', $data);
    }


    /**
     * 异步请求水牌数据
     */
    public function getNodeContentAction()
    {
        if (!$this->isAjax()) {
            return '';
        }
        $node = $this->post('node');
        //$mode = $this->post('mode');
        if (!isset($node)) {
            return '';
        }
        $nodeContent = BrandData::getByNodeContent($node);

        if (isset($nodeContent['code']) && $nodeContent['code'] == 200) {
            echo '<br>', $nodeContent['data'];
        }
    }

    /**
     * 异步请求品牌系列
     */
    public function getAdnavAction()
    {
        if (!$this->isAjax()) {
            return array();
        }
        $brandId = $this->post('brandId');
        //$status = $this->post('status');
        if (!isset($brandId)) {
            return array('code'=>201,'brandId is null');
        }
        $result = BrandsModel::getAdNav($brandId);

        $this->echoJson($result);
    }


    /**
     * 品牌介绍页
     */
    public function aboutAction()
    {
        //品牌域名
        $domain = $this->param('named');
        if (empty($domain)) {
            $this->go(SITE_MAIN);
        }
        $uid = $this->getUid();


        //根据品牌域名获取品牌id(同时判断品牌域名是否有效),无效跳转首页
        $type = 2;
        $result = BrandsModel::getBrandByDomain($domain, $type);
        if (!$result) {
            $this->go(SITE_MAIN);
        }

        //$options参数数组
        $options = array();
        $options['brandName'] = $domain;//品牌域名
        $options['uid'] = $uid;
        $options['brandId'] = isset($result['brandId']) ? $result['brandId'] : '';//品牌id
        $options['brandBanner'] = isset($result['brandBanner']) ? $result['brandBanner'] : '';//品牌banner
        $options['brandAbout'] = isset($result['brandAbout']) ? $result['brandAbout'] : '';//品牌介绍数据
        $options['channel'] = HomeModel::getSwitchChannel();//获取当前频道
        $options['brandNameEn'] = isset($result['brandNameEn']) ? $result['brandNameEn'] : '';//品牌英文名
        $options['brandNameCn'] = isset($result['brandNameCn']) ? $result['brandNameCn'] : '';//品牌中文名

        //调用模型获得数据
        $data = BrandsModel::getBrandIntro($options);
        $data = array(
            //初始化js
            'searchListPage' => true,
            'list' => $data
        );
        $this->setTitle($options['brandNameCn'] . $options['brandNameEn'], true, '|');
        $this->setWebNavHeader();
        //渲染模板
        $this->_view->display('list', $data);
    }

    /**
     * 收藏品牌ajax请求
     */
    public function favoriteBrandAction()
    {
        if (!$this->isAjax()) {
            return;
        }
        $result = array(
            'code' => 400,
            'message' => '操作失败'
        );
        $uid = $this->getUid();
        $brandId = $this->post('brandId');
        do {
            if (!$uid) {
                $result = array(
                    'code' => 403,
                    'message' => '用户ID不存在',
                    'data' => array('url' => Helpers::url('/signin.html', array())),
                );
                break;
            }
            if ($uid && $brandId) {
                //调用接口收藏或取消收藏
                $result = FavoriteData::changeFavoriteBrand($uid, $brandId);
                break;
            }
        }
        while (false);
        //返回json格式
        $this->echoJson($result);

    }
    
    /**
     * 判断品牌是否收藏
     */
    public function isFavoriteBrandAction()
    {
        if (!$this->isAjax()) {
            return;
        }
        $uid = $this->getUid();
        $brandId = $this->post('brandId');
        if ($uid && $brandId) {
            //调用接口收藏或取消收藏
            $result = FavoriteData::isFavoriteBrand($uid, $brandId);
            $this ->echoJson($result);
        }
    }


    /**
     * 设置品牌页面的SEO信息
     *
     * @return void
     */
    private function setBrandSeo($seo)
    {
        $this->setTitle($seo['title']);
        $this->setKeywords($seo['keywords']);
        $this->setDescription($seo['description']);
    }

    /**
     * 热销排行
     */
    public function hotrankAction()
    {
        //获取频道
        $channel = HotrankModel::getChannelResource();
        $this->setWebNavHeader();
        $this->setTitle('热销排行|YOHO!BUY有货');
        $page = $this->get('page', 1);
        $sort = $this->get('sid', 1);
        $data = array('hotrankPage' => true,
            'footerTop' => true,
            'hotrank' => HotrankModel::getHotrankResource($channel, $sort, $page)
        );

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

    /*
     * 一周热卖加载更多
     */

    public function getdataAction()
    {
        $page = $this->get('page', 1);
        //加载到100个以后停止
        if ($page > 2) {
            echo json_encode(array('code' => 201, 'data' => ''));
            exit;
        }
        $sid = $this->get('sid', 1);
        //获取频道资源
        $channel = HotrankModel::getChannelResource();
        //获取一周热卖资源
        $data = HotrankModel::getListData($channel, $sid, $page);
        $this->echoJson($data);
    }


}