Home.php 12.7 KB
<?php
namespace Index;

use Configs\WebCacheConfig;
use Configs\ChannelConfig;
use WebPlugin\Helpers;
use WebPlugin\Cache;
use LibModels\Web\Home\IndexData;
use LibModels\Web\Product\SearchData;
use WebPlugin\DataProcess\Channel as ChannelProcess;

/**
 * web首页模板数据模型
 *
 * @name HomeModel
 * @package models
 * @copyright yoho.inc
 * @version 1.0 (2015-12-20 11:08:21)
 * @author whb <huanbao.wang@yoho.cn>
 */
class HomeModel
{

    /* COOKIE标识访问的是男生频道 */
    const COOKIE_NAME_BOYS = 'boys';

    /* COOKIE标识访问的是女生频道 */
    const COOKIE_NAME_GIRLS = 'girls';

    /* COOKIE标识访问的是潮童频道 */
    const COOKIE_NAME_KIDS = 'kids';

    /* COOKIE标识访问的是创意生活频道 */
    const COOKIE_NAME_LIFESTYLE = 'lifestyle';
    
    //production:                                  test:
   //boys:79372627eee75d73afe7f9bac91e5ce6, c15bf0cbfbd4893fd70c869c991f6d3d
   //girls: 75215008957605c05e8cd375eac4f817, a519ba44ef3a85cf3c05e405c6ba8e53
   //kids:d71f4b27f2a7229fbb31a4bc490a6f36,a7741b94e8bb9d56d0d36e00c05956f7
   //lifestyle:8a341ca7eacc069ba80f02dec80eaf34,1b053fd044834f5c86d5eb15fb237af9
       
    // 男首首页
    const CODE_BOYS_CHANNEL = '79372627eee75d73afe7f9bac91e5ce6';
    // 女首首页
    const CODE_GIRLS_CHANNEL =  '75215008957605c05e8cd375eac4f817';

    // 潮童首页
    const CODE_KIDS_CHANNEL =  'd71f4b27f2a7229fbb31a4bc490a6f36';
                                            
    // 创意生活新
    const CODE_LIFESTYLE_CHANNEL = '8a341ca7eacc069ba80f02dec80eaf34';
    
    // 创意生活
    const CODE_LIFESTYLE_CHANNEL_1 = '380c38155fd8beee10913a3f5b462da6';

    const CODE_LIFESTYLE_CHANNEL_2 = '665f7c2fb9d037ee820766953ee34bf7';

    /**
     * 获取导航
     *
     * @param string $channel            
     * @return array
     */
    public static function getNavBars($channel = 'boys')
    {
        $key = WebCacheConfig::KEY_WEB_HOME_NAVBAR_DATA . '_' . $channel;
        $menu = Cache::get($key);
        //数据缓存不存在
        if(empty($menu)) {
            $data = IndexData::getNavData(); 
            //接口存在数据
            if(!empty($data['data'])) {
                $item = array();
                $subnav = array();
                $thirdnav = array();
                $indexMain = 0;
                $indexSub = 0;
                
                foreach ($data['data'] as $val) {
                    $item = array(
                        'name_cn' => $val['sort_name'], // 父级
                        'name_en' => $val['sort_name_en'],
                        'link' => $val['sort_url'],
                        'icon' => $val['sort_ico'],
                        'classname' => str_replace(' ', '', strtolower($val['sort_name_en'])) == $channel ? $channel .' cure' : '',
                        'index_main' => $indexMain ++,
                        'content_code' => $val['content_code'],
                        'is_hot' => $val['is_hot'] == 'Y' ? true : false,
                        'is_new' => $val['is_new'] == 'Y' ? true : false,
                        //  'subnav' => array()
                    );
                    $indexSub = 0;
                    foreach ($val['sub'] as $sub) { // 二级
                        $subnav = array(
                            'name' => $sub['sort_name'],
                            'name_en' => $sub['sort_name_en'],
                            'link' => $sub['sort_url'],
                            'is_hot' => $sub['is_hot'] == 'Y' ? true : false,
                            'is_new' => $sub['is_new'] == 'Y' ? true : false,
                            'content_code' => $sub['content_code'],
                            //    'thirdnav' => array(),
                            'index_sub' => $indexSub ++
                        );
                        if (isset($sub['sub'])) {
                            $thirdnav = array();
                            foreach ($sub['sub'] as $thirdsub) { // 三级
                                $thirdnav = array(
                                    'title' => $thirdsub['sort_name'],
                                    'name_en' => $thirdsub['sort_name_en'],
                                    'link' => $thirdsub['sort_url'],
                                    'content_code' => $thirdsub['content_code'],
                                    //       'branditems' => array()
                                );
                                if (isset($thirdsub['sub'])) {
                                    foreach ($thirdsub['sub'] as $fourthnav) { // 四级
                                        $thirdnav['branditems'][] = array(
                                            'brandname' => $fourthnav['sort_name'],
                                            'link' => $fourthnav['sort_url'],
                                            'hot' => $fourthnav['is_hot'] == 'Y' ? 'hot' : ''
                                        );
                                    }
                                }
                                $subnav['thirdnav'][] = $thirdnav;
                            }
                        }
                
                        $item['subnav'][] = $subnav;
                    }
                    $menu[] = $item;
                }
                Cache::set($key, $menu, 3600);
            }
        }
        
        //数据缓存和接口都空的,取二级数据
        if(empty($menu)) {
            $menu = Cache::get($key,'slave');
        }
        
        return $menu;
    }
    
