Authored by Rock Zhang

添加购物车首页的逻辑

  1 +<?php
  2 +
  3 +namespace LibModels\Wap\Home;
  4 +
  5 +use Api\Sign;
  6 +use Api\Yohobuy;
  7 +
  8 +/**
  9 + * 购物车的数据模型
  10 + *
  11 + * @name CartData
  12 + * @package LibModels/Wap/Home
  13 + * @copyright yoho.inc
  14 + * @version 1.0 (2015-11-09 13:58:27)
  15 + * @author Gtskk <tttt6399998@126.com>
  16 + */
  17 +class CartData
  18 +{
  19 +
  20 + /**
  21 + * 购物车数据
  22 + *
  23 + * @return array 购物车接口返回的数据
  24 + */
  25 + public static function cartData($uid)
  26 + {
  27 + $param = Yohobuy::param();
  28 + $param['method'] = 'app.Shopping.cart';
  29 + $param['uid'] = $uid;
  30 + $param['client_secret'] = Sign::getSign($param);
  31 +
  32 + return Yohobuy::get(Yohobuy::API_URL, $param);
  33 + }
  34 +
  35 +}
@@ -9,80 +9,16 @@ class ShoppingCartController extends AbstractAction @@ -9,80 +9,16 @@ class ShoppingCartController extends AbstractAction
9 { 9 {
10 public function indexAction() 10 public function indexAction()
11 { 11 {
  12 + $this->setTitle('购物车');
  13 + $this->setNavHeader('购物车');
  14 +
12 $data = array( 15 $data = array(
13 - 'cartNav' => true,  
14 - 'commonGoodsCount' => 2,  
15 - 'presellGoodsCount' => 2,  
16 - 'showLoginInfo' => true,  
17 - 'commonCart' => array(  
18 - 'goods' => array(  
19 - array(  
20 - 'id' => 1,  
21 - 'name' => '黄伟文Wyman X y yohood 联名商品YYYOHOOD圆领卫衣',  
22 - 'thumb' => 'http://img11.static.yhbimg.com/goodsimg/2015/09/17/03/014cacfa5c458b9732c68adf1af15d7a45.jpg?imageMogr2/thumbnail/120x120/extent/120x120/background/d2hpdGU=/position/center/quality/90',  
23 - 'color' => '黄色',  
24 - 'size' => 'F',  
25 - 'appearDate' => '12月',  
26 - 'price' => 399.00,  
27 - 'count' => 8,  
28 - 'lowStocks' => true  
29 - ),  
30 - array(  
31 - 'id' => 2,  
32 - 'name' => 'TYAKSHA圣诞树凭借三角领蓝色白条毛衣',  
33 - 'thumb' => 'http://img11.static.yhbimg.com/goodsimg/2015/10/03/10/01bc1878f9154e77ac4f7a6003c954f1b8.jpg?imageMogr2/thumbnail/235x314/extent/235x314/background/d2hpdGU=/position/center/quality/90',  
34 - 'color' => '黄色',  
35 - 'size' => 'F',  
36 - 'price' => 553.00,  
37 - 'count' => 1,  
38 - 'soldOut' => true  
39 - )  
40 - ),  
41 - 'freebieOrAdvanceBuy' => true,  
42 - 'freebie' => array(  
43 - 'url' => '',  
44 - 'count' => 2  
45 - ),  
46 - 'advanceBuy' => array(  
47 - 'url' => '',  
48 - 'count' => 3  
49 - ),  
50 - 'price' => 3192,  
51 - 'activityPrice' => 0,  
52 - 'count' => 8,  
53 - 'sumPrice' => 3192  
54 - ),  
55 - 'preSellCart' => array(  
56 - 'goods' => array(  
57 - array(  
58 - 'id' => 2,  
59 - 'name' => 'TYAKSHA圣诞树凭借三角领蓝色白条毛衣',  
60 - 'thumb' => 'http://img11.static.yhbimg.com/goodsimg/2015/10/03/10/01bc1878f9154e77ac4f7a6003c954f1b8.jpg?imageMogr2/thumbnail/235x314/extent/235x314/background/d2hpdGU=/position/center/quality/90',  
61 - 'color' => '黄色',  
62 - 'size' => 'F',  
63 - 'price' => 553.00,  
64 - 'count' => 1,  
65 - 'soldOut' => true  
66 - )  
67 - ),  
68 - 'freebieOrAdvanceBuy' => true,  
69 - 'freebie' => array(  
70 - 'url' => '',  
71 - 'count' => 2  
72 - ),  
73 - 'advanceBuy' => array(  
74 - 'url' => '',  
75 - 'count' => 3  
76 - ),  
77 - 'price' => 553,  
78 - 'activityPrice' => 0,  
79 - 'count' => 1,  
80 - 'sumPrice' => 553  
81 - ) 16 + 'shoppingCartPage' => true,
  17 + 'shoppingCart' => \Index\CartModel::getCartData(967016)
82 ); 18 );
  19 +
83 // 渲染模板 20 // 渲染模板
84 - $this->_view->display('index', array('shoppingCartPage' => true, 'pageHeader' => array(  
85 - 'navBack' => 'http://m.yohobuy.com', 'navTitle' => '购物车'), 'shoppingCart' => $data)); 21 + $this->_view->display('index', $data);
86 } 22 }
87 23
88 public function giftAdvanceAction() 24 public function giftAdvanceAction()
  1 +<?php
  2 +
  3 +namespace Index;
  4 +
  5 +use LibModels\Wap\Home\CartData;
  6 +use Plugin\Images;
  7 +
  8 +/**
  9 + *
  10 + * @name CartModel
  11 + * @package models/Index
  12 + * @copyright yoho.inc
  13 + * @version 1.0 (2015-11-09 14:05:09)
  14 + * @author Gtskk (tttt6399998@126.com)
  15 + */
  16 +class CartModel
  17 +{
  18 +
  19 + /**
  20 + * @param integer $uid 用户ID
  21 + * @return array|mixed 处理之后的购物车数据
  22 + */
  23 + public static function getCartData($uid)
  24 + {
  25 + $result = array(
  26 + 'cartNav' => true,
  27 + 'showLoginInfo' => true
  28 + );
  29 +
  30 + // 调用接口获取购物车的数据
  31 + $cartData = CartData::cartData($uid);
  32 +
  33 + // 处理普通购物车和预售购物车的数据
  34 + /* 普通购物车 */
  35 + if (isset($cartData['data']) && !empty($cartData['data'])) {
  36 + $cart = $cartData['data'];
  37 +
  38 + /* 普通购物车 */
  39 + if(isset($cart['ordinary_cart_data'])) {
  40 + $result['commonGoodsCount'] = count($cart['ordinary_cart_data']['goods_list']);
  41 + $result['commonCart'] = self::procCartData($cart['ordinary_cart_data']);
  42 + }
  43 +
  44 + /* 预售购物车 */
  45 + if(isset($cart['advance_cart_data'])) {
  46 + $result['presellGoodsCount'] = count($cart['advance_cart_data']['goods_list']);
  47 + $result['preSellCart'] = self::procCartData($cart['advance_cart_data']);
  48 + }
  49 +
  50 + }
  51 +
  52 + return $result;
  53 + }
  54 +
  55 +
  56 + /**
  57 + * 处理不同类型的购物车数据
  58 + *
  59 + * @param array $data 不同类型购物车数据
  60 + * @return array $result 处理之后的不同类型购物车数据
  61 + */
  62 + private static function procCartData($data)
  63 + {
  64 + $result = array();
  65 +
  66 + $oneGoods = array();
  67 + // 购买的商品列表
  68 + foreach ($data['goods_list'] as $value) {
  69 + $oneGoods['id'] = $value['product_id'];
  70 + $oneGoods['name'] = $value['product_name'];
  71 + $oneGoods['thumb'] = Images::getImageUrl($value['goods_images'], 120, 120);
  72 + $oneGoods['color'] = $value['color_name'];
  73 + $oneGoods['size'] = $value['size_name'];
  74 + $oneGoods['appearDate'] = '12月'; // 目前app接口没有返回该数据
  75 + $oneGoods['price'] = $value['real_price'];
  76 + $oneGoods['count'] = $value['buy_number'];
  77 + $oneGoods['lowStocks'] = true;
  78 +
  79 + $result['goods'][] = $oneGoods;
  80 + }
  81 + // 赠品
  82 + count($data['gift_list']) && $result['freebieOrAdvanceBuy'] = true;
  83 + $result['freebie'] = $data['gift_list'];
  84 + // 加价购
  85 + $result['advanceBuy'] = $data['price_gift'];
  86 + // 结算数据
  87 + $result['price'] = $data['shopping_cart_data']['order_amount'];
  88 + $result['activityPrice'] = $data['shopping_cart_data']['discount_amount'];
  89 + $result['count'] = $data['shopping_cart_data']['goods_count'];
  90 + $result['sumPrice'] = $data['shopping_cart_data']['order_amount'];
  91 +
  92 + return $result;
  93 + }
  94 +
  95 +}