Cart.php
2.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace addons\litestore\controller\api;
use app\common\controller\Api;
use app\common\library\Auth;
use addons\litestore\model\CacheCart;
class Cart extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
//设置返回的会员字段
protected $allowFields = ['id', 'username', 'nickname', 'mobile', 'avatar', 'score', 'level', 'bio', 'balance','group_id'];
public function _initialize()
{
parent::_initialize();
Auth::instance()->setAllowFields($this->allowFields);
$this->user_id = $this->auth->id;
$this->model = new CacheCart($this->user_id);
}
/* 加入购物车*/
public function add()
{
$rq_data = $this->request->request();
$goods_id = $rq_data['goods_id'];
$goods_num = $rq_data['goods_num'];
$goods_sku_id = $rq_data['goods_sku_id'];
if (!$this->model->add($goods_id, $goods_num, $goods_sku_id)) {
return $this->error($this->model->getError() ?: '加入购物车失败');
}
$total_num = $this->model->getTotalNum();
return $this->success('加入购物车成功',['cart_total_num' => $total_num]);
}
//获得购物车商品数量
public function getTotalNum(){
$total_num = $this->model->getTotalNum();
return $this->success('',['cart_total_num' => $total_num]);
}
public function getlists()
{
return $this->success('',$this->model->getList($this->user_id));
}
public function sub(){
$rq_data = $this->request->request();
$goods_id = $rq_data['goods_id'];
$goods_sku_id = $rq_data['goods_sku_id'];
$this->model->sub($goods_id, $goods_sku_id);
return $this->success();
}
public function delete()
{
$rq_data = $this->request->request();
$goods_id = $rq_data['goods_id'];
$goods_sku_id = $rq_data['goods_sku_id'];
$this->model->delete($goods_id, $goods_sku_id);
return $this->success();
}
}