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

interface

... ... @@ -185,8 +185,9 @@ class AesController extends HomeBaseController{
* @param name:key type:String require:1 default:无 other: desc:设备返回状态(未加密)
* @param name:type type:String require:1 default:无 other:1开锁返回指令,2状态返回指令 desc:命令类型
*
* @return code:1锁已开,0位锁已关,2为数据异常
* @return name:设备锁名称
* @return state:1成功 0失败
* @return message:错误类型
* @return error:错误描述
*/
public function state($users_id,$name,$key,$type){
if ($type==1){
... ... @@ -194,14 +195,22 @@ class AesController extends HomeBaseController{
if($key[0]=='0xff' && $key[4]=='0xef'){
$this->open_deal_with($users_id,$name,$key);
}else{
$this->apiResponse(2,'返回指令错误');
$data=[
'state'=>0,
'message'=>'返回指令错误'
];
$this->apiResponse(200,'success',$data);
}
}else if($type==2){
//判断命令是否合法
if($key[0]=='0xff' && $key[7]=='0xef'){
$this->lock_deal_with($users_id,$name,$key);
}else{
$this->apiResponse(2,'返回指令错误');
$data=[
'state'=>0,
'message'=>'返回指令错误'
];
$this->apiResponse(200,'success',$data);
}
}
}
... ... @@ -217,15 +226,39 @@ class AesController extends HomeBaseController{
Db::name('equipment')->where('name',$name)->update(['use'=>1]);
}catch (\Exception $exception){
Db::rollback();
$this->apiResponse(301,'数据库状态更改失败,错误信息:'.$exception->getMessage());
$data=[
'state'=>0,
'message'=>'数据库操作错误',
'error' => $exception->getMessage()
];
$this->apiResponse(200,'success',$data);
}
Db::commit();
//生成订单
$this->apiResponse(200,'锁已开',['设备名称'=>$name]);
$order=new OrderController();
$res=$order->createOrder($users_id,$name);
if ($res['state'==true]){
Db::commit();
$data=[
'state'=>1,
'name'=>$name,
'order_no' => $res['order_no']
];
$this->apiResponse(200,'success',$data);
}else{
Db::rollback();
$data=[
'state'=>0,
'message'=>'订单生成错误',
'error' => $res['message']
];
$this->apiResponse(200,'success',$data);
}
}else if($key[3]=='0x00'){
$this->apiResponse(302,'锁未开',['设备名称'=>$name]);
$data=[
'state'=>0,
'message'=>'开锁失败'
];
$this->apiResponse(200,'success',$data);
}
}
//关锁
... ... @@ -233,7 +266,11 @@ class AesController extends HomeBaseController{
//查看数据库中设备状态
$use=Db::name('equipment')->where('name',$name)->find()['use'];
if ($use==0){
$this->apiResponse(303,'此锁未打开,还床失败',['设备名称'=>$name]);
$data=[
'state'=>0,
'message'=>'该设备为关闭状态,不能还锁'
];
$this->apiResponse(200,'success',$data);
}
//电量过低,修改数据库状态
if ($key[5]=='0x01'){
... ... @@ -241,20 +278,15 @@ class AesController extends HomeBaseController{
}
if ($key[6]=='0x01'){
//修改数据库设备状态
$this->apiResponse(301,'锁未关,当前锁状态为:开锁',['设备名称'=>$name]);//开着
$data=[
'state'=>0,
'message'=>'未还床'
];
$this->apiResponse(200,'success',$data);
}else if($key[6]=='0x00'){
//修改数据库设备状态
try{
Db::startTrans();
Db::name('users')->where('id',$users_id)->update(['is_use'=>0]);
Db::name('equipment')->where('name',$name)->update(['use'=>0]);
}catch (\Exception $exception){
Db::rollback();
$this->apiResponse(302,'数据库状态更改失败,错误信息:'.$exception->getMessage());
}
Db::commit();
$this->apiResponse(200,'关锁成功',['设备名称'=>$name]);//关了
$order=new OrderController();
$order->order($users_id,$name);
}
}
... ...
... ... @@ -20,20 +20,18 @@ use wxapp\pay\WeixinPay;
*/
class OrderController extends HomeBaseController{
/**
* @title 生成订单
* @description 生成订单,加入开始时间和用户id
* @author 董瑞恩
* @url /portal/order/createOrder
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
* @param name:name type:String require:1 default:无 other: desc:设备名称
*
*/
public function createOrder(){
$users_id=$this->request->param('users_id');
$name=$this->request->param('name');
// /**
// * @title 生成订单
// * @description 生成订单,加入开始时间和用户id
// * @author 董瑞恩
// * @url /portal/order/createOrder
// * @method GET
// *
// * @param name:users_id type:String require:1 default:无 other: desc:用户id
// * @param name:name type:String require:1 default:无 other: desc:设备名称
// *
// */
public function createOrder($users_id,$name){
$order=[
'order_no' => cmf_get_order_sn(),
'eq_name' => $name,
... ... @@ -49,28 +47,31 @@ class OrderController extends HomeBaseController{
Db::rollback();
$data=[
'state'=>false,
'message'=>$exception
'message'=>$exception->getMessage()
];
return $data;
}
Db::commit();
$this->apiResponse(200,'success');
$data=[
'state'=>true,
'order_no' => $order['order_no']
];
return $data;
}
/**
* @title 完成订单
* @description 订单完成,加入结束时间和结算费用,并调用微信统一下单
* @author 董瑞恩
* @url /portal/order/order
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
*
* @return data:返回用于调用支付的参数
*/
public function order(){
$users_id=$this->request->param('users_id');
$users=Db::name('users')->where('id',$users_id)->find();
$order= Db::name('order')->where('order_no',$users['order_no'])->find();
// /**
// * @title 完成订单
// * @description 订单完成,加入结束时间和结算费用,并调用微信统一下单
// * @author 董瑞恩
// * @url /portal/order/order
// * @method GET
// *
// * @param name:users_id type:String require:1 default:无 other: desc:用户id
// *
// * @return data:返回用于调用支付的参数
// */
public function order($users_id,$name){
$order= Db::name('order')->where(['users_id'=>$users_id,'state'=>1])->find();
$end_time=time();
$price=$this->getPrice($order['start_time'],$end_time);
$time=ceil(($end_time-$order['start_time'])/3600);
... ... @@ -83,13 +84,20 @@ class OrderController extends HomeBaseController{
try{
Db::startTrans();
Db::name('users')->where('id',$users_id)->update(['is_use'=>0,'order_no'=>null]);
Db::name('order')->where('order_no',$users['order_no'])->update($data);
Db::name('equipment')->where('name',$name)->update(['use'=>0]);
Db::name('order')->where('order_no',$order['order_no'])->update($data);
}catch (\Exception $exception){
Db::rollback();
$this->apiResponse(301,'订单生成失败,错误信息:'.$exception->getMessage());
$data=[
'state'=>false,
'message'=>$exception->getMessage()
];
$this->apiResponse(200,'success',$data);
}
Db::commit();
//调起支付
$this->pay($order['order_no']);
}
public function getPrice($start_time,$end_time){
... ... @@ -133,10 +141,22 @@ class OrderController extends HomeBaseController{
$price=$order['price']*100;//订单价格
$notify_url=url('order/notify','','',true);//回调地址
$wxPay=new WeixinPay($openId,$order_no,$body,$price,$notify_url);
$data=$wxPay->pay();
if (isset($data['package'])){
$this->apiResponse(200,'下单成功',$data);//微信支付下单成功,返回调用支付的参数
$pay=$wxPay->pay();
if (isset($pay['package'])){
$data=[
'state'=>1,
'pay'=>$pay
];
$this->apiResponse(200,'success',$data);
}else{
$data=[
'state'=>0,
'message'=>'统一下单失败',
'error' => $pay
];
$this->apiResponse(200,'success',$data);
}
}
//支付回调接口
... ...