PlusstarData.php 5.78 KB
<?php

namespace LibModels\Wap\Guang;

use Api\Sign;
use Api\Yohobuy;
use Plugin\Helpers;

/**
 * 明星品牌和原创品牌相关的数据模型
 * 
 * @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/v1/plustar/getlist';
    const URI_BRANDINFO_PLUSSTAR = 'guang/service/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 brandList($gender, $channel)
    {
        // 存放接口列表
        $urlList = array();
        // 接口调用的URL
        $url = Yohobuy::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);
    }

    /**
     * 品牌介绍
     * 
     * @param int $id 唯一的ID
     * @param string $gender $gender "1,3"表示男, "2,3"表示女
     * @param int $uid 用户ID
     * @param string $udid cookie记录唯一标识
     * @return array(
     *     "getBrandInfo" => array(品牌详情信息),
     *     "getUidBrandFav" => false,
     *     "getArticleByBrand" => array(3篇相关资讯),
     *     "getNewProduct" => array(6篇新品到着),
     *     "getUidProductFav" => array(用户是否收藏新品)
     * )
     */
    public static function brandInfo($id, $gender, $uid, $udid = null)
    {
        $result = array();
        $result['getBrandInfo'] = array();
        $result['getUidBrandFav'] = false;
        $result['getNewProduct'] = array();
        $result['getUidProductFav'] = array();
        $result['getArticleByBrand'] = array();
        
        // 品牌详情信息
        $brandInfo = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_BRANDINFO_PLUSSTAR, 'getBrandInfo', array(array('id' => $id)) );
        if (!isset($brandInfo['data']['brand_id'])) {
            return $result;
        } else {
            $result['getBrandInfo'] = $brandInfo;
        }
        
        // 是否收藏店铺
        $isUidOk = $uid && is_numeric($uid);
        if ($isUidOk) {
            Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_BRANDINFO_FAVORITE, 'getUidBrandFav', array($uid, $id), function($retval) use(&$result) {
                $result['getUidBrandFav'] = empty($retval['data']) ? false : $retval['data'];
            });
        }
        
        // 相关资讯列表 (3篇)
        $result['getArticleByBrand'] = array();
        Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URI_BRANDINFO_ARTICLE, 'getArticleByBrand', array($brandInfo['data']['brand_id'], 3, $udid), function($retval) use(&$result) {
            $result['getArticleByBrand'] = empty($retval) ? array() : $retval;
        });
        
        // 新品到着列表 (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(Yohobuy::API_URL, $param); 
            if (empty($newProduct['data']['product_list'])) {
                break;
            }
            
            $skn = '';
            foreach ($newProduct['data']['product_list'] as $value) {
                if (empty($value['goods_list'])) {
                    continue;
                }
                
                // 商品信息列表
                $result['getNewProduct'][] = Helpers::formatProduct($value);
                
                // 用户是否收藏该商品
                $skn = $value['product_skn'];
                $result['getUidProductFav'][ $skn ] = false;
                if ($isUidOk) {
                    Yohobuy::yarConcurrentCall(Yohobuy::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'];
                    });
                }
            }
        } while (false);
        
        // 调用发起请求
        Yohobuy::yarConcurrentLoop();
        
        return $result;

//                    $goods_url = MakeUrl::h5ProductDetailUrl($baseUrl,$new_goods_info['product_id'],$new_goods_info['goods_list'][0]['goods_id'],$new_goods_info['cn_alphabet']);
//                    $url = MakeUrl::makeUrl('go.productDetail',$goods_url, array('product_skn'=>$new_goods_info['product_skn']),$client_type);
    }

}