作者 sgj
1 个管道 的构建 通过 耗费 4 秒

test api

@@ -403,7 +403,107 @@ class OrderController extends HomeBaseController{ @@ -403,7 +403,107 @@ class OrderController extends HomeBaseController{
403 } 403 }
404 return $price; 404 return $price;
405 } 405 }
  406 + /**
  407 + * @title 计算价格
  408 + * @description 微信统一下单
  409 + * @author 董瑞恩
  410 + * @url /portal/order/getPriceSGJ
  411 + * @method GET
  412 + *
  413 + * @param name:start_time type:String require:1 default:无 other: desc:起始时间
  414 + * @param name:end_time type:String require:1 default:无 other: desc:结束时间
  415 + *
  416 + * @return data:返回用于调用支付的参数
  417 + */
  418 + public function getPriceSGJ(){
  419 + /*先获取跨越天数*/
  420 + $users_id='11';
  421 + $data=input();
  422 + $start_time=$data['start_time']??'1545084020';
  423 + $end_time=$data['end_time']??time();
  424 + $spend=$this->timediff($start_time,$end_time);
  425 + if ($spend['min']!=0||$spend['sex']!=0){
  426 + $spend['hour']++;
  427 + }
  428 + $interval = Db::name('interval')->where('id',1)->find();
  429 + $cost=Db::name('cost')->where('id',1)->find();
  430 + $is_free=Db::name('users')->where('id',$users_id)->find()['is_free'];
  431 + $price=0;
  432 + $time=$start_time;
  433 + for ($i=0;$i<$spend['hour'];$i++){
  434 + $price+=$this->getPriceByTime($time,$interval['end_time'],$interval['start_time'],$interval['price'],$cost['cost']);
  435 + $time=$time+3600;
  436 + }
  437 +
  438 +
  439 + if ($price>$cost['ceiling']){
  440 + $price=$cost['ceiling'];
  441 + $free=0;
  442 + }else{
  443 + if ($cost['free']==1 && $is_free==0){
  444 + $free=$this->getPriceByTime($start_time,$interval['end_time'],$interval['start_time'],$interval['price'],$cost['cost']);
  445 + Db::name('users')->where('id',$users_id)->update(['is_free'=>1]);
  446 + }else{
  447 + $free=0;
  448 + }
  449 + }
  450 + $allPirce=$cost['ceiling']*$spend['day']+$price-$free;
  451 + return $allPirce;
  452 + }
  453 +
  454 + public function getOnlyPriceSGJ(){
  455 + /*先获取跨越天数*/
  456 + $users_id='11';
  457 + $data=input();
  458 + $start_time=$data['start_time']??'1545084020';
  459 + $end_time=$data['end_time']??time();
  460 + $spend=$this->timediff($start_time,$end_time);
  461 + if ($spend['min']!=0||$spend['sex']!=0){
  462 + $spend['hour']++;
  463 + }
  464 + $interval = Db::name('interval')->where('id',1)->find();
  465 + $cost=Db::name('cost')->where('id',1)->find();
  466 + $is_free=Db::name('users')->where('id',$users_id)->find()['is_free'];
  467 + $price=0;
  468 + $time=$start_time;
  469 + for ($i=0;$i<$spend['hour'];$i++){
  470 + $price+=$this->getPriceByTime($time,$interval['end_time'],$interval['start_time'],$interval['price'],$cost['cost']);
  471 + $time=$time+3600;
  472 + }
  473 +
406 474
  475 + if ($price>$cost['ceiling']){
  476 + $price=$cost['ceiling'];
  477 + $free=0;
  478 + }else{
  479 + if ($cost['free']==1 && $is_free==0){
  480 + $free=$this->getPriceByTime($start_time,$interval['end_time'],$interval['start_time'],$interval['price'],$cost['cost']);
  481 + Db::name('users')->where('id',$users_id)->update(['is_free'=>1]);
  482 + }else{
  483 + $free=0;
  484 + }
  485 + }
  486 + $allPirce=$cost['ceiling']*$spend['day']+$price-$free;
  487 + return $allPirce;
  488 + }
  489 +
  490 +
  491 + public function getPriceBytime($time,$first_time,$second_time,$cost1,$cost2){
  492 + $first_time=strtotime($first_time);
  493 + $second_time=strtotime($second_time);
  494 + if ($time<$first_time){
  495 + return $cost1;
  496 + }
  497 +
  498 + if ($time>$first_time && $time<$second_time){
  499 + return $cost2;
  500 + }
  501 +
  502 + if ($time>$second_time){
  503 + return $cost1;
  504 + }
  505 +
  506 + }
