Order.php
15.1 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
namespace app\api\controller;
use app\api\model\GoodsSpec;
use app\api\model\SpecValue;
use app\api\model\UserCoupon;
use app\common\controller\Api;
use think\Db;
use think\exception\PDOException;
/**
* 订单
*/
class Order extends Api
{
protected $noNeedLogin = ['*'];
// protected $noNeedRight = ['*'];
/**
* @ApiTitle (订单列表)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="type", type="integer", required=true, description="类型1全部2待付款3待发货4待收货5待评价")
* @ApiParams (name="page", type="integer", required=true, description="页数")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
'data':
"total": 1, 总条数
"per_page": 5, 每页数量
"current_page": 1, 当前页
"last_page": 1, 最后一页
"data": [
{
"id": 1, 订单id
"order_no": "adsadasdas", 订单编号
"discount_price": "0.00", 优惠金额
"pay_price": "0.00", 实付价格
"pay_status": "10", 10未支付20已支付
"freight_status": "10", 10未发货20已发货
"receipt_status": "10", 10未收货20已收货
"order_status": "10", 10进行中20已取消30已完成
"total_sum": 0, 商品总数量
"goods": [
{
"goods_name": "asdasd", 商品名
"goods_attr": "1", 规格名
"total_num": 0, 数量
"total_price": "0.00", 价格
"image_text": "" 图片
},
{
"goods_name": "asdasd",
"goods_attr": "1",
"total_num": 0,
"total_price": "0.00",
"image_text": ""
}
],
"createtime_text": "" 时间
}
]
})
*/
public function orderList()
{
$type = $this->request->post('type');
$page = $this->request->post('page',1);
if (!in_array($type,[1,2,3,4,5])) $this->error('type参数不合法');
if (!is_numeric($page)) $this->error('页数不合法');
switch ($type){
case 1:
$where = [
'fa_litestore_order.status' => 'normal',
'fa_litestore_order.user_id' => $this->auth->id,
];
break;
case 2:
$where = [
'fa_litestore_order.status' => 'normal',
'fa_litestore_order.user_id' => $this->auth->id,
'fa_litestore_order.pay_status' => '10',
];
break;
case 3:
$where = [
'fa_litestore_order.status' => 'normal',
'fa_litestore_order.user_id' => $this->auth->id,
'fa_litestore_order.freight_status' => '10',
];
break;
case 4:
$where = [
'fa_litestore_order.status' => 'normal',
'fa_litestore_order.user_id' => $this->auth->id,
'fa_litestore_order.receipt_status' => '10',
];
break;
default :
$where = [
'fa_litestore_order.status' => 'normal',
'fa_litestore_order.user_id' => $this->auth->id,
'fa_litestore_order.receipt_status' => '20',
];
break;
}
$model = new \app\api\model\Order();
$list = $model
->with(['goods'])
->where($where)
->order('id','desc')
->paginate(5,false,['page'=>$page])
->each(function ($item,$key){
$sum = 0;
foreach ($item->getRelation('goods')as $key => $value){
$sum += $value['total_num'];
$value->visible(['goods_name','goods_attr','total_num','total_price']);
}
$item['total_sum'] = $sum;
$item->visible([
'goods','total_sum','order_no','id','pay_price',
'discount_price','pay_status','order_status','receipt_status',
'freight_status'
]);
});
$this->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':
"list": [
{
"goods_id": 22, 商品id
"goods_name": "Mate 20 华为 HUAWEI ", 商品名称
"spec_type": "20", 20多规格10单规格
"sku_id": 106, 规格id
"sku_price": "6099.00", 规格单价
"sku_name": "极光色 8GB+128GB", 规格名称
"number": 2, 数量
"image_text": "", 图片
},
{
"goods_id": 23,
"goods_name": "MacBook Pro 13寸",
"spec_type": "20",
"sku_id": 66,
"sku_price": "12688.00",
"sku_name": "天空灰",
"number": 2,
"image_text": "",
}
],
"price": "37574.00" 总价
})
*/
public function orderCalculation()
{
$json = $this->request->post('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}]';
$data = json_decode($json,true);
$goodsmodel = new \app\api\model\Goods();
$skumodel = new GoodsSpec();
$specmodel = new SpecValue();
$goods_array = []; //商品列表
$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('商品不存在');
$goods->visibleAppend(['image_text']);
$goods->hidden(['image']);
$sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])
->field('goods_spec_id,spec_sku_id,goods_price')->find();
if (!$sku) $this->error('商品规格不存在');
$sku->unsetAppend();
$goods['sku_id'] = $sku['goods_spec_id'];
$goods['sku_price'] = $sku['goods_price'];
if ($goods['spec_type'] == 10){
$goods['sku_name'] =''; //规格名
}else{
$ids = explode('_',$sku['spec_sku_id']);
$sku_name = $specmodel->whereIn('id',$ids)->column('spec_value');
$goods['sku_name'] = implode(' ',$sku_name); //规格名
}
$goods['number'] = $value['number'];
$goods_array[] = $goods;
$sum_price = bcadd($sum_price,bcmul($sku['goods_price'],$value['number'],2),2);
}
$this->success('下单页详情',['list'=>$goods_array,'price'=>$sum_price]);
}
/**
* @ApiTitle (计算运费)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="address_id", type="integer", required=true, description="地址id")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
'data':
"price": "37574.00" 总价
})
*/
public function freightCalculation()
{
$address_id = $this->request->post('address_id');
if (!$address_id) $this->error('请选择地址');
$this->success('下单页详情',['price'=>$sum_price]);
}
/**
* @ApiTitle (选择优惠券)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="price", type="float", required=true, description="订单价格")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
"data": [
{
"id": 1,
"user_id": 1,
"coupon_id": 1,
"name": "手动阀手动阀",
"price": "1.00", 优惠券金额
"full_price": "10.00", 满减金额
"createtime": 111122244,
"endtime": 1641869388,
"status": "0",
"endtime_text": "2022年01月11日到期"
}
]
})
*/
public function chooseCoupon()
{
$price = $this->request->post('price');
if (!is_numeric($price)) $this->error('订单价格不合法');
$model = new UserCoupon();
$list = $model
->where('status','0')
->where('user_id',$this->auth->id)
->where('full_price','<',$price)
->select();
$this->success('用户优惠券列表',$list);
}
/**
* @ApiTitle (下单)
* @ApiMethod (POST)
* @ApiSummary ([{goods_id:22 goods_sku_id:106 number:2} {goods_id:23 goods_sku_id:66 number:2}])
* @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数组注释用")
* @ApiParams (name="coupon_id", type="integer", required=true, description="优惠券id 无优惠券传0")
* @ApiParams (name="address_id", type="integer", required=true, description="地址id")
* @ApiReturn ({
'code':'1',
'msg':'返回成功'
'data':
})
*/
public function addOrder()
{
$json = $this->request->post('data_json');
$coupon_id = $this->request->post('coupon_id');
$address_id = $this->request->post('address_id');
// 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}]';
$address = Db::name('user_address')
->where('id',$address_id)
->where('user_id',1)
->find();
if (!$address) $this->error('地址不存在');
$coupon = [];
if ($coupon_id > 0){
$coupon = Db::name('user_coupon')
->where('id',$coupon_id)
->where('user_id',$this->auth->id)
->find();
if (!$coupon) $this->error('优惠券不存在');
}
$data = json_decode($json,true);
$goodsmodel = new \app\api\model\Goods();
$skumodel = new GoodsSpec();
$ordermodel = new \app\api\model\Order();
$ordergoodsmodel = new \app\api\model\OrderGoods();
$orderaddressmodel = new \app\api\model\OrderAddress();
$specmodel = new SpecValue();
$goods_array = []; //商品列表
$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'])->find();
if (!$goods) $this->error('商品不存在');
$sku = $skumodel->where('goods_spec_id',$value['goods_sku_id'])->find();
if (!$sku) $this->error('商品规格不存在');
if ($goods['spec_type'] == 10){
$sku_name =''; //规格名
}else{
$ids = explode('_',$sku['spec_sku_id']);
$sku_name = $specmodel->whereIn('id',$ids)->column('spec_value');
$sku_name = implode(' ',$sku_name); //规格名
}
$goods_array[] = [
'goods_id' => $goods['goods_id'],
'goods_name' => $goods['goods_name'],
'image' => $goods['image'],
'deduct_stock_type' => $goods['deduct_stock_type'],
'spec_type' => $goods['spec_type'],
'spec_sku_id' => $sku['spec_sku_id'],
'goods_spec_id' => $sku['goods_spec_id'],
'goods_attr' => $sku_name,
'content' => $goods['content'],
'goods_no' => $goods['number'],
'goods_price' => $sku['goods_price'],
'goods_weight' => $sku['goods_weight'],
'total_num' => $value['number'],
'total_price' => bcmul($sku['goods_price'],$value['number'],2),
'user_id' => 1,
];
$sum_price = bcadd($sum_price,bcmul($sku['goods_price'],$value['number'],2),2);
}
if ($coupon !== [] && $coupon['full_price'] < $sum_price) $this->error('优惠券不可使用');
$order_address = [
'name' => $address['username'],
'phone' => $address['mobile'],
'address' => $address['address'],
'detail' => $address['address_detail'],
'lng' => $address['lng'],
'lat' => $address['lat'],
'user_id' => 1,
];
$order_no = 'LQ-'.time().mt_rand(1000,9999);
$couponprice = isset($coupon['price'])?$coupon['price']:0;
$order = [
'order_no' => $order_no,
'total_price' => $sum_price,
'discount_price' => $couponprice,
'pay_price' => bcadd($sum_price,$couponprice,2),
'user_id' => 1
];
try {
Db::startTrans();
$ordermodel->save($order);
foreach ($goods_array as $key => &$value){
$value['order_id'] = $ordermodel->id;
}
$ordergoodsmodel->saveAll($goods_array);
$order_address['order_id'] = $ordermodel->id;
$orderaddressmodel->save($order_address);
Db::commit();
}catch (PDOException $e){
Db::rollback();
$this->error($e->getMessage());
}
$this->success('下单页详情',$ordermodel->id);
}
}