123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace app\api\controller;
- use app\model\goods\Cart as CartModel;
- class Cart extends BaseApi
- {
- /**
- * 添加信息
- */
- public function add()
- {
- $token = $this->checkToken();
- if ($token['code'] < 0) return $this->response($token);
-
- $sku_id = isset($this->params['sku_id']) ? $this->params['sku_id'] : 0;
- $num = isset($this->params['num']) ? $this->params['num'] : 0;
- if (empty($sku_id)) {
- return $this->response($this->error('', 'REQUEST_SKU_ID'));
- }
- if (empty($num)) {
- return $this->response($this->error('', 'REQUEST_NUM'));
- }
- $cart = new CartModel();
- $data = [
- 'site_id' => $this->params['site_id'],
- 'member_id' => $token['data']['member_id'],
- 'sku_id' => $sku_id,
- 'num' => $num,
- 'create_time'=>time()
- ];
- $res = $cart->addCart($data);
- return $this->response($res);
- }
-
- /**
- * 编辑信息
- */
- public function edit()
- {
- $token = $this->checkToken();
- if ($token['code'] < 0) return $this->response($token);
-
- $cart_id = isset($this->params['cart_id']) ? $this->params['cart_id'] : 0;
- $num = isset($this->params['num']) ? $this->params['num'] : 0;
- if (empty($cart_id)) {
- return $this->response($this->error('', 'REQUEST_CART_ID'));
- }
- if (empty($num)) {
- return $this->response($this->error('', 'REQUEST_NUM'));
- }
-
- $cart = new CartModel();
- $data = [
- 'cart_id' => $cart_id,
- 'member_id' => $token['data']['member_id'],
- 'num' => $num
- ];
- $res = $cart->editCart($data);
- return $this->response($res);
- }
-
- /**
- * 删除信息
- */
- public function delete()
- {
- $token = $this->checkToken();
- if ($token['code'] < 0) return $this->response($token);
-
- $cart_id = isset($this->params['cart_id']) ? $this->params['cart_id'] : 0;
- if (empty($cart_id)) {
- return $this->response($this->error('', 'REQUEST_CART_ID'));
- }
- $cart = new CartModel();
- $data = [
- 'cart_id' =>$cart_id,
- 'member_id' => $token['data']['member_id']
- ];
- $res = $cart->deleteCart($data);
- return $this->response($res);
- }
-
- /**
- * 清空购物车
- */
- public function clear()
- {
- $token = $this->checkToken();
- if ($token['code'] < 0) return $this->response($token);
-
- $cart = new CartModel();
- $data = [
- 'member_id' => $token['data']['member_id']
- ];
- $res = $cart->clearCart($data);
- return $this->response($res);
- }
-
- /**
- * 列表信息
- */
- public function lists()
- {
- $token = $this->checkToken();
- if ($token['code'] < 0) return $this->response($token);
- $cart = new CartModel();
- $goods_model = new \app\model\goods\Goods();
- $list = $cart->getCart($token['data']['member_id']);
- foreach ($list['data'] as &$v){
- $gods_price =$goods_model->getGoodsInfo([['goods_id','=',$v['goods_id']]],'pay_num1,pay_num2,pay_num3,price1,price2,price3');
- if($v['num']>=$gods_price['data']['pay_num1']){
- $v['price'] =$gods_price['data']['price1'];
- }
- if($v['num']>=$gods_price['data']['pay_num2']){
- $v['price'] =$gods_price['data']['price2'];
- }
- if($v['num']>=$gods_price['data']['pay_num3']){
- $v['price'] =$gods_price['data']['price3'];
- }
- $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
- }
- return $this->response($list);
- }
-
- /**
- * 获取购物车数量
- * @return string
- */
- public function count()
- {
- $token = $this->checkToken();
- if ($token['code'] < 0) return $this->response($token);
- $cart = new CartModel();
- $list = $cart->getCartCount($token['data']['member_id']);
- return $this->response($list);
- }
- }
|