Blame view

library/LibModels/Wap/Guang/DetailData.php 5.37 KB
hf authored
1 2
<?php
3 4 5
namespace LibModels\Wap\Guang;

use Api\Yohobuy;
6
use Configs\CacheConfig;
7
use Plugin\Cache;
8 9 10 11 12 13 14 15 16

/**
 * 逛资讯详情相关的数据模型
 * 
 * @name DetailData
 * @package LibModels/Wap/Guang
 * @copyright yoho.inc
 * @version 1.0 (2015-10-10 14:49:15)
 * @author fei.hong <fei.hong@yoho.cn>
hf authored
17
 */
18 19
class DetailData
{
hf authored
20
hf authored
21 22
    const URI_PACKAGE_ARTICLE = 'guang/service/v2/article/';
    const URI_PACKAGE_AUTHOR = 'guang/service/v1/author/';
23 24 25

    /**
     * 逛资讯详情页数据封装
26 27 28 29
     * 
     * @param int $id 内容ID
     * @param bool $isApp 标识是否是APP访问
     * @return array
30
     */
31
    public static function package($id, $isApp = false)
32 33 34 35 36 37 38 39
    {
        $result = array();
        $result['getAuthor'] = array();
        $result['getArticle'] = array();
        $result['getArticleContent'] = array();
        $result['getBrand'] = array();
        $result['getOtherArticle'] = array();
40 41
        // 客户端类型
        $clientType = $isApp ? 'iphone' : 'h5';
hf authored
42 43 44 45 46 47
        $key = CacheConfig::KEY_ACTION_GUANG_DETAIL_DATA . strval($id) . $clientType;

        if (USE_CACHE) {
            // 先尝试获取二级缓存(slave), 有数据则直接返回.
            $cached = Cache::get($key, 'master');
            if (!empty($cached)) {
48 49 50 51
                $article = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticle', array($id, $clientType), false, 1000); 
                if (isset($article['pageViews'])) {
                    $cached['getArticle']['pageViews'] = $article['pageViews'];
                }
hf authored
52 53 54
                return $cached;
            }
        }
hf authored
55
56
        // 获取资讯
57
        $article = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticle', array($id, $clientType), false);        
hf authored
58
        if (!isset($article['author_id'])) {
59
            return $result;
hf authored
60
        }
hf authored
61
        $result['getArticle'] = $article;
62 63

        // 获取作者信息
64
        Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_AUTHOR, 'getAuthor', array($article['author_id'], $clientType), function ($retval) use (&$result) {
65 66 67 68
            $result['getAuthor'] = empty($retval) ? array() : $retval;
        });

        // 获取资讯内容
69
        Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticleContent', array($id, $clientType), function ($retval) use (&$result) {
70 71 72 73
            $result['getArticleContent'] = empty($retval) ? array() : $retval;
        });

        // 获取资讯相关的品牌
74
        Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getBrand', array($id, $clientType), function ($retval) use (&$result) {
75 76 77 78
            $result['getBrand'] = empty($retval) ? array() : $retval;
        });

        // 获取资讯相关的其它资讯
hf authored
79 80 81 82 83
        if (isset($article['tag'])) {
            Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getOtherArticle', array($article['tag'], $id, 0, 3, $clientType), function ($retval) use (&$result) {
                $result['getOtherArticle'] = empty($retval) ? array() : $retval;
            });
        }
84 85 86 87

        // 调用发起请求
        Yohobuy::yarConcurrentLoop();
hf authored
88 89 90 91 92 93 94 95 96
        if (USE_CACHE) {
            // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
            if (empty($result)) {
                $result = Cache::get($key, 'slave');
            }
            // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
            elseif (!empty($result['getArticle']) && !empty($result['getArticleContent'])) {
                Cache::set($key, $result);
            }
97 98
        }
99 100
        return $result;
    }
101 102

    /**
hf authored
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
     * 逛资讯详情页数据封装 (专为YOHO!潮流志APP提供)
     * 
     * @param int $id 内容ID
     * @param bool $isApp 标识是否是APP访问
     * @return array
     */
    public static function packageFoEzine($id, $isApp = false)
    {
        $result = array();
        $result['getAuthor'] = array();
        $result['getArticle'] = array();
        $result['getArticleContent'] = array();
        $result['getBrand'] = array();
        $result['getOtherArticle'] = array();

        // 客户端类型
        $clientType = $isApp ? 'iphone' : 'h5';

        // 获取资讯
        $article = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticle', array($id, $clientType));
        if (!isset($article['author_id'])) {
            return $result;
        }
        $result['getArticle'] = $article;

        // 获取资讯内容
        Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticleContent', array($id, $clientType), function ($retval) use (&$result) {
            $result['getArticleContent'] = empty($retval) ? array() : $retval;
        });

        // 获取资讯相关的品牌
        Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getBrand', array($id, $clientType), function ($retval) use (&$result) {
            $result['getBrand'] = empty($retval) ? array() : $retval;
        });

        // 调用发起请求
        Yohobuy::yarConcurrentLoop();

        return $result;
    }
hf authored
143 144 145 146 147 148 149 150
    
    /**
     * 获取详情信息
     */
    public static function intro($id)
    {
        return Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticleContent', array($id, 'h5'));
    }
hf authored
151
152
}