Authored by Rock Zhang

添加购物车中商品删除,加入收藏夹,修改接口”

@@ -32,4 +32,58 @@ class CartData @@ -32,4 +32,58 @@ class CartData
32 return Yohobuy::get(Yohobuy::API_URL, $param); 32 return Yohobuy::get(Yohobuy::API_URL, $param);
33 } 33 }
34 34
  35 + /**
  36 + * 移出购物车
  37 + *
  38 + * @param int $uid 用户ID
  39 + * @param string $sku 商品sku列表
  40 + * @return array 接口返回的数据
  41 + */
  42 + public static function removeFromCart($uid, $sku)
  43 + {
  44 + $param = Yohobuy::param();
  45 + $param['method'] = 'app.Shopping.remove';
  46 + $param['product_sku_list'] = $sku;
  47 + $param['uid'] = $uid;
  48 + $param['client_secret'] = Sign::getSign($param);
  49 +
  50 + return Yohobuy::get(Yohobuy::API_URL, $param);
  51 + }
  52 +
  53 + /**
  54 + * 修改购物车商品数据
  55 + *
  56 + * @param int $uid 用户ID
  57 + * @param string $swapData 商品数据
  58 + * @return array 接口返回的数据
  59 + */
  60 + public static function modifyCartProduct($uid, $swapData)
  61 + {
  62 + $param = Yohobuy::param();
  63 + $param['method'] = 'app.Shopping.swap';
  64 + $param['swap_data'] = $swapData;
  65 + $param['uid'] = $uid;
  66 + $param['client_secret'] = Sign::getSign($param);
  67 +
  68 + return Yohobuy::get(Yohobuy::API_URL, $param);
  69 + }
  70 +
  71 + /**
  72 + * 移入收藏夹
  73 + *
  74 + * @param int $uid 用户ID
  75 + * @param string $sku 商品sku列表
  76 + * @return array 接口返回的数据
  77 + */
  78 + public static function addToFav($uid, $sku)
  79 + {
  80 + $param = Yohobuy::param();
  81 + $param['method'] = 'app.Shopping.addfavorite';
  82 + $param['product_sku_list'] = $sku;
  83 + $param['uid'] = $uid;
  84 + $param['client_secret'] = Sign::getSign($param);
  85 +
  86 + return Yohobuy::get(Yohobuy::API_URL, $param);
  87 + }
  88 +
35 } 89 }
1 <?php 1 <?php
2 2
3 use Action\AbstractAction; 3 use Action\AbstractAction;
  4 +use Index\CartModel;
  5 +use Plugin\Helpers;
