ShopCar.php
6.9 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
<?php
/**
* Created by PhpStorm.
* User: 86132
* Date: 2020/7/15
* Time: 16:02
*/
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
/**
* 购物车接口
*/
class ShopCar extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
}
// public function ceshi()
// {
// $arr=Db::name('shopcar')->where(['user_id'=>2])->select();
// foreach ($arr as $k=>$v){
// $list[$k] = $this->CalculateCommodityAmount($v['product_id'], $v['id'], $v['buy_num']);
// }
// foreach ($list as $value) {
// foreach ($value as $v) {
// $listitem[] = $v;
// }
// }
// $return['list']=$listitem;
// $this->success('', $return);
// }
/**
* @ApiTitle (购物车接口-添加购物车)
* @ApiSummary (添加购物车)
* @ApiMethod (POST)
* @ApiRoute (/api/Shop_Car/AddShoppingCart)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="string", required=true, description="商品ID")
* @ApiParams (name="num", type="string", 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": "1"
})
*/
public function AddShoppingCart()
{
$user_id = $this->is_token($this->request->header());
$param = $this->request->param();
$is_shop = Db::name('shopcar')->where(['user_id' => $user_id])->where(['product_id' => $param['id']])->value('buy_num');
if (empty($is_shop)) {
$res = Db::name('shopcar')->insert(['product_id' => $param['id'], 'buy_num' => $param['num'], 'user_id' => $user_id]);
} else {
$shop_num = $is_shop + $param['num'];
$res = Db::name('shopcar')->where(['user_id' => $user_id])->where(['product_id' => $param['id']])->update(['buy_num' => $shop_num]);
}
if ($res) {
$this->success('成功', 1);
} else {
$this->error('失败', 0);
}
}
/**
* @ApiTitle (购物车接口-购物车渲染)
* @ApiSummary (购物车渲染)
* @ApiMethod (POST)
* @ApiRoute (/api/Shop_Car/ShoppingCartRendering)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="shop_id", type="string", required=true, description="购物车ID[无传null]")
* @ApiParams (name="num", type="int", 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": {
"count_price": 总价,
"list": [
{
"product_id": 商品ID,
"id": 购物车ID,
"avatar": "http://qco519e0n.bkt.clouddn.com/uploads/20200703/Fpi3RGA38eqT3eDk_sh99hOZ7wAA.png",
"name": "12kf(123)+10.50v",
"class_con": "详细分类2",
"logo": "品牌1",
"buy_num": 数量,
"price": 价格,
"gradient": [
{
"tidu": "100",
"price": "0.10"
}pa'g
]
}
]
}
})
*/
public function ShoppingCartRendering()
{
$user_id = $this->is_token($this->request->header());
$param = $this->request->param();
$is_kong=Db::name("shopcar")->where(['user_id' => $user_id])->find();
if(empty($is_kong)){
$return['count_price'] = 0;
$return['count_num'] = 0;
$return['list'] = [];
$this->success('', $return);
}
$product_id = Db::name('shopcar')->where(['id' => $param['shop_id']])->value('product_id');
$stock = Db::name('product')->where(['id' => $product_id])->value('stock');
if (!empty($param['shop_id']) || $param['shop_id'] != null || $param['shop_id'] != '') {
$product_id = Db::name('shopcar')->where(['id' => $param['shop_id']])->value('product_id');
$stock = Db::name('product')->where(['id' => $product_id])->value('stock');
if ($param['num'] > $stock) {
Db::name('shopcar')->where(['user_id' => $user_id])->where(['id' => $param['shop_id']])->update(['buy_num' => $stock]);
} else {
Db::name('shopcar')->where(['user_id' => $user_id])->where(['id' => $param['shop_id']])->update(['buy_num' => $param['num']]);
}
}
$arr = Db::name('shopcar')->where(['user_id' => $user_id])->select();
foreach ($arr as $k => $v) {
$list[$k] = $this->CalculateCommodityAmount($v['product_id'], $v['id'], $v['buy_num']);
}
if (empty($list)) {
$listitem = [];
$return['count_price'] = 0;
$return['count_num'] = 0;
} else {
foreach ($list as $value) {
foreach ($value as $v) {
$listitem[] = $v;
}
}
$count_price = array_sum(array_column($listitem, 'price'));
$count_num = array_sum(array_column($listitem, 'buy_num'));
$return['count_price'] = round($count_price, 2);
$return['count_num'] = $count_num;
}
$return['list'] = $listitem;
$this->success('', $return);
}
/**
* @ApiTitle (购物车接口-购物车删除)
* @ApiSummary (购物车渲染)
* @ApiMethod (POST)
* @ApiRoute (/api/Shop_Car/DeleteCart)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="shop_id", type="string", required=true, description="购物车ID[单个不带,],[多选删除,分割]")
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
"data": {
}
})
*/
public function DeleteCart()
{
$shop_id = input('shop_id');
if (stristr($shop_id, ',')) {
$shop_id_arr = explode(',', $shop_id);
foreach ($shop_id_arr as $k => $v) {
$res[$k] = Db::name('shopcar')->where(['id' => $v])->delete();
}
if ($res) {
$this->success('删除成功', 1);
} else {
$this->error('删除失败', 0);
}
} else {
$res = Db::name('shopcar')->where(['id' => $shop_id])->delete();
if ($res) {
$this->success('删除成功', 1);
} else {
$this->error('删除失败', 0);
}
}
}
}