Blame view

yohobuy/www.yohobuy.com/application/models/Index/Home.php 7.65 KB
whb authored
1 2
<?php
namespace Index;
whb authored
3
whb authored
4 5 6 7 8 9
use Configs\CacheConfig;
use Configs\ChannelConfig;
use Plugin\Helpers;
use Plugin\Cache;
use LibModels\Web\Home\IndexData;
use LibModels\Web\Product\SearchData;
whb authored
10
use Plugin\DataProcess\WebChannel\Channel as ChannelProcess;
whb authored
11
whb authored
12 13
/**
 * web首页模板数据模型
whb authored
14
 *
whb authored
15 16 17 18 19 20 21 22
 * @name HomeModel
 * @package models
 * @copyright yoho.inc
 * @version 1.0 (2015-12-20 11:08:21)
 * @author whb <huanbao.wang@yoho.cn>
 */
class HomeModel
{
whb authored
23
whb authored
24 25
    /* COOKIE标识访问的是男生频道 */
    const COOKIE_NAME_BOYS = 'boys';
whb authored
26
whb authored
27 28
    /* COOKIE标识访问的是女生频道 */
    const COOKIE_NAME_GIRLS = 'girls';
whb authored
29
whb authored
30 31
    /* COOKIE标识访问的是潮童频道 */
    const COOKIE_NAME_KIDS = 'kids';
whb authored
32
whb authored
33 34 35
    /* COOKIE标识访问的是创意生活频道 */
    const COOKIE_NAME_LIFESTYLE = 'lifestyle';
    
whb authored
36 37 38 39 40
    // 男首首页
    const CODE_BOYS_CHANNEL = 'c15bf0cbfbd4893fd70c869c991f6d3d';
    // 女首首页
    const CODE_GIRLS_CHANNEL = '4d897f3b8eec0c465db0125e5b1f9edf';
 // 'a519ba44ef3a85cf3c05e405c6ba8e53';
whb authored
41
    // 潮童首页
whb authored
42 43 44 45 46 47 48
    const CODE_KIDS_CHANNEL = '331994d6fa8dc87f92a26dd45c0aa071';
 // 'a7741b94e8bb9d56d0d36e00c05956f7';
                                                                  
    // 创意生活新
    const CODE_LIFESTYLE_CHANNEL = '1b053fd044834f5c86d5eb15fb237af9';
    
    // 创意生活
whb authored
49
    const CODE_LIFESTYLE_CHANNEL_1 = '380c38155fd8beee10913a3f5b462da6';
whb authored
50
whb authored
51
    const CODE_LIFESTYLE_CHANNEL_2 = '665f7c2fb9d037ee820766953ee34bf7';
whb authored
52
whb authored
53 54
    /**
     * 获取导航
whb authored
55 56
     *
     * @param string $channel            
whb authored
57 58 59 60 61
     * @return array
     */
    public static function getNavBars($channel = 'boys')
    {
        $data = IndexData::getNavData();
whb authored
62
        if (empty($data)) {
whb authored
63 64 65 66
            return array();
        }
        $menu = array();
        $item = array();
whb authored
67 68 69
        foreach ($data['data'] as $val) {
            $item = array(
                'name_cn' => $val['sort_name'], // 父级
whb authored
70
                'name_en' => $val['sort_name_en'],
whb authored
71 72
                'link' => $val['sort_url'],
                'icon' => $val['sort_ico'],
whb authored
73 74
                'classname' => str_replace(' ', '', strtolower($val['sort_name_en'])) == $channel ? $channel : '',
                'index_main' => 0,
whb authored
75 76 77
                'is_hot' => $val['is_hot'] == 'Y' ? true : false,
                'is_new' => $val['is_new'] == 'Y' ? true : false,
                'subnav' => array()
whb authored
78
            );
whb authored
79
            foreach ($val['sub'] as $sub) { // 二级
whb authored
80 81 82
                $index_sub = 0;
                $subnav = array(
                    'name' => $sub['sort_name'],
whb authored
83 84 85 86 87 88
                    '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,
                    'thirdnav' => array(),
                    'index_sub' => $index_sub ++
whb authored
89
                );
whb authored
90 91
                if (isset($sub['sub'])) {
                    foreach ($sub['sub'] as $thirdsub) { // 三级
whb authored
92 93
                        $thirdnav = array(
                            'title' => $thirdsub['sort_name'],
whb authored
94 95 96
                            'name_en' => $thirdsub['sort_name_en'],
                            'link' => $thirdsub['sort_url'],
                            'branditems' => array()
whb authored
97
                        );
whb authored
98 99
                        if (isset($thirdsub['sub'])) {
                            foreach ($thirdsub['sub'] as $fourthnav) { // 四级
whb authored
100
                                $thirdnav['branditems'][] = array(
whb authored
101
                                    'brandname' => $fourthnav['sort_name'],
whb authored
102
                                    'link' => $fourthnav['sort_url'],
whb authored
103
                                    'hot' => $fourthnav['is_hot'] == 'Y' ? 'hot' : ''
whb authored
104 105 106 107 108 109 110 111 112 113 114 115
                                );
                            }
                        }
                        $subnav['thirdnav'][] = $thirdnav;
                    }
                }
                $item['subnav'][] = $subnav;
            }
            $menu[] = $item;
        }
        return $menu;
    }
116 117 118 119 120 121 122 123 124 125 126 127 128 129
    