4 6
5 /** 7 /**
6 * 购物车 8 * 购物车
7 */ 9 */
8 class ShoppingCartController extends AbstractAction 10 class ShoppingCartController extends AbstractAction
9 { 11 {
  12 + protected $_uid;
  13 +
  14 + /**
  15 + * 初始化
  16 + */
  17 + public function init()
  18 + {
  19 + // 检查用户是否登录, 未登录则跳转到登录页
  20 + $this->_uid = $this->getUid();
  21 + if (!$this->_uid) {
  22 + $this->go(Helpers::url('/signin.html'));
  23 + }
  24 +
  25 + parent::init();
  26 + }
  27 +
  28 + /*
  29 + * 首页
  30 + */
10 public function indexAction() 31 public function indexAction()
11 { 32 {
12 $this->setTitle('购物车'); 33 $this->setTitle('购物车');
13 $this->setNavHeader('购物车'); 34 $this->setNavHeader('购物车');
14 35
15 - $uid = $this->getUid();  
16 $data = array( 36 $data = array(
17 'shoppingCartPage' => true, 37 'shoppingCartPage' => true,
18 - 'shoppingCart' => \Index\CartModel::getCartData($uid) 38 + 'shoppingCart' => CartModel::getCartData($this->_uid)
19 ); 39 );
20 40
21 // 渲染模板 41 // 渲染模板
22 $this->_view->display('index', $data); 42 $this->_view->display('index', $data);
23 } 43 }
24 44
  45 + /**
  46 + * 移出购物车
  47 + */
  48 + public function delAction()
  49 + {
  50 + $result = array();
  51 +
  52 + if ($this->isAjax()) {
  53 + $productId = $this->post('id', 0);
  54 + $result = CartModel::removeFromCart($this->_uid, $productId);
  55 + }
  56 +
  57 + if (empty($result)) {
  58 + echo ' ';
  59 + } else {
  60 + $this->echoJson($result);
  61 + }
  62 + }
  63 +
  64 + /**
  65 + * 移入收藏夹
  66 + */
  67 + public function colAction()
  68 + {
  69 + $result = array();
  70 +
  71 + if ($this->isAjax()) {
  72 + $productId = $this->post('id', 0);
  73 + $result = CartModel::addToFav($this->_uid, $productId);
  74 + }
  75 +
  76 + if (empty($result)) {
  77 + echo ' ';
  78 + } else {
  79 + $this->echoJson($result);
  80 + }
  81 + }
  82 +
  83 + /**
  84 + * 修改购物车商品数据
  85 + */
  86 + public function modifyAction()
  87 + {
  88 + $result = array();
  89 +
  90 + if ($this->isAjax()) {
  91 +
  92 + $params = array();
  93 + $params['old_product_sku']= $this->post('old_product_sku', 0);
  94 + $params['new_product_sku']= $this->post('new_product_sku', 0);
  95 + $params['buy_number']= $this->post('buy_number', 0);
  96 + $params['selected']= $this->post('selected', null);
  97 + $result = CartModel::modifyCartProduct($this->_uid, $params);
  98 + }
  99 +
  100 + if (empty($result)) {
  101 + echo ' ';
  102 + } else {
  103 + $this->echoJson($result);
  104 + }
  105 + }
  106 +
25 public function giftAdvanceAction() 107 public function giftAdvanceAction()
26 { 108 {
27 $data = array( 109 $data = array(
@@ -51,6 +51,75 @@ class CartModel @@ -51,6 +51,75 @@ class CartModel
51 return $result; 51 return $result;
52 } 52 }
53 53
  54 + /**
  55 + * 移出购物车
  56 + *
  57 + * @param int $uid 用户ID
  58 + * @param string $sku 商品sku列表
  59 + * @return array 接口返回的数据
  60 + */
  61 + public static function removeFromCart($uid, $sku)
  62 + {
  63 + $result = array('code' => 400, 'message' => '出错啦~');
  64 +
  65 + // 处理sku
  66 + $sku_list = json_encode(array($sku => 1));
  67 +
  68 + $remove = CartData::removeFromCart($uid, $sku_list);
  69 + if ($remove && isset($remove['code'])) {
  70 + $result['code'] = $remove['code'];
  71 + $result['message'] = $remove['message'];
  72 + }
  73 +
  74 + return $result;
  75 + }
  76 +
  77 + /**
  78 + * 移入收藏夹
  79 + *
  80 + * @param int $uid 用户ID
  81 + * @param string $sku 商品sku列表
  82 + * @return array 接口返回的数据
  83 + */
  84 + public static function addToFav($uid, $sku)
  85 + {
  86 + $result = array('code' => 400, 'message' => '出错啦~');
  87 +
  88 + // 处理sku
  89 + $sku_list = json_encode(array($sku => 1));
  90 +
  91 + $add = CartData::addToFav($uid, $sku_list);
  92 + if ($add && isset($add['code'])) {
  93 + $result['code'] = $add['code'];
  94 + $result['message'] = $add['message'];
  95 + }
  96 +
  97 + return $result;
  98 + }
  99 +
  100 + /**
  101 + * 修改购物车商品数据
  102 + *
  103 + * @param int $uid 用户ID
  104 + * @param string $param 要更改的数据
  105 + * @return array 接口返回的数据
  106 + */
  107 + public static function modifyCartProduct($uid, $param)
  108 + {
  109 + $result = array('code' => 400, 'message' => '出错啦~');
  110 +
  111 + // 处理要更改的数据
  112 + $swapData = json_encode(array($param));
  113 +
  114 + $modify = CartData::addToFav($uid, $swapData);
  115 + if ($modify && isset($modify['code'])) {
  116 + $result['code'] = $modify['code'];
  117 + $result['message'] = $modify['message'];
  118 + }
  119 +
  120 + return $result;
  121 + }
  122 +
54 123
55 /** 124 /**
56 * 处理不同类型的购物车数据 125 * 处理不同类型的购物车数据
@@ -65,7 +134,7 @@ class CartModel @@ -65,7 +134,7 @@ class CartModel
65 $oneGoods = array(); 134 $oneGoods = array();
66 // 购买的商品列表 135 // 购买的商品列表
67 foreach ($data['goods_list'] as $value) { 136 foreach ($data['goods_list'] as $value) {
68 - $oneGoods['id'] = $value['product_id']; 137 + $oneGoods['id'] = $value['product_sku'];
69 $oneGoods['name'] = $value['product_name']; 138 $oneGoods['name'] = $value['product_name'];
70 $oneGoods['thumb'] = Images::getImageUrl($value['goods_images'], 120, 120); 139 $oneGoods['thumb'] = Images::getImageUrl($value['goods_images'], 120, 120);
71 $oneGoods['color'] = $value['color_name']; 140 $oneGoods['color'] = $value['color_name'];