Brands.php 20.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
<?php

namespace Index;

use Api\Yohobuy;
use LibModels\Web\Product\BrandData;
use Index\HomeModel;
use WebPlugin\Helpers;
use Plugin\Images;
use WebPlugin\Cache;
use LibModels\Web\Home\IndexData;
use LibModels\Web\Product\SearchData;
use WebPlugin\DataProcess\Channel;
use Configs\WebCacheConfig;
use Configs\ChannelConfig;

/**
 * 品牌首页模板数据模型
 *
 */
class BrandsModel
{

    //品牌一览资源位CODE码
    const BOYS_BRAND_CODE = '8b16b7baf9a66fbe553a6caa97d2ce2a';
    const GIRLS_BRAND_CODE = 'c95ae9e40f0add10549b819f821ad626';
    const KIDS_BRAND_CODE = 'c575c6bfdfa4125fae7d24bbec7119c8';
    const LIFESTYLE_BRAND_CODE = '84b7926282fdef92f1039bdcf77c18ba';
    const BRAND_LIST_CODE = 'f0f72b1e8f30e6ad086dfc4401f3a856'; //品牌列表资源位CODE码
    const URL_PRODUCT_BRAND = '/cmsproduct/service/v1/brands';
    const URL_GUANG_ARTICLE = '/guang/service/v1/article';
    const BRAND_PLUSSTAR_BANNER_BOYS = 'd0149783b8dd2adaf083fd10556c39a9';
    const BRAND_PLUSSTAR_BANNER_GIRLS = 'aad7a43e9a04ac7c70ae7f0c1acf86ef';
    const BRAND_PLUSSTARINDEX_BOYS = 'a833aed63d28457156310e97faa7fa37'; //plusstarindex男首资源位
    const BRAND_PLUSSTARINDEX_GIRLS = '6e4f162be3b3ba44f3bfcf1c38bdb745'; //plusstarindex女首资源位
    const BOYS = 'boys';
    const GIRLS = 'girls';
    const KIDS = 'kids';
    const LIFESTYLE = 'lifestyle';

    /**
     * 获取品牌一览页面,品牌top
     * @param string $channel 频道名称
     */
    public static function getBrandViewTop($channel)
    {
        if (USE_CACHE) {
            // 先尝试从缓存获取数据
            $key = WebCacheConfig::KEY_WEB_BRAND_INDEX_VIEW_TOP_DATA . '_' . $channel;
            $result = Cache::get($key);
            if (!empty($result['tabHeader'])) {
                return $result;
            }
        }
        //获取参数
        $switchParams = self::switchBrandParams($channel);

        //取接口数据
        $result = array();
        $brandAds = array();
        $brandLogos = array();
        $res = BrandData::getBrandTopData($switchParams['brandCode']);
        //头部10个品牌小图块 url
        if (isset($res['data'][1]['data']['list']) && $res['data'][1]['data']['list']) {
            foreach ($res['data'][1]['data']['list'] as $tbk => $tbv) {
                $brandAds[$tbk] = array(
                    'name' => $tbv['name'],
                    'src' => Helpers::getImageUrl($tbv['src'], 80, 50, 3),
                    'url' => $tbv['url'],
                );
            }
        }
        //头部品牌图块,广告位
        if (isset($res['data'][0]['data']) && $res['data'][0]['data']) {
            foreach ($res['data'][0]['data'] as $tk => $tv) {
                //kids lifestyle 第一张图尺寸不同
                if ($switchParams['channelType'] == 1 || $switchParams['channelType'] == 2) {
                    $srcUrl = Helpers::getImageUrl($tv['src'], 222, 180, 3);
                }
                else {
                    $srcUrl = ($tk == 0) ? Helpers::getImageUrl($tv['src'], 570, 280, 3) : Helpers::getImageUrl($tv['src'], 280, 280, 3);
                }
                $brandLogos[$tk] = array(
                    'name' => $tv['title'],
                    'src' => $srcUrl,
                    'url' => $tv['url']
                );
            }
        }
        //整合brandTop数据结构,boys、girls
        if ($switchParams['channelType'] == 1 || $switchParams['channelType'] == 2) {
            $result['isTab'] = true;
        }
        $result['tabHeader'] = $brandLogos;
        $result['logos'] = $brandAds;

        if (USE_CACHE) {
            // 设置一级和二级缓存
            if (!empty($result['tabHeader'])) {
                Cache::set($key, $result);
            }
            // 接口异常,一级缓存失效时从二缓存取数据
            else {
                $result = Cache::get($key, 'slave');
            }
        }

        return $result;
    }

