Cart.php
2.5 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace app\api\controller;
use app\common\controller\Api;
/**
* 购物车
*/
class Cart extends Api
{
// protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* @ApiTitle (加入购物车)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="sku_id", type="integer", required=true, description="规格id")
* @ApiParams (name="goods_id", type="integer", required=true, description="商品id")
* @ApiParams (name="number", type="integer", required=true, description="购买数量")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
})
*/
public function addCart()
{
$sku_id = $this->request->post('sku_id');
$goods_id = $this->request->post('goods_id');
$number = $this->request->post('number');
if (!is_numeric($sku_id) || !is_numeric($goods_id) || !is_numeric($number)) $this->error('参数不合法');
$data = [
'user_id' => $this->auth->id,
'sku_id' => $sku_id,
'goods_id' => $goods_id,
'number' => $number,
];
$model = new \app\api\model\Cart();
$model->save($data);
$this->success('SUCCESS');
}
/**
* @ApiTitle (增加购物车数量)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="购物车id")
* @ApiParams (name="number", type="integer", required=true, description="增加数量")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
})
*/
public function addCartNumber()
{
$id = $this->request->post('id');
$number = $this->request->post('number');
if (!is_numeric($id) || !is_numeric($number)) $this->error('参数不合法');
$model = new \app\api\model\Cart();
$model->where('id',$id)->setInc('number',$number);
$this->success('SUCCESS');
}
/**
* @ApiTitle (购物车列表)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
})
*/
public function cartList()
{
$model = new \app\api\model\Cart();
$list = $model
->with(['goods','sku'])
->where('user_id',$this->auth->id)
->select();
$this->success('SUCCESS',$list);
}
}