Info.php 13.5 KB
<?php

namespace Guang;

use Api\Yohobuy;
use LibModels\Web\Guang\InfoData;
use LibModels\Web\Product\SearchData;
use Plugin\Images;
use WebPlugin\Helpers;
use WebPlugin\HelperSearch;


/**
 * 逛-文章页
 */
class InfoModel
{
    /**
     * 获取详情页数据
     * @param $id 文章id
     * @param $uid 用户id
     * @param $udid 客户端标示
     * @param $page 评论分页
     * @param $gender 性别
     * @param $channel 频道
     * @param $limit 评论每页显示条数
     * @return array
     */
    public static function articleInfo($id, $uid, $udid, $page, $gender, $channel, $limit)
    {
        $result = array();
        //文章相关信息
        $articleInfo = InfoData::getArticleInfo($id);
        if (isset($articleInfo['code']) && $articleInfo['code'] == 200 && isset($articleInfo['data']) && !empty($articleInfo['data'])) {
            //作者
            if (isset($articleInfo['data']['author_id']) && !empty($articleInfo['data']['author_id'])) {
                $url['author'] = InfoData::author($articleInfo['data']['author_id'], true);
            }
            //文章内容
            $url['contents'] = InfoData::articleContent($id, true);
            //热门标签
            $url['hotTags'] = InfoData::tagTop(1, 20, true);
            //评论
            $url['comments'] = InfoData::comment($id, $page, $limit, true);
            //基本信息
            $url['baseInfo'] = InfoData::baseInfo($id, $uid, $udid, true);
            //相关品牌
            $url['brands'] = InfoData::relateBrand($id, true);
            //相关文章
            if (isset($articleInfo['data']['tag']) && !empty($articleInfo['data']['tag'])) {
                $url['relate'] = InfoData::relateList($id, $articleInfo['data']['tag'], 3, true);
            }
            //推荐文章
            $url['recommend'] = InfoData::recommend($gender, 1, 10, true);
            $data = Yohobuy::getMulti($url);
            $result = self::formatInfo($articleInfo['data'], $data, $channel, $limit);
        }
        return $result;
    }