    /*
     * 获取品牌一览list
     * @param string $channel 频道名称 
     * @param int start  开始位置 1 开始
     * @param int length 取数长度 0 取到最后
     */

    public static function getBrandViewList($channel, $start = 1, $length = 0)
    {
        if (USE_CACHE) {
            // 先尝试从缓存获取数据
            $key = WebCacheConfig::KEY_WEB_BRAND_INDEX_VIEW_LIST_DATA . '_' . $channel . $start . $length;
            $result = Cache::get($key);
            if (!empty($result['navigation'])) {
                return $result;
            }
        }
        //获取参数
        $switchParams = self::switchBrandParams($channel);

        //取接口数据
        $result = array();
        $moreBrand = array();
        $navigation = array();
        $res = BrandData::getBrandListData($switchParams['channelType']);

        //品牌list A-Z 0-9
        if (isset($res['data']['brands']) && $res['data']['brands']) {
            $listTmp = array();
            foreach ($res['data']['brands'] as $lk => $lv) {
                if (empty($lv)) {
                    continue;
                }
                $listTmp = array();
                foreach ($lv as $ltv) {
                    //为品牌名称
                    $ltk = $ltv['brand_name'];
                    if ($switchParams['channelType'] == 1) {
                        $href = Helpers::url('', array(), $ltv['brand_domain']) . '?gender=1,3';
                    }
                    elseif ($switchParams['channelType'] == 2) {
                        $href = Helpers::url('', array(), $ltv['brand_domain']) . '?gender=2,3';
                    }
                    else {
                        $href = Helpers::url('', array(), $ltv['brand_domain']);
                    }
                    $listTmp[$ltk] = array(
                        'name' => $ltv['brand_name'],
                        'key' => $ltv['id'],
                        'href' => $href
                    );
                    if ($ltv['is_hot'] === 'Y') {
                        $listTmp[$ltk]['hot'] = 'hot';
                    }
                    //判断是否是多品店
                    if (in_array($ltv['id'], ChannelConfig::$moreBrandIds)) {
                        $moreBrand[$ltk] = $listTmp[$ltk];
                    }
                }
                $result[$lk] = $listTmp;
                //按键值排序
                ksort($result[$lk]);
            }
            //将brandList里的0-9 排序到最后
            if (isset($result['0~9'])) {
                $brand = $result['0~9'];
                unset($result['0~9']);
                $result['0-9'] = $brand;
            }
            //写死多品店数据
            $result['多品店'] = $moreBrand;
            $navigation = array_keys($result);
        }
        //只取部分数据
        $begin = ($start - 1) ? ($start - 1) : 0;
        $begin = ($begin > 0) ? $begin : 0;
        $result = $length ? array_slice($result, $begin, $length) : array_slice($result, $begin);
        $result['navigation'] = $navigation;

        if (USE_CACHE) {
            // 设置一级和二级缓存
            if (!empty($result['navigation'])) {
                Cache::set($key, $result);
            }
            // 接口异常,一级缓存失效时从二缓存取数据
            else {
                $result = Cache::get($key, 'slave');
            }
        }

        return $result;
    }

