Cart.php
8.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace app\api\controller;
use app\api\model\GoodsSpec;
use app\api\model\SpecValue;
use app\common\controller\Api;
use think\Db;
/**
* 购物车
*/
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,
];
$model = new \app\api\model\Cart();
$cart = $model->where($data)->find();
if ($cart){
$cart->setInc('number',$number);
}else{
$data['number'] = $number;
$model->isUpdate(false)->save($data);
}
$this->success('SUCCESS');
}
/**
* @ApiTitle (删除购物车)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="cart_ids", type="integer", required=true, description="购物车id 逗号隔开")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
})
*/
public function delCart()
{
$ids = $this->request->post('cart_ids');
if (!$ids) $this->error('参数不可为空');
$ids = explode(',',$ids);
foreach ($ids as $key => $value){
if (!is_numeric($value)) $this->error('参数不合法');
}
$model = new \app\api\model\Cart();
$model->whereIn('id',$ids)->delete();
$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();
$order = $model->with('sku')->where('id',$id)->find();
if ($number+$order['number'] > $order['sku']['stock_num']) $this->error('已达到最大库存,请勿重复提交');
$model->where('id',$id)->setInc('number',$number);
$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 subCartNumber()
{
$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();
if ($model->where('id',$id)->value('number') ==1){
$this->error('购物车数量不可再减少');
}else{
$model->where('id',$id)->setDec('number',$number);
}
$this->success('SUCCESS');
}
/**
* @ApiTitle (购物车列表)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiReturn ({
'code':'1',
'msg':'SUCCESS'
'data'
{
"id": 1, 购车id
"user_id": 0,
"goods_id": 21, 商品id
"sku_id": 125, 规格id
"number": 10, 购买数量
"createtime": null,
"goods": {
"goods_id": 21,
"goods_name": "小米Mix3",
"image_text": "http://temporaryfood.qiniu.broing.cn123132", 封面图
"images_text": [
"https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/ffc4440df18661948b9c2d4dd4ae419b.jpg",
"https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/83bf8f141969a9e3e607a768407fc7e0.jpg",
"https://her-family.oss-cn-qingdao.aliyuncs.com/addons_store_uploads/20181105/c5d85254fc17b1a1b0e2254470881e59.jpg"
],
"down_image_text": "http://temporaryfood.qiniu.broing.cn/uploads/20220114/f8bb72e2ce3e70f0a9f54e6c0b4a6c14.png",
"four_image_text": []
},
"sku": {
"goods_spec_id": 125,
"goods_price": "100.00", 价格
"spec_sku_id": "40_54",
"sku_name": "黑色 1" 规格名
}
}
})
*/
public function cartList()
{
$model = new \app\api\model\Cart();
$list = $model
->with(['goods','sku'])
->where('user_id',$this->auth->id)
->order('createtime desc')
->select();
foreach ($list as $key => $value){
$value->getRelation('goods')->visible(['goods_name','goods_id']);
$value->getRelation('sku')->visible(['goods_spec_id','goods_price','spec_sku_id']);
}
$list = collection($list)->toArray();
foreach ($list as $k => &$val){
$skuids = explode('_', $val['sku']['spec_sku_id']);
$sku = Db::name('litestore_spec_value')->where('id','in',$skuids)->column('spec_value');
$skuname = !empty($sku)?implode(' ',$sku):'';
$val['sku']['sku_name'] = $skuname;
}
$this->success('SUCCESS',$list);
}
/**
* @ApiTitle (价格计算)
* @ApiSummary ([{goods_id:22 goods_sku_id:106 number:2} {goods_id:23 goods_sku_id:66 number:2}])
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="data_json", type="string", required=true, description="下单的商品json数据")
* @ApiParams (name="goods_id", type="integer", required=false, description="商品id 此值不传 json数组注释用")
* @ApiParams (name="goods_sku_id", type="integer", required=false, description="规格id 此值不传 json数组注释用")
* @ApiParams (name="number", type="integer", required=false, description="购买数量 此值不传 json数组注释用")
* @ApiReturn ({
'code':'1',
'msg':'购物车页面价格'
'data':
"price": "37574.00" 总价
})
*/
public function priceCalculation()
{
$json = $this->request->param('data_json');
if (!$json) $this->error('data_json参数不能为空');
// $json = '[{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}]';
$json = str_replace('"','"',$json);
$data = json_decode($json,true);
$goodsmodel = new \app\api\model\Goods();
$skumodel = new GoodsSpec();
$sum_price = 0; //总价格
foreach ($data as $key => $value){
if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])){
$this->error('参数不合法');
}
$goods = $goodsmodel->where('goods_id',$value['goods_id'])->field('goods_id,goods_name,image,spec_type')->find();
if (!$goods) $this->error('商品不存在');
$sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])
->field('goods_spec_id,spec_sku_id,goods_price')->find();
if (!$sku) $this->error('商品规格不存在');
$sum_price = bcadd($sum_price,bcmul($sku['goods_price'],$value['number'],2),2);
}
$this->success('购物车页面价格',['price'=>$sum_price]);
}
}