Index.php 7.56 KB
<?php
namespace Guang;
use LibModels\Web\Home\IndexData;
use WebPlugin\Cache;
use WebPlugin\Images;
use LibModels\Web\Guang\ListData;
use WebPlugin\Helpers;
use LibModels\Web\Guang\InfoData;
use Configs\WebCacheConfig;
class IndexModel
{
    //boys的逛的banner
    const CODE_BOYS_GUANG = '15a288635c2ed9f6c807417be90f5d2d';
    //girls的逛的banner
    const CODE_GIRLS_GUANG = 'e14e12e35af8626650979f7af8a0de2b';
    //lifestyle
    const CODE_LIFESTYLE_GUANG = '1d398e899f759c6d88971be680521a6f';
    //kids
    const CODE_KIDS_GUANG = 'ad14ee01ad048ce308aa3ca416133d2a';
    //boys的逛的ads
    const CODE_BOYS_GUANG_ADS = '41777aa7ac86347692c5aa0d394b2f59';
    const CODE_GIRLS_GUANG_ADS = '722253573823ebb994e313e71b0a4fb9';
    const CODE_LIFESTYLE_GUANG_ADS = '02568b6042510e4be739cc688dc7d6ae';
    const CODE_KIDS_GUANG_ADS = '1ffdd6ea22c58af52ee6408cd353c2d5';
    
    
    /**
     * 获取各频道的banner
     * 
     * @param string $channel
     * @return array
     */
    public static function getBanner($channel)
    {
        $contentCode = self::CODE_BOYS_GUANG;
        $channels = array(
            'boys' => self::CODE_BOYS_GUANG,
            'girls'=> self::CODE_GIRLS_GUANG,
            'lifestyle'=> self::CODE_LIFESTYLE_GUANG,
            'kids'=> self::CODE_KIDS_GUANG
        );
        if(isset($channels[$channel])) {
            $contentCode = $channels[$channel];
        }
        $key = WebCacheConfig::KEY_WEB_GUANG_BANNER_DATA.'_'.$contentCode;
        $data = Cache::get($key);
        if(empty($data)) {
            $resource = IndexData::getResourceData($contentCode);
            if (isset($resource['data']) && !empty($resource['data']) && $resource['code'] == 200) {
                $list = current($resource['data']);
                foreach($list['data'] as $val) {
                    $data [] = array( 'img' => Images::getImageUrl($val['src'], 830, 327, 1),
                        'url' => Helpers::getUrlSafe($val['url'])); 
                }
                //格式化数据
                Cache::set($key, $data, 3600);
            }
        }
        //master没有数据,资源位没有数据, 取二层缓存
        if(empty($data)) {
            $data = Cache::get($key, 'slave');
        }
        return $data;
    }
    
    /**
     * 获取所有分类
     * 
     * @param string $currentSortId
     * @return array
     */
    public static function getCategory($currentSortId = 0)
    {
        $ret = array();
        $data = ListData::category();
        foreach($data['data'] as $category)
        {
            // if ($category['name'] == '专题') {
            //     continue;
            // }
            
            $ret[] = array('typeId' => $category['id'],'type' => $category['name'],
                'isActive' => $currentSortId == $category['id'] ? true :false,
                'navUrl' => Helpers::url('/index/index', array('type'=> $category['id']),'guang'));
        }
        return $ret;
    }
    
    /**
     * 获取分类列表
     * 
     * @param int $sortId
     * @param string $gender
     * @param string $page
     * @param int $limit
     * @param string $udid
     */
    public static function article($gender, $sortId, $uid = 0, $udid = '', $page = 1, $tag = null, $authorId = null, $limit = null)
    {
        $total = 0;
        $article = ListData::article($gender, $sortId, $uid, $udid, $page, $tag, $authorId, $limit);
        $articleList = array();
        if (!empty($article['data']['list']['artList'])) {
            foreach ($article['data']['list']['artList'] as $articleItem) {
                $articleList[] = Helpers::formatArticle($articleItem, true, false, false, $uid);
            }
        }
        
        $adlist = array();
        if (!empty($article['data']['list']['adlist'])) {
            foreach ($article['data']['list']['adlist'] as $ad) {
                $adlist[] = Helpers::formatAd($ad, true, false, false, $uid);
            }
        }
        
        if(!empty($article['data']['total'])) {
            $total = intval($article['data']['total']);
        }
        return array('msgs' => $articleList, 'ads' => $adlist, 'total' => $total);
    }
    
    /**
     * 获取热门标签
     * 
     * @return array
     */
    public static function getHotTags($page, $limit)
    {
        $tags = array();
        $data = InfoData::tagTop($page, $limit);
        if(!empty($data['data'])) {
            foreach($data['data'] as $tag) {
                $tags[] = array('tagName' => $tag['tag_name'],
                         'url'=> Helpers::url('/tags/index',array('query' => $tag['tag_name']),'guang'),
                );
            }
        }
        return $tags;
    }
    
    /**
     * 获取精彩推荐
     * 
     * @param string $gender
     * @param int $page
     * @param int $limit
     * @return array
     */
    public static function getExcellectRecos($gender, $page, $limit)
    {
        $list = array();
        $data = InfoData::recommend($gender, $page, $limit);
        if(!empty($data['data'])) {
            foreach($data['data'] as $key => $article) {
                $list[] = array(
                    'url' => Helpers::getArticleUrl($article['url'], $article['id']),  //Helpers::url('/'.$article['id'].'.html', '', 'guang'),
                    'title' => $article['title']
                );
                if (isset($article['src']) && !empty($article['src'])) {
                    $list[$key]['img'] = Images::getImageUrl($article['src'], 90, 60, 1);
                }
            }
        }
        return $list;
    }
    
    /**
     * 获取广告
     * 
     * @param string $channel
     * @return array
     */
    public static function getAds($channel)
    {
        $contentCode = self::CODE_BOYS_GUANG_ADS;
        $channels = array(
            'boys' => self::CODE_BOYS_GUANG_ADS,
            'girls'=> self::CODE_GIRLS_GUANG_ADS,
            'lifestyle'=> self::CODE_LIFESTYLE_GUANG_ADS,
            'kids'=> self::CODE_KIDS_GUANG_ADS
        );
        if(isset($channels[$channel])) {
            $contentCode = $channels[$channel];
        }
        $key = WebCacheConfig::KEY_WEB_GUANG_ADS_DATA.'_'.$contentCode;
        $data = Cache::get($key);
        if(empty($data)) {
            $resource = IndexData::getResourceData($contentCode);
            if (isset($resource['data']) && !empty($resource['data']) && $resource['code'] == 200) {
                $list = current($resource['data']);
                foreach($list['data'] as $val) {
                    $data [] = array( 'img' => Images::getImageUrl($val['src'], 640,640, 1),
                        'url' => Helpers::getUrlSafe($val['url']));
                }
                //最多取5个
                $data = array_slice($data, 0, 5);
                //格式化数据
                Cache::set($key, $data, 1800);
            }
        }
        //master没有数据,资源位没有数据, 取二层缓存
        if(empty($data)) {
            $data = Cache::get($key, 'slave');
        }
        return $data;
    }
    
    /**
     * 获取path导航
     * 
     * @param string $tag
     * @return array
     */
    public static function getPathNav($tag = '')
    {
        $paths[] = array('href' => Helpers::url('/index/index',array(),'guang'), 'name' => '逛','pathTitle' => '逛');
        if(!empty($tag)) {
            $paths[] = array('name' => $tag,'pathTitle' => $tag);
        }
        return $paths;
    }
}