Item.php 10.8 KB
<?php
use Action\WebAction;
use Product\ItemModel;
use Index\HomeModel;
use WebPlugin\Helpers;
use LibModels\Web\Product\FavoriteData;
class ItemController extends WebAction
{
    public function indexAction()
    {
        $productId = $this->param('productId','');
        if (!is_numeric($productId)) {
            $this->error();
        }
        $goodsId = $this->param('goodsId','');
        if (!is_numeric($goodsId)) {
            $this->error();
        }
        $uid = $this->getUid();
        $vipLevel = -1;
        $sortNames = array();
        if(!empty($this->_vip)) {
            $vipLevel = Helpers::getVipLevel($this->_vip);
        }
        //获取商品信息
        $productInfo = ItemModel::getProductInfo($productId, $goodsId, $uid, $vipLevel);
        if(empty($productInfo)) {
            $this->error();
        }
        $navs = ItemModel::getSortNav($productInfo['goodsInfo']['smallSortId']);
        if(!empty($navs)) {
            foreach($navs as $nav) {
                $sortNames[] = $nav['name'];
            }
        }
        $seo = $this->getSeoByGoodsInfo($productInfo['goodsInfo'], $navs);
        $this->setTitle($seo['title'], true, '|');
        $this->setKeywords($seo['keywords']);
        $this->setDescription($seo['description'], true);
        
        // 除限量商品外, 其余的都往COOKIE里存
        if (!isset($productInfo['goodsInfo']['fashionTopGoods'])) {
            $this->saveCookie($goodsId, $productInfo['goodsInfo']['skn']);
        }
        
        //获取商品尺寸相关
        $sizeInfo = ItemModel::getSizeInfo($productInfo['goodsInfo']['skn'], $productInfo['goodsInfo']['maxSortId']);
        $data = array('productDetailPage' => true, 
                'detail'=> array(), 
                'statGoodsInfo'=> array_merge(
                    array('fullSortName'=> implode('-', $sortNames)), 
                   $productInfo['statGoodsInfo']),
        );

        //设置头部
        $this->setWebNavHeader($productInfo['goodsInfo']['isOutlets']);
        //导航
        if ($productInfo['goodsInfo']['isOutlets'] === 'outlets') {
            $data['detail']['pathNav'] = array(
                array('href' => Helpers::url('/product/outlets'), 'name' => 'OUTLET', 'pathTitle' => 'OUTLET'),// TODO 首页地址
                //array('href' => '/', 'name' => 'OUTLETS', 'pathTitle' => 'OUTLETS'), TODO 奥莱频道
                array('name' => $productInfo['goodsInfo']['name'], 'pathTitle' => $productInfo['goodsInfo']['name']),
            );
        } else {
            $data['detail']['pathNav'] = array_merge(
                array(HomeModel::getHomeChannelNav()),
                $navs,
                array(array('name' => $productInfo['goodsInfo']['name']))
            );
        }
        $data['detail']['latestWalk'] = 5;
        $data['detail'] += $productInfo + $sizeInfo;
        $this->_view->display('index', $data);
    }
    
    /**
     * 显示商品信息
     * 
     * @param int productId 商品ID
     * @param int productSkn 商品SKN
     */
    public function showAction()
    {
        $this->setWebNavHeader();
        
        $productSkn = $this->param('productSkn','');
        if (!is_numeric($productSkn)) {
            $this->error();
        }
        $sortNames = array();
        $uid = $this->getUid();
        $vipLevel = -1;
        if(!empty($this->_vip)) {
            $vipLevel = Helpers::getVipLevel($this->_vip);
        }
        //获取商品信息
        $productInfo = ItemModel::getProductInfo(null, null, $uid, $vipLevel, $productSkn);
        if(empty($productInfo)) {
            $this->error();
        }
        $navs = ItemModel::getSortNav($productInfo['goodsInfo']['smallSortId']);
        if(!empty($navs)) {
            foreach($navs as $nav) {
                $sortNames[] = $nav['name'];
            }
        }
        $seo = $this->getSeoByGoodsInfo($productInfo['goodsInfo'], $navs);
        $this->setTitle($seo['title'], true, '|');
        $this->setKeywords($seo['keywords']);
        $this->setDescription($seo['description'], true);
        //获取商品尺寸相关
        $sizeInfo = ItemModel::getSizeInfo($productInfo['goodsInfo']['skn'], $productInfo['goodsInfo']['maxSortId']);
        $data = array('productDetailPage' => true, 
            'detail'=> array(),
            'statGoodsInfo'=> array_merge(array('fullSortName'=> implode('-', $sortNames)), 
                $productInfo['statGoodsInfo']),
        );
        //导航
        $data['detail']['pathNav'] = array_merge(
                array(HomeModel::getHomeChannelNav()), 
                $navs,
                array(array('name' => $productInfo['goodsInfo']['name']))
            );
        $data['detail']['latestWalk'] = 5;
        $data['detail'] += $productInfo + $sizeInfo;
        $data['uid'] = $uid;
        $data['productId'] = $productInfo['goodsInfo']['productId'];
        $this->_view->display('index', $data);
    }