    /**
     * 格式化逛详情页数据
     * @param $article 文章信息
     * @param $data 作者信息、文章内容、热门标签、评论、相关文章、推荐文章
     * @param $channel 频道
     * @param $limit 评论一页显示记录数
     * @return array
     */
    public static function formatInfo($article, $data, $channel, $limit)
    {
        $result['content'] = $other = array();
        //推荐商品列表
        $goods = $goodsGroup =  array();
        $result['pathNav'] = self::getCenterCrumb($article['article_title'], $channel);
        //文章头部信息
        $result['header']['title'] = $article['article_title'];
        $result['header']['avatar'] = isset($data['author']['avatar']) && !empty($data['author']['avatar']) ? $data['author']['avatar'] : '';
        $result['header']['name'] = isset($data['author']['name']) && !empty($data['author']['name']) ? $data['author']['name'] : '';
        $result['header']['authorUrl'] = isset($data['author']['name']) && !empty($data['author']['name']) ? Helpers::url('/Index/editor', array('author_id'=>$article['author_id']), 'guang') : '';
        $result['header']['intro'] = isset($data['author']['author_desc']) && !empty($data['author']['author_desc']) ? $data['author']['author_desc'] : '';
        $result['header']['time'] = date('Y年m月d日 H:i', $article['publish_time']);
        $result['header']['click'] = $article['pageViews'];
        $result['header']['commentNum'] = $data['comments']['total'];
        $result['header']['shareImg'] = Images::getImageUrl($article['cover_image'], 600, 600);
        $result['header']['desc'] = $article['article_summary'];
        $result['header']['weixinUrl'] = $article['url'];
        //文章详细
        if (isset($data['contents']) && !empty($data['contents'])) {
            foreach ($data['contents'] as $key => $val) {
                foreach ($val as $type => $con) {
                    if ($type === 'goods') {
                        $goods = array_merge_recursive($goods, self::goods($con['data']));
                    } elseif ($type === 'goodsGroup') {
                        $goodsGroup = array_merge_recursive($goodsGroup, self::goodsGroup($con['data']));
                    } else {
                        $other = is_callable("self::$type") ? self::$type($con['data']) : '';
                        $result['content'] = array_merge_recursive($result['content'], $other);
                    }
                }
            }
        }
        if (!empty($goods)) {
            $result['content']['relatedReco'] = array('recos'=>$goods);
        }
        if (!empty($goodsGroup)) {
            $result['content']['relatedReco'] = $goodsGroup;
        }
        //赞、收藏
        if (isset($data['baseInfo']) && !empty($data['baseInfo'])) {
            $result['userInfo']['isLike'] = $data['baseInfo']['isPraise'] == 'Y' ? true : false;
            $result['userInfo']['likeNum'] = $data['baseInfo']['praise_num'];
            $result['userInfo']['isCollected'] = $data['baseInfo']['isFavor'] == 'Y' ? true : false;;
        }
        //文章tags
        if (isset($article['tags']) && !empty($article['tags'])) {
            foreach ($article['tags'] as $tagKey => $tagsValue) {
                $result['tag'][$tagKey]['name'] = $tagsValue['name'];
                $result['tag'][$tagKey]['url'] = Helpers::url('/tags/index', array('query' => $tagsValue['name']),'guang');
            }
        }
        //相关品牌
        if (isset($data['brands']) && !empty($data['brands'])) {
            $result['brands'] = $data['brands'];
        }
        //相关文章
        if (isset($data['relate']) && !empty($data['relate'])) {
            foreach ($data['relate'] as $key => $val) {
                $result['relatedPost'][$key]['title'] = $val['title'];
                $result['relatedPost'][$key]['thumb'] = Images::getImageUrl($val['thumb'], 264, 173, 1);
                $result['relatedPost'][$key]['url'] = Helpers::url('/'.$val['id'].'.html', array(), 'guang');
            }
        }
        //精彩推荐
        if (isset($data['recommend']) && !empty($data['recommend'])) {
            foreach ($data['recommend'] as $key => $val) {
                $result['exRecos'][$key]['title'] = $val['title'];
                $result['exRecos'][$key]['img'] = isset($val['src']) ? Images::getImageUrl($val['src'], 90, 60) : '';
                $result['exRecos'][$key]['url'] = Helpers::url('/'.$val['id'].'.html', array(), 'guang');
            }
        }
        //热门标签
        if (isset($data['hotTags']) && !empty($data['hotTags'])) {
            foreach ($data['hotTags'] as $key => $val) {
                $result['hotTags'][$key]['tagName'] = $val['tag_name'];
                $result['hotTags'][$key]['url'] = Helpers::url('/tags/index',array('query'=>$val['tag_name']), 'guang');
            }
        }
        //评论
        if (isset($data['comments']) && !empty($data['comments']) && isset($data['comments']['total'])) {
            $result['comment']['commentNum'] = $data['comments']['total'];
            $result['comment']['commentPager'] = HelperSearch::pager($data['comments']['total'], $limit);
            if ($data['comments']['total'] > 0 && !empty($data['comments']['list'])){
                foreach ($data['comments']['list'] as $key => $val) {
                    $result['comment']['list'][$key]['avatar'] = Images::getImageUrl($val['avator'], 100, 100);
                    $result['comment']['list'][$key]['name'] = $val['username'];
                    $result['comment']['list'][$key]['content'] = $val['content'];
                    $result['comment']['list'][$key]['time'] = $val['create_time'];
                }
            }
        }
        return $result;
    }

    /**
     * 逛详情文本
     * @param $con
     * @return mixed
     */
    public static function text($con)
    {
        return $con;
    }

    /**
     * 逛详情单张图片
     * @param $con
     * @return array
     */
    public static function singleImage($con)
    {
        return array('pic' => Images::getImageUrl($con[0]['src'], 640, 640));
    }

