ShopCar.php
6.6 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* Created by PhpStorm.
* User: 86132
* Date: 2020/11/18
* Time: 16:26
*/
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
/**
* 购物车接口
*/
class ShopCar extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* 购物车接口
* @ApiTitle (添加购物车)
* @ApiSummary (添加购物车)
* @ApiMethod (POST)
* @ApiRoute (/api/Shop_Car/InsertShopCar)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="Goods_id", type="integer", required=true, description=" 商品ID")
* @ApiParams (name="num", type="integer", required=true, description="数量")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
}
})
*/
public function InsertShopCar()
{
$UserId = $this->is_token($this->request->header());
$params = $this->request->param();
//Json转数组 json_decode($Json, true);
//检查是否存在商品
$IsCar = Db::name('shopcar')->where('user_id', $UserId)->where('Goods_id', $params['Goods_id'])->find();
if (empty($IsCar)) {
//为空 检查库存
$this->StockLook($params['Goods_id'], $params['num']);
$data = [
'user_id' => $UserId,
'Goods_id' => $params['Goods_id'],
'num' => $params['num']
];
$Res = Db::name('shopcar')->insert($data);
} else {
// 检查库存
$this->StockLook($params['Goods_id'], $params['num'] + $IsCar['num']);
$Res = Db::name('shopcar')->where('user_id', $UserId)->where('Goods_id', $params['Goods_id'])->update(
[
'num' => $params['num'] + $IsCar['num']
]
);
}
$this->res($Res);
}
/**
* 购物车接口
* @ApiTitle (删除购物车)
* @ApiSummary (删除购物车)
* @ApiMethod (POST)
* @ApiRoute (/api/Shop_Car/DeleteShopCar)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="type", type="integer", required=true, description="1=删除单个,2=一键清空")
* @ApiParams (name="id", type="integer", required=true, description="购物车ID 一键清空为0")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
}
})
*/
public function DeleteShopCar()
{
$UserId = $this->is_token($this->request->header());
$params = $this->request->param();
$map = [];
if ($params['type'] == 2) {
//一键清空
$map['id'] = ['EQ', $params['id']];
}
$Res = Db::name('shopcar')->where('user_id', $UserId)->where($map)->delete();
$this->res($Res);
}
/**
* 购物车接口
* @ApiTitle (购物车操作)
* @ApiSummary (购物车操作)
* @ApiMethod (POST)
* @ApiRoute (/api/Shop_Car/ShopcarAddDel)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="type", type="integer", required=true, description="1=增加,2=减少")
* @ApiParams (name="id", type="integer", required=true, description="购物车ID")
* @ApiParams (name="num", type="integer", required=true, description="数量")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
}
})
*/
public function ShopcarAddDel()
{
$UserId = $this->is_token($this->request->header());
$params = $this->request->param();
$ShopCarArray = Db::name('shopcar')->where('user_id', $UserId)->where('id', $params['id'])->find();
if (empty($ShopCarArray)) {
$this->error('参数错误', 0);
die;
}
if ($params['type'] == 1) {
//增加
//检查库存
$this->StockLook($ShopCarArray['Goods_id'], $params['num'] + $ShopCarArray['num']);
$Res = Db::name('shopcar')->where('user_id', $UserId)->where('id', $params['id'])->update(
[
'num' => $params['num'] + $ShopCarArray['num']
]
);
} else {
//减少
$Res = Db::name('shopcar')->where('user_id', $UserId)->where('id', $params['id'])->update(
[
'num' => $ShopCarArray['num'] - $params['num']
]
);
}
$this->res($Res);
}
/**
* 购物车接口
* @ApiTitle (购物车列表-有更改(增加规格))
* @ApiSummary (购物车列表-有更改(增加规格))
* @ApiMethod (POST)
* @ApiRoute (/api/Shop_Car/ShopcarList)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功',
"data": {
}
})
*/
public function ShopcarList()
{
$UserId = $this->is_token($this->request->header());
$Array = Db::name('shopcar')
->alias('a')
->where('a.user_id', $UserId)
->order('a.id desc')
->join('goods g', 'g.id=a.Goods_id')
->field('a.id,g.id as Goods_id,g.image,g.title,g.price,g.delete_price,a.num,g.speci')
->select();
$List = [];
if (!empty($Array)) {
foreach ($Array as $k => $v) {
$List[$k]['id'] = $v['id'];
$List[$k]['image'] = cdnurl($v['image']);
$List[$k]['title'] = $v['title'];
$List[$k]['Goods_id'] = $v['Goods_id'];
$List[$k]['price'] = $v['price'];
$List[$k]['delete_price'] = $v['delete_price'];
$List[$k]['num'] = $v['num'];
$List[$k]['speci'] = $v['speci'];
}
}
$this->success('成功', $List);
}
}