Index.php
4.87 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
<?php
namespace Product;
use WebPlugin\HelperSearch;
use Product\SearchModel;
use LibModels\Web\Product\SearchData;
use Api\Yohobuy;
use Configs\WebCacheConfig;
use WebPlugin\Cache;
use Index\HomeModel;
/**
* list Index页模板数据模型
*
*/
class IndexModel
{
/**
* 搜索list首页数据
* @param $customCondition array(
* 'brand' => int (品牌id,必传)
* 'folder_id' => string (搜索品牌系列参数)
* )
* @param $customOptions array(
'reviewNum' => int (底部浏览记录显示个数)
'positionId' => int (搜索banner 位置id)
'controller' => string (说明当前控制器名)
'action' => string (说明当前方法名)
* ) 排序条件
* @return array() (处理后的list首页数据)
*/
public static function getIndexData($customCondition, $customOptions)
{
$searchCondition = SearchModel::searchCondition($customCondition, $customOptions);
if (USE_CACHE) {
$key = WebCacheConfig::KEY_WEB_PRODUCT_LIST_INDEX;
if (!empty($searchCondition['userInput'])) {
$key .= http_build_query($searchCondition['userInput'], null, '&');
}
$channel = HomeModel::getSwitchChannel();
//key加上性别参数
$key .= $channel;
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
// 组合搜索商品url
$urlList['product'] = SearchData::getProductUrl($searchCondition['condition']);
// 组合搜索分类url
$sortCondition = array();
if (isset($searchCondition['condition']['misort']) && !empty($searchCondition['condition']['misort'])) {
$sortCondition['needSmallSort'] = 1;
}
$urlList['sort'] = SearchData::getClassesUrl($sortCondition);
//批量调接口获取数据
$data = Yohobuy::getMulti($urlList);
// 组织模板数据
$result = HelperSearch::getList($data, $searchCondition['options'], $searchCondition['userInput']);
//调用接口获取list-index banner数据
$msort = isset($searchCondition['condition']['msort']) ? $searchCondition['condition']['msort'] : false;
$misort = isset($searchCondition['condition']['misort']) ? $searchCondition['condition']['misort'] : false;
$positionId = isset($searchCondition['options']['positionId']) ? $searchCondition['options']['positionId'] : '';
$gender = $searchCondition['condition']['gender'] == '2,3' ? 2 : 1;
$banner = array();
if ($msort && $misort) {
$bannerData = SearchData::getBannerData($positionId, $msort, $misort, $gender);
if (isset($bannerData['code']) && $bannerData['code'] == 200 && !empty($bannerData['data'])) {
$banner = self::formatBanner($bannerData['data']);
}
}
//插入banner数据
if (isset($banner)) {
$result['sortIntro'] = $banner;
}
//广告数据
$adsId = $searchCondition['options']['adsId'];
$res = array();
$ads = SearchData::getListAd($msort, $misort, $adsId);
if (isset($ads['code']) && $ads['code'] == '200' && !empty($ads['data'])) {
foreach($ads['data'] as $key=>$value) {
$res['picLink']['list'][$key]['href'] = isset($value['ads_url']) ? $value['ads_url'] : '';
$res['picLink']['list'][$key]['src'] = isset($value['ads_image']) ? $value['ads_image'] : '';
}
}
if (!empty($res)) {
$result['leftContent'][] = $res;
}
if (USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($key, 'slave');
}
// 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
else {
Cache::set($key, $result, 600); // 缓存10分钟
}
}
return $result;
}
/**
* 组织list-index banner数据
* @param $bannerData
* @return array() 处理后的banner格式
*/
public static function formatBanner($bannerData)
{
$banner = array();
$banner['name'] = $bannerData['title'];
$banner['enName'] = $bannerData['subtitle'];
$banner['desc'] = $bannerData['intro'];
$banner['img'] = $bannerData['logo'];
foreach($bannerData['keyword'] as $key => $vo){
$banner['keyEntry'][$key]['name'] = $vo['word'];
$banner['keyEntry'][$key]['url'] = $vo['url'];
}
return $banner;
}
}