Authored by Rock Zhang

修复收藏的商品以及收藏的品牌会报错的bug

Code Review By Rock Zhang
@@ -24,17 +24,17 @@ class Yohobuy @@ -24,17 +24,17 @@ class Yohobuy
24 // const YOHOBUY_URL = 'http://www.yohobuy.com/'; 24 // const YOHOBUY_URL = 'http://www.yohobuy.com/';
25 25
26 //java API 26 //java API
27 -// const API_URL = 'http://apih5.yoho.cn/';  
28 -// const API_URL2 = 'http://apih5.yoho.cn/';  
29 -// const SERVICE_URL = 'http://serviceh5.yoho.cn/';  
30 -// const YOHOBUY_URL = 'http://www.yohobuy.com/';  
31 -// const API_OLD = 'http://api2.open.yohobuy.com/'; 27 + const API_URL = 'http://apih5.yoho.cn/';
  28 + const API_URL2 = 'http://apih5.yoho.cn/';
  29 + const SERVICE_URL = 'http://serviceh5.yoho.cn/';
  30 + const YOHOBUY_URL = 'http://www.yohobuy.com/';
  31 + const API_OLD = 'http://api2.open.yohobuy.com/';
32 32
33 /* 测试环境 */ 33 /* 测试环境 */
34 - const API_URL = 'http://testapi.yoho.cn:28078/'; // 'http://192.168.102.205:8080/gateway/'  
35 - const SERVICE_URL = 'http://testservice.yoho.cn:28077/';  
36 - const YOHOBUY_URL = 'http://www.yohobuy.com/';  
37 - const API_OLD = 'http://test2.open.yohobuy.com/'; 34 +// const API_URL = 'http://testapi.yoho.cn:28078/'; // 'http://192.168.102.205:8080/gateway/'
  35 +// const SERVICE_URL = 'http://testservice.yoho.cn:28077/';
  36 +// const YOHOBUY_URL = 'http://www.yohobuy.com/';
  37 +// const API_OLD = 'http://test2.open.yohobuy.com/';
