CartController.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace mobile\controllers;
  3. use Yii;
  4. use common\components\util;
  5. use common\services\xhCartService;
  6. class CartController extends MobileendController{
  7. public function actionIndex()
  8. {
  9. $this->layout = 'goodsMain';
  10. $userId = $this->userId;
  11. $this->webTitle = '购物车-'.$this->webTitle;//标题
  12. $cart = xhCartService::getUserCartList($userId);
  13. return $this->render('index',['cart' => $cart]);
  14. }
  15. /**
  16. * 添加到购物车
  17. */
  18. public function actionAdd()
  19. {
  20. $post = Yii::$app->request->post();
  21. //dump($post);die;
  22. $goodsId = isset($post['goodsId']) ? $post['goodsId'] : 0;
  23. $post['merchantId'] = $this->merchantId;
  24. $post['createTime'] = date("Y-m-d H:i:s");
  25. $post['userId'] = $this->userId;
  26. unset($post['_csrf']);
  27. xhCartService::replace($this->userId, $goodsId, $post);
  28. util::successInfo('添加成功');
  29. }
  30. /**
  31. * 删除购物车的一个商品
  32. */
  33. public function actionDel()
  34. {
  35. $post = Yii::$app->request->post();
  36. $goodsId = isset($post['goodsId']) ? $post['goodsId'] : 0;
  37. $cart = xhCartService::getByIds($this->userId, $goodsId, $post['goodsPriceId']);
  38. if($cart['userId'] != $this->userId){
  39. util::stop('非法访问');
  40. }
  41. $goodsPriceId = $cart['goodsPriceId'];
  42. xhCartService::delByIds($this->userId, $goodsId, $goodsPriceId, $cart['goodsPriceId']);
  43. util::successInfo('删除成功');
  44. }
  45. }