    /**
     * 逛详情商品组
     * @param $goodsGroup
     * @return mixed
     */
    public static function goodsGroup($goodsGroup)
    {
        $result = $groupSkn = array();
        foreach ($goodsGroup as $key => $val) {
            if (!isset($val['list']) || empty($val['list'])) {
                continue;
            }
            $productSkn = array();
            foreach ($val['list'] as $k => $v) {
                $groupSkn[$key][]  = $v['id'];
                $productSkn[]  = $v['id'];
            }
            if (!empty($productSkn)) {
                $product =  SearchData::searchElasticByCondition(array('query' => implode(',', $productSkn)));
                if (isset($product['data']['product_list'])) {
                    $result[$key]['recos'] = array_slice(self::formatProduct($product['data']['product_list']), 0, 4);
                }
            }
        }
        return $result;
    }

    /**
     * 逛详情商品列表
     * @param $goods
     * @return array
     */
    public static function goods($goods)
    {
        $result = $productSkn = array();
        foreach ($goods as $key => $val) {
            $productSkn[] = $val['id'];
        }
        if (!empty($productSkn)) {
            $product =  SearchData::searchElasticByCondition(array('query'=> implode(',', $productSkn)));
            if (isset($product['data']['product_list'])) {
                $result = self::formatProduct($product['data']['product_list']);
            }
        } else {
            return array();
        }
        return  $result;
    }

    /**
     * 逛详情两张小图
     * @param $pic
     * @return array
     */
    public static function smallPic($pic)
    {
        $result = array();
        foreach ($pic as $key => $val) {
            if ($key < 2) {
                $result['smallPic'][] = Images::getImageUrl($val['src'], 600, 600);
            }
        }
        return $result;
    }

    /**
     * 逛详情链接
     * @param $link
     * @return array
     */
    public static function link($link)
    {
        $result = array();
        return $result;
    }

    /**
     * 逛-面包屑
     * name 当前停留面包屑名称
     * nav 逛 之后的面包屑
     */
    public static function getCenterCrumb($name, $channel)
    {
        //根据频道判断home页地址
        switch ($channel) {
            case 'boys':
                $home = Helpers::url('', '', 'default');
                break;
            case 'girls':
                $home = Helpers::url('/woman', '', 'new');
                break;
            case 'lifestyle':
                $home = Helpers::url('/lifestyle', '', 'new');
                break;
            case 'kids':
                $home = Helpers::url('/kids', '', 'new');
                break;
            default:
                $home = Helpers::url('', '', 'default');
                break;
        }
        $retNav = array(
            array(
                'name' => strtoupper($channel) . '首页',
                'href' => $home
            ),
            array(
                'name' => '逛',
                'href' => Helpers::url('', '', 'guang')
            )
        );
        $retNav[] = array(
            'name' => $name
        );
        return $retNav;
    }

    /**
     * 文章评论列表
     * @param $id
     * @param $page
     * @param $limit
     * @return array
     */
    public static function commentList($id, $page, $limit)
    {
        $result = array();
        $data = InfoData::comment($id, $page, $limit);
        //评论
        if (isset($data['code']) && $data['code'] == 200) {
            $result['comment']['commentNum'] = $data['data']['total'];
            $result['comment']['commentPager'] = HelperSearch::pager($data['data']['total'], $limit);
            if ($data['data']['total'] > 0 && !empty($data['data']['list'])){
                foreach ($data['data']['list'] as $key => $val) {
                    $result['comment']['list'][$key]['avatar'] = Images::getImageUrl($val['avator'], 100, 100);
                    $result['comment']['list'][$key]['name'] = $val['username'];
                    $result['comment']['list'][$key]['content'] = $val['content'];
                    $result['comment']['list'][$key]['time'] = $val['create_time'];
                }
            }
        }
        return $result;
    }

    /**
     * 格式化推荐商品
     * @param $product
     * @return arr
     */
    public static function formatProduct($product)
    {
        $result = array();
        foreach ($product as $key => $val) {
            $result[$key]['thumb'] = Images::getImageUrl($val['default_images'],235,314);
            $result[$key]['name'] = $val['product_name'];
            //市场价不等于售价时显示
            if ($val['market_price'] != $val['sales_price']) {
                $result[$key]['marketPrice'] = $val['market_price'];
            }
            $result[$key]['salePrice'] = $val['sales_price'];
            $result[$key]['url'] = Helpers::getUrlBySkc($val['product_id'], $val['goods_list'][0]['goods_id'], $val['cn_alphabet']);;
        }
        return $result;
    }

}