407 507
408 /** 508 /**
409 * @title 统一下单 509 * @title 统一下单
@@ -442,6 +542,7 @@ class OrderController extends HomeBaseController{ @@ -442,6 +542,7 @@ class OrderController extends HomeBaseController{
442 542
443 } 543 }
444 544
  545 +
445 //支付回调接口 546 //支付回调接口
446 public function notify(){ 547 public function notify(){
447 $param = $this->request->param(); 548 $param = $this->request->param();
@@ -471,6 +572,8 @@ class OrderController extends HomeBaseController{ @@ -471,6 +572,8 @@ class OrderController extends HomeBaseController{
471 } 572 }
472 } 573 }
473 574
  575 +
  576 +
474 function create_noncestr($length = 4){ 577 function create_noncestr($length = 4){
475 $chars = "0123456789"; 578 $chars = "0123456789";
476 $str = ""; 579 $str = "";
@@ -480,4 +583,93 @@ class OrderController extends HomeBaseController{ @@ -480,4 +583,93 @@ class OrderController extends HomeBaseController{
480 return $str; 583 return $str;
481 } 584 }
482 585
  586 + /**
  587 + * 计算两个时间戳戳相差时间
  588 + * @param $begin_time
  589 + * @param $end_time
  590 + * @return array
  591 + */
  592 + public function timediff($begin_time,$end_time)
  593 + {
  594 + if ($begin_time < $end_time) {
  595 + $starttime = $begin_time;
  596 + $endtime = $end_time;
  597 + } else {
  598 + $starttime = $end_time;
  599 + $endtime = $begin_time;
  600 + }
  601 + //计算天数
  602 + $timediff = $endtime - $starttime;
  603 + $days = intval($timediff / 86400);
  604 + //计算小时数
  605 + $remain = $timediff % 86400;
  606 + $hours = intval($remain / 3600);
  607 + //计算分钟数
  608 + $remain = $remain % 3600;
  609 + $mins = intval($remain / 60);
  610 + //计算秒数
  611 + $secs = $remain % 60;
  612 + $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
  613 + return $res;
  614 + }
  615 + /**
  616 + * 计算出不同收费时间段的使用时间
  617 + * @param $start 开始时间
  618 + * @param $end 结束时间
  619 + * @param $first 早上计时点
  620 + * @param $second 晚上计时点
  621 + * @return mixed
  622 + */
  623 + public function getTimeOneDay($start,$end,$first,$second){
  624 + $start_date=date('Y-m-d',$start);
  625 + $first=$start_date.' '.$first;
  626 + $first=strtotime($first);
  627 + $second=$start_date.' '.$second;
  628 + $second=strtotime($second);
  629 +// dump($start);
  630 +// dump($end);
  631 +// dump($first);
  632 +// dump($second);
  633 + /*分为六种情况分别处理*/
  634 +
  635 + if ($end<=$first){
  636 + $time['first']=$start-$end;
  637 + $time['second']=0;
  638 + return $time;
  639 + }
  640 +
  641 + if ($start<=$first && $end<=$second && $end>$first){
  642 + $time['first']=$first-$start;
  643 + $time['second']=$end-$first;
  644 + return $time;
  645 + }
  646 +
  647 + if ($start<=$first && $end>$second){
  648 + $time['first']=($first-$start)+($end-$second);
  649 + $time['second']=$second-$first;
  650 + return $time;
  651 + }
  652 +
  653 + if ($start>$first && $start<=$second && $end<=$second){
  654 + $time['first']='0';
  655 + $time['second']=$end-$start;
  656 + return $time;
  657 + }
  658 +
  659 + if ($start>$first && $start<=$second && $end>$second ){
  660 + $time['first']=$second-$start;
  661 + $time['second']=$end-$second;
  662 + return $time;
  663 + }
  664 +
  665 + if ($start>$second && $end>$second){
  666 + $time['first']=$end-$start;
  667 + $time['second']=0;
  668 + return $time;
  669 + }
  670 +
  671 + }
  672 +
  673 +
  674 +
483 } 675 }
@@ -51,8 +51,6 @@ class UsersController extends HomeBaseController{ @@ -51,8 +51,6 @@ class UsersController extends HomeBaseController{
51 } 51 }
52 } 52 }
53 53
54 -  
55 -  
56 /** 54 /**
57 * @title 状态验证 55 * @title 状态验证
58 * @description 开锁前判断是否有未支付订单与是否提交押金 56 * @description 开锁前判断是否有未支付订单与是否提交押金
@@ -7,7 +7,7 @@ return [ @@ -7,7 +7,7 @@ return [
7 // 数据库类型 7 // 数据库类型
8 'type' => 'mysql', 8 'type' => 'mysql',
9 // 服务器地址 9 // 服务器地址
10 - 'hostname' => 'rm-m5eblhc9o3515i25n.mysql.rds.aliyuncs.com', 10 + 'hostname' => 'rm-m5eblhc9o3515i25neo.mysql.rds.aliyuncs.com',
11 // 数据库名 11 // 数据库名
12 'database' => 'shuibao', 12 'database' => 'shuibao',
13 // 用户名 13 // 用户名