Plusstar.php
5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Guang;
use Configs\CacheConfig;
use LibModels\Wap\Guang\PlusstarData;
use Plugin\Helpers;
use Plugin\Cache;
/**
* 明星原创品牌相关的模板数据模型
*
* @name PlusstarModel
* @package models/Guang
* @copyright yoho.inc
* @version 1.0 (2015-10-23 15:30:00)
* @author fei.hong <fei.hong@yoho.cn>
*/
class PlusstarModel
{
/**
* 获取国际优选的品牌列表
*
* @param string $gender "1,3"表示男, "2,3"表示女
* @return array
*/
public static function getFirstBrands($gender)
{
$channel = '1';
switch (strval($gender)) {
case '1,3': // 男
$channel = '1';
break;
case '2,3': // 女
$channel = '2';
break;
default: // 默认
$gender = '1,3';
$channel = '1';
break;
}
if (USE_CACHE) {
$key = CacheConfig::KEY_ACTION_GUANG_PLUSSTAR_INDEX . $channel;
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
$result = array();
$brandList = PlusstarData::firstBrandList($gender, $channel);
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($brandList['recom']['data']['list'][0]['data']) || empty($brandList['all']['data']['list'][0]['data'])) {
if (USE_CACHE) {
$result = Cache::get($key, 'slave');
}
return $result;
}
$result['ps']['star'] = self::formatData($brandList['recom']['data']['list'][0]['data'], $gender, true);
$result['ps']['plus'] = self::formatData($brandList['all']['data']['list'][0]['data'], $gender, true);
// 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
if (USE_CACHE) {
Cache::set($key, $result);
}
return $result;
}
/**
* 获取明星原创的品牌列表
*
* @param string $gender "1,3"表示男, "2,3"表示女
* @return array
*/
public static function getBrands($gender)
{
$channel = '1';
switch (strval($gender)) {
case '1,3': // 男
$channel = '1';
break;
case '2,3': // 女
$channel = '2';
break;
default: // 默认
$gender = '1,3';
$channel = '1';
break;
}
if (USE_CACHE) {
$key = CacheConfig::KEY_ACTION_GUANG_PLUSSTAR_LIST . $channel;
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
$result = array();
$brandList = PlusstarData::brandList($gender, $channel);
if (!empty($brandList['star']['data']['list'][0]['data'])) {
$result['ps']['star'] = self::formatData($brandList['star']['data']['list'][0]['data'], $gender, false);
//$result['ps']['pName'] = isset($brandList['star']['data']['brand_type_name']) ? $brandList['star']['data']['brand_type_name'] : '';
}
if (!empty($brandList['original']['data']['list'][0]['data'])) {
$result['ps']['plus'] = self::formatData($brandList['original']['data']['list'][0]['data'], $gender, true);
//$result['ps']['sName'] = isset($brandList['original']['data']['brand_type_name']) ? $brandList['original']['data']['brand_type_name'] : '';
}
if (USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($key, 'slave');
}
// 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
else {
Cache::set($key, $result);
}
}
return $result;
}
/**
* 格式化成模板数据格式
*
* @param array $data 源数据
* @param string $gender "1,3"表示男, "2,3"表示女
* @param bool $showDesciption 控制是否显示摘要内容
* @return array
*/
private static function formatData($data, $gender, $showSummary = true)
{
$result = array();
$build = array();
$imgs = array();
foreach ($data as $value) {
$build = array();
// 情况1: 多张图
if (isset($value['data'][1])) {
$imgs = array();
foreach ($value['data'] as $ps) {
$build['url'] = Helpers::url('/plustar/brandinfo', array('id' => $value['id'], 'gender' => $gender), 'guang');
$build['img'] = Helpers::getImageUrl($ps['src'], 640, 310);
$build['deps'] = $showSummary ? $value['brand_title'] : false;
$imgs[] = $build;
}
$result[] = array('imgs' => $imgs);
}
// 情况2: 单张图
elseif (isset($value['data'][0])) {
$build['url'] = Helpers::url('/plustar/brandinfo', array('id' => $value['id'], 'gender' => $gender), 'guang');
$build['img'] = Helpers::getImageUrl($value['data'][0]['src'], 640, 310);
$build['deps'] = $showSummary ? $value['brand_title'] : false;
$result[] = $build;
}
}
return $result;
}
}