Class.php
5.35 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
<?php
namespace Category;
use LibModels\Wap\Category\ClassData;
use Configs\CacheConfig;
use Plugin\Helpers;
use Plugin\Cache;
/**
* 品牌相关的模板数据模型
*
* @name BrandModel
* @package Models/Category
* @copyright yoho.inc
* @version 1.0 (2015-10-21 13:52:44)
* @author fei.hong <fei.hong@yoho.cn>
*/
class ClassModel
{
/**
* 根据频道获取品类数据
*
* @return array
*/
public static function getClassData()
{
$result = array();
if (USE_CACHE) {
$key = CacheConfig::KEY_COMMON_CATEGORY_CLASSES;
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
do {
$data = ClassData::getClassesData();
if (empty($data['data'])) {
break;
}
$genderArr = array('boy' => '1,3', 'girl' => '2,3', 'kids' => '1,2,3', 'lifestyle' => '1,2,3');
$oneClass = array();
$item = array();
foreach ($data['data'] as $k => $v) {
$oneClass = array('name' => ucfirst(rtrim($k, 's')), 'ca' => array());
foreach ($v as $val) {
$item = array();
$item['name'] = $val['category_name'];
$item['id'] = $val['relation_parameter']['sort'];
if (!isset($val['sub'])) {
//ficx-bug根节点没有子分类,直接跳转相应品类页面
$item['url'] = Helpers::url('/', array(
'sort' => $item['id'],
'sort_name' => $item['name'],
'gender' => $genderArr[$k]), 'list');
$oneClass['ca'][] = $item;
continue;
}
//有子分类的,首先添加一级分类
$item['sub'][] = array(
'name' => '全部' . $item['name'],
'id' => $item['id'],
'url' => Helpers::url('/', array(
'sort' => $item['id'],
'sort_name' => $item['name'],
'gender' => $genderArr[$k]), 'list')
);
$subitem = array();
foreach ($val['sub'] as $value) {
$subitem = array();
$subitem['name'] = $value['category_name'];
$subitem['id'] = $value['relation_parameter']['sort'];
$subitem['url'] = Helpers::url('/', array(
'sort' => $value['relation_parameter']['sort'],
'sort_name' => $value['category_name'],
'gender' => $genderArr[$k]
), 'list');
$item['sub'][] = $subitem;
}
$oneClass['ca'][] = $item;
}
$result[] = $oneClass;
}
}
while (false);
if (USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($key, 'slave');
}
// 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
else {
Cache::set($key, $result);
}
}
return $result;
}
/**
* 根据频道获取所有品类名称
*
* @return array(
* "first" => '一级品类',
* "second" => '二级品类',
* )
*/
public static function getClassNames()
{
$result = array('first' => array(), 'second' => array());
if (USE_CACHE) {
$key = CacheConfig::KEY_COMMON_CATEGORY_CLASSES_NAMES;
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
$classes = ClassData::getClassesData();
if (!empty($classes['data'])) {
$id = 0;
foreach ($classes['data'] as $data) {
foreach ($data as $item) {
$id = $item['category_id'];
$result['first'][$id] = $item['category_name'];
if (!isset($item['sub'])) {
continue;
}
foreach ($item['sub'] as $sub) {
$id = $sub['category_id'];
$result['second'][$id] = $sub['category_name'];
}
}
}
}
$classes = array();
if (USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($key, 'slave');
}
// 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
else {
Cache::set($key, $result);
}
}
return $result;
}
}