    /**
     * 选择频道
     *
     * @return void
     */
    public static function goSwitchChannel()
    {
        // 通过COOKIE检查是否已设置过首页频道时,跳转到对应的频道页
        if (!empty($_COOKIE['_Channel'])) {
            headers_sent() || header('Location: /' . $_COOKIE['_Channel']);
            exit();
        }
    }

    /**
     * 设置选择的频道保存到浏览器COOKIE
     *
     * @param string $cookie            
     * @return void
     */
    public static function setSwitchToCookie($cookie)
    {
        setcookie('_Channel', $cookie, time() + 86400 * 300, '/', COOKIE_DOMAIN);
    }

    /**
     * 获取选择频道
     *
     * @return string
     */
    public static function getSwitchChannel()
    {
        $channel = self::COOKIE_NAME_BOYS;
        if (empty($_COOKIE['_Channel'])) { 
            self::setSwitchToCookie(self::COOKIE_NAME_BOYS);
        } else {
            $channel = $_COOKIE['_Channel'];
        }

        return $channel;
    }
    
    /**
     * 获取首页频道nav
     * 
     * @param string $channel
     * @return array
     */
    public static function getHomeChannelNav($channel = null) 
    {
        if (null === $channel) {
            $channel = self::getSwitchChannel();
        }
        
        //根据频道判断home页地址
        switch ($channel) {
            case 'boys':
                $home = Helpers::url('', '', 'default');
                break;
            case 'girls':
                $home = Helpers::url('/woman', '', 'new');
                break;
            case 'lifestyle':
                $home = Helpers::url('/lifestyle', '', 'new');
                break;
            case 'kids':
                $home = Helpers::url('/kids', '', 'new');
                break;
            default:
                $home = Helpers::url('', '', 'default');
                break;
        }
        return array('href' => $home, 'name' => strtoupper($channel) . '首页', 'pathTitle' => 'YOHO!有货');
        
    }
    /**
     * 获取频道资源
     *
     * @param string $channel 频道标识(boys,girls,kids,lifestyle)        
     * @param string $contentCode 资源码           
     * @return array
     */
    public static function getChannelResource($channel, $contentCode)
    {
        $key = WebCacheConfig::KEY_WEB_HOME_CHANNEL_DATA . '_' . $contentCode;
        $data = Cache::get($key);
        if(empty($data)) {
            $resource = IndexData::getResourceData($contentCode);
            if (isset($resource['data']) && !empty($resource['data']) && $resource['code'] == 200) {
                //格式化数据
                $data = ChannelProcess::getFormat($channel, $resource['data']);
                //设置master,slave
                Cache::set($key, $data, 3600);
                //清空变量
                $resource = array();
            }
        }
        //master没有数据,资源位没有数据, 取二层缓存
        if(empty($data)) {
            $data = Cache::get($key, 'slave');
        }
        return $data;
    }
    
