Info.php 13.3 KB
<?php

use Action\AbstractAction;
use LibModels\Wap\Guang\DetailData;
use LibModels\Wap\Product\ListData;
use Plugin\Helpers;

/**
 * 逛详情页
 */
class InfoController extends AbstractAction
{

    /**
     * 详情页
     * 
     * @param int id 内容ID
     */
    public function indexAction()
    {
        $id = $this->get('id');

        // 判断参数是否有效, 无效会跳转到错误页面
        if (!is_numeric($id)) {
            $this->error();
        }

        $data = array();
        $data['guangDetail'] = true; // 模板中使用JS的标识
        $data['guang']['id'] = $id;

        // 标识是不是APP访问的
        $isApp = null !== $this->get('app_version', null);
        // 标识是否是微信访问
        $isWeixin = stripos($this->server('HTTP_USER_AGENT', ''), 'MicroMessenger') !== false;

        // WAP上设置头部导航
        if (!$isApp && !$isWeixin) {
            $this->setNavHeader('逛', true, SITE_MAIN);
        }

        // 获取详情内容信息, 异常则跳到错误页面
        $detail = DetailData::package($id, $isApp);
        if (empty($detail['getArticle'])) {
            $this->_view->display('index', $data);
            return;
        }

        // 作者信息数据
        if (isset($detail['getAuthor']['name'])) {
            $data['guang']['author'] = array();
            $data['guang']['author']['avatar'] = $detail['getAuthor']['avatar'];
            $data['guang']['author']['name'] = $detail['getAuthor']['name'];
            $data['guang']['author']['intro'] = $detail['getAuthor']['author_desc'];
            $data['guang']['author']['url'] = $isApp ? $detail['getAuthor']['url'] : Helpers::url('/author/index', array('id' => $detail['getArticle']['author_id']), 'guang');
        }

        $data['detail'] = array();
        $data['detail']['title'] = $detail['getArticle']['article_title'];
        $data['detail']['publishTime'] = $detail['getArticle']['publishTime'];
        $data['detail']['pageView'] = $detail['getArticle']['pageViews'];
        $data['detail']['content'] = array();

        if (!empty($detail['getArticleContent'])) {
            $build = array();
            $good = array();
            $skns = array();
            $product = array();

            foreach ($detail['getArticleContent'] as $value) {
                $build = array();
                // 文字
                if (isset($value['text'])) {
                    $build['text'] = $value['text']['data']['text'];
                }
                // 单张图
                elseif (isset($value['singleImage'])) {
                    $build['bigImage'] = Helpers::getImageUrl($value['singleImage']['data'][0]['src'], 640, 640);
                } elseif (isset($value['smallPic']['data'])) {
                    $imgs = $value['smallPic']['data'];
                    $build['smallImage'] = array(
                        array(
                            'src' => Helpers::getImageUrl($imgs[0]['src'], 315, 420)
                        ),
                        array(
                            'src' => Helpers::getImageUrl($imgs[1]['src'], 315, 420)
                        )
                    );
                }
                // 相关推荐
                elseif (isset($value['goods']['data'])) {
                    $good = array();
                    // 遍历取得SKN
                    $skns = array();
                    foreach ($value['goods']['data'] as $goods) {
                        $skns[] = $goods['id'];
                    }
                    // 通过SKN获取商品信息
                    $product = ListData::productInfoBySkns($skns);
                    if (!empty($product['data']['product_list'])) {
                        foreach ($product['data']['product_list'] as $i => $goods) {
                            // 最多显示4个
                            if ($i > 3) {
                                break;
                            }
                            $good[] = Helpers::formatProduct($goods, false, true, true, 235, 314, $isApp);
                        }
                    }
                    // 没有商品
                    if (!isset($i)) {
                        continue;
                    }
                    // 单个商品
                    elseif ($i === 0 && isset($good[0])) {
                        $build['relatedReco'] = $good[0];
                    }
                    // 多个商品
                    else {
                        $build['relatedReco'] = $good;
                    }
                }
                // 悬停浮动商品
                elseif (isset($value['goodsGroup']['data'])) {
                    foreach ($value['goodsGroup']['data'] as $goods) {
                        $good = array();
                        $good['thumb'] = Helpers::getImageUrl($goods['cover']['cover'], 235, 314);
                        $good['type'] = Helpers::getProductIcon($goods['cover']['maxSortId']);
                        $good['goods'] = array();
                        $skns = array();
                        foreach ($goods['list'] as $mini) {
                            $skns[] = $mini['id'];
                        }
                        // 通过SKN获取商品信息
                        $product = ListData::productInfoBySkns($skns);
                        if (!empty($product['data']['product_list'])) {
                            foreach ($product['data']['product_list'] as $i => $goods) {
                                $good['goods'][] = Helpers::formatProduct($goods, false, true, true, 235, 314, $isApp);
                            }
                            $build['collocation'][] = $good;
                        }
                    }
                }

                $data['detail']['content'][] = $build;
            }
        }

        // 相关品牌
        if (!empty($detail['getBrand'])) {
            $data['relatedBrand'] = $detail['getBrand'];
        }

        // 相关标签
        if (!empty($detail['getArticle']['tags'])) {
            foreach ($detail['getArticle']['tags'] as $value) {
                if (!$isApp) {
                    $value['url'] = Helpers::url('/tags/index', array('query' => $value['name']), 'guang');
                }
                $data['relatedTag'][] = $value;
            }
        }

        // 相关文章
        if (!empty($detail['getOtherArticle'])) {
            foreach ($detail['getOtherArticle'] as $value) {
                if (!$isApp) {
                    $value['url'] = Helpers::url('/info/index', array('id' => $value['id']), 'guang');
                }
                $value['thumb'] = Helpers::getImageUrl($value['thumb'], 279, 175);
                $data['relatedInfo'][] = $value;
            }
        }

        // 分享参数
        if (isset($detail['getArticle']['cover_image'])) {
            $data['shareLink'] = Helpers::url('/info/index', array('id' => $id), 'guang');
            $data['shareTitle'] = $detail['getArticle']['article_title'];
            $data['shareDesc'] = $detail['getArticle']['article_summary'];
            if ($detail['getArticle']['cover_image_type'] == 1) {
                $data['shareImg'] = Helpers::getImageUrl($detail['getArticle']['cover_image'], 640, 640);
            } else {
                $data['shareImg'] = Helpers::getImageUrl($detail['getArticle']['cover_image'], 640, 320);
            }
            $this->setTitle($detail['getArticle']['article_title']);
            $this->setDescription($detail['getArticle']['article_summary']);
        }

        // 标识有微信分享
        $data['hasWxShare'] = true;

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

        $detail = array();
        $data = array();
    }

