Porders.php
16.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
<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\admin\model\Porder;
use think\Db;
use think\Validate;
/**
* 商品订单接口**
*/
class Porders extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
protected $user_id = '';//token存贮user_id
protected $normal = '';//商品正常状态,1:下架
protected $cancel = '';//商品取消订单
protected $order_status = [];//订单状态
protected $is_read = '';//订单消息已读
public function _initialize()
{
parent::_initialize();
$this->normal = config('site.goods_status');
$this->cancel = config('site.cancel_status');//-1
$this->order_status = config('site.order_status');
$this->is_read = config('site.is_read');
$this->user_id = $this->auth->getUserId();
}
/**
* @ApiTitle (从购物车选中结算)
* @ApiSummary (从购物车选中结算)
* @ApiMethod (GET)
* @ApiRoute (/api/porders/settle)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="p_id", type="string", required=true, description="商品id(以逗号隔开组成字符串)")
* @ApiParams (name="p_num", type="string", required=true, description="商品数量(以逗号隔开组成字符串)")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1553836874",
"data": {
"user": {
"username": "风起时呀",//收货用户名
"mobile": ""//联系方式
收货地址:微信地址(由小程序自己调取,这里不提供)
},
"orders": [
{
"id": 1,
"title": "我的垃圾商品1",//订单标题
"price": 500,//订单价格
"images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg",
"username": "admin",//发货人
"mobile": "13888888888",//发货人联系方式
"address": "河南省邓州市",//发货地址
"num": "5",//订单数量
"order_sn": "gc_155358129765296600"//订单编号
},
{
"id": 2,
"title": "废弃家电",
"price": 1200.08,
"images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg",
"username": "test1",
"mobile": "13752011725",
"address": "河南省邓州市2",
"num": "8",
"order_sn": "gc_155383687460739100"
}
],
"total_price": "12209.09"//总价格
}
})
*/
public function settle(){
if($this->request->isGet()){
$p_id = $this->request->get('p_id');//商品id,以逗号隔开字符(“1,2,3”)
$p_num = $this->request->get('p_num');//商品数量,以逗号隔开字符串(“2,3,3”)
$rule = config('site.p_settle');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['p_id'=>$p_id,'p_num'=>$p_num])) {
$this->error($validate->getError());
}
$p_ids = explode(',',$p_id);
$p_nums = explode(',',$p_num);
$data = Db::table('gc_product')
->alias('p')
->join('gc_user u','p.uid = u.id','LEFT')
->where(['p.status'=>$this->normal,'u.status'=>'normal'])
->whereIn('p.id',$p_ids)
->field('p.id,p.title,p.price,p.images,u.username,u.mobile,u.address')
->select();
$total = 0;
foreach($data as $key=>$value){
$data[$key]['num'] = $p_nums[$key];
$data[$key]['order_sn'] = $this->auth->genOrderSn('gc_');//订单编号
$total += $value['price']*$p_nums[$key];
}
$user = Db::table('gc_user')
->where(['id'=>$this->user_id,'status'=>'normal'])
->field('username,mobile')
->find();
$total = sprintf("%.2f",$total);//总价格
$this->success('成功',['user'=>$user,'orders'=>$data,'total_price'=>$total]);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (提交订单)
* @ApiSummary (提交订单)
* @ApiMethod (POST)
* @ApiRoute (/api/porders/purchase)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="p_id", type="string", required=true, description="商品id(以逗号隔开组成字符串)")
* @ApiParams (name="p_num", type="string", required=true, description="商品数量(以逗号隔开组成字符串)")
* @ApiParams (name="order_sn", type="string", required=true, description="订单号(以逗号隔开组成字符串)")
* @ApiParams (name="address", type="string", required=true, description="收货地址(微信地址)")
* @ApiReturn({
"code": 1,
"msg": "提交成功",
"time": "1553837090",
"data": [
'pay_order_sn'=> "155359076329654300172"//支付订单号
]
})
*/
public function purchase(){
if($this->request->isPost()){
$p_id = $this->request->post('p_id');//商品id,以逗号隔开字符(“1,2,3”)
$p_num = $this->request->post('p_num');//商品数量,以逗号隔开字符串(“2,3,3”)
$order_sn = $this->request->post('order_sn');//订单号,以逗号隔开(“‘我是谁’,’我是谁‘,”)
$address = $this->request->post('address');//收货地址(微信地址)
$rule = config('site.p_orders');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['p_id'=>$p_id,'p_num'=>$p_num,'order_sn'=>$order_sn,'address'=>$address])) {
$this->error($validate->getError());
}
$p_ids = explode(',',$p_id);
$p_nums = explode(',',$p_num);
$p_order_sns = explode(',',$order_sn);
$data = Db::table('gc_product')
->where(['status'=>$this->normal])
->whereIn('id',$p_ids)
->field('uid,price,stock')
->select();
$orderData = [];
$order = [];
$pay = $this->auth->genPayOrderSn();//支付订单号
foreach($data as $key=>$value){
$order['order_sn'] = $p_order_sns[$key];//订单编号
$order['pay_order_sn'] = $pay;//支付订单号
$order['uid'] = $this->user_id;//用户uid
$order['s_uid'] = $value['uid'];//所属uid
$order['p_id'] = $p_ids[$key];//商品id
$order['address'] = $address;//收货地址(微信地址)
$order['unit_price'] = $value['price'];//商品单价
$order['num'] = $p_nums[$key];//商品数量
$order['total_price'] = $value['price']*$p_nums[$key];//商品总价
//可用库存
$avaiNum = $value['stock'] - $p_nums[$key];
if($avaiNum < 0){
$this->error('抱歉,您的订单存在库存不足哦');
}
array_push($orderData,$order);
}
$p_order = new Porder();
$res = $p_order->saveAll($orderData);
if($res){
$this->success('提交成功',['pay_order_sn'=>$pay]);
}else{
$this->error('提交失败');
}
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (商品订单列表)
* @ApiSummary (商品订单列表)
* @ApiMethod (GET)
* @ApiRoute (/api/porders/goodsOrderList)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="status", type="integer", required=true, description="商品状态 -1:全部 0:待付款 1:待收货 2:待评价 3:已完成")
* @ApiParams (name="page", type="integer", required=true, description="页码分页")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1553837175",
"data": [
{
"id": 1,
"order_sn": "gc_155358608729209200",//订单号
"status": 0,//状态码 0:待付款 1:待收货 2:待评价 3:已完成
"title": "我的垃圾商品1",//商品标题
"unit_price": 500,//商品单价
"num": 5,//商品数量
"p_id":2,//商品id
"images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg"
},
{
"id": 2,
"order_sn": "gc_155358608729211100",
"status": 0,
"title": "废弃家电",
"unit_price": 1200.08,
"num": 9,
"images": "/uploads/20190319/8e17442bd500a4842d456a95ec022826.jpg,/uploads/20190319/4d82786ab0f7866110519f221cbf29a6.jpg",
"p_id": 2
},
{
"id": 3,
"order_sn": "gc_155358608729212300",
"status": 0,
"title": "废品商品2",
"unit_price": 12.05,
"num": 2,
"images": "/uploads/20190328/c7eab178376c71ae1892cd23a1fb5779.jpg",
"p_id": 3
}
]
})
*/
public function goodsOrderList(){
if($this->request->isGet()){
$status = $this->request->get('status');//状态 -1:全部 0:待付款 1:待收货 2:待评价 3:已完成
$page = $this->request->get('page');//分页页码
$limit = config('site.page_limit');//分页限制数量
$rule = config('site.order_list');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['status'=>$status,'page'=>$page])) {
$this->error($validate->getError());
}
$where['o.uid'] = $this->user_id;
$where['p.status'] = $this->normal;
if($status != -1){
$where['o.status'] = $status;
}
$data = Db::table('gc_porder')
->alias('o')
->join('gc_product p','o.p_id = p.id','LEFT')
->where($where)
->page($page,$limit)
->field('o.id,o.order_sn,o.status,p.title,o.unit_price,o.num,p.images,o.p_id')
->select();
$this->success('成功',$data);
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (取消商品订单)
* @ApiSummary (取消商品订单)
* @ApiMethod (GET)
* @ApiRoute (/api/porders/cancelGoodsOrder)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="商品订单id")
* @ApiReturn ({
"code": 1,
"msg": "取消成功",
"time": "1553837090",
"data": null
})
*/
public function cancelGoodsOrder(){
if($this->request->isGet()){
$order_id = $this->request->get('id');//商品订单id
$rule = config('site.goods');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['id'=>$order_id])) {
$this->error($validate->getError());
}
$p_order = new Porder();
$res = $p_order->where(['id'=>$order_id,'uid'=>$this->user_id,'status'=>$this->order_status[0]])
->update(['status'=>$this->cancel]);
if($res){
$this->success('取消成功');
}else{
$this->error('取消失败');
}
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (收货商品订单)
* @ApiSummary (收货商品订单)
* @ApiMethod (GET)
* @ApiRoute (/api/porders/receiveGoodsOrder)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="商品订单id")
* @ApiReturn ({
"code": 1,
"msg": "收货成功",
"time": "1553837090",
"data": null
})
*/
public function receiveGoodsOrder(){
if($this->request->isGet()){
$order_id = $this->request->get('id');//商品订单id
$rule = config('site.goods');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['id'=>$order_id])) {
$this->error($validate->getError());
}
$p_order = new Porder();
$res = $p_order->where(['id'=>$order_id,'uid'=>$this->user_id,'status'=>$this->order_status[1]])
->update(['status'=>$this->order_status[2]]);
if($res){
$this->success('收货成功');
}else{
$this->error('收货失败');
}
}else{
$this->error('请求方式错误');
}
}
/**
* @ApiTitle (商品订单详情)
* @ApiSummary (商品订单详情)
* @ApiMethod (GET)
* @ApiRoute (/api/porders/goodsOrderDetail)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="integer", required=true, description="商品订单id")
* @ApiReturn ({
"code": 1,
"msg": "成功",
"time": "1553837375",
"data": [
{
"id": 1,
"status": 3,//订单状态 0:待付款 1:待收货 2:待评价 3:已完成
"order_sn": "gc_155358608729209200",//订单号
"username": "风起时呀",//收货人
"mobile": "",//联系方式
"address": "微信地址",//地址
"title": "我的垃圾商品1",//商品名称
"num": 5,//商品数量
"total_price": 2500,//总金额
"createtime": "2019-03-26"//下单时间
}
]
})
*/
public function goodsOrderDetail(){
if($this->request->isGet()){
$order_id = $this->request->get('id');//商品订单id
$rule = config('site.goods');
$validate = new Validate($rule['rule'],$rule['msg']);
if (!$validate->check(['id'=>$order_id])) {
$this->error($validate->getError());
}
$data = Db::table('gc_porder')
->alias('o')
->join('gc_product p','o.p_id = p.id','LEFT')
->join('gc_user u','o.uid = u.id','LEFT')
->where(['o.id'=>$order_id,'o.uid'=>$this->user_id,'p.status'=>$this->normal,'u.status'=>'normal'])
->field('o.id,o.status,o.order_sn,u.username,u.mobile,o.address,p.title,o.num,o.total_price,o.createtime')
->select();
foreach($data as &$value){
$value['createtime'] = date('Y-m-d',$value['createtime']);
}
$p_order = new Porder();
$p_order->where(['id'=>$order_id,'s_uid'=>$this->user_id])->update(['is_read'=>$this->is_read]);
$this->success('成功',$data);
}else{
$this->error('请求方式错误');
}
}
}