|
@@ -7,6 +7,8 @@ namespace app\api\controller; |
|
@@ -7,6 +7,8 @@ namespace app\api\controller; |
7
|
use app\api\model\GoodsSpec;
|
7
|
use app\api\model\GoodsSpec;
|
8
|
use app\api\model\SpecValue;
|
8
|
use app\api\model\SpecValue;
|
9
|
use app\common\controller\Api;
|
9
|
use app\common\controller\Api;
|
|
|
10
|
+use think\Db;
|
|
|
11
|
+use think\exception\PDOException;
|
10
|
|
12
|
|
11
|
/**
|
13
|
/**
|
12
|
* 订单
|
14
|
* 订单
|
|
@@ -18,7 +20,6 @@ class Order extends Api |
|
@@ -18,7 +20,6 @@ class Order extends Api |
18
|
|
20
|
|
19
|
/**
|
21
|
/**
|
20
|
* @ApiTitle (订单列表)
|
22
|
* @ApiTitle (订单列表)
|
21
|
- * @ApiSummary ([{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}])
|
|
|
22
|
* @ApiMethod (POST)
|
23
|
* @ApiMethod (POST)
|
23
|
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
24
|
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
24
|
* @ApiParams (name="type", type="integer", required=true, description="类型1全部2待付款3待发货4待收货5待评价")
|
25
|
* @ApiParams (name="type", type="integer", required=true, description="类型1全部2待付款3待发货4待收货5待评价")
|
|
@@ -26,6 +27,41 @@ class Order extends Api |
|
@@ -26,6 +27,41 @@ class Order extends Api |
26
|
* @ApiReturn ({
|
27
|
* @ApiReturn ({
|
27
|
'code':'1',
|
28
|
'code':'1',
|
28
|
'msg':'返回成功'
|
29
|
'msg':'返回成功'
|
|
|
30
|
+ 'data':
|
|
|
31
|
+ "total": 1, 总条数
|
|
|
32
|
+ "per_page": 5, 每页数量
|
|
|
33
|
+ "current_page": 1, 当前页
|
|
|
34
|
+ "last_page": 1, 最后一页
|
|
|
35
|
+ "data": [
|
|
|
36
|
+ {
|
|
|
37
|
+ "id": 1, 订单id
|
|
|
38
|
+ "order_no": "adsadasdas", 订单编号
|
|
|
39
|
+ "discount_price": "0.00", 优惠金额
|
|
|
40
|
+ "pay_price": "0.00", 实付价格
|
|
|
41
|
+ "pay_status": "10", 10未支付20已支付
|
|
|
42
|
+ "freight_status": "10", 10未发货20已发货
|
|
|
43
|
+ "receipt_status": "10", 10未收货20已收货
|
|
|
44
|
+ "order_status": "10", 10进行中20已取消30已完成
|
|
|
45
|
+ "total_sum": 0, 商品总数量
|
|
|
46
|
+ "goods": [
|
|
|
47
|
+ {
|
|
|
48
|
+ "goods_name": "asdasd", 商品名
|
|
|
49
|
+ "goods_attr": "1", 规格名
|
|
|
50
|
+ "total_num": 0, 数量
|
|
|
51
|
+ "total_price": "0.00", 价格
|
|
|
52
|
+ "image_text": "" 图片
|
|
|
53
|
+ },
|
|
|
54
|
+ {
|
|
|
55
|
+ "goods_name": "asdasd",
|
|
|
56
|
+ "goods_attr": "1",
|
|
|
57
|
+ "total_num": 0,
|
|
|
58
|
+ "total_price": "0.00",
|
|
|
59
|
+ "image_text": ""
|
|
|
60
|
+ }
|
|
|
61
|
+ ],
|
|
|
62
|
+ "createtime_text": "" 时间
|
|
|
63
|
+ }
|
|
|
64
|
+ ]
|
29
|
})
|
65
|
})
|
30
|
*/
|
66
|
*/
|
31
|
public function orderList()
|
67
|
public function orderList()
|
|
@@ -34,12 +70,11 @@ class Order extends Api |
|
@@ -34,12 +70,11 @@ class Order extends Api |
34
|
$page = $this->request->post('page',1);
|
70
|
$page = $this->request->post('page',1);
|
35
|
if (!in_array($type,[1,2,3,4,5])) $this->error('type参数不合法');
|
71
|
if (!in_array($type,[1,2,3,4,5])) $this->error('type参数不合法');
|
36
|
if (!is_numeric($page)) $this->error('页数不合法');
|
72
|
if (!is_numeric($page)) $this->error('页数不合法');
|
37
|
- $model = new \app\api\model\Order();
|
|
|
38
|
switch ($type){
|
73
|
switch ($type){
|
39
|
case 1:
|
74
|
case 1:
|
40
|
$where = [
|
75
|
$where = [
|
41
|
'fa_litestore_order.status' => 'normal',
|
76
|
'fa_litestore_order.status' => 'normal',
|
42
|
- 'fa_litestore_order.user_id' => 1,
|
77
|
+ 'fa_litestore_order.user_id' => $this->auth->id,
|
43
|
];
|
78
|
];
|
44
|
break;
|
79
|
break;
|
45
|
case 2:
|
80
|
case 2:
|
|
@@ -71,11 +106,26 @@ class Order extends Api |
|
@@ -71,11 +106,26 @@ class Order extends Api |
71
|
];
|
106
|
];
|
72
|
break;
|
107
|
break;
|
73
|
}
|
108
|
}
|
|
|
109
|
+
|
|
|
110
|
+ $model = new \app\api\model\Order();
|
74
|
$list = $model
|
111
|
$list = $model
|
75
|
->with(['goods'])
|
112
|
->with(['goods'])
|
76
|
->where($where)
|
113
|
->where($where)
|
77
|
->order('id','desc')
|
114
|
->order('id','desc')
|
78
|
- ->paginate(5,false,['page'=>$page]);
|
115
|
+ ->paginate(5,false,['page'=>$page])
|
|
|
116
|
+ ->each(function ($item,$key){
|
|
|
117
|
+ $sum = 0;
|
|
|
118
|
+ foreach ($item->getRelation('goods')as $key => $value){
|
|
|
119
|
+ $sum += $value['total_num'];
|
|
|
120
|
+ $value->visible(['goods_name','goods_attr','total_num','total_price']);
|
|
|
121
|
+ }
|
|
|
122
|
+ $item['total_sum'] = $sum;
|
|
|
123
|
+ $item->visible([
|
|
|
124
|
+ 'goods','total_sum','order_no','id','pay_price',
|
|
|
125
|
+ 'discount_price','pay_status','order_status','receipt_status',
|
|
|
126
|
+ 'freight_status'
|
|
|
127
|
+ ]);
|
|
|
128
|
+ });
|
79
|
$this->success('订单列表',$list);
|
129
|
$this->success('订单列表',$list);
|
80
|
}
|
130
|
}
|
81
|
|
131
|
|
|
@@ -91,36 +141,64 @@ class Order extends Api |
|
@@ -91,36 +141,64 @@ class Order extends Api |
91
|
* @ApiReturn ({
|
141
|
* @ApiReturn ({
|
92
|
'code':'1',
|
142
|
'code':'1',
|
93
|
'msg':'返回成功'
|
143
|
'msg':'返回成功'
|
|
|
144
|
+ 'data':
|
|
|
145
|
+ "list": [
|
|
|
146
|
+ {
|
|
|
147
|
+ "goods_id": 22, 商品id
|
|
|
148
|
+ "goods_name": "Mate 20 华为 HUAWEI ", 商品名称
|
|
|
149
|
+ "spec_type": "20", 20多规格10单规格
|
|
|
150
|
+ "sku_id": 106, 规格id
|
|
|
151
|
+ "sku_price": "6099.00", 规格单价
|
|
|
152
|
+ "sku_name": "极光色 8GB+128GB", 规格名称
|
|
|
153
|
+ "number": 2, 数量
|
|
|
154
|
+ "image_text": "", 图片
|
|
|
155
|
+ },
|
|
|
156
|
+ {
|
|
|
157
|
+ "goods_id": 23,
|
|
|
158
|
+ "goods_name": "MacBook Pro 13寸",
|
|
|
159
|
+ "spec_type": "20",
|
|
|
160
|
+ "sku_id": 66,
|
|
|
161
|
+ "sku_price": "12688.00",
|
|
|
162
|
+ "sku_name": "天空灰",
|
|
|
163
|
+ "number": 2,
|
|
|
164
|
+ "image_text": "",
|
|
|
165
|
+ }
|
|
|
166
|
+ ],
|
|
|
167
|
+ "price": "37574.00" 总价
|
94
|
})
|
168
|
})
|
95
|
*/
|
169
|
*/
|
96
|
public function orderCalculation()
|
170
|
public function orderCalculation()
|
97
|
{
|
171
|
{
|
98
|
$json = $this->request->post('data_json');
|
172
|
$json = $this->request->post('data_json');
|
99
|
if (!$json) $this->error('data_json参数不能为空');
|
173
|
if (!$json) $this->error('data_json参数不能为空');
|
100
|
- $json = '[{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}]';
|
174
|
+// $json = '[{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}]';
|
101
|
$data = json_decode($json,true);
|
175
|
$data = json_decode($json,true);
|
102
|
$goodsmodel = new \app\api\model\Goods();
|
176
|
$goodsmodel = new \app\api\model\Goods();
|
103
|
$skumodel = new GoodsSpec();
|
177
|
$skumodel = new GoodsSpec();
|
104
|
$specmodel = new SpecValue();
|
178
|
$specmodel = new SpecValue();
|
105
|
- $goods_array = [];
|
|
|
106
|
- $sum_price = 0;
|
179
|
+ $goods_array = []; //商品列表
|
|
|
180
|
+ $sum_price = 0; //总价格
|
107
|
foreach ($data as $key => $value){
|
181
|
foreach ($data as $key => $value){
|
108
|
if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])){
|
182
|
if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])){
|
109
|
$this->error('参数不合法');
|
183
|
$this->error('参数不合法');
|
110
|
}
|
184
|
}
|
111
|
$goods = $goodsmodel->where('goods_id',$value['goods_id'])->field('goods_id,goods_name,image,spec_type')->find();
|
185
|
$goods = $goodsmodel->where('goods_id',$value['goods_id'])->field('goods_id,goods_name,image,spec_type')->find();
|
112
|
if (!$goods) $this->error('商品不存在');
|
186
|
if (!$goods) $this->error('商品不存在');
|
|
|
187
|
+ $goods->visibleAppend(['image_text']);
|
|
|
188
|
+ $goods->hidden(['image']);
|
113
|
$sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])
|
189
|
$sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])
|
114
|
->field('goods_spec_id,spec_sku_id,goods_price')->find();
|
190
|
->field('goods_spec_id,spec_sku_id,goods_price')->find();
|
115
|
if (!$sku) $this->error('商品规格不存在');
|
191
|
if (!$sku) $this->error('商品规格不存在');
|
|
|
192
|
+ $sku->unsetAppend();
|
|
|
193
|
+
|
116
|
$goods['sku_id'] = $sku['goods_spec_id'];
|
194
|
$goods['sku_id'] = $sku['goods_spec_id'];
|
117
|
$goods['sku_price'] = $sku['goods_price'];
|
195
|
$goods['sku_price'] = $sku['goods_price'];
|
118
|
if ($goods['spec_type'] == 10){
|
196
|
if ($goods['spec_type'] == 10){
|
119
|
- $goods['sku_name'] ='';
|
197
|
+ $goods['sku_name'] =''; //规格名
|
120
|
}else{
|
198
|
}else{
|
121
|
$ids = explode('_',$sku['spec_sku_id']);
|
199
|
$ids = explode('_',$sku['spec_sku_id']);
|
122
|
$sku_name = $specmodel->whereIn('id',$ids)->column('spec_value');
|
200
|
$sku_name = $specmodel->whereIn('id',$ids)->column('spec_value');
|
123
|
- $goods['sku_name'] = implode(' ',$sku_name);
|
201
|
+ $goods['sku_name'] = implode(' ',$sku_name); //规格名
|
124
|
}
|
202
|
}
|
125
|
$goods['number'] = $value['number'];
|
203
|
$goods['number'] = $value['number'];
|
126
|
$goods_array[] = $goods;
|
204
|
$goods_array[] = $goods;
|
|
@@ -128,4 +206,129 @@ class Order extends Api |
|
@@ -128,4 +206,129 @@ class Order extends Api |
128
|
}
|
206
|
}
|
129
|
$this->success('下单页详情',['list'=>$goods_array,'price'=>$sum_price]);
|
207
|
$this->success('下单页详情',['list'=>$goods_array,'price'=>$sum_price]);
|
130
|
}
|
208
|
}
|
|
|
209
|
+
|
|
|
210
|
+
|
|
|
211
|
+ /**
|
|
|
212
|
+ * @ApiTitle (下单)
|
|
|
213
|
+ * @ApiMethod (POST)
|
|
|
214
|
+ * @ApiSummary ([{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}])
|
|
|
215
|
+ * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
|
|
|
216
|
+ * @ApiParams (name="data_json", type="string", required=true, description="下单的商品json数据")
|
|
|
217
|
+ * @ApiParams (name="goods_id", type="integer", required=false, description="商品id 此值不传 json数组注释用")
|
|
|
218
|
+ * @ApiParams (name="goods_sku_id", type="integer", required=false, description="规格id 此值不传 json数组注释用")
|
|
|
219
|
+ * @ApiParams (name="number", type="integer", required=false, description="购买数量 此值不传 json数组注释用")
|
|
|
220
|
+ * @ApiParams (name="coupon_id", type="integer", required=true, description="优惠券id 无优惠券传0")
|
|
|
221
|
+ * @ApiParams (name="address_id", type="integer", required=true, description="地址id")
|
|
|
222
|
+ * @ApiReturn ({
|
|
|
223
|
+ 'code':'1',
|
|
|
224
|
+ 'msg':'返回成功'
|
|
|
225
|
+ 'data':
|
|
|
226
|
+
|
|
|
227
|
+ })
|
|
|
228
|
+ */
|
|
|
229
|
+ public function addOrder()
|
|
|
230
|
+ {
|
|
|
231
|
+ $json = $this->request->post('data_json');
|
|
|
232
|
+ $coupon_id = $this->request->post('coupon_id');
|
|
|
233
|
+ $address_id = $this->request->post('address_id');
|
|
|
234
|
+// if (!$json) $this->error('data_json参数不能为空');
|
|
|
235
|
+ $json = '[{"goods_id":22,"goods_sku_id":106,"number":2},{"goods_id":23,"goods_sku_id":66,"number":2}]';
|
|
|
236
|
+ $address = Db::name('user_address')
|
|
|
237
|
+ ->where('id',$address_id)
|
|
|
238
|
+ ->where('user_id',1)
|
|
|
239
|
+ ->find();
|
|
|
240
|
+ if (!$address) $this->error('地址不存在');
|
|
|
241
|
+ $coupon = [];
|
|
|
242
|
+ if ($coupon_id > 0){
|
|
|
243
|
+ $coupon = Db::name('user_coupon')
|
|
|
244
|
+ ->where('id',$coupon_id)
|
|
|
245
|
+ ->where('user_id',$this->auth->id)
|
|
|
246
|
+ ->find();
|
|
|
247
|
+ if (!$coupon) $this->error('优惠券不存在');
|
|
|
248
|
+ }
|
|
|
249
|
+
|
|
|
250
|
+ $data = json_decode($json,true);
|
|
|
251
|
+ $goodsmodel = new \app\api\model\Goods();
|
|
|
252
|
+ $skumodel = new GoodsSpec();
|
|
|
253
|
+ $ordermodel = new \app\api\model\Order();
|
|
|
254
|
+ $ordergoodsmodel = new \app\api\model\OrderGoods();
|
|
|
255
|
+ $orderaddressmodel = new \app\api\model\OrderAddress();
|
|
|
256
|
+ $specmodel = new SpecValue();
|
|
|
257
|
+ $goods_array = []; //商品列表
|
|
|
258
|
+ $sum_price = 0; //总价格
|
|
|
259
|
+
|
|
|
260
|
+ foreach ($data as $key => $value){
|
|
|
261
|
+ if (!is_numeric($value['goods_id']) || !is_numeric($value['goods_sku_id']) || !is_numeric($value['number'])){
|
|
|
262
|
+ $this->error('参数不合法');
|
|
|
263
|
+ }
|
|
|
264
|
+ $goods = $goodsmodel->where('goods_id',$value['goods_id'])->find();
|
|
|
265
|
+ if (!$goods) $this->error('商品不存在');
|
|
|
266
|
+ $sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])->find();
|
|
|
267
|
+ if (!$sku) $this->error('商品规格不存在');
|
|
|
268
|
+
|
|
|
269
|
+ if ($goods['spec_type'] == 10){
|
|
|
270
|
+ $sku_name =''; //规格名
|
|
|
271
|
+ }else{
|
|
|
272
|
+ $ids = explode('_',$sku['spec_sku_id']);
|
|
|
273
|
+ $sku_name = $specmodel->whereIn('id',$ids)->column('spec_value');
|
|
|
274
|
+ $sku_name = implode(' ',$sku_name); //规格名
|
|
|
275
|
+ }
|
|
|
276
|
+ $goods_array[] = [
|
|
|
277
|
+ 'goods_id' => $goods['goods_id'],
|
|
|
278
|
+ 'goods_name' => $goods['goods_name'],
|
|
|
279
|
+ 'image' => $goods['image'],
|
|
|
280
|
+ 'deduct_stock_type' => $goods['deduct_stock_type'],
|
|
|
281
|
+ 'spec_type' => $goods['spec_type'],
|
|
|
282
|
+ 'spec_sku_id' => $sku['spec_sku_id'],
|
|
|
283
|
+ 'goods_spec_id' => $sku['goods_spec_id'],
|
|
|
284
|
+ 'goods_attr' => $sku_name,
|
|
|
285
|
+ 'content' => $goods['content'],
|
|
|
286
|
+ 'goods_no' => $goods['number'],
|
|
|
287
|
+ 'goods_price' => $sku['goods_price'],
|
|
|
288
|
+ 'goods_weight' => $sku['goods_weight'],
|
|
|
289
|
+ 'total_num' => $value['number'],
|
|
|
290
|
+ 'total_price' => bcmul($sku['goods_price'],$value['number'],2),
|
|
|
291
|
+ 'user_id' => 1,
|
|
|
292
|
+ ];
|
|
|
293
|
+ $sum_price = bcadd($sum_price,bcmul($sku['goods_price'],$value['number'],2),2);
|
|
|
294
|
+ }
|
|
|
295
|
+
|
|
|
296
|
+ if ($coupon !== [] && $coupon['full_price'] < $sum_price) $this->error('优惠券不可使用');
|
|
|
297
|
+
|
|
|
298
|
+ $order_address = [
|
|
|
299
|
+ 'name' => $address['username'],
|
|
|
300
|
+ 'phone' => $address['mobile'],
|
|
|
301
|
+ 'address' => $address['address'],
|
|
|
302
|
+ 'detail' => $address['address_detail'],
|
|
|
303
|
+ 'lng' => $address['lng'],
|
|
|
304
|
+ 'lat' => $address['lat'],
|
|
|
305
|
+ 'user_id' => 1,
|
|
|
306
|
+ ];
|
|
|
307
|
+ $order_no = 'LQ-'.time().mt_rand(1000,9999);
|
|
|
308
|
+ $couponprice = isset($coupon['price'])?$coupon['price']:0;
|
|
|
309
|
+ $order = [
|
|
|
310
|
+ 'order_no' => $order_no,
|
|
|
311
|
+ 'total_price' => $sum_price,
|
|
|
312
|
+ 'discount_price' => $couponprice,
|
|
|
313
|
+ 'pay_price' => bcadd($sum_price,$couponprice,2),
|
|
|
314
|
+ 'user_id' => 1
|
|
|
315
|
+ ];
|
|
|
316
|
+
|
|
|
317
|
+ try {
|
|
|
318
|
+ Db::startTrans();
|
|
|
319
|
+ $ordermodel->save($order);
|
|
|
320
|
+ foreach ($goods_array as $key => &$value){
|
|
|
321
|
+ $value['order_id'] = $ordermodel->id;
|
|
|
322
|
+ }
|
|
|
323
|
+ $ordergoodsmodel->saveAll($goods_array);
|
|
|
324
|
+ $order_address['order_id'] = $ordermodel->id;
|
|
|
325
|
+ $orderaddressmodel->save($order_address);
|
|
|
326
|
+ Db::commit();
|
|
|
327
|
+ }catch (PDOException $e){
|
|
|
328
|
+ Db::rollback();
|
|
|
329
|
+ $this->error($e->getMessage());
|
|
|
330
|
+ }
|
|
|
331
|
+
|
|
|
332
|
+ $this->success('下单页详情',$ordermodel->id);
|
|
|
333
|
+ }
|
131
|
} |
334
|
} |