Search.php
14.1 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
171
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
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
381
382
383
384
385
386
387
388
389
390
391
<?php
namespace Product;
use Configs\CacheConfig;
use LibModels\Wap\Product\SearchData;
use LibModels\Wap\Category\BrandData;
use LibModels\Wap\Category\ClassData;
use Plugin\DataProcess\ListProcess;
use Plugin\Cache;
use Plugin\Helpers;
/**
* 搜索相关的模板数据模型
*
* @name Search
* @package Product
* @copyright yoho.inc
* @version 1.0 (2015-11-1 14:35:52)
*/
class SearchModel
{
/**
* 获取搜索的数据
*
* @param array $condition 查询条件
* @return array
*/
public static function getSearchData($condition, $showTag = true, $tagNew = true, $tagSale = true)
{
$result = array();
if (USE_CACHE) {
$key = CacheConfig::KEY_ACTION_SEARCH_SEARCH;
if (!empty($condition)) {
$key .= http_build_query($condition, null, '&');
}
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
// 调用接口查询数据 (使用新的 Elastic Engine)
$listData = SearchData::searchElasticByCondition($condition);
// 调用接口查询数据 (使用老的 Sphinx Engine)
// $listData = SearchData::searchByCondition($condition);
// 处理返回的数据
if (isset($listData['code']) && $listData['code'] === 200) {
$result = ListProcess::getListData($listData['data'], false, $showTag, $tagNew, $tagSale);
}
if (USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($key, 'slave');
} // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
else {
Cache::set($key, $result, 300); // 缓存5分钟
}
}
return $result;
}
/**
* 获取筛选的数据
*
* @param array $condition 查询条件
* @return array
*/
public static function getFilterData($condition)
{
$result = array();
if (USE_CACHE) {
$key = CacheConfig::KEY_ACTION_SEARCH_FILTER;
if (!empty($condition)) {
$key .= http_build_query($condition, null, '&');
}
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
// 设置选定的gender
$gender = '1,2,3';
if (isset($condition['gender']) && !empty($condition['gender'])) {
$gender = $condition['gender'];
}
// 区别各种列表页面的筛选数据
if (isset($condition['shop_id'])) {
$listData = SearchData::searchByCondition($condition);
$exclude = 'null';
} elseif (isset($condition['brand'])) {
$listData = BrandData::filterBrandData($condition);
$exclude = 'brand';
} else if (isset($condition['sort'])) {
$listData = ClassData::filterClassData($condition);
$exclude = 'group_sort';
} else if (isset($condition['filter_poolId'])) {
$listData = SearchData::getFilter($condition);
$exclude = null;
} else if (isset($condition['students'])) {
$listData = ClassData::filterStudentsData($condition);
$exclude = 'students';
} else {
$listData = SearchData::searchByCondition($condition);
$exclude = null;
}
if (isset($listData['data']['filter'])) {
$result['filter'] = ListProcess::getFilterData($listData['data']['filter'], $gender, $exclude);
}
if (USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($key, 'slave');
} // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
else {
Cache::set($key, $result, 300); // 缓存5分钟
}
}
return $result;
}
/**
* @param array $params 直接传筛选参数
* @return array
*/
public static function filter($params = array())
{
$data = array();
if (empty($params)) {
$condition = filter_input_array(INPUT_GET, array(
'query' => FILTER_DEFAULT,
'brand' => FILTER_DEFAULT,
'shop_id' => FILTER_DEFAULT,
'sort' => FILTER_DEFAULT,
'msort' => FILTER_DEFAULT,
'misort' => FILTER_DEFAULT,
'color' => FILTER_DEFAULT,
'size' => FILTER_DEFAULT,
'style' => FILTER_DEFAULT,
'price' => FILTER_DEFAULT,
'discount' => FILTER_DEFAULT,
'gender' => FILTER_DEFAULT,
'channel' => FILTER_DEFAULT,
'ageLevel' => FILTER_DEFAULT,
'p_d' => FILTER_DEFAULT,
'students' => FILTER_DEFAULT,
'filter_poolId' => FILTER_DEFAULT
), false);
// 转义分类
if (isset($condition['sort'])) {
$condition['sort'] = rawurldecode($condition['sort']);
}
// 转义分类
if (isset($condition['msort'])) {
$condition['msort'] = rawurldecode($condition['msort']);
}
// 转义分类
if (isset($condition['misort'])) {
$condition['misort'] = rawurldecode($condition['misort']);
}
// 转义颜色
if (isset($condition['color'])) {
$condition['color'] = rawurldecode($condition['color']);
}
//
if (isset($condition['ageLevel'])) {
$condition['age_level'] = rawurldecode($condition['ageLevel']);
unset($condition['ageLevel']);
}
// 为了兼容现在运营在用的p_d
if (isset($condition['p_d'])) {
$condition['p_d'] = rawurldecode($condition['p_d']);
} // 转换折扣
elseif (isset($condition['discount'])) {
$condition['p_d'] = rawurldecode($condition['discount']);
unset($condition['discount']);
}
// 转义性别
if (isset($condition['gender'])) {
$condition['gender'] = rawurldecode($condition['gender']);
}
// 转义价格
if (isset($condition['price'])) {
$condition['price'] = rawurldecode($condition['price']);
}
// 转义风格
if (isset($condition['style'])) {
$condition['style'] = rawurldecode($condition['style']);
}
} else {
$condition = $params;
}
// 区别各种列表页面的筛选数据
$data = self::getFilterData($condition);
return $data;
}
/**
* @param array $params
* @return array
*/
public static function search($params = array())
{
do {
/* 如果参数为空是 Ajax 调用 */
if (empty($params)) {
/* Ajax 调用过滤请求参数 */
$condition = filter_input_array(INPUT_GET, array(
'shop_id' => FILTER_DEFAULT,
'query' => FILTER_DEFAULT,
'specialsale_id' => FILTER_DEFAULT,
'promotion' => FILTER_DEFAULT,
'brand' => FILTER_DEFAULT,
'sort' => FILTER_DEFAULT,
'msort' => FILTER_DEFAULT,
'misort' => FILTER_DEFAULT,
'color' => FILTER_DEFAULT,
'size' => FILTER_DEFAULT,
'style' => FILTER_DEFAULT,
'price' => FILTER_DEFAULT,
'discount' => FILTER_DEFAULT,
'gender' => FILTER_DEFAULT,
'channel' => FILTER_DEFAULT,
'p_d' => FILTER_DEFAULT,
'outlets' => FILTER_DEFAULT,
'age_level' => FILTER_DEFAULT,
'page' => FILTER_VALIDATE_INT,
'filter_poolId' => FILTER_DEFAULT,
'showTag' => FILTER_DEFAULT,
'tagNew' => FILTER_DEFAULT,
'students' => FILTER_DEFAULT,
'tagSale' => FILTER_DEFAULT), false);
if (!empty($condition['shop_id'])) {
$condition['shop'] = $condition['shop_id'];
}
// 转义分类
if (isset($condition['sort'])) {
$condition['sort'] = rawurldecode($condition['sort']);
}
// 转义分类
if (isset($condition['msort'])) {
$condition['msort'] = rawurldecode($condition['msort']);
}
// 转义分类
if (isset($condition['misort'])) {
$condition['misort'] = rawurldecode($condition['misort']);
}
// 转义活动ID
if (isset($condition['specialsale_id'])) {
$condition['specialsale_id'] = rawurldecode($condition['specialsale_id']);
}
if (isset($condition['promotion'])) {
$condition['promotion'] = rawurldecode($condition['promotion']);
}
// 转义颜色
if (isset($condition['color'])) {
$condition['color'] = rawurldecode($condition['color']);
}
// 转义价格
if (isset($condition['price'])) {
$condition['price'] = rawurldecode($condition['price']);
}
// 转义风格
if (isset($condition['style'])) {
$condition['style'] = rawurldecode($condition['style']);
}
// 为了兼容现在运营在用的p_d
if (isset($condition['p_d'])) {
$condition['p_d'] = rawurldecode($condition['p_d']);
} // 转换折扣
elseif (isset($condition['discount'])) {
$condition['p_d'] = rawurldecode($condition['discount']);
unset($condition['discount']);
}
// 转义性别
if (isset($condition['gender'])) {
$condition['gender'] = rawurldecode($condition['gender']);
}
// 转义性别
if (isset($condition['age_level'])) {
$condition['age_level'] = rawurldecode($condition['age_level']);
}
if (isset($condition['query'])) {
$condition['query'] = rawurldecode($condition['query']);
}
// 转义奥莱
if (isset($condition['outlets'])) {
$condition['outlets'] = rawurldecode($condition['outlets']);
}
// 用户自定义商品类目
if (isset($condition['filter_poolId'])) {
$condition['filter_poolId'] = rawurldecode($condition['filter_poolId']);
}
// 转换排序方式
// $type = $this->get('type', '');
// $order = $this->get('order', 0);
$order = empty($_GET['order']) ? 0 : $_GET['order'];
$condition['order'] = Helpers::transOrder($order, $_GET['type']);
/* 如果参数不为空是直接调用 */
} else {
$condition = $params;
}
//标签显示控制
// $showTag = $this->get('showTag', 0);
$showTag = empty($condition['showTag']);
// $showTag = $showTag === 0;
//显示标签new
// $tagNew = $this->get('tagNew', 0);
$tagNew = empty($condition['tagNew']);
// $tagNew = $tagNew === 0;
//显示标签sale
$tagSale = empty($condition['tagSale']);
// $tagSale = $tagSale === 0;
// 过滤掉值为空的条件
$condition = array_filter($condition);
// /* 模糊搜索关键词 */
// if (isset($condition['query'])) {
// $data = Product\SearchModel::getSearchData($condition);
// break;
// }
//
// /* 精确搜索品牌 */
// if (isset($condition['brand'])) {
// $title = '';
// $data = Product\ListModel::getBrandData($condition, $title);
// break;
// }
//
// /* 精确搜索品类 */
// $data = Product\ListModel::getClassData($condition);
// 产品吴婷要求这么做,目的是保持性别中的所有性别和当前频道性别一致
if (isset($condition['gender']) && isset($condition['channel']) && $condition['gender'] === '1,2,3') {
switch ($condition['channel']) {
case 1:
$condition['gender'] = '1,3';
break;
case 2:
$condition['gender'] = '2,3';
break;
default:
break;
}
}
$data = self::getSearchData($condition, $showTag, $tagNew, $tagSale);
} while (false);
//奥莱商品过滤新品标签
if (isset($condition['outlets']) && $condition['outlets'] === '1') {
foreach ($data['new'] as $outletskey => $outletsval) {
unset($data['new'][$outletskey]['tags']);
}
}
// 是否显示学生价
if (isset($condition['students'])) {
$data['students'] = true;
}
return $data;
}
}