    /**
     * APP里使用的收藏内容的接口
     * 
     * @param id 资讯ID
     * @param type "fav"表示收藏 或者 "cancel"表示取消收藏
     * @return json
     */
    public function miniAction()
    {
        $id = $this->get('id');

        // 判断参数是否有效, 无效会跳转到错误页面
        if (!is_numeric($id)) {
            $this->error();
        }

        $data = array();
        $data['guangEzine'] = true; // 模板中使用JS的标识
        $data['guang']['id'] = $id;

        // 获取详情内容信息, 异常则跳到错误页面
        $detail = DetailData::packageFoEzine($id, true);
        if (empty($detail['getArticle'])) {
            $this->_view->display('index', $data);
            return;
        }

        $data['detail'] = array();
        $data['detail']['title'] = $detail['getArticle']['article_title'];
        $data['detail']['publishTime'] = $detail['getArticle']['publishTime'];
        $data['detail']['pageView'] = $detail['getArticle']['pageViews'];
        $data['detail']['content'] = array();

        if (!empty($detail['getArticleContent'])) {
            $build = array();
            $good = array();
            $skns = array();
            $product = array();

            foreach ($detail['getArticleContent'] as $value) {
                $build = array();
                // 文字
                if (isset($value['text'])) {
                    $build['text'] = $value['text']['data']['text'];
                }
                // 单张图
                elseif (isset($value['singleImage'])) {
                    $build['bigImage'] = Helpers::getImageUrl($value['singleImage']['data'][0]['src'], 640, 640);
                } elseif (isset($value['smallPic']['data'])) {
                    $imgs = $value['smallPic']['data'];
                    $build['smallImage'] = array(
                        array(
                            'src' => Helpers::getImageUrl($imgs[0]['src'], 315, 420)
                        ),
                        array(
                            'src' => Helpers::getImageUrl($imgs[1]['src'], 315, 420)
                        )
                    );
                }
                // 相关推荐
                elseif (isset($value['goods']['data'])) {
                    $good = array();
                    // 遍历取得SKN
                    $skns = array();
                    foreach ($value['goods']['data'] as $goods) {
                        $skns[] = $goods['id'];
                    }
                    // 通过SKN获取商品信息
                    $product = ListData::productInfoBySkns($skns);
                    if (!empty($product['data']['product_list'])) {
                        foreach ($product['data']['product_list'] as $i => $goods) {
                            // 最多显示4个
                            if ($i > 3) {
                                break;
                            }
                            $good[] = Helpers::formatProduct($goods, false, true, true, 235, 314, true);
                        }
                    }
                    // 没有商品
                    if (!isset($i)) {
                        continue;
                    }
                    // 单个商品
                    elseif ($i === 0 && isset($good[0])) {
                        $build['relatedReco'] = $good[0];
                    }
                    // 多个商品
                    else {
                        $build['relatedReco'] = $good;
                    }
                }
                // 悬停浮动商品
                elseif (isset($value['goodsGroup']['data'])) {
                    foreach ($value['goodsGroup']['data'] as $goods) {
                        $good = array();
                        $good['thumb'] = Helpers::getImageUrl($goods['cover']['cover'], 235, 314);
                        $good['type'] = Helpers::getProductIcon($goods['cover']['maxSortId']);
                        $good['goods'] = array();
                        $skns = array();
                        foreach ($goods['list'] as $mini) {
                            $skns[] = $mini['id'];
                        }
                        // 通过SKN获取商品信息
                        $product = ListData::productInfoBySkns($skns);
                        if (!empty($product['data']['product_list'])) {
                            foreach ($product['data']['product_list'] as $i => $goods) {
                                $good['goods'][] = Helpers::formatProduct($goods, false, true, true, 235, 314, true);
                            }
                            $build['collocation'][] = $good;
                        }
                    }
                }

                $data['detail']['content'][] = $build;
            }
        }

        // 相关品牌
        if (!empty($detail['getBrand'])) {
            $data['relatedBrand'] = $detail['getBrand'];
        }

        // 分享参数
        if (isset($detail['getArticle']['cover_image'])) {
            $data['shareLink'] = Helpers::url('/info/index', array('id' => $id), 'guang');
            $data['shareTitle'] = $detail['getArticle']['article_title'];
            $data['shareDesc'] = $detail['getArticle']['article_summary'];
            if ($detail['getArticle']['cover_image_type'] == 1) {
                $data['shareImg'] = Helpers::getImageUrl($detail['getArticle']['cover_image'], 640, 640);
            } else {
                $data['shareImg'] = Helpers::getImageUrl($detail['getArticle']['cover_image'], 640, 320);
            }
        }

        // 标识有微信分享
        $data['hasWxShare'] = true;

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

        $detail = array();
        $data = array();
    }

}