Side.php 6.75 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.'_'.$guangChoosed, 'master');
            if (!empty($result)) {
//                $result[] = self::genLeftNavGuang($guangChoosed);
                return $result;
            }
        }

        // 调用接口获取后台配置的侧边栏数据
        $side = SideData::leftNav();
        if (!empty($side['data']) && is_array($side['data'])) {
            $group = array();
            $num = 0;
            $groupKey = 0;
            $count = count($side['data']);
            foreach ($side['data'] as $key => $value) {
                if ($value['separative_sign'] === 'Y') {
                    $result[$num] = $group;
                    $num++;
                    $groupKey = 0;
                    $group = array();
                }

                // 逛的按照原来的做法,使用配置
                /*if ($value['sort_name_en'] === 'TRENDFINDER') {
                    continue;
                }*/
                $group[$groupKey] = self::formatSideItem($value);

                // 如果存在子菜单,就输出子菜单
                if (isset($value['sub']) && !empty($value['sub'])) {
                    unset($group[$groupKey]['url']);
                    
                    $subs = array(
                        array(
                            'textCn' => $group[$groupKey]['textCn'],
                            'textEn' => $group[$groupKey]['textEn'],
                            'back' => true,
                            'isSelect' => false,
                            'bgColor' => self::getSideTopColor($guangChoosed)
                        )
                    );
                    foreach ($value['sub'] as $oneSub) {
                        $subs[] = self::formatSideItem($oneSub);
                    }

                    $group[$groupKey]['styleClass'] = 'guang';
                    $group[$groupKey]['subNav']['list'] = $subs;
                }
                $groupKey++;

                if ($count === $key + 1) {
                    $result[$num] = $group;
                }
            }
        }

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

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

        return $result;
    }

    /**
     * 获取并生成左侧边栏中逛的数据
     *
     * @param string $guangChoosed 逛默认选中项 "all"表示全部,"boys":只看男生,"girls":只看女生
     * @return array
     */
    private static function genLeftNavGuang($guangChoosed = 'all')
    {
        // 判断逛的背景色
        $color = self::getSideTopColor($guangChoosed);

        return
            array(
                array(
                'textCn' => '逛',
                'textEn' => 'TRENDFINDER',
                'styleClass' => 'guang',
                'subNav' => array(
                    'list' => array(
                        0 => array(
                            'textCn' => '逛',
                            'textEn' => 'TrendFinder',
                            'back' => true,
                            'isSelect' => false,
                            'bgColor' => $color,
                        ),
                        1 => array(
                            'textCn' => '查看全部',
                            'textEn' => '',
                            'back' => false,
                            'isSelect' => ($guangChoosed === 'lifestyle' || $guangChoosed === 'kids') ? 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')
                        ),
                    )
                )
            )
        );
    }

    /**
     * 根据选择的频道获取逛的背景色
     * @param string $guangChoosed 逛默认选中项 "all"表示全部,"boys":只看男生,"girls":只看女生
     * @return string color值
     */
    private static function getSideTopColor($guangChoosed)
    {
        // 判断逛的背景色
        $color = false;
        if ($guangChoosed === 'girls') {
            $color = '#FF88AE';
        } else if ($guangChoosed === 'kids') {
            $color = '#7ad9f9';
        } else if ($guangChoosed === 'lifestyle') {
            $color = '#4f4138';
        }

        return $color;
    }


    /**
     * 格式化侧边栏数据
     *
     * @param array $data 要格式化的侧边栏数据
     * @return array
     */
    private static function formatSideItem($data)
    {
        $result = array();

        $result['textCn'] = $data['sort_name'];
        $result['textEn'] = $data['sort_name_en'];
        $result['styleClass'] = strtolower($data['sort_name_en']);
        $result['url'] = Helpers::getFilterUrl($data['sort_url']);
        $result['img'] = Helpers::getImageUrl($data['sort_ico'], 60, 60, 1);
        if (isset($data['sort_name_color'])) {
            $result['color'] = $data['sort_name_color'];
        }

        return $result;
    }

}