Side.php 4.1 KB
<?php

namespace Index;

use LibModels\Wap\Home\SideData;
use Configs\CacheConfig;
use Plugin\Helpers;
use Plugin\Cache;

/**
 * 
 * @name SideModel
 * @package models/Index
 * @copyright yoho.inc
 * @version 1.0 (2015-10-21 21:21:09)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class SideModel
{

    /**
     * 获取左侧边栏数据
     * 
     * @param string $guangChoosed 逛默认选中项 "all"表示全部,"boys":只看男生,"girls":只看女生
     */
    public static function getLeftNav($guangChoosed = 'all')
    {
        $result = array();

        if (USE_CACHE) {
            // 先尝试获取一级缓存(master), 有数据则直接返回.
            $result = Cache::get(CacheConfig::KEY_COMMON_SIDE_NAV, 'master');
            if (!empty($result)) {
                $result[] = self::genLeftNavGuang($guangChoosed);
                return $result;
            }
        }

        // 调用接口获取后台配置的侧边栏数据
        $side = SideData::leftNav(); 
        if (!empty($side['data'])) {
            foreach ($side['data'] as $key => $value) {
                // 逛的按照原来的做法,使用配置
                if ($value['sort_name_en'] === 'TRENDFINDER') {
                    continue;
                }
                $result[$key]['textCn'] = $value['sort_name'];
                $result[$key]['textEn'] = $value['sort_name_en'];
                $result[$key]['styleClass'] = strtolower($value['sort_name_en']);
                $result[$key]['url'] = Helpers::getFilterUrl($value['sort_url']);
                $result[$key]['img'] = Helpers::getImageUrl($value['sort_ico'], 60, 60);
            }
        }

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

        // 追加逛的子分类数据
        $result[] = self::genLeftNavGuang($guangChoosed);

        return $result;
    }
    
    /**
     * 获取并生成左侧边栏中逛的数据
     * 
     * @param string $guangChoosed 逛默认选中项 "all"表示全部,"boys":只看男生,"girls":只看女生
     * @return array
     */
    private static function genLeftNavGuang($guangChoosed = 'all')
    {
        return array(
            'textCn' => '逛',
            'textEn' => 'TRENDFINDER',
            'styleClass' => 'guang',
            'subNav' => array(
                'list' => array(
                    0 => array(
                        'textCn' => '逛',
                        'textEn' => 'TrendFinder',
                        'back' => true,
                        'isSelect' => false,
                        'bgColor' => ($guangChoosed === 'girls') ? '#FF88AE' : false,
                    ),
                    1 => array(
                        'textCn' => '查看全部',
                        'textEn' => '',
                        'back' => false,
                        'isSelect' => ($guangChoosed === 'all') ? true : false,
                        'url' => Helpers::url('', array('gender' => '1,2,3'), 'guang')
                    ),
                    2 => array(
                        'textCn' => '只看男生',
                        'textEn' => 'Boys',
                        'back' => false,
                        'isSelect' => ($guangChoosed === 'boys') ? true : false,
                        'url' => Helpers::url('', array('gender' => '1,3'), 'guang')
                    ),
                    3 => array(
                        'textCn' => '只看女生',
                        'textEn' => 'Girls',
                        'back' => false,
                        'isSelect' => ($guangChoosed === 'girls') ? true : false,
                        'url' => Helpers::url('', array('gender' => '2,3'), 'guang')
                    ),
                )
            )
        );
    }

}