Detail.php 5.72 KB
<?php

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

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

    /**
     * 详情页
     * 
     * @param int id 内容ID
     */
    public function indexAction()
    {
        $id = $this->get('id');
        
        // 判断参数是否有效, 无效会跳转到错误页面
        if (!is_numeric($id)) {
            $this->error();
        }
        
        // 获取详情内容信息, 异常则跳到错误页面
        $detail = DetailData::package($id);
        if (empty($detail['getArticle'])) {
            $this->error();
        }
        
        $data = array();
        $data['guangDetail'] = true; // 模板中使用JS的标识
        $data['guang']['id'] = $id;
        
        // 作者信息数据
        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'] = '/guang/list/editor?id=' . $detail['getArticle']['author_id'];
        }

        $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'];
                    $data['detail']['content'][] = $build;
                } 
                // 单张图
                elseif (isset($value['singleImage'])) {
                    $build['bigImage'] = Helpers::getImageUrl($value['singleImage']['data'][0]['src'], 640, 640);
                    $data['detail']['content'][] = $build;
                }
                // 相关推荐
                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);
                        }
                    }
                    // 没有商品
                    if (!isset($i)) {
                        continue;
                    }
                    // 单个商品
                    elseif ($i === 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);
                            }
                            $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) {
                $value['url'] = '/guang/list/tag?query=' . $value['name'];
                $data['relatedTag'][] = $value;
            }
        }
        
        // 相关文章
        if (!empty($detail['getOtherArticle'])) {
            foreach ($detail['getOtherArticle'] as $value) {
                $value['url'] = '/guang/detail/index?id=' . $value['id'];
                $data['relatedInfo'][] = $value;
            }
        }

        $this->_view->display('index', $data);
        
        $detail = array();
        $data = array();
    }
}