Order.php
23.5 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/1/13
* Time: 11:35
*/
namespace app\api\controller;
use app\common\controller\Api;
use RongCloud\RongCloud;
use think\Db;
use think\Log;
use think\Validate;
use wxapp\pay\WeixinPay;
use wxapp\pay\WeixinRefund;
/**
* 创建订单/支付接口
*/
class Order extends Api
{
/**
* @ApiTitle (创建订单)
* @ApiSummary (创建订单)
* @ApiMethod (POST)
* @ApiRoute (/api/order/create_order)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="teacher_id", type="int", required=true, description="老师ID")
* @ApiParams (name="territory_id", type="int", required=true, description="咨询领域ID")
* @ApiParams (name="is_use", type="int", required=true, description="是否适用优惠劵(0不使用,1使用)")
* @ApiParams (name="coupon_id", type="int", required=false, description="优惠劵ID")
* @ApiParams (name="money", type="string", required=false, description="金额")
* @ApiParams (name="business", type="string", required=false, description="所属行业")
* @ApiParams (name="address", type="string", required=false, description="经营地区/职能类别")
* @ApiParams (name="product", type="string", required=false, description="产品/职位等级")
* @ApiParams (name="scale", type="string", required=false, description="公司规模/下属人数")
* @ApiParams (name="content", type="string", required=false, description="问题描述")
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1553839125",
"data": {
"order_id":"order_id",//订单id
}
})
*/
public function create_order()
{
$param = $this->request->param();
$param['user_id'] = $this->getUserId();
$validate = new Validate([
'teacher_id' => 'require',
'territory_id' => 'require',
'is_use' => 'require',
'money'=>'require',
'business'=>'require',
'address'=>'require',
'product'=>'require',
'scale'=>'require',
'content'=>'require',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
if($param['is_use'] == 1){
if(empty($param['coupon_id'])){
$this->error('优惠劵ID错误');
}
$yong = Db::name('commission')
->where('id',1)
->find();
$youhui = Db::name('coupon')->where('id',$param['coupon_id'])->find();
if($youhui['is_use'] != 0){
$this->error('优惠劵已过期或者已使用');
}
$param['commission'] = $yong['proportion'];
$param['createtime'] = time();
$param['num'] = get_order_sn();
$data = Db::name('order')->insertGetId($param);
if(empty($data)){
$this->error('失败');
}else{
//修改优惠劵为已使用
Db::name('coupon')->where('id',$param['coupon_id'])->update(['is_use'=>1]);
$this->success('success',['order_id'=>$data]);
}
}else{
$yong = Db::name('commission')
->where('id',1)
->find();
$param['commission'] = $yong['proportion'];
$param['createtime'] = time();
$param['num'] = get_order_sn();
$data = Db::name('order')->insertGetId($param);
$this->success('success',['order_id'=>$data]);
}
}
/**
* @ApiTitle (续约订单)
* @ApiSummary (续约订单)
* @ApiMethod (POST)
* @ApiRoute (/api/order/sonorder)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="order_id", type="int", required=true, description="订单ID")
* @ApiParams (name="is_use", type="int", required=true, description="是否适用优惠劵(0不使用,1使用)")
* @ApiParams (name="coupon_id", type="int", required=false, description="优惠劵ID")
* @ApiParams (name="money", type="string", required=false, description="金额")
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1553839125",
"data": {
"order_id":"order_id",//子订单id
}
})
*/
public function sonorder()
{
$user_id = $this->getUserId();
$param = $this->request->param();
$validate = new Validate([
'order_id' => 'require',
'is_use' => 'require',
'money'=>'require',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
$yong = Db::name('commission')
->where('id',1)
->find();
$param['commission'] = $yong['proportion'];
if($param['is_use'] == 1){
if(empty($param['coupon_id'])){
$this->error('优惠劵ID错误');
}
$youhui = Db::name('coupon')->where('id',$param['coupon_id'])->find();
if($youhui['is_use'] != 0){
$this->error('优惠劵已过期或者已使用');
}
$param['createtime'] = time();
$param['num'] = get_order_sn();
$data = Db::name('sonorder')->insertGetId($param);
if(empty($data)){
$this->error('失败');
}else{
//修改优惠劵为已使用
Db::name('coupon')->where('id',$param['coupon_id'])->update(['is_use'=>1]);
$this->success('success',['order_id'=>$data]);
}
}else{
$param['createtime'] = time();
$param['num'] = get_order_sn();
$data = Db::name('sonorder')->insertGetId($param);
$this->success('success',['order_id'=>$data]);
}
}
/**
* @ApiTitle (续约订单支付)
* @ApiSummary (续约订单支付)
* @ApiMethod (POST)
* @ApiRoute (/api/order/sonpay)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="order_id", type="inter", required=true, description="续约订单id")
*
*/
public function sonpay()
{
$user_id = $this->getUserId();
$order_id = $this->request->param('order_id');
if(empty($order_id)){
$this->error('缺少必要参数');
}
$order = Db::name('sonorder')->where('id',$order_id)->find();
if(empty($order)){
$this->error('查询为空');
}
if($order['status'] != 1){
$this->error('订单状态不合法');
}
//微信支付
$openid = $this->getOpenId();
$pay = new WeixinPay();
$this->success('SUCCESS',$pay->pay($openid,$order['num'],"小微问问",$order['money'],url('api/pay/sonnotify','','',true)));
}
/**
* @ApiTitle (支付)
* @ApiSummary (支付)
* @ApiMethod (POST)
* @ApiRoute (/api/order/pay)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="order_id", type="inter", required=true, description="订单id")
*
*/
public function pay()
{
$user_id = $this->getUserId();
$order_id = $this->request->param('order_id');
if(empty($order_id)){
$this->error('缺少必要参数');
}
$order = Db::name('order')->where(['id'=>$order_id,'user_id'=>$user_id])->find();
if(empty($order)){
$this->error('查询为空');
}
if($order['status'] != 1){
$this->error('订单状态不合法');
}
//微信支付
$openid = $this->getOpenId();
$pay = new WeixinPay();
$this->success('SUCCESS',$pay->pay($openid,$order['num'],"小微问问",$order['money'],url('api/pay/notify','','',true)));
}
/**
* @ApiTitle (学生身份我的订单列表)
* @ApiSummary (学生身份订单列表)
* @ApiMethod (POST)
* @ApiRoute (/api/order/orderList)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="type", type="int", required=false, description="类型(如果为空或者为1是进行中 2是已完成3是售后)")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"id": //订单id,
"num"://订单号,
"content"://问题描述
"expirationtime"://到期时间
"teacher_id"://老师ID
"name"://老师名称
"thumbnail"://头像
"finish_status"://完成状态(1进行中2已完成3售后
"is_summarize"://是否总结过(1是2否
"is_complaint"://是否被投诉(1是2否3已处理
"is_comment"://是否评价过(1是2否
"right_status"://学生售后订单中的状态(1已处理2未处理
"chat_id"://房间id
"user"用户:{
"id"://融云id
"token"://融云token
},
"teacher"老师:{
"id"://融云id
"token"://融云token
}
}
})
*/
public function orderList()
{
$user_id = $this->getUserId();
$type = $this->request->param('type');
$page = $this->request->param('page', 1, 'intval');
$pageNum = $this->request->param('pageNum', 10, 'intval');
if($type == '' || $type == 1){
$data = Db::name('order')
->alias('a')
->join('teacher b','a.teacher_id = b.id')
->where('a.user_id',$user_id)
->where('a.finish_status',1)
->field('a.*,b.name,b.thumbnail')
->page($page,$pageNum)
->select();
}elseif ($type == 2){
$data = Db::name('order')
->alias('a')
->join('teacher b','a.teacher_id = b.id')
->where('a.finish_status',2)
->where('a.user_id',$user_id)
->field('a.*,b.name,b.thumbnail')
->page($page,$pageNum)
->select();
}elseif ($type == 3){
$data = Db::name('order')
->alias('a')
->join('teacher b','a.teacher_id = b.id')
->where('a.finish_status',3)
->where('a.is_complaint',['=',1],['=',3],'or')
->where('a.user_id',$user_id)
->field('a.*,b.name,b.thumbnail')
->page($page,$pageNum)
->select();
foreach ($data as &$v){
$detail = Db::name('order')->where('id',$v['id'])->find();
if($detail['order_status'] == 2){
$v['right_status'] = 2;
}else{
$v['right_status'] = 1;
}
// 融云账号信息
$v['user'] = $v['teacher'] = [
'id' => '',
'token' => ''
];
}
}else{
$this->error('类型有误');
}
foreach ($data as &$v){
$http = get_addon_config('qiniu')['cdnurl'];
$v['thumbnail'] = $http.$v['thumbnail'];
$is_have = Db::name('comment')
->where('order_id',$v['id'])
->find();
if(empty($is_have)){
$v['is_comment'] = 2;
}else{
$v['is_comment'] = 1;
}
$v['expirationtime'] = date('Y-m-d H:i',$v['expirationtime']);
if($v['status'] == 2) {
$v['user'] = [
'id' => $this->user['rongyun_id'],
'token' => $this->user['rongyun_token']
];
$teacher_id = Db::name('teacher')->where('id',$v['teacher_id'])->value('user_id');
$teacher = Db::name('user')->field('rongyun_id,rongyun_token')->where('id',$teacher_id)->find();
$v['teacher'] = [
'id' => $teacher['rongyun_id'],
'token' => $teacher['rongyun_token']
];
}
}
$this->success('success',$data);
}
/**
* @ApiTitle (老师身份我的订单列表)
* @ApiSummary (老师身份订单列表)
* @ApiMethod (POST)
* @ApiRoute (/api/order/teacherorder)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="type", type="int", required=false, description="类型(如果为空或者为1是进行中2待总结3是结算中4已完成5退款/投诉)")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"id": //订单id,
"num"://订单号,
"content"://问题描述
"expirationtime"://到期时间
"teacher_id"://老师ID
"name"://老师名称
"thumbnail"://头像
"finish_status"://完成状态(1进行中2已完成3售后
"is_summarize"://是否总结过(1是2否
"is_complaint"://是否被投诉(1是2否3已处理
"is_comment"://是否评价过(1是2否
"is_chargeback"://是否退单(1已退款2未申请退款
"chat_id"://房间id
"user"用户:{
"id"://融云id
"token"://融云token
},
"teacher"老师:{
"id"://融云id
"token"://融云token
}
}
})
*/
public function teacherorder()
{
$user_id = $this->getUserId();
$teacher_id = Db::name('teacher')->where('user_id',$user_id)->find();
$type = $this->request->param('type');
$page = $this->request->param('page', 1, 'intval');
$pageNum = $this->request->param('pageNum', 10, 'intval');
if($type == '' || $type == 1){
//进行中
$data = Db::name('order')
->alias('a')
->join('teacher b','a.teacher_id = b.id')
->where('a.teacher_id',$teacher_id['id'])
->where('a.finish_status',1)
->field('a.*,b.name,b.thumbnail')
->page($page,$pageNum)
->select();
}elseif ($type == 2){
//待总结
$data = Db::name('order')
->alias('a')
->join('teacher b','a.teacher_id = b.id')
->where('a.finish_status',2)
->where('a.is_summarize',2)
->where('a.teacher_id',$teacher_id['id'])
->field('a.*,b.name,b.thumbnail')
->page($page,$pageNum)
->select();
}elseif ($type == 3){
//结算中
$data = Db::name('order')
->alias('a')
->join('teacher b','a.teacher_id = b.id')
->where('a.finish_status',2)
->where('a.is_summarize',1)
->where('a.teacher_id',$teacher_id['id'])
->where('a.order_status',2)
->field('a.*,b.name,b.thumbnail')
->page($page,$pageNum)
->select();
}elseif ($type == 4){
//已完成
$data = Db::name('order')
->alias('a')
->join('teacher b','a.teacher_id = b.id')
->where('a.finish_status',2)
->where('a.is_summarize',1)
->where('a.order_status',1)
->where('a.teacher_id',$teacher_id['id'])
->field('a.*,b.name,b.thumbnail')
->page($page,$pageNum)
->select();
}elseif ($type == 5 ){
//退款/投诉
$data = Db::name('order')
->alias('a')
->join('teacher b','a.teacher_id = b.id')
->where('a.finish_status',3)
->where('a.is_complaint',['=',1],['=',3],'or')
->where('a.teacher_id',$teacher_id['id'])
->field('a.*,b.name,b.thumbnail')
->page($page,$pageNum)
->select();
}else{
$this->error('类型有误');
}
foreach ($data as &$v){
$http = get_addon_config('qiniu')['cdnurl'];
$v['thumbnail'] = $http.$v['thumbnail'];
$is_have = Db::name('comment')
->where('order_id',$v['id'])
->find();
if(empty($is_have)){
$v['is_comment'] = 2;
}else{
$v['is_comment'] = 1;
}
$v['expirationtime'] = date('Y-m-d H:i',$v['expirationtime']);
if($v['status'] == 2) {
$v['teacher'] = [
'id' => $this->user['rongyun_id'],
'token' => $this->user['rongyun_token']
];
$user = Db::name('user')->field('rongyun_id,rongyun_token')->where('id',$v['user_id'])->find();
$v['user'] = [
'id' => $user['rongyun_id'],
'token' => $user['rongyun_token']
];
}
}
$this->success('success',$data);
}
/**
* @ApiTitle (提现)
* @ApiSummary (提现)
* @ApiMethod (POST)
* @ApiRoute (/api/order/withdraw)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="teacher_id", type="int", required=true, description="老师ID")
* @ApiParams (name="money", type="int", required=true, description="提现金额")
* @ApiParams (name="card", type="string", required=true, description="卡号")
* @ApiParams (name="bank", type="string", required=true, description="开户行")
* @ApiParams (name="name", type="string", required=true, description="真实姓名")
* @ApiParams (name="phone", type="string", required=true, description="手机号")
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1553839125",
"data": {
"id"://id
}
})
*/
public function withdraw()
{
$param = $this->request->param();
$param['user_id'] = $this->getUserId();
$validate = new Validate([
'teacher_id' => 'require',
'money'=>'require',
'card' => 'require',
'bank' => 'require',
'name' => 'require',
'phone'=>'require',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
//查询提现金额是否满足后台设置允许提现额度
$money = Db::name('allow')
->where('id',1)
->find();
//查询老师余额是否跟提现的金额匹配
$teacher = Db::name('teacher')
->where('id',$param['teacher_id'])
->field('id,balance')
->find();
if($param['money'] < $money['money'] || $param['money'] > $teacher['balance']){
$this->error('提现金额有误,请重新选择');
}
//加入佣金明细
$record['createtime'] = time();
$record['type'] = 2;
$record['user_id'] = $param['user_id'];
$record['teacher_id'] = $param['teacher_id'];
$record['money'] = $param['money'];
Db::name('record')->insertGetId($record);
$param['createtime'] = time();
Db::startTrans();
$data = Db::name('withdraw')
->insertGetId($param);
//提现成功将对应的老师余额修改
$res = Db::name('teacher')->where('id',$param['teacher_id'])->update(['balance'=>$teacher['balance']-$param['money']]);
if($data && $res){
Db::commit();
$this->success('成功');
}else{
Db::rollback();
$this->error('失败');
}
}
/**
* @ApiTitle (收益记录列表)
* @ApiSummary (收益记录列表)
* @ApiMethod (POST)
* @ApiRoute (/api/order/record)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"id": //id,
"teacher_id"://老师ID,
"user_id"://用户ID
"type"://类型(1订单结算2提现审核中3提现已结算
"money"://金额
"createtime"://创建时间
}
})
*/
public function record()
{
$user_id = $this->getUserId();
$teacher = Db::name('teacher')
->where('user_id',$user_id)
->find();
$data = Db::name('record')
->where('teacher_id',$teacher['id'])
->order('createtime desc')
->select();
foreach ($data as &$v){
$v['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
}
$this->success('success',$data);
}
/**
* @ApiTitle (老师退单)
* @ApiSummary (老师退单)
* @ApiMethod (POST)
* @ApiRoute (/api/order/chargeback)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="order_id", type="int", required=true, description="订单金额ID")
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1553839125",
"data": {
}
})
*/
public function chargeback()
{
$user_id = $this->getUserId();
$param = $this->request->param();
$validate = new Validate([
'order_id' => 'require',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
$order = Db::name('order')
->where('id',$param['order_id'])
->find();
if(empty($order)){
$this->error('查询为空');
}
if($order['status'] != 2 || $order['finish_status'] != 1){
$this->error('非法的订单状态');
}
if($order['expirationtime'] < time()) {
$this->error('该订单已到期');
}
$pay = new WeixinRefund();
$openid = Db::name('third')->where('user_id',$order['user_id'])->find();
$result = $pay->refund($openid['openid'],$order['num'],$order['money'],get_order_sn(),$order['money']);
if($result['return_code'] == 'FAIL'){
$this->error($result['return_msg']);
}else{
if($result['result_code'] == 'FAIL'){
$this->error($result['err_code_des']);
}else{
Db::name('order')->where('id',$param['order_id'])->update(['status'=>3,'finish_status'=>3,'is_chargeback'=>1]);
// 群组禁言
$return = rongyun_ban($order);
Log::write(date('Y-m-d H:i') . '添加指定群组全部禁言成功,返回结果:' . $order['chat_id'] . json_encode($return, JSON_UNESCAPED_UNICODE), 'rongyun_log');
$this->success('success');
}
}
}
}