Blame view

yohobuy/www.yohobuy.com/application/models/Index/Home.php 16 KB
whb authored
1 2
<?php
namespace Index;
whb authored
3
4
use Configs\WebCacheConfig;
whb authored
5
use Configs\ChannelConfig;
whb authored
6 7
use WebPlugin\Helpers;
use WebPlugin\Cache;
whb authored
8 9
use Api\Yohobuy;
use WebPlugin\Images;
whb authored
10 11
use LibModels\Web\Home\IndexData;
use LibModels\Web\Product\SearchData;
whb authored
12
use WebPlugin\DataProcess\Channel as ChannelProcess;
13
whb authored
14 15
/**
 * web首页模板数据模型
whb authored
16
 *
whb authored
17 18 19 20 21 22 23 24
 * @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
25
whb authored
26 27
    /* COOKIE标识访问的是男生频道 */
    const COOKIE_NAME_BOYS = 'boys';
whb authored
28
whb authored
29 30
    /* COOKIE标识访问的是女生频道 */
    const COOKIE_NAME_GIRLS = 'girls';
whb authored
31
whb authored
32 33
    /* COOKIE标识访问的是潮童频道 */
    const COOKIE_NAME_KIDS = 'kids';
whb authored
34
whb authored
35 36 37
    /* COOKIE标识访问的是创意生活频道 */
    const COOKIE_NAME_LIFESTYLE = 'lifestyle';
    
whb authored
38
    //production:                                  test:
whb authored
39 40 41 42
   //boys:79372627eee75d73afe7f9bac91e5ce6, c15bf0cbfbd4893fd70c869c991f6d3d
   //girls: 75215008957605c05e8cd375eac4f817, a519ba44ef3a85cf3c05e405c6ba8e53
   //kids:d71f4b27f2a7229fbb31a4bc490a6f36,a7741b94e8bb9d56d0d36e00c05956f7
   //lifestyle:8a341ca7eacc069ba80f02dec80eaf34,1b053fd044834f5c86d5eb15fb237af9
whb authored
43
       
whb authored
44
    // 男首首页
45
    const CODE_BOYS_CHANNEL = '79372627eee75d73afe7f9bac91e5ce6';
whb authored
46
    // 女首首页
47
    const CODE_GIRLS_CHANNEL =  '75215008957605c05e8cd375eac4f817';
whb authored
48
whb authored
49
    // 潮童首页
50
    const CODE_KIDS_CHANNEL =  'd71f4b27f2a7229fbb31a4bc490a6f36';
whb authored
51
                                            
whb authored
52
    // 创意生活新
53
    const CODE_LIFESTYLE_CHANNEL = '8a341ca7eacc069ba80f02dec80eaf34';
whb authored
54 55
    
    // 创意生活
whb authored
56
    const CODE_LIFESTYLE_CHANNEL_1 = '380c38155fd8beee10913a3f5b462da6';
whb authored
57
whb authored
58
    const CODE_LIFESTYLE_CHANNEL_2 = '665f7c2fb9d037ee820766953ee34bf7';
