Authored by 周少峰

healpersearch 修改

@@ -63,5 +63,6 @@ class CacheConfig @@ -63,5 +63,6 @@ class CacheConfig
63 const KEY_WEB_HOME_NAVBAR_DATA = 'key_web_home_navbar_data';//web的导航数据 63 const KEY_WEB_HOME_NAVBAR_DATA = 'key_web_home_navbar_data';//web的导航数据
64 const KEY_WEB_HOME_CHANNEL_DATA = 'key_web_home_channel_data';//web首页频道数据[boys, girls, kids, lifestyle] 64 const KEY_WEB_HOME_CHANNEL_DATA = 'key_web_home_channel_data';//web首页频道数据[boys, girls, kids, lifestyle]
65 const KEY_WEB_HOME_CHANNEL_NEWARRIVAL_DATA = 'key_web_home_newarrival_data';//web频道最新上架数据[boys, girls, kids, lifestyle] 65 const KEY_WEB_HOME_CHANNEL_NEWARRIVAL_DATA = 'key_web_home_newarrival_data';//web频道最新上架数据[boys, girls, kids, lifestyle]
  66 + const KEY_WEB_PRODUCT_SEARCH_DATA = 'key_web_product_search_data'; // web搜索的数据
66 67
67 } 68 }
@@ -73,11 +73,17 @@ class HelperSearch @@ -73,11 +73,17 @@ class HelperSearch
73 //排序方式、显示数量等其他选项 73 //排序方式、显示数量等其他选项
74 $result['opts'] = isset($data['product']['filter']) ? self::getOpts() : array(); 74 $result['opts'] = isset($data['product']['filter']) ? self::getOpts() : array();
75 //下一页 75 //下一页
76 - $result['hasNextPage'] = self::next($data['product']['page_total']); 76 + if (isset($data['product']['page_total'])) {
  77 + $result['hasNextPage'] = self::next($data['product']['page_total']);
  78 + }
77 //全部折扣 79 //全部折扣
78 - $result['leftContent'][]['allDiscount'] = isset($data['discount']) ? self::getDiscount($data['discount']['discount']) : array();  
79 - //分类条件  
80 - $result['filters']['sort'] = isset($data['sort']) ? self::searchSort($data['sort']['sort']) : array(); 80 + if ($data['discount']['discount'] && !empty($data['discount']['discount'])) {
  81 + $result['leftContent'][]['allDiscount'] = self::getDiscount($data['discount']['discount']);
  82 + }
  83 + //分类条件(搜索页面)
  84 + if (isset($data['sort']) && isset($options['controller']) && $options['controller'] != 'Search') {
  85 + $result['filters']['sort'] = self::searchSort($data['sort']['sort']);
  86 + }
81 //左侧分类分类 87 //左侧分类分类
82 $result['leftContent'][]['allSort'] = isset($data['sort']) ? self::groupSort($data['sort']['sort']) : array(); 88 $result['leftContent'][]['allSort'] = isset($data['sort']) ? self::groupSort($data['sort']['sort']) : array();
83 //一周新品上架 89 //一周新品上架
@@ -309,9 +315,6 @@ class HelperSearch @@ -309,9 +315,6 @@ class HelperSearch
309 { 315 {
310 $result = array(); 316 $result = array();
311 $options = self::$options; 317 $options = self::$options;
312 - if (!isset($options['controller']) || $options['controller'] != 'Search') {  
313 - return $result;  
314 - }  
315 $params = self::$params; 318 $params = self::$params;
316 foreach ($sort as $key => $val) { 319 foreach ($sort as $key => $val) {
317 $result[$key]['id'] = $val['sort_id']; 320 $result[$key]['id'] = $val['sort_id'];
@@ -959,9 +962,6 @@ class HelperSearch @@ -959,9 +962,6 @@ class HelperSearch
959 public static function getDiscount(array $list = array()) 962 public static function getDiscount(array $list = array())
960 { 963 {
961 $params = self::$params; 964 $params = self::$params;
962 - if (empty($list)) {  
963 - return array();  
964 - }  
965 $query = array(); 965 $query = array();
966 $total = 0; 966 $total = 0;
967 foreach ($list as $k => $v) { 967 foreach ($list as $k => $v) {
@@ -5,6 +5,7 @@ use Api\Yohobuy; @@ -5,6 +5,7 @@ use Api\Yohobuy;
5 use Plugin\Helpers; 5 use Plugin\Helpers;
6 use Plugin\HelperSearch; 6 use Plugin\HelperSearch;
7 use \LibModels\Web\Product\SearchData; 7 use \LibModels\Web\Product\SearchData;
  8 +use Configs\CacheConfig;
8 9
9 /** 10 /**
10 * sale首页模板数据模型 11 * sale首页模板数据模型
@@ -165,19 +166,41 @@ class SearchModel @@ -165,19 +166,41 @@ class SearchModel
165 { 166 {
166 $urlList = array(); 167 $urlList = array();
167 $searchCondition = self::searchCondition($customCondition, $customOptions); 168 $searchCondition = self::searchCondition($customCondition, $customOptions);
  169 + if (USE_CACHE) {
  170 + $key = CacheConfig::KEY_WEB_PRODUCT_SEARCH_DATA;
  171 + if (!empty($condition)) {
  172 + $key .= http_build_query($searchCondition['condition'], null, '&');
  173 + }
  174 + // 先尝试获取一级缓存(master), 有数据则直接返回.
  175 + $result = Cache::get($key, 'master');
  176 + if (!empty($result)) {
  177 + return $result;
  178 + }
  179 + }
168 // 组合搜索商品url 180 // 组合搜索商品url
169 $urlList['product'] = HelperSearch::getProductUrl($searchCondition['condition']); 181 $urlList['product'] = HelperSearch::getProductUrl($searchCondition['condition']);
170 // 组合搜索分类url 182 // 组合搜索分类url
171 - $urlList['sort'] = HelperSearch::getClassesUrl($searchCondition['condition']); 183 + $urlList['sort'] = HelperSearch::getClassesUrl($searchCondition['sortCondition']);
172 // 组合搜索店铺url 184 // 组合搜索店铺url
173 if (isset($searchCondition['condition']['query'])) { 185 if (isset($searchCondition['condition']['query'])) {
174 $param['keyword'] = $searchCondition['condition']['query']; 186 $param['keyword'] = $searchCondition['condition']['query'];
175 $urlList['shop'] = HelperSearch::getShopUrl($param); 187 $urlList['shop'] = HelperSearch::getShopUrl($param);
176 } 188 }
177 - $result = Yohobuy::getMulti($urlList, array(), true); 189 + $data = Yohobuy::getMulti($urlList, array(), true);
178 // 组织模板数据 190 // 组织模板数据
179 - $data = HelperSearch::getList($result, $searchCondition['options']);  
180 - return $data; 191 + $result = HelperSearch::getList($data, $searchCondition['options']);
  192 +
  193 + if (USE_CACHE) {
  194 + // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
  195 + if (empty($result)) {
  196 + $result = Cache::get($key, 'slave');
  197 + }
  198 + // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
  199 + else {
  200 + Cache::set($key, $result, 1800); // 缓存30分钟
  201 + }
  202 + }
  203 + return $result;
181 } 204 }
182 /** 205 /**
183 * 根据product_sn查询产品图片信息 206 * 根据product_sn查询产品图片信息