        /**
     * 选择频道
     *
     * @return void
     */
    public static function goSwitchChannel()
    {
        // 通过COOKIE检查是否已设置过首页频道时,跳转到对应的频道页
        if (!empty($_COOKIE['_Channel'])) {
            headers_sent() || header('Location: /' . $_COOKIE['_Channel']);
            exit();
        }
    }
whb authored
130
whb authored
131 132 133
    /**
     * 设置选择的频道保存到浏览器COOKIE
     *
whb authored
134
     * @param string $cookie            
whb authored
135 136 137 138 139 140
     * @return void
     */
    public static function setSwitchToCookie($cookie)
    {
        setcookie('_Channel', $cookie, time() + 86400 * 300, '/', COOKIE_DOMAIN);
    }
whb authored
141
whb authored
142 143
    /**
     * 获取选择频道
whb authored
144
     *
whb authored
145 146 147 148 149 150 151
     * @return string
     */
    public static function getSwitchChannel()
    {
        $channel = self::COOKIE_NAME_BOYS;
        if (empty($_COOKIE['_Channel'])) {
            self::setSwitchToCookie(self::COOKIE_NAME_BOYS);
whb authored
152
        } else {
whb authored
153 154 155 156
            $channel = $_COOKIE['_Channel'];
        }
        return $channel;
    }
whb authored
157
whb authored
158 159
    /**
     * 获取频道资源
whb authored
160 161 162
     *
     * @param string $channel            
     * @param string $content_code            
whb authored
163 164
     * @return array
     */
whb authored
165
    public static function getChannelResource($channel, $content_code)
whb authored
166
    {
whb authored
167 168 169
        $resource = IndexData::getResourceData($content_code);
        // 格式化数据
        $data = ChannelProcess::getFormat($channel, $resource['data']);
whb authored
170
        return $data;
whb authored
171
    }
whb authored
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

    /**
     * 获取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;
    }
whb authored
206 207
    /**
     * 获取最新上架商品
whb authored
208 209
     *
     * @param string $channel            
whb authored
210 211 212 213 214 215 216
     * @return array
     */
    public static function getNewArrival($channel)
    {
        $result = array();
        $params = array(
            'order' => 's_t_desc',
whb authored
217
            'shelve_time' => strtotime("-60 days") . ',' . time()
whb authored
218
        );
whb authored
219 220
        // 最新上架分类
        if (isset(ChannelConfig::$newArrivalSortList[$channel])) {
whb authored
221
            $sortList = ChannelConfig::$newArrivalSortList[$channel];
whb authored
222 223 224 225 226 227 228 229 230 231 232 233
            // 获取分类列表获取商品信息
            $goodsList = SearchData::getSearchDataBySort($params, $sortList);
            foreach ($goodsList as $goods) {
                // 格式化数据
                $val = Helpers::formatProduct($goods, true, true, true, 280, 373);
                if ($val['price'] == false) {
                    $val['price'] = $val['salePrice'];
                }
                if (! empty($val)) {
                    $result[] = $val;
                }
            }
whb authored
234 235 236 237
        }
        return $result;
    }
}