AesController.php 4.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: ruidiudiu
 * Date: 2018/11/12
 * Time: 11:02
 */

namespace app\portal\controller;



use aesutil\AESUtil;
use cmf\controller\HomeBaseController;
use think\Db;

/**
 * @title 蓝牙锁相关接口
 * @description 蓝牙锁相关接口
 * @group 蓝牙锁相关接口
 */
class AesController extends HomeBaseController{
    public function index(){
        $strKey='2222222222222222';
        $message='ff0ca23403de4f4aaf5500ef';
        $aes=new AESUtil($strKey);
        dump("加密".$message.":".$aes->encryption($message));
        dump("解密密2857153e26d8dda190834c3a1a3f8f3c:".$aes->decryption('2857153e26d8dda190834c3a1a3f8f3c'));
    }
    /**
     * @title 开锁
     * @description 获取加密后的“开锁”命令
     * @author 董瑞恩
     * @url /portal/Aes/lock
     * @method GET
     *
     * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
     *
     *
     * @return lockKey:加密的开锁指令(数组)
     */
    public function lock(){
        $MACAddress=$this->request->param('MACAddress');
        $strKey=Db::name('equipment')->where('mac_address',$MACAddress)->find();
        if (!empty($strKey)){
            $key="ff0ca2".$MACAddress."5500ef";
            $aes=new AESUtil($strKey['key']);
            $lockKey=$aes->encryption($key);
            $this->apiResponse(200,'success',$lockKey);
        }else{
            $this->apiResponse(301,'MAC地址未认证');
        }

    }

    /**
     * @title 获取电压、开关状态
     * @description 获得加密后“获取电压、开关状态”的命令
     * @author 董瑞恩
     * @url /portal/Aes/getState
     * @method GET
     *
     * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
     *
     *
     * @return lockKey:获取电压、开关状态的指令(数组)
     */

    public function getState(){
        $MACAddress=$this->request->param('MACAddress');
        $strKey=Db::name('equipment')->where('mac_address',$MACAddress)->find();
        if (!empty($strKey)){
            $key="ff05a301ef";
            $aes=new AESUtil($strKey['key']);
            $StateKey=$aes->encryption($key);
            $this->apiResponse(200,'success',$StateKey);
        }else{
            $this->apiResponse(301,'MAC地址未认证');
        }
    }

    /**
     * @title 命令解密
     * @description 将设备的十六进制码进行解码,获得设备的返回信息
     * @author 董瑞恩
     * @url /portal/Aes/decryption
     * @method GET
     *
     * @param name:decryKey type:String require:1 default:无 other: desc:加密命令
     * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
     *
     * @return key:解密后的指令(数组)
     */
    public function decryption(){
        $MACAddress=$this->request->param('MACAddress');
        $decryKey=$this->request->param('decryKey');
        $strKey=Db::name('equipment')->where('mac_address',$MACAddress)->find();
        if (!empty($strKey)){
            $aes=new AESUtil($strKey['key']);
            $key=$aes->decryption($decryKey);
            $this->apiResponse(200,'success',$key);
        }else{
            $this->apiResponse(301,'MAC地址未认证');
        }
    }

    /**
     * @title 状态验证
     * @description 开锁前判断是否有未支付订单与是否提交押金
     * @author 董瑞恩
     * @url /portal/Aes/lock_check
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
     */
    public function lock_check(){
        $users_id=$this->request->param('users_id');
        //获取提交押金的状态
        $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,'验证通过');
            }else{
                $this->apiResponse(302,'有未支付订单');
            }
        }else{
            $this->apiResponse(301,'未支付押金');
        }
    }

}