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

cost

... ... @@ -153,7 +153,7 @@ class AesController extends HomeBaseController{
if (!empty($strKey)){
$aes=new AESUtil($strKey['key']);
$key=$aes->decryption($decryKey);
$this->state($key,$type);
$this->state($MACAddress,$key,$type);
}else{
$this->apiResponse(301,'MAC地址未认证');
}
... ... @@ -166,15 +166,22 @@ class AesController extends HomeBaseController{
* @url /portal/Aes/decryption
* @method GET
*
* @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
* @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为数据异常
*/
public function state($key,$type){
public function state($MACAddress,$key,$type){
if ($type==1){
if($key[0]=='0xff' && $key[4]=='0xef'){
if ($key[3]=='0x01'){
//修改数据库设备状态
try{
Db::name('equipment')->where('mac_address',$MACAddress)->update(['use'=>1]);
}catch (\Exception $exception){
$this->apiResponse(301,'数据库状态更改失败,错误信息:'.$exception->getMessage());
}
$this->apiResponse(1,'锁已开');
}else if($key[3]=='0x00'){
$this->apiResponse(0,'开锁失败');
... ... @@ -185,8 +192,15 @@ class AesController extends HomeBaseController{
}else if($type==2){
if($key[0]=='0xff' && $key[7]=='0xef'){
if ($key[6]=='0x01'){
//修改数据库设备状态
$this->apiResponse(1,'状态为开锁');//开着
}else if($key[6]=='0x00'){
//修改数据库设备状态
try{
Db::name('equipment')->where('mac_address',$MACAddress)->update(['use'=>0]);
}catch (\Exception $exception){
$this->apiResponse(301,'数据库状态更改失败,错误信息:'.$exception->getMessage());
}
$this->apiResponse(0,'状态为关锁');//关了
}
}else{
... ...
<?php
/**
* Created by PhpStorm.
* User: ruidiudiu
* Date: 2018/11/24
* Time: 8:54
*/
namespace app\portal\controller;
use cmf\controller\HomeBaseController;
use think\Db;
use wxapp\pay\WeixinPay;
/**
* @title 订单相关接口
* @description 订单相关接口
* @group 订单相关接口
*/
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
*
*
*/
public function createOrder(){
$users_id=$this->request->param('users_id');
$order=[
'order_no' => cmf_get_order_sn(),
'users_id' => $users_id,
'start_time' => time(),
'state' => 1
];
try{
Db::startTrans();
Db::name('users')->where('users_id',$users_id)->update(['is_use'=>1,'order_no'=>$order['order_no']]);
Db::name('order')->insert($order);
}catch (\Exception $exception){
Db::rollback();
$this->apiResponse(301,'订单生成失败,错误信息:'.$exception->getMessage());
}
Db::commit();
$this->apiResponse(200,'success');
}
/**
* @title 完成订单
* @description 订单完成,加入结束时间和结算费用,并调用微信统一下单
* @author 董瑞恩
* @url /portal/order/order
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
*
*
*/
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();
$end_time=time();
$price=$this->getPrice($order['start_time'],$end_time);
$data=[
'end_time'=>$end_time,
'price' => $price,
'state' => 2
];
try{
Db::startTrans();
Db::name('users')->where('users_id',$users_id)->update(['is_use'=>0,'order_no'=>null]);
Db::name('order')->where('order_no',$users['order_no'])->update($data);
}catch (\Exception $exception){
Db::rollback();
$this->apiResponse(301,'订单生成失败,错误信息:'.$exception->getMessage());
}
Db::commit();
$this->pay($order['order_no']);
}
public function getPrice($start_time,$end_time){
$price=1;
return $price;
}
/**
* @title 统一下单
* @description 微信统一下单
* @author 董瑞恩
* @url /portal/order/pay
* @method GET
*
* @param name:order_no type:String require:1 default:无 other: desc:订单号
* @param name:openId type:String require:1 default:无 other: desc:openId
*
*/
public function pay($order_no){
$order=Db::name('order')->where(['order_no'=>$order_no,'state'=>2])->find();
$openId=Db::name('users')->where('users_id',$order['users_id'])->find()['open_id'];
$body='支付';
$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);//微信支付下单成功,返回调用支付的参数
}
}
//支付回调接口
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') {
try{
Db::name('order')->where(['order_no'=>$order_no])->update(['state'=>3]);
}catch (\Exception $exception){
$this->apiResponse(301,'error:'.$exception->getMessage());
}
return "<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>";
}
}
}
\ No newline at end of file
... ...
... ... @@ -11,14 +11,18 @@ namespace app\portal\controller;
use cmf\controller\HomeBaseController;
use think\Db;
/**
* @title 用户相关接口
* @description 用户相关接口
* @group 用户相关接口
*/
class UsersController extends HomeBaseController{
/**
* @title 状态验证
* @description 开锁前判断是否有未支付订单与是否提交押金
* @author 董瑞恩
* @url /portal/Aes/lock_check
* @url /portal/users/lock_check
* @method GET
*
* @param name:users_id type:String require:1 default:无 other: desc:用户id
... ... @@ -28,14 +32,39 @@ class UsersController extends HomeBaseController{
//获取提交押金的状态
$users=Db::name('users')->where('id',$users_id)->find();
if ($users['is_deposit']===1){
$order=Db::name('order')->where(['users_id'=>$users_id,'state'=>1])->find();
if (empty($order)){
$this->apiResponse(200,'验证通过');
if ($users['is_use']==1){
$this->apiResponse(303,'用户正在使用设备');
}else{
$this->apiResponse(302,'有未支付订单');
$order=Db::name('order')->where(['users_id'=>$users_id,'state'=>2])->find();
if (empty($order)){
$this->apiResponse(200,'验证通过');
}else{
$this->apiResponse(302,'有未支付订单');
}
}
}else{
$this->apiResponse(301,'未支付押金');
}
}
/**
* @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,'用户尚未使用设备');
}
}
}
\ No newline at end of file
... ...
<?php
namespace wxapp\pay;
/*
* 小程序微信支付
*/
... ... @@ -11,10 +12,10 @@ class WeixinPay {
protected $body;
protected $total_fee;
protected $notify_url;
function __construct($openid,$out_trade_no,$body,$total_fee,$notify_url) {
$this->appid = config('mini_app_id');
$this->mch_id = config('mch_id');
$this->key = config('key');
function __construct($openid=null,$out_trade_no=null,$body=null,$total_fee=null,$notify_url=null) {
$this->appid = config('wechat_config.app_id');
$this->mch_id = config('wechat_config.payment')['merchant_id'];
$this->key = config('wechat_config.payment')['key'];
$this->openid = $openid;
$this->out_trade_no = $out_trade_no;
$this->body = $body;
... ... @@ -91,7 +92,7 @@ class WeixinPay {
return $xml;
}
//xml转换成数组
private function xmlToArray($xml) {
public function xmlToArray($xml) {
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
... ... @@ -104,7 +105,7 @@ class WeixinPay {
$unifiedorder = $this->unifiedorder();
$parameters = array(
'appId' => $this->appid, //小程序ID
'timeStamp' => '' . time() . '', //时间戳
'timeStamp' => ''.time().'', //时间戳
'nonceStr' => $this->createNoncestr(), //随机串
'package' => 'prepay_id=' . $unifiedorder['prepay_id'], //数据包
'signType' => 'MD5'//签名方式
... ... @@ -123,7 +124,7 @@ class WeixinPay {
return $str;
}
//作用:生成签名
private function getSign($Obj) {
public function getSign($Obj) {
foreach ($Obj as $k => $v) {
$Parameters[$k] = $v;
}
... ... @@ -143,10 +144,12 @@ class WeixinPay {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if ($urlencode) {
$v = urlencode($v);
if(null != $v && "null" != $v && "sign" != $k) {
if ($urlencode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
$buff .= $k . "=" . $v . "&";
}
$reqPar='';
if (strlen($buff) > 0) {
... ...
... ... @@ -9,6 +9,8 @@ class Doc
'copyright'=>'银河百荣科技',
'controller' => [
'app\\portal\\controller\\AesController',
'app\\portal\\controller\\UsersController',
'app\\portal\\controller\\OrderController',
],
'password'=>'bronet',
'static_path'=>'',
... ...