whb authored
59
whb authored
60 61
    /**
     * 获取导航
whb authored
62 63
     *
     * @param string $channel            
whb authored
64 65 66 67
     * @return array
     */
    public static function getNavBars($channel = 'boys')
    {
68
        $key = WebCacheConfig::KEY_WEB_HOME_NAVBAR_DATA . '_' . $channel;
whb authored
69
        $menu = Cache::get($key);
70 71
        //数据缓存不存在
        if(empty($menu)) {
72
            $data = IndexData::getNavData(); 
whb authored
73 74 75
            //接口存在数据
            if(!empty($data['data'])) {
                $item = array();
76 77 78 79
                $subnav = array();
                $thirdnav = array();
                $indexMain = 0;
                $indexSub = 0;
wangqing authored
80
                
whb authored
81 82
                foreach ($data['data'] as $val) {
                    $item = array(
whb authored
83 84
                        'name_cn' => $val['sort_name'], // 父级
                        'name_en' => $val['sort_name_en'],
梁志锋 authored
85
                        'link' =>  Helpers::getUrlSafe($val['sort_url']),
郝肖肖 authored
86
                        'icon' => Helpers::getUrlSafe($val['sort_ico']),
87
                        'classname' => str_replace(' ', '', strtolower($val['sort_name_en'])) == $channel ? $channel .' cure' : '',
wangqing authored
88
                        'index_main' => $indexMain ++,
whb authored
89 90 91
                        'content_code' => $val['content_code'],
                        'is_hot' => $val['is_hot'] == 'Y' ? true : false,
                        'is_new' => $val['is_new'] == 'Y' ? true : false,
92
                        'isNewPage' => $val['is_new_page'] == 'Y' ? true : false,
whb authored
93
                        //  'subnav' => array()
whb authored
94
                    );
yyq authored
95
yyq authored
96 97 98
                    // 奥莱头部显示配置图标
                    if (strtolower($val['sort_name_en']) === 'outlets' &&
                        $channel !== 'outlets') {
yyq authored
99 100 101
                        $item['showIcon'] = true;
                    };
102
                    $indexSub = 0;
梁志锋 authored
103 104 105

                    //判断sub变量是否存在
                    $val['sub'] = empty($val['sub']) ? array() : $val['sub'];
whb authored
106 107
                    foreach ($val['sub'] as $sub) { // 二级
                        $subnav = array(
whb authored
108 109
                            'name' => $sub['sort_name'],
                            'name_en' => $sub['sort_name_en'],
梁志锋 authored
110
                            'link' =>  Helpers::getUrlSafe($sub['sort_url']),
whb authored
111 112 113 114
                            'is_hot' => $sub['is_hot'] == 'Y' ? true : false,
                            'is_new' => $sub['is_new'] == 'Y' ? true : false,
                            'content_code' => $sub['content_code'],
                            //    'thirdnav' => array(),
115
                            'index_sub' => $indexSub ++
whb authored
116 117
                        );
                        if (isset($sub['sub'])) {
118
                            $thirdnav = array();
whb authored
119 120
                            foreach ($sub['sub'] as $thirdsub) { // 三级
                                $thirdnav = array(
whb authored
121 122
                                    'title' => $thirdsub['sort_name'],
                                    'name_en' => $thirdsub['sort_name_en'],
梁志锋 authored
123
                                    'link' =>  Helpers::getUrlSafe($thirdsub['sort_url']),
whb authored
124 125
                                    'content_code' => $thirdsub['content_code'],
                                    //       'branditems' => array()
whb authored
126 127 128 129
                                );
                                if (isset($thirdsub['sub'])) {
                                    foreach ($thirdsub['sub'] as $fourthnav) { // 四级
                                        $thirdnav['branditems'][] = array(
whb authored
130
                                            'brandname' => $fourthnav['sort_name'],
梁志锋 authored
131
                                            'link' =>  Helpers::getUrlSafe($fourthnav['sort_url']),
whb authored
132
                                            'hot' => $fourthnav['is_hot'] == 'Y' ? 'hot' : ''
whb authored
133 134
                                        );
                                    }
whb authored
135
                                }
whb authored
136
                                $subnav['thirdnav'][] = $thirdnav;
whb authored
137 138
                            }
                        }
whb authored
139 140
                
                        $item['subnav'][] = $subnav;
whb authored
141
                    }
whb authored
142
                    $menu[] = $item;
whb authored
143
                }
whb authored
144
                Cache::set($key, $menu, 1800);
whb authored
145
            }
whb authored
146
        }
147
        
whb authored
148
        //数据缓存和接口都空的,取二级数据
149
        if(empty($menu)) {
whb authored
150 151
            $menu = Cache::get($key,'slave');
        }
whb authored
152 153
        return $menu;
    }
154
    
whb authored
155
    /**
156 157 158 159 160 161 162 163 164 165 166 167
     * 选择频道
     *
     * @return void
     */
    public static function goSwitchChannel()
    {
        // 通过COOKIE检查是否已设置过首页频道时,跳转到对应的频道页
        if (!empty($_COOKIE['_Channel'])) {
            headers_sent() || header('Location: /' . $_COOKIE['_Channel']);
            exit();
        }
    }
whb authored
168
whb authored
169 170 171
    /**
     * 设置选择的频道保存到浏览器COOKIE
     *
whb authored
172
     * @param string $cookie            
whb authored
173 174 175 176 177 178
     * @return void
     */
    public static function setSwitchToCookie($cookie)
    {
        setcookie('_Channel', $cookie, time() + 86400 * 300, '/', COOKIE_DOMAIN);
    }
whb authored
179
whb authored
180 181
    /**
     * 获取选择频道
whb authored
182
     *
whb authored
183 184 185 186 187
     * @return string
     */
    public static function getSwitchChannel()
    {
        $channel = self::COOKIE_NAME_BOYS;
whb authored
188
        if (empty($_COOKIE['_Channel'])) { 
whb authored
189
            self::setSwitchToCookie(self::COOKIE_NAME_BOYS);
whb authored
190
        } else {
whb authored
191 192
            $channel = $_COOKIE['_Channel'];
        }
whb authored
193
whb authored
194 195
        return $channel;
    }
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
    
    /**
     * 获取首页频道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;
        }
227
        return array('href' => $home, 'name' => strtoupper($channel) . '首页', 'pathTitle' => 'YOHO!BUY 有货');
228 229
        
    }
whb authored
230 231
    /**
     * 获取频道资源
whb authored
232
     *
233 234
     * @param string $channel 频道标识(boys,girls,kids,lifestyle)        
     * @param string $contentCode 资源码           
whb authored
235 236
     * @return array
     */
237
    public static function getChannelResource($channel, $contentCode)
