UsersController.php
19.6 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
<?php
/**
* Created by PhpStorm.
* User: ruidiudiu
* Date: 2018/11/23
* Time: 18:26
*/
namespace app\portal\controller;
use cmf\controller\HomeBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
use wxapp\pay\WeixinPay;
/**
* @title 用户相关接口
* @description 用户相关接口
* @group 用户相关接口
*/
class UsersController extends HomeBaseController{
protected $options;
function _initialize()
{
parent::_initialize();
$this->options = [
'app_id' => config('wechat_config.app_id'),
'secret' => config('wechat_config.secret'),
'payment' => config('wechat_config.payment'),
];
}
/**
* @title 修改订单状态
* @description
* @author sgj
* @url /portal/users/changeorder
* @method GET
* @group 用户相关接口2
* @param name:id type:String require:1 default:无 other: desc:订单id
*
*/
public function changeorder(){
$map['id']=input('id');
$data['state']='3';
$result=\db('order')->where($map)->update($data);
if ($result==1){
$this->success('修改成功');
}else{
$this->error('修改失败');
}
}
/**
* @title 状态验证
* @description 开锁前判断是否有未支付订单与是否提交押金
* @author 董瑞恩
* @url /portal/users/lock_check
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
*
* @return is_use:是否在使用设备
* @return is_deposit:是否交付押金
* @return is_order:是否有未支付订单
*/
public function lock_check(){
$users_id=$this->request->param('users_id');
//获取提交押金的状态
$users=Db::name('users')->where('id',$users_id)->find();
$data=[
'is_use'=>$users['is_use'],
'is_deposit' => $users['is_deposit']
];
$order=Db::name('order')->where(['users_id'=>$users_id,'state'=>2,'type'=>'1'])->find();
if (empty($order)){
$data['is_order']=0;
}else{
$data['is_order']=1;
}
//所有用户都是交押金用户
$data['is_deposit']=1;
$this->apiResponse(200,'success',$data);
}
// /**
// * @title 用户使用状态验证
// * @description 判断用户当前是否在使用设备
// * @author 董瑞恩
// * @url /portal/users/isUse
// * @method GET
// *
// * @param name:users_id type:String require:1 default:无 other: desc:用户id
// *
// *
// */
public function isUse(){
$users_id=$this->request->param('users_id');
$users=Db::name('users')->where('id',$users_id)->find();
if ($users['is_use']==1){
$this->apiResponse(200,'用户正在使用设备');
}else{
$this->apiResponse(301,'用户尚未使用设备');
}
}
/**
* @title 获取用户信息
* @description 获取用户信息
* @author 董瑞恩
* @url /portal/users/getUsersInfo
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
*
* @return users_id:用户id
* @return phone:用户手机号
* @return is_deposit:用户是否提交押金
* @return deposit:用户提交押金金额(单位:元)
*/
public function getUsersInfo(){
$users_id=$this->request->param('users_id');
$data=Db::name('users')->field('id as users_id,phone,is_deposit,deposit')->where('id',$users_id)->find();
$this->apiResponse(200,'success',$data);
}
/**
* @title 获取计费规则
* @description 获取计费规则
* @author 董瑞恩
* @url /portal/users/bill_rules
* @method GET
*
* @return bill_rules:计费规则
*/
public function bill_rules(){
$data=Db::name('set_up')->field('bill_rules')->where('id',1)->find();
$data['bill_rules']=html_entity_decode($data['bill_rules']);
$this->apiResponse(200,'success',$data);
}
/**
* @title 获取使用须知
* @description 获取使用须知
* @author 董瑞恩
* @url /portal/users/directions
* @method GET
* @return directions:使用须知
*/
public function directions(){
$data=Db::name('set_up')->field('directions')->where('id',1)->find();
$data['directions']=html_entity_decode($data['directions']);
$this->apiResponse(200,'success',$data);
}
/**
* @title 获取客服电话
* @description 获取客服电话
* @author 董瑞恩
* @url /portal/users/service_phone
* @method GET
* @return service_phone:客服电话
*/
public function service_phone(){
$data=Db::name('set_up')->field('service_phone')->where('id',1)->find();
$this->apiResponse(200,'success',$data);
}
/**
* @title 用户查询订单列表
* @description 用户查询订单列表
* @author 董瑞恩
* @url /portal/users/getOrder
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
* @param name:page type:String require:1 default:无 other: desc:页码
*
* @return currentPage:当前页
* @return allPage:总页数
* @return allData:总数据量
* @return data:当前页的数据(以下是数组字段)
*
* @return id:订单id
* @return order_no:订单号
* @return eq_name:设备名称
* @return users_id:用户id
* @return start_time:开始使用时间
* @return end_time:结束使用时间
* @return time:使用时长(小时)
* @return price:金额(元)
* @return state:订单状态(1:没完成 2:已完成 3:已支付)
*/
public function getOrder(){
$users_id=$this->request->param('users_id');
$map['state']=['neq',1];
$map['type']=1;
$order=Db::name('order')->where(['users_id'=>$users_id,])->where($map)->order('id','desc')->paginate(8);
$data_info=$order->items();
foreach ($data_info as $k=>$v){
$time_info=$this->getTimeInfo($v['start_time'],$v['end_time']);
$data_info[$k]=array_merge($data_info[$k],$time_info);
}
$data=[
'currentPage'=>$order->currentPage(),
'allPage' => $order->lastPage(),
'allData'=>$order->total(),
'data'=>$data_info
];
$this->apiResponse(200,'success',$data);
}
/**
* @title 用户充值列表
* @description 用户充值列表
* @author panhaowen
* @url /portal/users/getOrder2
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
* @param name:page type:String require:1 default:无 other: desc:页码
*
* @return currentPage:当前页
* @return allPage:总页数
* @return allData:总数据量
* @return data:当前页的数据(以下是数组字段)
*
* @return id:订单id
* @return order_no:订单号
* @return users_id:用户id
* @return start_time:开始使用时间
* @return price:金额(元)
* @return state:订单状态(1:没完成 2:已完成 3:已支付)
*/
public function getOrder2(){
$users_id=$this->request->param('users_id');
$map['state']=3;
$map['type']=2;
$order=Db::name('order')->where(['users_id'=>$users_id,])->where($map)->order('start_time','desc')->paginate(8);
$this->apiResponse(200,'success',$order);
}
/**
* @title 用户提现列表
* @description 用户提现列表
* @author panhaowen
* @url /portal/users/getOrder3
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
* @param name:page type:String require:1 default:无 other: desc:页码
*
* @return currentPage:当前页
* @return allPage:总页数
* @return allData:总数据量
* @return data:当前页的数据(以下是数组字段)
*
* @return id:订单id
* @return order_no:订单号
* @return users_id:用户id
* @return start_time:开始使用时间
* @return money:金额(元)
*/
public function getOrder3(){
$users_id=$this->request->param('users_id');
$data=Db::name('withdraw')->where([
'user_id'=>$users_id,
])->order('create_time','desc')->paginate(6);
$this->apiResponse(200,'success',$data);
}
/**
* @title 用户查询未完成订单信息
* @description 用户查询在使用订单信息
* @author 董瑞恩
* @url /portal/users/getOrderById1
* @method GET
* @param name:users_id type:String require:1 default:无 other: desc:用户id
* @return order_no:订单号
* @return users_id:用户id
* @return start_time:开始使用时间
* @return end_time:结束使用时间
* @return time:使用时长(小时)
* @return hour:计时时长(小时)
* @return min:计时时长(分钟)
* @return price:金额(元)
*/
public function getOrderById1(){
$users_id=$this->request->param('users_id');
$order= Db::name('order')
->alias('o')
->join('equipment e','o.eq_name=e.name')
->field('o.order_no,o.users_id,o.start_time,e.serial_number,e.hospital')
->where(['o.users_id'=>$users_id,'o.state'=>1,'o.type'=>1,'e.is_delete'=>0])
->find();
if (empty($order)){
$this->apiResponse(200,'没有未完成订单');
}
$order['end_time']=time();
$order['time']=ceil(($order['end_time']-$order['start_time'])/3600);
$price=new OrderController();
$order['price']=$price->onlyGetPrice($users_id,$order['start_time'],$order['end_time'],$order['hospital']);
$time_info=$this->getTimeInfo($order['start_time'],$order['end_time']);
$order=array_merge($order,$time_info);
$this->apiResponse(200,'success',$order);
}
/**
* @title 用户查询未支付订单信息
* @description 用户查询未支付订单信息
* @author 董瑞恩
* @url /portal/users/getOrderById2
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
*
* @return id:订单id
* @return order_no:订单号
* @return eq_name:设备名称
* @return users_id:用户id
* @return start_time:开始使用时间
* @return end_time:结束使用时间
* @return time:使用时长(小时)
* @return hour:计时时长(小时)
* @return min:计时时长(分钟)
* @return price:金额(元)
* @return state:订单状态(1:没完成 2:已完成 3:已支付)
*/
public function getOrderById2(){
$users_id=$this->request->param('users_id');
$order= Db::name('order')
->alias('o')
->join('equipment e','o.eq_name=e.name')
->where(['o.users_id'=>$users_id,'o.state'=>2,'o.type'=>'1','e.is_delete'=>0])
->field('o.*,e.serial_number')
->find();
if (empty($order)){
$this->apiResponse(200,'没有未支付订单');
}
$time_info=$this->getTimeInfo($order['start_time'],$order['end_time']);
$order=array_merge($order,$time_info);
$this->apiResponse(200,'success',$order);
}
/**
* @title 用户交纳押金
* @description 交纳押金
* @author 董瑞恩
* @url /portal/users/payDeposit
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
*
* @return data:下单返回值
*/
public function payDeposit(){
$users_id=$this->request->param('users_id');
$users=Db::name('users')->where('id',$users_id)->find();
$openId=$users['open_id'];
$order_no = cmf_get_order_sn();
$body="押金-支付";
$price=1;//数据库查询
$notify_url=url('users/notify','','',true);//回调地址
$wxPay=new WeixinPay($openId,$order_no,$body,$price,$notify_url);
$data=$wxPay->pay();
if (isset($data['package'])){
try{
Db::name('users')->where(['id'=>$users_id])->update(['deposit_order_no'=>$order_no]);
}catch (\Exception $exception){
}
$this->apiResponse(200,'下单成功',$data);//微信支付下单成功,返回调用支付的参数
}
}
//支付回调接口
public function notify(){
$param = $this->request->param();
if ($param == null) {
$param = file_get_contents("php://input");
if ($param == null) {
$param = $GLOBALS['HTTP_RAW_POST_DATA'];
}
}
$wxPay=new WeixinPay();
$data = $wxPay->xmlToArray($param);
$Sign = $data['sign'];
//支付成功回调后变更订单状态
$mySign = $wxPay->getSign($data);
$order_no = $data['out_trade_no'];
if ($Sign===$mySign && $data['return_code'] == 'SUCCESS') {
$data=[
'is_deposit' => 1,
'deposit' => $data['total_fee'],
];
try{
Db::name('users')->where(['deposit_order_no'=>$order_no])->update($data);
}catch (\Exception $exception){
$this->apiResponse(301,'error:'.$exception->getMessage());
}
return "<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>";
}
}
/**
* @title 用户退回押金
* @description 用户退回押金
* @author 董瑞恩
* @url /portal/users/refundDeposit
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
*
*/
public function refundDeposit(){
$users_id=$this->request->param('users_id');
$users=Db::name('users')->where('id',$users_id)->find();
if ($users['is_deposit']==0){
$this->apiResponse(302,'用户未缴纳押金');
}
$order=Db::name('order')->where(['users_id'=>$users_id,'state'=>['in',[1,2]]])->find();
if (!empty($order)){
$this->apiResponse(303,'用户还有未支付订单');
}
$orderNo=$users['deposit_order_no'];//需要退款的订单
$price=$users['deposit'];
Db::startTrans();
try{
Db::name('users')->where('id',$users_id)->update(['is_deposit'=>0,'deposit'=>null,'deposit_order_no'=>null]);
}catch (\Exception $exception){
$this->apiResponse(301,'数据库修改失败');
}
$app= new Application($this->options);
$payment = $app->payment;
//使用商户订单号退款 PS.其他形式参考文档
$refundNo =cmf_get_order_sn();//退款单号
$result = $payment->refund($orderNo, $refundNo, $price); // 总金额 100, 退款 80,refundFee可选(为空时全额退款)
if ($result['return_code']==='SUCCESS' && $result['result_code']==='SUCCESS'){
Db::commit();
}else{
Db::rollback();
$this->apiResponse(302,'error:'.$result);
}
$this->apiResponse(200,'success');
}
/**
* 退款结果回调
*/
public function refundNotify() {
$app = new Application($this->options);
$response = $app->payment->handleRefundNotify(function ($message, $reqInfo) {
cache('message',$message);
cache('reqInfo',$reqInfo);
// 其中 $message['req_info'] 获取到的是加密信息
// $reqInfo 为 message['req_info'] 解密后的信息
// 你的业务逻辑...
return true; // 返回 true 告诉微信“我已处理完成”
// 或返回错误原因 $fail('参数格式校验错误');
});
$response->send();
}
//每天重置免费试用权限
public function resetFree(){
$token=input('token');
if($token=='broSleep'){
Db::name('users')->where('is_free',1)->update(['is_free'=>0]);
}else{
echo 'ok';
}
}
public function getTimeInfo($start,$end){
if(empty($end)){
$end=time();
}
$time=$end-$start;
$return['hour']=floor($time/3600);
$return['min']=floor(($time-$return['hour']*3600)/60);
return $return;
}
/**
* @title 获取用户余额
* @description
* @author
* @url /portal/users/getUserBalance
* @method GET
*
* @param name:user_id type:String require:1 default:无 other: desc:用户id
*
*/
public function getUserBalance(){
$user_id=input('user_id');
$return['fee']=\db('users')->where('id',$user_id)->value('fee');
$this->apiResponse(200,'success',$return);
}
/**
* @title 用户提现
* @description
* @author panhaowen
* @url /portal/users/withdraw
* @method GET
* @param name:user_id type:String require:1 default:无 other: desc:用户id
* @param name:fee type:int require:1 default:无 other: desc:用户id
*/
public function withdraw(){
$user_id=input('user_id');
$fee=$this->request->param('fee');
$user=Db::name('users')->where('id',$user_id)->find();
$order= Db::name('order')
->alias('o')
->join('equipment e','o.eq_name=e.name')
->field('o.order_no,o.users_id,o.start_time,e.serial_number,e.hospital')
->where(['o.users_id'=>$user_id,'o.state'=>['in',[1,2]],'o.type'=>1,'e.is_delete'=>0])
->find();
if ($order){
$this->apiResponse(302,'有未完成订单');
}
if ($user['fee']>=$fee) {
//生成订单号
$order_sn=cmf_get_order_sn();
//进行提现
$app = new Application($this->options);
$merchantPay = $app->merchant_pay;
$merchantPayData = [
'partner_trade_no' => $order_sn, //随机字符串作为订单号,跟红包和支付一个概念。
'openid' => $user['open_id'], //收款人的openid
'check_name' => 'NO_CHECK', //文档中有三种校验实名的方法 NO_CHECK OPTION_CHECK FORCE_CHECK
// 're_user_name'=>'张三', //OPTION_CHECK FORCE_CHECK 校验实名的时候必须提交
'amount' => $fee * 100, //单位为分
// 'amount' => 100, //单位为分
'desc' => '企业付款',
'spbill_create_ip' => get_client_ip(0, true), //发起交易的IP地址
];
$re = $merchantPay->send($merchantPayData);
if ($re['return_code']=='SUCCESS' && $re['result_code']=='SUCCESS'){
Db::startTrans();
try{
Db::name('users')->where('id',$user_id)->setDec('fee', $fee);
Db::name('withdraw')->insert([
'user_id'=>$user_id,
'money'=>$fee,
'create_time'=>time(),
'order_sn'=>$order_sn
]);
Db::commit();
}catch (\Exception $e) {
// 回滚事务
Db::rollback();
$this->apiResponse(301,'提现失败');
}
$this->apiResponse(200,'success');
}else{
$this->apiResponse(301,'提现失败');
}
}else{
$this->apiResponse(300,'余额不足提现失败');
}
}
}