    /**
     * 收藏商品
     * 
     * return jsonp
     */
    public function togglecollectAction()
    {
        $uid = $this->getUid();
        $productId = $this->post('productId');
        $type = $this->post('type', 'add');//add 添加,canncel取消
        $result = array('code'=> 400 ,'message' =>'收藏失败');
        if(!empty($uid) && is_numeric($productId)) {
            if($type == 'add') {
                $result = FavoriteData::addUidProductFav($uid, $productId);
                if($result['code'] == 413) {
                    $result['message'] = '该商品已经收藏';
                }
            } else if($type == 'cancel') {
                $result = FavoriteData::delUidProductFav($uid, $productId);
            } 
        } else if(empty($uid)) {
            $result['code'] = 403;
            $result['message'] = '用户没有登录';
            $result['data'] = array('url'=> Helpers::url('/signin.html', array()));
        }
        $this->echoJson($result);
    }
    
    /**
     * 获取评论
     */
    public function commentsAction()
    {
        $productId = $this->get('productId', 0);
        $page = $this->get('page', 1);
        $size = $this->get('size', 10);
        $data = ItemModel::getComments($productId, $page, $size);
        $this->echoJson(
            array(
                'code' => 200,
                'data' => $data
            )
        );
    }
    
    /**
     * 获取咨询列表
     */
    public function consultsAction()
    {
        $uid = $this->getUid();
        $productId = $this->get('productId', 0);
        $page = $this->get('page', 1);
        $size = $this->get('size', 10);
        $data = ItemModel::getConsults($uid, $productId, $page, $size);
        $this->echoJson(
            array(
                'code' => 200,
                'data' => $data
            )
        );
    }
    
    /**
     * 获取热区图
     */
    public function hotareaAction()
    {
        $productId = $this->get('productId', 0);
        $data = ItemModel::getProductHotArea($productId);
        $this->_view->display('hotarea', array('hotArea'=> $data));
    }
    
    /**
     * 添加咨询操作
     *
     * @return json
     */
    public function addconsultAction()
    {
        $result = array('code' => 400, 'message' => '请输入咨询内容', 'data' => '');
        if ($this->isAjax()) {
            $uid = $this->getUid();
            $productId = $this->post('productId', 0);//商品ID
            $content = $this->post('content');//咨询内容
            if(!empty($content) && !empty($uid)){
                $result = ItemModel::addConsult($uid, $productId, $content);
            } else if(empty($content)) {
                $result['code'] = 400;
                $result['message'] = '请输入咨询内容';
            }  
            else if(empty($uid)) {
                $result['code'] = 403;
                $result['message'] = '用户没有登录';
                $result['data'] = array('url'=> Helpers::url('/signin.html', array()));
            }
        }
        $this->echoJson($result);
    }
    
    /**
     * 保存商品ID
     * 
     * @param string $productId
     * @param string $skn
     * @param string $goodsId
     */
    private function saveCookie($goodsId, $skn) 
    {
        $browseList = $this->getCookie('_browse');
        $browseSknList = $this->getCookie('_browseskn');
        $goodsList = array();
        $goodsSknList = array();
        //有一个空,都清除
        if(empty($browseList) || empty($browseSknList)) {
            $browseList = '';
            $browseSknList = '';
        }
        if (!empty($browseList)) {
            $goodsList = explode(',', $browseList);
        }
        if(!empty($browseSknList)) {
            $goodsSknList = explode(',', $browseSknList);
        }
        //处理存在
        if(in_array($goodsId, $goodsList) || in_array($skn . '-' . $goodsId, $goodsSknList)) {
            $key = array_search($goodsId, $goodsList);
            if($key !== false) {
                unset($goodsList[$key]);
            }
            $key = array_search($skn . '-' . $goodsId, $goodsSknList);
            if($key !== false) {
                unset($goodsSknList[$key]);
            }
        }
        array_unshift($goodsList, $goodsId);
        array_unshift($goodsSknList, $skn . '-' . $goodsId);
        $browse = implode(',', array_slice($goodsList, 0, 30));
        $browseSkn = implode(',', array_slice($goodsSknList, 0, 30));
        $this->setCookie('_browse', $browse, 2000000000);
        $this->setCookie('_browseskn', $browseSkn, 2000000000);
    }
    
    /**
     * 获取seo信息
     * 
     * @param array $goodsInfo
     * @param array $navs
     * @return array
     */
    private function getSeoByGoodsInfo(&$goodsInfo, &$navs)
    {
        $title = '';
        $keywords = '';
        $brandName = '';
        $sortName = '';
        if(!empty($goodsInfo['brandName'])) {
            $title = $goodsInfo['brandName'] .' ';
            $brandName = $goodsInfo['brandName'];
        }
        if(!empty($navs) && isset($navs[1]['name'])) {
            $sortName = $navs[1]['name'];
            $title.= $navs[1]['name'].'|';
        }
        $title .= $goodsInfo['name'].'正品 ';
        $keywords = $brandName.$sortName.','.$brandName.'官网专卖店,'. $brandName.'官方授权店,'.
        $brandName.'正品,'.$brandName.'打折,'.$brandName.'折扣店,'.$brandName.'真品,'.$brandName.'代购';
        $description = empty($goodsInfo['shareDesc']) ? $goodsInfo['name'] : $goodsInfo['shareDesc'];
        return array('title' => $title, 'keywords'=> $keywords, 'description' =>$description);
    }
}