作者 董瑞恩
1 个管道 的构建 通过 耗费 3 秒

cost

@@ -153,7 +153,7 @@ class AesController extends HomeBaseController{ @@ -153,7 +153,7 @@ class AesController extends HomeBaseController{
153 if (!empty($strKey)){ 153 if (!empty($strKey)){
154 $aes=new AESUtil($strKey['key']); 154 $aes=new AESUtil($strKey['key']);
155 $key=$aes->decryption($decryKey); 155 $key=$aes->decryption($decryKey);
156 - $this->state($key,$type); 156 + $this->state($MACAddress,$key,$type);
157 }else{ 157 }else{
158 $this->apiResponse(301,'MAC地址未认证'); 158 $this->apiResponse(301,'MAC地址未认证');
159 } 159 }
@@ -166,15 +166,22 @@ class AesController extends HomeBaseController{ @@ -166,15 +166,22 @@ class AesController extends HomeBaseController{
166 * @url /portal/Aes/decryption 166 * @url /portal/Aes/decryption
167 * @method GET 167 * @method GET
168 * 168 *
  169 + * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
169 * @param name:key type:String require:1 default:无 other: desc:设备返回状态(未加密) 170 * @param name:key type:String require:1 default:无 other: desc:设备返回状态(未加密)
170 * @param name:type type:String require:1 default:无 other:1开锁返回指令,2状态返回指令 desc:命令类型 171 * @param name:type type:String require:1 default:无 other:1开锁返回指令,2状态返回指令 desc:命令类型
171 * 172 *
172 * @return code:1锁已开,0位锁已关,2为数据异常 173 * @return code:1锁已开,0位锁已关,2为数据异常
173 */ 174 */
174 - public function state($key,$type){ 175 + public function state($MACAddress,$key,$type){
175 if ($type==1){ 176 if ($type==1){
176 if($key[0]=='0xff' && $key[4]=='0xef'){ 177 if($key[0]=='0xff' && $key[4]=='0xef'){
177 if ($key[3]=='0x01'){ 178 if ($key[3]=='0x01'){
  179 + //修改数据库设备状态
  180 + try{
  181 + Db::name('equipment')->where('mac_address',$MACAddress)->update(['use'=>1]);
  182 + }catch (\Exception $exception){
  183 + $this->apiResponse(301,'数据库状态更改失败,错误信息:'.$exception->getMessage());
  184 + }
178 $this->apiResponse(1,'锁已开'); 185 $this->apiResponse(1,'锁已开');
179 }else if($key[3]=='0x00'){ 186 }else if($key[3]=='0x00'){
180 $this->apiResponse(0,'开锁失败'); 187 $this->apiResponse(0,'开锁失败');
@@ -185,8 +192,15 @@ class AesController extends HomeBaseController{ @@ -185,8 +192,15 @@ class AesController extends HomeBaseController{
185 }else if($type==2){ 192 }else if($type==2){
186 if($key[0]=='0xff' && $key[7]=='0xef'){ 193 if($key[0]=='0xff' && $key[7]=='0xef'){
187 if ($key[6]=='0x01'){ 194 if ($key[6]=='0x01'){
  195 + //修改数据库设备状态
188 $this->apiResponse(1,'状态为开锁');//开着 196 $this->apiResponse(1,'状态为开锁');//开着
189 }else if($key[6]=='0x00'){ 197 }else if($key[6]=='0x00'){
  198 + //修改数据库设备状态
  199 + try{
  200 + Db::name('equipment')->where('mac_address',$MACAddress)->update(['use'=>0]);
  201 + }catch (\Exception $exception){
  202 + $this->apiResponse(301,'数据库状态更改失败,错误信息:'.$exception->getMessage());
  203 + }
190 $this->apiResponse(0,'状态为关锁');//关了 204 $this->apiResponse(0,'状态为关锁');//关了
191 } 205 }
192 }else{ 206 }else{
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: ruidiudiu
  5 + * Date: 2018/11/24
  6 + * Time: 8:54
  7 + */
  8 +
  9 +namespace app\portal\controller;
  10 +
  11 +
  12 +use cmf\controller\HomeBaseController;
  13 +use think\Db;
  14 +use wxapp\pay\WeixinPay;
  15 +
  16 +/**
  17 + * @title 订单相关接口
  18 + * @description 订单相关接口
  19 + * @group 订单相关接口
  20 + */
  21 +class OrderController extends HomeBaseController{
  22 +
  23 + /**
  24 + * @title 生成订单
  25 + * @description 生成订单,加入开始时间和用户id
  26 + * @author 董瑞恩
  27 + * @url /portal/order/createOrder
  28 + * @method GET
  29 + *
  30 + * @param name:users_id type:String require:1 default:无 other: desc:用户id
  31 + *
  32 + *
  33 + */
  34 + public function createOrder(){
  35 + $users_id=$this->request->param('users_id');
  36 + $order=[
  37 + 'order_no' => cmf_get_order_sn(),
  38 + 'users_id' => $users_id,
  39 + 'start_time' => time(),
  40 + 'state' => 1
  41 + ];
  42 + try{
  43 + Db::startTrans();
  44 + Db::name('users')->where('users_id',$users_id)->update(['is_use'=>1,'order_no'=>$order['order_no']]);
  45 + Db::name('order')->insert($order);
  46 + }catch (\Exception $exception){
  47 + Db::rollback();
  48 + $this->apiResponse(301,'订单生成失败,错误信息:'.$exception->getMessage());
  49 + }
  50 + Db::commit();
  51 + $this->apiResponse(200,'success');
  52 + }
  53 +
  54 + /**
  55 + * @title 完成订单
  56 + * @description 订单完成,加入结束时间和结算费用,并调用微信统一下单
  57 + * @author 董瑞恩
  58 + * @url /portal/order/order
  59 + * @method GET
  60 + *
  61 + * @param name:users_id type:String require:1 default:无 other: desc:用户id
  62 + *
  63 + *
  64 + */
  65 + public function order(){
  66 + $users_id=$this->request->param('users_id');
  67 + $users=Db::name('users')->where('id',$users_id)->find();
  68 + $order= Db::name('order')->where('order_no',$users['order_no'])->find();
  69 + $end_time=time();
  70 + $price=$this->getPrice($order['start_time'],$end_time);
  71 + $data=[
  72 + 'end_time'=>$end_time,
  73 + 'price' => $price,
  74 + 'state' => 2
  75 + ];
  76 + try{
  77 + Db::startTrans();
  78 + Db::name('users')->where('users_id',$users_id)->update(['is_use'=>0,'order_no'=>null]);
  79 + Db::name('order')->where('order_no',$users['order_no'])->update($data);
  80 + }catch (\Exception $exception){
  81 + Db::rollback();
  82 + $this->apiResponse(301,'订单生成失败,错误信息:'.$exception->getMessage());
  83 + }
  84 + Db::commit();
  85 + $this->pay($order['order_no']);
  86 + }
  87 +
  88 + public function getPrice($start_time,$end_time){
  89 + $price=1;
  90 + return $price;
  91 + }
  92 +
  93 + /**
  94 + * @title 统一下单
  95 + * @description 微信统一下单
  96 + * @author 董瑞恩
  97 + * @url /portal/order/pay
  98 + * @method GET
  99 + *
  100 + * @param name:order_no type:String require:1 default:无 other: desc:订单号
  101 + * @param name:openId type:String require:1 default:无 other: desc:openId
  102 + *
  103 + */
  104 + public function pay($order_no){
  105 + $order=Db::name('order')->where(['order_no'=>$order_no,'state'=>2])->find();
  106 + $openId=Db::name('users')->where('users_id',$order['users_id'])->find()['open_id'];
  107 + $body='支付';
  108 + $price=$order['price']*100;//订单价格
  109 + $notify_url=url('order/notify','','',true);//回调地址
  110 + $wxPay=new WeixinPay($openId,$order_no,$body,$price,$notify_url);
  111 + $data=$wxPay->pay();
  112 + if (isset($data['package'])){
  113 + $this->apiResponse(200,'下单成功',$data);//微信支付下单成功,返回调用支付的参数
  114 + }
  115 + }
  116 +
  117 + //支付回调接口
  118 + public function notify(){
  119 + $param = $this->request->param();
  120 + if ($param == null) {
  121 + $param = file_get_contents("php://input");
  122 + if ($param == null) {
  123 + $param = $GLOBALS['HTTP_RAW_POST_DATA'];
  124 + }
  125 + }
  126 + $wxPay=new WeixinPay();
  127 + $data = $wxPay->xmlToArray($param);
  128 + $Sign = $data['sign'];
  129 + //支付成功回调后变更订单状态
  130 + $mySign = $wxPay->getSign($data);
  131 + $order_no = $data['out_trade_no'];
  132 + if ($Sign===$mySign && $data['return_code'] == 'SUCCESS') {
  133 + try{
  134 + Db::name('order')->where(['order_no'=>$order_no])->update(['state'=>3]);
  135 + }catch (\Exception $exception){
  136 + $this->apiResponse(301,'error:'.$exception->getMessage());
  137 + }
  138 + return "<xml>
  139 + <return_code><![CDATA[SUCCESS]]></return_code>
  140 + <return_msg><![CDATA[OK]]></return_msg>
  141 + </xml>";
  142 +
  143 + }
  144 + }
  145 +
  146 +}
@@ -11,14 +11,18 @@ namespace app\portal\controller; @@ -11,14 +11,18 @@ namespace app\portal\controller;
11 11
12 use cmf\controller\HomeBaseController; 12 use cmf\controller\HomeBaseController;
13 use think\Db; 13 use think\Db;
14 - 14 +/**
  15 + * @title 用户相关接口
  16 + * @description 用户相关接口
  17 + * @group 用户相关接口
  18 + */
15 class UsersController extends HomeBaseController{ 19 class UsersController extends HomeBaseController{
16 20
17 /** 21 /**
18 * @title 状态验证 22 * @title 状态验证
19 * @description 开锁前判断是否有未支付订单与是否提交押金 23 * @description 开锁前判断是否有未支付订单与是否提交押金
20 * @author 董瑞恩 24 * @author 董瑞恩
21 - * @url /portal/Aes/lock_check 25 + * @url /portal/users/lock_check
22 * @method GET 26 * @method GET
23 * 27 *
24 * @param name:users_id type:String require:1 default:无 other: desc:用户id 28 * @param name:users_id type:String require:1 default:无 other: desc:用户id
@@ -28,14 +32,39 @@ class UsersController extends HomeBaseController{ @@ -28,14 +32,39 @@ class UsersController extends HomeBaseController{
28 //获取提交押金的状态 32 //获取提交押金的状态
29 $users=Db::name('users')->where('id',$users_id)->find(); 33 $users=Db::name('users')->where('id',$users_id)->find();
30 if ($users['is_deposit']===1){ 34 if ($users['is_deposit']===1){
31 - $order=Db::name('order')->where(['users_id'=>$users_id,'state'=>1])->find(); 35 + if ($users['is_use']==1){
  36 + $this->apiResponse(303,'用户正在使用设备');
  37 + }else{
  38 + $order=Db::name('order')->where(['users_id'=>$users_id,'state'=>2])->find();
32 if (empty($order)){ 39 if (empty($order)){
33 $this->apiResponse(200,'验证通过'); 40 $this->apiResponse(200,'验证通过');
34 }else{ 41 }else{
35 $this->apiResponse(302,'有未支付订单'); 42 $this->apiResponse(302,'有未支付订单');
36 } 43 }
  44 + }
37 }else{ 45 }else{
38 $this->apiResponse(301,'未支付押金'); 46 $this->apiResponse(301,'未支付押金');
39 } 47 }
40 } 48 }
  49 + /**
  50 + * @title 用户使用状态验证
  51 + * @description 判断用户当前是否在使用设备
  52 + * @author 董瑞恩
  53 + * @url /portal/users/isUse
  54 + * @method GET
  55 + *
  56 + * @param name:users_id type:String require:1 default:无 other: desc:用户id
  57 + */
  58 + public function isUse(){
  59 + $users_id=$this->request->param('users_id');
  60 + $users=Db::name('users')->where('id',$users_id)->find();
  61 + if ($users['is_use']==1){
  62 + $this->apiResponse(200,'用户正在使用设备');
  63 + }else{
  64 + $this->apiResponse(301,'用户尚未使用设备');
  65 + }
  66 + }
  67 +
  68 +
  69 +
41 } 70 }
1 <?php 1 <?php
  2 +namespace wxapp\pay;
2 /* 3 /*
3 * 小程序微信支付 4 * 小程序微信支付
4 */ 5 */
@@ -11,10 +12,10 @@ class WeixinPay { @@ -11,10 +12,10 @@ class WeixinPay {
11 protected $body; 12 protected $body;
12 protected $total_fee; 13 protected $total_fee;
13 protected $notify_url; 14 protected $notify_url;
14 - function __construct($openid,$out_trade_no,$body,$total_fee,$notify_url) {  
15 - $this->appid = config('mini_app_id');  
16 - $this->mch_id = config('mch_id');  
17 - $this->key = config('key'); 15 + function __construct($openid=null,$out_trade_no=null,$body=null,$total_fee=null,$notify_url=null) {
  16 + $this->appid = config('wechat_config.app_id');
  17 + $this->mch_id = config('wechat_config.payment')['merchant_id'];
  18 + $this->key = config('wechat_config.payment')['key'];
18 $this->openid = $openid; 19 $this->openid = $openid;
19 $this->out_trade_no = $out_trade_no; 20 $this->out_trade_no = $out_trade_no;
20 $this->body = $body; 21 $this->body = $body;
@@ -91,7 +92,7 @@ class WeixinPay { @@ -91,7 +92,7 @@ class WeixinPay {
91 return $xml; 92 return $xml;
92 } 93 }
93 //xml转换成数组 94 //xml转换成数组
94 - private function xmlToArray($xml) { 95 + public function xmlToArray($xml) {
95 //禁止引用外部xml实体 96 //禁止引用外部xml实体
96 libxml_disable_entity_loader(true); 97 libxml_disable_entity_loader(true);
97 $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); 98 $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
@@ -104,7 +105,7 @@ class WeixinPay { @@ -104,7 +105,7 @@ class WeixinPay {
104 $unifiedorder = $this->unifiedorder(); 105 $unifiedorder = $this->unifiedorder();
105 $parameters = array( 106 $parameters = array(
106 'appId' => $this->appid, //小程序ID 107 'appId' => $this->appid, //小程序ID
107 - 'timeStamp' => '' . time() . '', //时间戳 108 + 'timeStamp' => ''.time().'', //时间戳
108 'nonceStr' => $this->createNoncestr(), //随机串 109 'nonceStr' => $this->createNoncestr(), //随机串
109 'package' => 'prepay_id=' . $unifiedorder['prepay_id'], //数据包 110 'package' => 'prepay_id=' . $unifiedorder['prepay_id'], //数据包
110 'signType' => 'MD5'//签名方式 111 'signType' => 'MD5'//签名方式
@@ -123,7 +124,7 @@ class WeixinPay { @@ -123,7 +124,7 @@ class WeixinPay {
123 return $str; 124 return $str;
124 } 125 }
125 //作用:生成签名 126 //作用:生成签名
126 - private function getSign($Obj) { 127 + public function getSign($Obj) {
127 foreach ($Obj as $k => $v) { 128 foreach ($Obj as $k => $v) {
128 $Parameters[$k] = $v; 129 $Parameters[$k] = $v;
129 } 130 }
@@ -143,11 +144,13 @@ class WeixinPay { @@ -143,11 +144,13 @@ class WeixinPay {
143 $buff = ""; 144 $buff = "";
144 ksort($paraMap); 145 ksort($paraMap);
145 foreach ($paraMap as $k => $v) { 146 foreach ($paraMap as $k => $v) {
  147 + if(null != $v && "null" != $v && "sign" != $k) {
146 if ($urlencode) { 148 if ($urlencode) {
147 $v = urlencode($v); 149 $v = urlencode($v);
148 } 150 }
149 $buff .= $k . "=" . $v . "&"; 151 $buff .= $k . "=" . $v . "&";
150 } 152 }
  153 + }
151 $reqPar=''; 154 $reqPar='';
152 if (strlen($buff) > 0) { 155 if (strlen($buff) > 0) {
153 $reqPar = substr($buff, 0, strlen($buff) - 1); 156 $reqPar = substr($buff, 0, strlen($buff) - 1);
@@ -9,6 +9,8 @@ class Doc @@ -9,6 +9,8 @@ class Doc
9 'copyright'=>'银河百荣科技', 9 'copyright'=>'银河百荣科技',
10 'controller' => [ 10 'controller' => [
11 'app\\portal\\controller\\AesController', 11 'app\\portal\\controller\\AesController',
  12 + 'app\\portal\\controller\\UsersController',
  13 + 'app\\portal\\controller\\OrderController',
12 ], 14 ],
13 'password'=>'bronet', 15 'password'=>'bronet',
14 'static_path'=>'', 16 'static_path'=>'',