whb authored
238
    {
239
        $key = WebCacheConfig::KEY_WEB_HOME_CHANNEL_DATA . '_' . $contentCode;
whb authored
240 241
        $data = Cache::get($key);
        if(empty($data)) {
242 243
            $resource = IndexData::getResourceData($contentCode);
            if (isset($resource['data']) && !empty($resource['data']) && $resource['code'] == 200) {
whb authored
244 245
                //格式化数据
                $data = ChannelProcess::getFormat($channel, $resource['data']);
246
                //设置master,slave
whb authored
247
                Cache::set($key, $data);
248 249
                //清空变量
                $resource = array();
whb authored
250 251 252
            }
        }
        //master没有数据,资源位没有数据, 取二层缓存
253
        if(empty($data)) {
whb authored
254
            $data = Cache::get($key, 'slave');
whb authored
255
        }
whb authored
256
        return $data;
whb authored
257
    }
whb authored
258
    
whb authored
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
    /**
     * 获取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
292 293
    /**
     * 获取最新上架商品
whb authored
294 295
     *
     * @param string $channel            
whb authored
296 297 298 299
     * @return array
     */
    public static function getNewArrival($channel)
    {
300
        $key = WebCacheConfig::KEY_WEB_HOME_CHANNEL_NEWARRIVAL_DATA . '_' . $channel;
whb authored
301
        //缓存数据
whb authored
302
        $result = Cache::get($key);
303
        if (empty($result)) {
whb authored
304
            $params = array(
305 306
                'order' => 's_t_desc',
                'shelve_time' => strtotime("-60 days") . ',' . time()
whb authored
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
            );
            //男首频道最新上架参数是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;
322
                $val = array();
whb authored
323 324 325
                foreach ($goodsList as $goods) {
                    // 格式化数据
                    $val = Helpers::formatProduct($goods, true, true, true, 280, 373);
326 327 328
                    if (!$val) {
                        continue;
                    }
whb authored
329 330 331 332 333 334 335 336 337 338 339 340 341 342
                    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']);
343
                    $result[] = $val;
whb authored
344 345
                }
            }
whb authored
346
            Cache::set($key, $result);
whb authored
347
        }
348 349 350 351 352
        // 当一级缓存失效,并且接口异常时, 尝试从二级缓存获取数据
        if (empty($result)) {
            $result = Cache::get($key, 'slave');
        }
        
whb authored
353 354
        return $result;
    }
whb authored
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
    
    
    /**
     * 获取首页导航
     *
     * @return array
     */
    public static function getIndexGuide()
    {
        $channels = array(
            'boys'=> array('gender'=>'1,3','limit' => 1),
            'girls' => array('gender'=>'2,3', 'limit' => 1),
            'lifestyle' => array('msort' => 10 , 'misort' => '266,280,101,103,259', 'limit' => 1),
            'kids' => array('msort' => 365, 'limit' => 1)
        );
        $multiUrls = array();
        $key = WebCacheConfig::KEY_WEB_INDEX_GUIDE;
        $list = Cache::get($key);
        if(empty($list)) {
            $data = IndexData::getNavData();
            if(!empty($data['data']) && isset($data['code']) && $data['code'] == 200) {
                //获取分类
                foreach($data['data'] as $nav) {
                    $channel = str_replace(' ', '', strtolower($nav['sort_name_en']));
                    $list[] = array(
                        'channel' => $channel,
梁志锋 authored
381
                        'url' =>  Helpers::getUrlSafe($nav['sort_url']),
whb authored
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
                        'sort_name' => $nav['sort_name'],
                        'sort_name_en' => $nav['sort_name_en'],
                        'content_code' => $nav['content_code'],
                        'src'=> '',
                        'num' => 0,
                    );
                    //频道页面的搜索
                    if(isset($channels[$channel])) {
                        $multiUrls[$channel] = SearchData::getProductUrl($channels[$channel]);
                    }
                    //频道页的资源位
                    $multiUrls[$nav['content_code']] = IndexData::getResourceData($nav['content_code'], 'web', true);
                }
                $resources = Yohobuy::getMulti($multiUrls);
                foreach ($list as &$val) {
                    $code = $val['content_code'];
                    $channel = $val['channel'];
                    if(isset($resources[$code])) {
                        $data = current($resources[$code]);
                        if(isset($data['data'][0]['src'])) {
                            $val['src'] = Images::getImageUrl($data['data'][0]['src'], 180, 168);
                        }
                    }
                    if(isset($resources[$channel]['total'])) {
                        $val['num'] = $resources[$channel]['total'];
                    }
                }
whb authored
409
                Cache::set($key, $list, 1800);
whb authored
410 411 412 413 414 415 416 417
            }
        }
        // 当一级缓存失效,并且接口异常时, 尝试从二级缓存获取数据
        if (empty($list)) {
            $list = Cache::get($key, 'slave');
        }
        return $list;
    }
whb authored
418
}