Index.php 13.5 KB
<?php

namespace Guang;

use LibModels\Wap\Guang\ListData;
use LibModels\Wap\Guang\DetailData;
use Configs\CacheConfig;
use Plugin\Helpers;
use Plugin\Cache;

/**
 * 
 * @name IndexModel
 * @package models/Guang
 * @copyright yoho.inc
 * @version 1.0 (2015-10-27 11:31:49)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class IndexModel
{

    /**
     * 获取逛首页分类内容列表
     * 
     * @param string $gender "1,3"表示男, "2,3"表示女
     * @param int channel APP客户端标识 1表示男,2:表示女,3:潮童,4:创意生活
     * @param int type 分类ID 0:最新,1:话题,2:搭配,3:潮人,4:潮品,5:小贴士
     * @param int $uid 用户ID
     * @param string $udid 客户端唯一标识
     * @param int $page 分页第几页, 默认第1页
     * @return array
     */
    public static function getArticleGroup($gender, $type, $uid, $udid, $page = 1)
    {
        // 逛首页的分类名称列表
        $category = ListData::category();
        $articleGroup = array();

        switch ($gender) {
            case '1,3': // 男
                $articleGroup = ListData::articleGroup($category['data'], '1,3', $uid, $udid, $page);
                break;
            case '2,3': // 女
                $articleGroup = ListData::articleGroup($category['data'], '2,3', $uid, $udid, $page);
                break;
            default: // 其它所有
                $articleGroup = ListData::articleGroup($category['data'], '1,2,3', $uid, $udid, $page);
                break;
        }

        $data = array();
        $build = array();

        // 模板中使用JS的标识
        $data['guangHome'] = true;

        // 顶部的分类列表
        foreach ($category['data'] as $value) {
            $build = array();
            $build['typeId'] = $value['id'];
            $build['type'] = $value['name'];
            $build['focus'] = ($value['id'] == $type);
            $data['navs'][] = $build;
        }

        $data['guang']['swiper'] = array();
        $data['guang']['infos'] = array();
        foreach ($articleGroup as $id => $value) {
            // 判断是否有内容
            if (empty($value['list'])) {
                continue;
            }

            // 轮番广告
            if ($id == 0) {
                $build = array();
                foreach ($value['list']['adlist'] as $banner) {
                    $build['url'] = Helpers::getFilterUrl($banner['url']);
                    $build['img'] = Helpers::getImageUrl($banner['src'], 830, 327);
                    $data['guang']['swiper'][] = $build;
                }
            }

            // 内容列表
            $build = array();
            $build['show'] = ($id == $type);
            foreach ($value['list']['artList'] as $article) {
                $build['info'][] = Helpers::formatArticle($article, true, false);
            }
            $data['guang']['infos'][] = $build;
        }

        // 分页需要参数
        $data['guang']['gender'] = $gender;

        $category = array();

        return $data;
    }

    /**
     * 获取逛首页分类内容列表
     * 
     * @param string $gender "1,3"表示男, "2,3"表示女
     * @param int channel APP客户端标识 1表示男,2:表示女,3:潮童,4:创意生活
     * @param int type 分类ID 0:最新,1:话题,2:搭配,3:潮人,4:潮品,5:小贴士
     * @param int $uid 用户ID
     * @param string $udid 客户端唯一标识
     * @param int $page 分页第几页, 默认第1页
     * @return array
     */
    public static function getArticle($gender, $type, $uid, $udid, $page = 1)
    {
        // 逛首页的分类名称列表
        $category = ListData::category();
        $article = array();

        switch ($gender) {
            case '1,3': // 男
                $article = ListData::article('1,3', $type, $uid, $udid, $page);
                break;
            case '2,3': // 女
                $article = ListData::article('2,3', $type, $uid, $udid, $page);
                break;
            default: // 其它所有
                $article = ListData::article('1,2,3', $type, $uid, $udid, $page);
                break;
        }

        $data = array();
        $build = array();

        // 模板中使用JS的标识
        $data['guangHome'] = true;
        
        // 分页需要参数
        $data['guang']['gender'] = $gender;

        // 显示底部
        $data['pageFooter'] = true;
        
        // 缓存键名
        $key = CacheConfig::KEY_ACTION_GUANG_LIST_DATA . sprintf('%s_%s_%s', $gender, $type, $page);

        // 顶部的分类列表
        if (!empty($category['data'])) {
            foreach ($category['data'] as $value) {
                $build = array();
                $build['typeId'] = $value['id'];
                $build['type'] = $value['name'];
                $build['focus'] = ($value['id'] == $type);
                $data['navs'][] = $build;
                $data['guang']['infos'][] = array(
                    'show' => $build['focus'],
                    'info' => array(),
                );
            }
        }

        // 广告列表
        if (!empty($article['data']['list']['adlist'])) {
            foreach ($article['data']['list']['adlist'] as $value) {
                $build = array();
                $build['url'] = Helpers::getFilterUrl($value['url']);
                $build['img'] = Helpers::getImageUrl($value['src'], 830, 327);
                $data['guang']['swiper'][] = $build;
            }
        }
//        // 只有当$type=0时才有广告参数返回, 所以需要再调一次!!
//        else {
//            $temp = ListData::article('1,3', 0, 0, '', $page);
//            if (!empty($temp['data']['list']['adlist'])) {
//                foreach ($temp['data']['list']['adlist'] as $value) {
//                    $build = array();
//                    $build['url'] = Helpers::getFilterUrl($value['url']);
//                    $build['img'] = Helpers::getImageUrl($value['src'], 830, 327);
//                    $data['guang']['swiper'][] = $build;
//                }
//            }
//            $temp = array();
//        }

        // 内容列表
        if (!empty($article['data']['list']['artList'])) {
            $type = intval($type);
            foreach ($article['data']['list']['artList'] as $value) {
                $data['guang']['infos'][$type]['info'][] = Helpers::formatArticle($value, true, false, true);
            }
            
            if (USE_CACHE) {
                $result = Cache::get($key, 'master');
                if (empty($result)) {
                // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
                    Cache::set($key, $data, 1800);
                }
            }
        } 
        // 当接口异常时,尝试从二级缓存(slave)获取
        elseif (USE_CACHE) {
            $result = Cache::get($key, 'slave');
            if (!empty($result)) {
                return $result;
            }
        }

        $category = array();
        $article = array();

        return $data;
    }

    /**
     * 获取订阅的资讯文章列表
     * 
     * @param string $gender 性别参数(1,3表示男, 2,3表示女, 默认为所有)
     * @return array
     */
    public static function getRssArticle($gender)
    {
        $result = array();

        if (USE_CACHE) {
            $key = CacheConfig::KEY_ACTION_GUANG_RSS_DATA;
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get($key, 'master');
            if (!empty($result)) {
                return $result;
            }
        }

        $article = array();
        switch ($gender) {
            case '1,3':
                $article = ListData::article('1,3', 0, 0, '', 1, null, null, 100, false);
                break;
            case '2,3':
                $article = ListData::article('2,3', 0, 0, '', 1, null, null, 100, false);
                break;
            default:
                $article = ListData::article('1,2,3', 0, 0, '', 1, null, null, 100, false);
                break;
        }

        // 内容列表
        if (!empty($article['data']['list']['artList'])) {
            $build = array();
            foreach ($article['data']['list']['artList'] as $value) {
                if (isset($value['id'])) {
                    $build = Helpers::formatArticle($value, false, false, true);
                    if (!isset($build['author']['name'])) {
                        $build['author']['name'] = '';
                    }
                    $build['intro'] = self::genIntro($value['id']);
                    $result[] = $build;
                }
            }
        }

        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            else {
                Cache::set($key, $result);
            }
        }

        return $result;
    }
    
    /**
     * 逛标签列表或作者内容列表
     * 
     * @param string $gender "1,3"表示男, "2,3"表示女, "1,2,3"表示所有
     * @param int $sortId 分类ID
     * @param int $uid 用户ID
     * @param string $udid 客户端唯一标识
     * @param int $page 分页第几页, 默认第1页
     * @param string $tag 标签
     * @param int $authorId 作者ID
     * @return array
     */
    public static function getArticleByTagOrEditor($gender, $sortId, $uid = 0, $udid = '', $page = 1, $tag = null, $authorId = null)
    {
        // 缓存键名
        if (isset($tag)) {
            $key = CacheConfig::KEY_ACTION_GUANG_LIST_DATA . sprintf('tag_%s_%s_%s_%s', $gender, $sortId, $page, $tag);
        } else {
            $key = CacheConfig::KEY_ACTION_GUANG_LIST_DATA . sprintf('editor_%s_%s_%s_%s', $gender, $sortId, $page, $authorId);
        }
        
        // 调用接口,获取内容
        $article = ListData::article($gender, $sortId, $uid, $udid, $page, $tag, $authorId);
        // 内容列表
        if (!empty($article['data']['list']['artList'])) {
            if (USE_CACHE) {
                $result = Cache::get($key, 'master');
                if (empty($result)) {
                    // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
                    Cache::set($key, $article, 1800);
                }
            }
        } 
        // 当接口异常时,尝试从二级缓存(slave)获取
        elseif (USE_CACHE) {
            $result = Cache::get($key, 'slave');
            if (!empty($result)) {
                return $result;
            }
        }
        
        return $article;
    }

    /**
     * 获取切换逛类别或者分页时的文章数据
     *
     * @param string $gender "1,3"表示男, "2,3"表示女
     * @param int $sortId 分类ID
     * @param int $uid 用户ID
     * @param string $udid 客户端唯一标识
     * @param int $page 分页第几页, 默认第1页
     * @param string $tag 标签
     * @param string $authorId 作者ID
     * @param boolean $isApp 是否是APP
     * @param boolean $showAuthor 是否显示作者
     * @param boolean $isTab 是否为tab切换操作
     * @return array
     */
    public static function getPageData($gender, $sortId, $uid, $udid, $page, $tag, $authorId, $isApp, $showAuthor, $isTab)
    {
        $result = array();

        do {
            $article = ListData::article($gender, $sortId, $uid, $udid, $page, $tag, $authorId);
            if (empty($article['data']['list']['artList'])) {
                break;
            }

            // 广告列表
            if ($isTab && !empty($article['data']['list']['adlist'])) {
                foreach ($article['data']['list']['adlist'] as $value) {
                    $build = array();
                    $build['url'] = Helpers::getFilterUrl($value['url']);
                    $build['img'] = Helpers::getImageUrl($value['src'], 830, 327);
                    $result['swiper'][] = $build;
                }
            }

            /* 构建资讯文章内容 */
            $build = array();
            foreach ($article['data']['list']['artList'] as $article) {
                $build[] = Helpers::formatArticle($article, true, $isApp, $showAuthor, $uid);
            }
            $result['infos'] = $build;
        } while (false);

        return $result;
    }

    /**
     * 构建信息内容
     * 
     * @param array $data
     * @return array
     */
    private static function genIntro($id)
    {
        $result = '';

        $data = DetailData::intro($id);
        if (!empty($data)) {
            foreach ($data as $value) {
                // 文字
                if (isset($value['text'])) {
                    $result .= trim(strip_tags(htmlspecialchars_decode($value['text']['data']['text']))) . '<br/>';
                }
                // 单张图
                elseif (isset($value['singleImage'])) {
                    $result .= '<img src="' . Helpers::getImageUrl($value['singleImage']['data'][0]['src'], 640, 640) . '"/><br/>';
                }
                // 多张小图
                elseif (isset($value['smallPic']['data'])) {
                    foreach ($value['smallPic']['data'] as $small) {
                        $result .= '<img src="' . Helpers::getImageUrl($small['src'], 315, 420) . '"/>';
                    }
                    $result .= '<br/>';
                }
            }
        }
        $data = array();

        return $result;
    }

}