    /**
     * 获取单个广告浮窗内容
     * 
     * @param int $brandId
     * @param int $uid
     * @return array
     */
    public static function getBrandInfo($brandId, $uid)
    {
        $data = array();
        $imgs = array();
        //获取品牌简介
        $res = BrandData::getBrandIntro($brandId, $uid);
        if (isset($res['data']) && $res['data']) {
            //获取品牌下的产品信息
            $proInfo = self::getProductByBrand($brandId);
            $proInfoTmp = $proInfo['data']['product_list'];
            if (isset($proInfoTmp) && $proInfoTmp) {
                foreach ($proInfoTmp as $v) {
                    $imgs[] = array(
                        'src' => Images::getImageUrl($v['default_images'], 80, 100, 3, 'goodsimg')
                    );
                }
            }
            //整合
            $data = array(
                'key' => $res['data']['brand_id'],
                'icon' => Helpers::getImageUrl($res['data']['brand_ico'], 80, 50, 3),
                'title' => $res['data']['brand_name'],
                'content' => $res['data']['brand_intro'],
                'subtitle' => 'FEATURED ITEMS',
                'imgs' => $imgs
            );
        }
        return $data;
    }

    /**
     * 根据brandId获取产品信息
     *
     * @param int $brandId
     * @param int $num
     * @return array
     */
    public static function getProductByBrand($brandId, $num = 3)
    {
        $condition['viewNum'] = $num;
        $condition['brand'] = $brandId;
        return SearchData::searchElasticByCondition($condition);
    }

    /**
     * 获取品牌一览资源位&channelType
     * 
     * @param string $channelStr
     * @return array
     */
    public static function switchBrandParams($channelStr = 'boys')
    {
        $res = array('channelType' => 1, 'brandCode' => self::BOYS_BRAND_CODE);
        switch ($channelStr) {
            case HomeModel::COOKIE_NAME_BOYS:
                $res['channelType'] = 1;
                $res['brandCode'] = self::BRAND_PLUSSTAR_BANNER_BOYS; //self::BOYS_BRAND_CODE;
                break;
            case HomeModel::COOKIE_NAME_GIRLS:
                $res['channelType'] = 2;
                $res['brandCode'] = self::BRAND_PLUSSTAR_BANNER_GIRLS; //self::GIRLS_BRAND_CODE;
                break;
            case HomeModel::COOKIE_NAME_KIDS:
                $res['channelType'] = 3;
                $res['brandCode'] = self::KIDS_BRAND_CODE;
                break;
            case HomeModel::COOKIE_NAME_LIFESTYLE:
                $res['channelType'] = 4;
                $res['brandCode'] = self::LIFESTYLE_BRAND_CODE;
                break;
            default:
                $res['channelType'] = 1;
                $res['brandCode'] = self::BOYS_BRAND_CODE;
                break;
        }
        return $res;
    }

    /**
     * 多个品牌ID获取品牌信息
     * 
     * @param array $brandIds
     * @return array
     */
    public static function getBrandInfoByIds(array $brandIds)
    {
        $key = WebCacheConfig::KEY_WEB_INDEX_BRANDS_BRANDINFO . '_' . implode(',', $brandIds);
        $brandsInfo = Cache::get($key);
        if (empty($brandsInfo)) {
            Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URL_PRODUCT_BRAND, 'getBrandByIds', array(implode(',', $brandIds), 'id,brand_name,brand_intro,brand_domain'), function($data)use(&$brandsInfo) {
                foreach ($data['data'] as $k => $val) {
                    $val['desc'] = trim(str_replace(array("\t", "\n", "\r", "&nbsp;"), '', strip_tags($val['brand_intro'])));
                    $val['url'] = Helpers::url('', array(), $val['brand_domain']);
                    unset($val['brand_intro']);
                    $brandsInfo[$k] = $val;
                }
            });

            /* foreach ($brandIds as $brandId) {
              Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URL_PRODUCT_BRAND, 'getOneBrand', array($brandId, 'id,brand_name,brand_intro,brand_domain'), function($data)use(&$brandsInfo) {
              if (isset($data['data']['id'])) {
              $data['data']['desc'] = trim(str_replace(array("\t", "\n", "\r", "&nbsp;"), '', strip_tags($data['data']['brand_intro'])));
              $data['data']['url'] = Helpers::url('', array(), $data['data']['brand_domain']);
              unset($data['data']['brand_intro']);
              $brandsInfo[$data['data']['id']] = $data['data'];
              }
              });
              } */
            Yohobuy::yarConcurrentLoop();
            if (!empty($brandsInfo)) {
                Cache::set($key, $brandsInfo, 3600);
            }
        }