    /**
     * 获取lifesytle频道资源
     *
     * @return array
     */
    public static function getLifestyleChannel()
    {
        $resouce1 = IndexData::getResourceData(self::CODE_LIFESTYLE_CHANNEL_1);
        $focus = $resouce1['data'];
        $resouce2 = IndexData::getResourceData(self::CODE_LIFESTYLE_CHANNEL_2);
        $floor = $resouce2['data'];
        $resource = array(
            0 => $focus[0],
            1 => array(
                'template_name' => 'text',
                'data' => array(
                    'text' => '优选品牌 BRAND'
                )
            ),
            2 => $focus[1],
            3 => $focus[2],
            4 => $focus[3]
        );
        foreach ($floor as $val) {
            $resource[] = $val;
        }
        foreach (array_slice($focus, 4, 20) as $val) {
            $resource[] = $val;
        }
        $data = ChannelProcess::getFormat('lifestyle', $resource);
        return $data;
    }

    /**
     * 获取最新上架商品
     *
     * @param string $channel            
     * @return array
     */
    public static function getNewArrival($channel)
    {
        $key = WebCacheConfig::KEY_WEB_HOME_CHANNEL_NEWARRIVAL_DATA . '_' . $channel;
        //缓存数据
        $result = Cache::get($key);
        if (empty($result)) {
            $params = array(
                'order' => 's_t_desc',
                'shelve_time' => strtotime("-60 days") . ',' . time()
            );
            //男首频道最新上架参数是gender=1,3
            if($channel == self::COOKIE_NAME_BOYS) {
                $params['gender'] = '1,3';
            }
            //女首频道最新上架参数是gender=2,3
            else if($channel == self::COOKIE_NAME_GIRLS) {
                $params['gender'] = '2,3';
            }
            // 最新上架分类
            if (isset(ChannelConfig::$newArrivalSortList[$channel])) {
                $sortList = ChannelConfig::$newArrivalSortList[$channel];
                // 获取分类列表获取商品信息
                $goodsList = SearchData::getSearchDataBySort($params, $sortList);
                $pos = 1;
                $val = array();
                foreach ($goodsList as $goods) {
                    // 格式化数据
                    $val = Helpers::formatProduct($goods, true, true, true, 280, 373);
                    if (!$val) {
                        continue;
                    }
                    if ($val['price'] == false) {
                        $val['price'] = $val['salePrice'];
                    }
                    $val['url'] = sprintf('%s?channel=%s&from=%s-n_%s', $val['url'], $channel , $channel, $pos++);
                    //TODO 字段要调整
                    $val['isFew'] = $val['is_soon_sold_out'];
                    $val['tags']['isLimit'] = $val['tags']['is_limited'];
                    $val['tags']['isSale'] = false;//$val['tags']['is_discount'];暂时不显示
                    $val['tags']['isNew'] = false;//$val['tags']['is_new'];暂时不显示
                    $val['tags']['isYearMidPromotion'] = $val['tags']['midYear'];
                    $val['tags']['isYearEndPromotion'] = $val['tags']['yearEnd'];
                    $val['tags']['isReNew'] = false;//$val['tags']['is_advance'];暂时不显示
                    unset($val['tags']['is_advance'], $val['tags']['is_discount'], $val['tags']['is_limited'], $val['tags']['is_new'],
                            $val['tags']['is_yohood'], $val['tags']['midYear'], $val['tags']['yearEnd']);
                    $result[] = $val;
                }
            }
            Cache::set($key, $result, 3600);
        }
        // 当一级缓存失效,并且接口异常时, 尝试从二级缓存获取数据
        if (empty($result)) {
            $result = Cache::get($key, 'slave');
        }
        
        return $result;
    }
}