PlusstarData.php 10.9 KB
<?php

namespace LibModels\Wap\Guang;

use Api\Sign;
use Api\Yohobuy;
use Configs\CacheConfig;
use Plugin\Helpers;
use Plugin\Cache;
use LibModels\Wap\Product\SearchData;

/**
 * 明星品牌和原创品牌相关的数据模型
 * 
 * @name PlusstarData
 * @package Library/LibModels/Wap/Guang
 * @copyright yoho.inc
 * @version 1.0 (2015-10-9 10:22:10)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class PlusstarData
{

    const URI_BRANDLIST = 'guang/api/v3/plustar/getlist';
    const URI_BRANDINFO_PLUSSTAR = 'guang/service/v3/plustar/'; // 'guang/service/v3/plustar/'
    const URI_BRANDINFO_PLUSSTAR_NEW = 'guang/api/v1/plustar/';
    const URI_BRANDINFO_FAVORITE = 'shops/service/v1/favorite/';
    const URI_BRANDINFO_ARTICLE = 'guang/service/v1/article/';

    /**
     * 国际优选-品牌列表
     * 
     * @param string $gender "1,3"表示男, "2,3"表示女
     * @param string $channel 1表示男, 2表示女
     * @return array(
     *     "star" => array(推荐品牌列表数据),
     *     "original" => array(所有品牌列表数据)
     * )
     */
    public static function firstBrandList($gender, $channel)
    {
        // 存放接口列表
        $urlList = array();
        // 接口调用的URL
        $url = SERVICE_URL . self::URI_BRANDLIST;

        // 公共的参数
        $param = Yohobuy::param();
        $param['gender'] = $gender;
        $param['yh_channel'] = $channel;

        // 构建推荐的品牌参数及调用接口的URL
        $build = $param;
        $build['brand_type'] = '4';
        //$build['is_recommend'] = '1';
        $build['client_secret'] = Sign::getSign($build);
        $urlList['recom'] = Yohobuy::httpBuildQuery($url, $build);

        // 构建所有的品牌参数及调用接口的URL
        $build = $param;
        $build['brand_type'] = '1';
        //$build['is_recommend'] = '0';
        $build['client_secret'] = Sign::getSign($build);
        $urlList['all'] = Yohobuy::httpBuildQuery($url, $build);

        return Yohobuy::getMulti($urlList, array(), 1800); // 缓存30分钟
    }

    /**
     * 明星原创-品牌列表
     * 
     * @param string $gender "1,3"表示男, "2,3"表示女
     * @param string $channel 1表示男, 2表示女
     * @return array(
     *     "star" => array(明星品牌列表数据),
     *     "original" => array(原创品牌列表数据)
     * )
     */
    public static function brandList($gender, $channel)
    {
        // 存放接口列表
        $urlList = array();
        // 接口调用的URL
        $url = SERVICE_URL . self::URI_BRANDLIST;

        // 公共的参数
        $param = Yohobuy::param();
        $param['gender'] = $gender;
        $param['is_recommend'] = '0';
        $param['yh_channel'] = $channel;

        // 构建明星品牌参数及调用接口的URL
        $star = $param;
        $star['brand_type'] = '2';
        $star['client_secret'] = Sign::getSign($star);
        $urlList['star'] = Yohobuy::httpBuildQuery($url, $star);

        // 构建原创品牌参数及调用接口的URL
        $original = $param;
        $original['brand_type'] = '3';
        $original['client_secret'] = Sign::getSign($original);
        $urlList['original'] = Yohobuy::httpBuildQuery($url, $original);

        return Yohobuy::getMulti($urlList, array(), 1800); // 缓存30分钟
    }

    /**
     * 品牌介绍
     * 
     * @param int $id 唯一的ID
     * @param string $gender $gender "1,3"表示男, "2,3"表示女
     * @param int $uid 用户ID
     * @param string $udid cookie记录唯一标识
     * @param bool $isApp 判断是不是APP访问
     * @return array(
     *     "getBrandInfo" => array(品牌详情信息),
     *     "getUidBrandFav" => false,
     *     "getArticleByBrand" => array(3篇相关资讯),
     *     "getNewProduct" => array(6篇新品到着),
     *     "getUidProductFav" => array(用户是否收藏新品)
     * )
     */
    public static function brandInfo($id, $gender, $uid, $udid = null, $isApp = false)
    {
        $result = array();
        $result['getBrandInfo'] = array();
        $result['getUidBrandFav'] = false;
        $result['getNewProduct'] = array();
        $result['getUidProductFav'] = array();
        $result['getArticleByBrand'] = array();
        
        $clientType = 'iphone';
        // 客户端类型
        if (stristr($_SERVER['HTTP_USER_AGENT'], 'android')) {
            $clientType = 'android';
        }
        $clientType = $isApp ? $clientType : 'h5';

        $isUidOk = $uid && is_numeric($uid);
        $key = CacheConfig::KEY_ACTION_GUANG_PLUSTAR_DATA . sprintf('_%s_%s_%s', $id, $clientType, $gender);

        if (USE_CACHE) {
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $cached = Cache::get($key, 'master');
            if (!empty($cached)) {
                // 判断用户是否已收藏
                if ($isUidOk) {
                    $param = Yohobuy::param();
                    $param['uid'] = $uid;
                    $param['brandId'] = $cached['getBrandInfo']['data']['brand_id'];
                    $param['client_type'] = $clientType;
                    $param['private_key'] = Yohobuy::$privateKeyList[$clientType];
                    $param['client_secret'] = Sign::getSign($param);
                    $getUidBrandFav = Yohobuy::get(SERVICE_URL . self::URI_BRANDINFO_FAVORITE . 'getUidBrandFav', $param);
                    if (isset($getUidBrandFav['code']) && $getUidBrandFav['code'] == 200) {
                        $cached['getUidBrandFav'] = true;
                    }else{
                        $cached['getUidBrandFav'] = false;
                    }

                }

                return $cached;
            }
        }

        // 品牌详情信息
        $param = Yohobuy::param();
        $param['id'] = $id;
        $param['client_type'] = $clientType;
        $param['private_key'] = Yohobuy::$privateKeyList[$clientType];
        $param['client_secret'] = Sign::getSign($param);
        $brandInfo = Yohobuy::get(SERVICE_URL . self::URI_BRANDINFO_PLUSSTAR_NEW . 'getbrandinfo', $param, 3600);

//        if (!isset($brandInfo['data']['brand_id'])) {
//            // 再尝试获取二级缓存(master), 有数据则直接返回.
//            $cached = Cache::get($key, 'slave');
//            if (!empty($cached)) {
//                return $cached;
//            }
//            return $result;
//        } else {
//            $result['getBrandInfo'] = $brandInfo;
//        }

        if (isset($brandInfo['data']['brand_id'])) {
            $result['getBrandInfo'] = $brandInfo;
        }

        // 相关资讯列表 (3篇)
        $result['getArticleByBrand'] = array();
        $param = Yohobuy::param();
        $param['brand_id'] = $brandInfo['data']['brand_id'];
        $param['uid'] = $uid;
        $param['udid'] = $udid;
        $param['limit'] = 3;
        $param['client_type'] = $clientType;
        $param['private_key'] = Yohobuy::$privateKeyList[$clientType];
        $param['client_secret'] = Sign::getSign($param);

        $article = Yohobuy::get(SERVICE_URL.self::URI_BRANDINFO_ARTICLE.'getArticleByBrand', $param);
        $result['getArticleByBrand'] = empty($article['data']) ? array() : $article['data'];

        // 是否收藏店铺
        $param = Yohobuy::param();
        $param['uid'] = $uid;
        $param['brandId'] = $brandInfo['data']['brand_id'];
        $param['client_type'] = $clientType;
        $param['private_key'] = Yohobuy::$privateKeyList[$clientType];
        $param['client_secret'] = Sign::getSign($param);
        $uidBrandFavRes = Yohobuy::get(SERVICE_URL . self::URI_BRANDINFO_FAVORITE . 'getUidBrandFav', $param);

        if (isset($uidBrandFavRes['code']) && $uidBrandFavRes['code'] == 200) {
            $result['getUidBrandFav'] = true;
        }else{
            $result['getUidBrandFav'] = false;
        }
        // 相关资讯列表 (3篇)
//        $result['getArticleByBrand'] = array();
//        Yohobuy::yarConcurrentCall(Yohobuy::PRD_SERVICE_URL . self::URI_BRANDINFO_ARTICLE, 'getArticleByBrand', array($brandInfo['data']['brand_id'], 3, $udid), function($retval) use(&$result) {
//            $result['getArticleByBrand'] = empty($retval) ? array() : $retval;
//            file_put_contents('../../../../../../Desktop/app/o/getArticleByBrand.txt', var_export($retval, true));
//        });

        // 新品到着列表 (6篇)
        $result['getNewProduct'] = array();
        $result['getUidProductFav'] = array();

        do {
//            // 调用搜索接口
//            $param = Yohobuy::param();
//            $param['method'] = 'app.search.li';
//            $param['brand'] = $brandInfo['data']['brand_id'];
//            $param['page'] = '0';
//            $param['limit'] = '6';
//            $param['gender'] = $gender;
//            $param['order'] = 's_t_desc';
//            $param['client_secret'] = Sign::getSign($param);
//            $newProduct = Yohobuy::get(API_URL, $param, 1800); // 缓存30分钟

            $condition = array();
            $condition['brand'] = $brandInfo['data']['brand_id'];
            $condition['gender'] = $gender;
            $condition['limit'] = 6;
            $condition['order'] = 's_t_desc';
            $newProduct = SearchData::searchElasticByCondition($condition, 1800); // 有缓存30分钟

            if (empty($newProduct['data']['product_list'])) {
                break;
            }

            $skn = '';
            $i = 0;
            foreach ($newProduct['data']['product_list'] as $value) {
                // 最多6个
                if ($i > 5) {
                    break;
                }

                if (empty($value['goods_list'])) {
                    continue;
                }

                // 商品信息列表
                $result['getNewProduct'][$i] = Helpers::formatProduct($value, true, true, true, 235, 314, $isApp);

                // 用户是否收藏该商品
                $skn = $value['product_skn'];
                $result['getUidProductFav'][$skn] = false;
//                if ($isUidOk) {
//                    Yohobuy::yarConcurrentCall(SERVICE_URL . self::URI_BRANDINFO_FAVORITE, 'getUidProductFav', array($uid, $value['product_skn']), function($retval) use(&$result, &$skn) {
//                        $result['getUidProductFav'][$skn] = empty($retval['data']) ? false : $retval['data'];
//                    });
//                }

                $i ++;
            }
        } while (false);

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

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

}