        //master没有数据, 取二层缓存
        if (empty($brandsInfo)) {
            $brandsInfo = Cache::get($key, 'slave');
        }
        return $brandsInfo;
    }

    /**
     * 获取plusstar品牌列表项目
     * 
     * @param string $channel
     * @return array 
     */
    public static function getPlusstarBrandListItem($channel)
    {
        $channels = array(
            'boys' => self::BRAND_PLUSSTAR_BANNER_BOYS,
            'girls' => self::BRAND_PLUSSTAR_BANNER_GIRLS,
            'kids' => self::BRAND_PLUSSTAR_BANNER_BOYS,
            'lifestyle' => self::BRAND_PLUSSTAR_BANNER_BOYS
        );
        $code = $channels[$channel];
        $key = WebCacheConfig::KEY_WEB_BRAND_PLUSSTAR_BANNER_DATA . '_' . $code;
        $items = Cache::get($key);
        if (empty($items)) {
            $resource = IndexData::getResourceData($code, 'web');
            if (isset($resource['data']) && !empty($resource['data']) && $resource['code'] == 200) {
                $items = array(
                    0 => array('name' => '所有品牌', 'src' => '', 'url' => Helpers::url('/brands'), 'brandType' => ''),
                    1 => array('name' => '设计新潮', 'src' => '', 'url' => '', 'brandType' => 4),
                    2 => array('name' => '潮流经典', 'src' => '', 'url' => '', 'brandType' => 1),
                    3 => array('name' => '明星潮牌', 'src' => '', 'url' => '', 'brandType' => 2),
                    4 => array('name' => '原创潮牌', 'src' => '', 'url' => '', 'brandType' => 3),
                );
                $resource = $resource['data'];
                $pos = 0;
                foreach ($items as $id => &$val) {
                    if (empty($val['url'])) {
                        $val['url'] = Helpers::url('/brands/plusstar?id=' . $id);
                    }
                    if (isset($resource[0]['data'][$pos])) {
                        $val['url'] = $resource[0]['data'][$pos]['url'];
                        $val['src'] = Images::getImageUrl($resource[0]['data'][$pos]['src'], 222, 180, 1);
                        $val['name'] = $resource[0]['data'][$pos]['title'];
                    }
                    $pos++;
                }
                //设置master,slave
                Cache::set($key, $items, 3600);
            }
        }
        //master没有数据, 取二层缓存
        if (empty($items)) {
            $items = Cache::get($key, 'slave');
        }
        return $items;
    }

    /**
     * 获取文章
     *
     * @param array $newIds
     * @return array
     */
    public static function getArticles($newIds)
    {
        $key = WebCacheConfig::KEY_WEB_BRAND_PLUSSTARINDEX_ARTICLES_DATA . '_' . implode(',', $newIds);
        $articles = Cache::get($key);
        if (empty($articles)) {
            foreach ($newIds as $newId) {
                Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URL_GUANG_ARTICLE, 'getArticle', array($newId, 'h5'), function($val)use(&$articles) {
                    if (isset($val['id'])) {
                        $articles[$val['id']] = array('id' => $val['id'], 'title' => $val['article_title'],
                            'url' => Helpers::url('/' . $val['id'] . '.html', array(), 'guang'), 'src' => $val['cover_image'], 'summary' => $val['article_summary']);
                    }
                });
            }
//             Yohobuy::yarConcurrentCall(Yohobuy::SERVICE_URL . self::URL_GUANG_ARTICLE, 'getArtilceByIds', array($newIds, 'h5'), function($data)use(&$articles) {
//                 foreach ($data['data'] as $val) {
//                     if (isset($val['id'])) {
//                         $articles[$val['id']] = array('id' => $val['id'], 'title' => $val['title'],
//                             'url' => Helpers::getFilterUrl($val['url']), 'src' => $val['src'], 'summary' => $val['intro']);
//                     }
//                 }
//             });
            Yohobuy::yarConcurrentLoop();
            if (!empty($articles)) {
                //设置master,slave
                Cache::set($key, $articles, 3600);
            }
        }
        //master没有数据, 取二层缓存
        if (empty($articles)) {
            $articles = Cache::get($key, 'slave');
        }
        return $articles;
    }

    /**
     * 获取plusstar资源信息
     * 
     * @param string $channel
     * @param array $articles
     * @return array
     */
    public static function getPlusstarIndexInfo($channel, array $articles)
    {
        $channels = array(
            'boys' => self::BRAND_PLUSSTARINDEX_BOYS,
            'girls' => self::BRAND_PLUSSTARINDEX_GIRLS,
            'kids' => self::BRAND_PLUSSTARINDEX_BOYS,
            'lifestyle' => self::BRAND_PLUSSTARINDEX_BOYS
        );
        $code = $channels[$channel];
        $key = WebCacheConfig::KEY_WEB_BRAND_PLUSSTARINDEX_DATA . '_' . $code;
        $data = Cache::get($key);
        if (empty($data)) {
            $resource = IndexData::getResourceData($code);
            if (isset($resource['data']) && !empty($resource['data']) && $resource['code'] == 200) {
                $data = self::getFormatResourceData($resource['data'], $articles);
                //设置master,slave
                Cache::set($key, $data, 3600);
                //清空变量
                $resource = array();
            }
        }
        //master没有数据,资源位没有数据, 取二层缓存
        if (empty($data)) {
            $data = Cache::get($key, 'slave');
        }
        return $data;
    }

    /**
     * 获取格式化资源数据
     * 
     * @param array $resource
     * @param array $articles
     * @return array
     */
    private static function getFormatResourceData(array $resource, array $articles)
    {
        $data = Channel::getFormat('plusstarIndex', $resource);
        $brands = $data[1]['category'];
        foreach ($brands['list'] as &$val) {//品牌列表
            $val['img'] = preg_replace('@imageView.+@is', 'imageView/1/w/378/h/250', $val['img']);
        }
        $data[1] = array('brand' => $brands);
        $newsList = $ads = $newsIds = $news = array();
        $len = count($data);
        $adsPos = $len - 1; //最后一个位置 :四个资讯
        $newsPos = $len - 2; //倒数第二个位置: 资讯的banner 
        if (isset($data[$newsPos]['news'])) {//资讯的banner
            $newsList = $data[$newsPos]['news'];
            $newsIds = array_keys($newsList['pics']['list']);
        }
        if (isset($data[$adsPos]['ads'])) {//四个资讯
            $ads = $data[$adsPos]['ads'];
            $newsIds = array_merge($newsIds, array_keys($ads['list']));
        }
        if (!empty($newsIds)) {
            $news = self::getArticles($newsIds);
        }
        if (!empty($newsList)) {//资讯的banner
            foreach ($newsList['pics']['list'] as $key => &$val) {
                $id = $val['id'];
                if (isset($news[$id])) {
                    $val['img'] = Images::getImageUrl($news[$id]['src'], 764, 436, 1);
                    $val['tips'] = $news[$id]['title'];
                    $val['href'] = $news[$id]['url'];
                }
                else {
                    unset($newsList['pics']['list'][$key]);
                }
            }
            $newsList['pics']['list'] = array_values($newsList['pics']['list']);
            $newsList['txts']['list'] = array_slice($articles, 0, 11); //资讯列表
            $data[$newsPos]['news'] = $newsList; //资讯
        }
        if (!empty($ads)) {//四个资讯
            foreach ($ads['list'] as $key => &$val) {
                $id = $val['id'];
                if (isset($news[$id])) {
                    $val['img'] = Images::getImageUrl($news[$id]['src'], 280, 160, 1);
                    $val['name'] = $news[$id]['title'];
                    $val['href'] = $news[$id]['url'];
                    //  $val['des'] = $news[$id]['summary'];
                }
                else {
                    unset($ads['list'][$key]);
                }
            }
            $data[$adsPos]['ads']['list'] = array_values(array_slice($ads['list'], 0, 4));
        }
        return $data;
    }

}