38 38
39 /* 预览环境 */ 39 /* 预览环境 */
40 // const API_URL = 'http://preapi.yoho.cn/'; 40 // const API_URL = 'http://preapi.yoho.cn/';
@@ -79,8 +79,8 @@ function loadData($parent, url, page) { @@ -79,8 +79,8 @@ function loadData($parent, url, page) {
79 page: page 79 page: page
80 }, 80 },
81 success: function(data) { 81 success: function(data) {
82 - var $loadingMask = $parent.closest('.fav-type').find('.fav-content-loading');  
83 - 82 + var $loadingMask = $parent.closest('.fav-type').find('.fav-content-loading');
  83 +
84 if (url === 'favBrand') { 84 if (url === 'favBrand') {
85 $brandLoadMore.addClass('hide'); 85 $brandLoadMore.addClass('hide');
86 } else { 86 } else {
@@ -91,23 +91,23 @@ function loadData($parent, url, page) { @@ -91,23 +91,23 @@ function loadData($parent, url, page) {
91 $loadingMask.addClass('hide'); 91 $loadingMask.addClass('hide');
92 $parent.closest('.fav-type').find('.fav-null-box').removeClass('hide'); 92 $parent.closest('.fav-type').find('.fav-null-box').removeClass('hide');
93 } else if (data === 'end') { 93 } else if (data === 'end') {
94 -  
95 - //处理data等于end时如果loadingMask存在且没有hide样式的情况  
96 - if ($loadingMask && !$loadingMask.hasClass('hide')) { 94 +
  95 + //处理data等于end时如果loadingMask存在且没有hide样式的情况
  96 + if ($loadingMask && !$loadingMask.hasClass('hide')) {
97 $loadingMask.addClass('hide'); 97 $loadingMask.addClass('hide');
98 - $parent.closest('.fav-type').find('.fav-null-box').removeClass('hide');  
99 - }  
100 - 98 + //$parent.closest('.fav-type').find('.fav-null-box').removeClass('hide');
  99 + }
  100 +
101 $parent.closest('.fav-type').find('.fav-load-background') 101 $parent.closest('.fav-type').find('.fav-load-background')
102 - .removeClass('fav-load-background').html('没有更多了');  
103 - 102 + .removeClass('fav-load-background').html('没有更多了');
  103 +
104 // hf: fixes bug to 修改没有数据还调接口,加载错误页面问题 104 // hf: fixes bug to 修改没有数据还调接口,加载错误页面问题
105 brandLockId = true; 105 brandLockId = true;
106 lockId = true; 106 lockId = true;
107 } else if (data.length > 10) { 107 } else if (data.length > 10) {
108 $parent.append(data); 108 $parent.append(data);
109 -  
110 - //如果有数据loadingMask会被remove掉 109 +
  110 + //如果有数据loadingMask会被remove掉
111 $loadingMask.remove(); 111 $loadingMask.remove();
112 if (url === 'favBrand') { 112 if (url === 'favBrand') {
113 initSwiper(data);//如果是收藏品牌需要初始化swiper 113 initSwiper(data);//如果是收藏品牌需要初始化swiper
@@ -188,31 +188,48 @@ class UserModel @@ -188,31 +188,48 @@ class UserModel
188 $favProduct = UserData::favoriteProductData($uid, $page, $limit); 188 $favProduct = UserData::favoriteProductData($uid, $page, $limit);
189 189
190 // 处理用户收藏的商品数据 190 // 处理用户收藏的商品数据
191 - if (isset($favProduct['data']) && $page <= $favProduct['data']['page_total']) {  
192 - $datas = array();  
193 - $product = array();  
194 - foreach ($favProduct['data']['product_list'] as $val) {  
195 - if (empty($val['product_skn'])) {  
196 - continue;  
197 - } 191 + do {
  192 + // 开始就没获取到数据或者获取的数据为空时的处理
  193 + if (!isset($favProduct['data']['product_list']) && $page == 1) {
  194 + break;
  195 + }
198 196
  197 + // 加载第二页以及第二页之后的数据时接口不返回时的处理
  198 + if ($page > 1 && !$favProduct) {
  199 + $result['end'] = true;
  200 + break;
  201 + }
  202 +
  203 + if ($page <= $favProduct['data']['page_total']) {
  204 + $datas = array();
199 $product = array(); 205 $product = array();
200 - $product['fav_id'] = $val['product_id'];  
201 - $product['link'] = isset($val['goodsId'], $val['cnAlphabet']) ? Helpers::url('/product/pro_' . $val['product_id'] . '_' . $val['goodsId'] . '/' . $val['cnAlphabet'] . '.html') : '';  
202 - $product['imgUrl'] = !empty($val['image']) ? Helpers::getImageUrl($val['image'], 447, 596) : '';  
203 - ;  
204 - $product['title'] = $val['product_name'];  
205 - $product['price'] = '¥' . Helpers::transPrice($val['market_price']);  
206 - $product['discountPrice'] = ($val['market_price'] - $val['sales_price'] > 0) ? '¥' . Helpers::transPrice($val['sales_price']) : false;  
207 - $product['savePrice'] = ($val['price_down'] > 0) ? '¥' . Helpers::transPrice($val['price_down']) : false;  
208 - $product['sellOut'] = ($val['storage'] <= 0);  
209 -  
210 - $datas[] = $product;  
211 - }  
212 - !empty($datas) && $result['hasFavProduct'] = $datas;  
213 - } else if (($page > 1 && !$favProduct) || $page > $favProduct['data']['page_total']) {  
214 - $result['end'] = true;  
215 - } 206 + foreach ($favProduct['data']['product_list'] as $val) {
  207 + if (empty($val['product_skn'])) {
  208 + continue;
  209 + }
  210 +
  211 + $product = array();
  212 + $product['fav_id'] = $val['product_id'];
  213 + $product['link'] = isset($val['goodsId'], $val['cnAlphabet']) ? Helpers::url('/product/pro_' . $val['product_id'] . '_' . $val['goodsId'] . '/' . $val['cnAlphabet'] . '.html') : '';
  214 + $product['imgUrl'] = !empty($val['image']) ? Helpers::getImageUrl($val['image'], 447, 596) : '';
  215 + ;
  216 + $product['title'] = $val['product_name'];
  217 + $product['price'] = '¥' . Helpers::transPrice($val['market_price']);
  218 + $product['discountPrice'] = ($val['market_price'] - $val['sales_price'] > 0) ? '¥' . Helpers::transPrice($val['sales_price']) : false;
  219 + $product['savePrice'] = ($val['price_down'] > 0) ? '¥' . Helpers::transPrice($val['price_down']) : false;
  220 + $product['sellOut'] = ($val['storage'] <= 0);
  221 +
  222 + $datas[] = $product;
  223 + }
  224 +
  225 + if (!empty($datas)) {
  226 + $result['hasFavProduct'] = $datas;
  227 + }
  228 + } else {
  229 + $result['end'] = true;
  230 + }
  231 +
  232 + } while (false);
216 233
217 return $result; 234 return $result;
218 } 235 }
@@ -234,40 +251,56 @@ class UserModel @@ -234,40 +251,56 @@ class UserModel
234 $favBrand = UserData::favoriteBrandData($uid, $gender, $page, $limit); 251 $favBrand = UserData::favoriteBrandData($uid, $gender, $page, $limit);
235 252
236 // 处理用户收藏的品牌数据 253 // 处理用户收藏的品牌数据
237 - if (isset($favBrand['data']) && $page <= $favBrand['data']['page_total']) {  
238 - $datas = array();  
239 - $brand = array();  
240 - foreach ($favBrand['data']['brand_list'] as $val) {  
241 - $brand = array();  
242 - $brand['id'] = $val['brand_id'];  
243 - $brand['brandImg'] = !empty($val['brand_ico']) ? Images::getImageUrl($val['brand_ico'], 47, 47) : '';  
244 - $brand['brandName'] = $val['brand_name'];  
245 - $brand['update'] = $val['new_product_num'];  
246 - $brand['discount'] = $val['product_discount_num'];  
247 - $brand['link'] = Helpers::url('', array(), $val['brand_domain']);  
248 -  
249 - // 处理品牌产品  
250 - $product = array();  
251 - foreach ($val['new_product'] as $one) {  
252 - if (empty($one['product_skn'])) {  
253 - continue;  
254 - } 254 + do {
  255 + // 开始就没获取到数据或者获取的数据为空时的处理
  256 + if (!isset($favBrand['data']['brand_list']) && $page == 1) {
  257 + break;
  258 + }
255 259
  260 + // 加载第二页以及第二页之后的数据时接口不返回时的处理
  261 + if ($page > 1 && !$favBrand) {
  262 + $result['end'] = true;
  263 + break;
  264 + }
  265 +
  266 + if ($page <= $favBrand['data']['page_total']) {
  267 + $datas = array();
  268 + $brand = array();
  269 + foreach ($favBrand['data']['brand_list'] as $val) {
  270 + $brand = array();
  271 + $brand['id'] = $val['brand_id'];
  272 + $brand['brandImg'] = !empty($val['brand_ico']) ? Images::getImageUrl($val['brand_ico'], 47, 47) : '';
  273 + $brand['brandName'] = $val['brand_name'];
  274 + $brand['update'] = $val['new_product_num'];
  275 + $brand['discount'] = $val['product_discount_num'];
  276 + $brand['link'] = Helpers::url('', array(), $val['brand_domain']);
  277 +
  278 + // 处理品牌产品
256 $product = array(); 279 $product = array();
257 - $product['link'] = isset($one['goods'][0], $one['cnAlphabet']) ? Helpers::url('/product/pro_' . $one['product_id'] . '_' . $one['goods'][0]['id'] . '/' . $one['cnAlphabet'] . '.html') : '';  
258 - $product['imgUrl'] = (isset($one['default_images']) && !empty($one['default_images'])) ? Images::getImageUrl($one['default_images'], 235, 314) : '';  
259 - $product['price'] = '¥' . Helpers::transPrice($one['market_price']);  
260 - $product['discount'] = ($one['market_price'] > $one['sales_price']) ? '¥' . Helpers::transPrice($one['sales_price']) : false; 280 + foreach ($val['new_product'] as $one) {
  281 + if (empty($one['product_skn'])) {
  282 + continue;
  283 + }
  284 +
  285 + $product = array();
  286 + $product['link'] = isset($one['goods'][0], $one['cnAlphabet']) ? Helpers::url('/product/pro_' . $one['product_id'] . '_' . $one['goods'][0]['id'] . '/' . $one['cnAlphabet'] . '.html') : '';
  287 + $product['imgUrl'] = (isset($one['default_images']) && !empty($one['default_images'])) ? Images::getImageUrl($one['default_images'], 235, 314) : '';
  288 + $product['price'] = '¥' . Helpers::transPrice($one['market_price']);
  289 + $product['discount'] = ($one['market_price'] > $one['sales_price']) ? '¥' . Helpers::transPrice($one['sales_price']) : false;
  290 +
  291 + $brand['productList'][] = $product;
  292 + }
261 293
262 - $brand['productList'][] = $product; 294 + $datas[] = $brand;
263 } 295 }
264 296
265 - $datas[] = $brand; 297 + if (!empty($datas)) {
  298 + $result['hasFavBrand'] = $datas;
  299 + }
  300 + } else {
  301 + $result['end'] = true;
266 } 302 }
267 - !empty($datas) && $result['hasFavBrand'] = $datas;  
268 - } else if (($page > 1 && !$favBrand) || $page > $favBrand['data']['page_total']) {  
269 - $result['end'] = true;  
270 - } 303 + } while (false);
271 304
272 return $result; 305 return $result;
273 } 306 }