审查视图

app/portal/controller/AesController.php 11.9 KB
董瑞恩 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<?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);
董瑞恩 authored
27 28
        dump($aes->encryption($message));
        dump($aes->decryption('2857153e26d8dda190834c3a1a3f8f3c'));
董瑞恩 authored
29
    }
董瑞恩 authored
30 31 32 33 34 35 36 37 38 39 40 41
//    /**
//     * @title 开锁(加密)
//     * @description 获取加密后的“开锁”命令
//     * @author 董瑞恩
//     * @url /portal/Aes/secretLock
//     * @method GET
//     *
//     * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
//     *
//     *
//     * @return lockKey:加密的开锁指令(数组)
//     */
董瑞恩 authored
42
    public function secretLock(){
董瑞恩 authored
43 44 45 46 47 48 49 50 51 52 53 54 55 56
        $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地址未认证');
        }

    }

    /**
董瑞恩 authored
57 58 59 60 61 62
     * @title 开锁(不加密)
     * @description 获取“开锁”命令
     * @author 董瑞恩
     * @url /portal/Aes/lock
     * @method GET
     *
董瑞恩 authored
63
     * @param name:name type:String require:1 default:无 other: desc:设备名称
董瑞恩 authored
64 65 66 67 68
     *
     *
     * @return lockKey:加密的开锁指令(数组)
     */
    public function lock(){
董瑞恩 authored
69
        $name=$this->request->param('name');
董瑞恩 authored
70
        $strKey=Db::name('equipment')->where('name',$name)->find();
董瑞恩 authored
71
        if (!empty($strKey)){
董瑞恩 authored
72 73 74
            if ($strKey['electricity']==0){
                $this->apiResponse(302,'设备电量低');
            }
董瑞恩 authored
75 76 77 78 79 80 81
            if ($strKey['use']==1){
                $this->apiResponse(303,'当前设备正在使用');
            }
            $strMac=$this->macToStr($strKey['mac_address']);
            $key="ff0ca2".$strMac."5500ef";
            $lockKey=$this->ToArray($key);
            $this->apiResponse(200,'success',$lockKey);
董瑞恩 authored
82 83 84 85 86 87
        }else{
            $this->apiResponse(301,'MAC地址未认证');
        }

    }
董瑞恩 authored
88 89 90 91 92 93 94
    public function macToStr($mac){
        $mac_address=explode(":",$mac);
        $strMac="";
        foreach ($mac_address as $k=>$v){
            $strMac.=$v;
        }
        return $strMac;
董瑞恩 authored
95
    }
董瑞恩 authored
96
董瑞恩 authored
97 98 99 100 101 102 103 104 105 106 107 108
//    /**
//     * @title 获取电压、开关状态(加密)
//     * @description 获得加密后“获取电压、开关状态”的命令
//     * @author 董瑞恩
//     * @url /portal/Aes/getSecretState
//     * @method GET
//     *
//     * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址
//     *
//     *
//     * @return lockKey:获取电压、开关状态的指令(数组)
//     */
董瑞恩 authored
109
董瑞恩 authored
110
    public function getSecretState(){
董瑞恩 authored
111 112 113 114 115 116 117 118 119 120 121 122 123
        $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地址未认证');
        }
    }

    /**
董瑞恩 authored
124 125 126 127 128 129
     * @title 获取电压、开关状态(不加密)
     * @description 获得加密后“获取电压、开关状态”的命令
     * @author 董瑞恩
     * @url /portal/Aes/getState
     * @method GET
     *
董瑞恩 authored
130
     * @param name:name type:String require:1 default:无 other: desc:设备名称
董瑞恩 authored
131 132 133 134 135 136
     *
     *
     * @return lockKey:获取电压、开关状态的指令(数组)
     */

    public function getState(){
董瑞恩 authored
137 138 139
        $name=$this->request->param('name');
        $equipment=Db::name('equipment')->where('mac_address',$name)->find();
        if (!empty($equipment)){
董瑞恩 authored
140 141 142 143 144 145 146 147 148
            $key="ff05a301ef";
            $StateKey=$this->ToArray($key);
            $this->apiResponse(200,'success',$StateKey);
        }else{
            $this->apiResponse(301,'MAC地址未认证');
        }
    }

董瑞恩 authored
149 150 151 152 153 154 155 156 157 158 159 160 161
//    /**
//     * @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地址
//     * @param name:type type:String require:1 default:无 other:1开锁返回指令,2状态返回指令 desc:命令类型
//     *
//     * @return code:1锁已开,0位锁已关,2为数据异常
//     */
董瑞恩 authored
162 163 164
    public function decryption(){
        $MACAddress=$this->request->param('MACAddress');
        $decryKey=$this->request->param('decryKey');
董瑞恩 authored
165
        $type=$this->request->param('type');
董瑞恩 authored
166 167 168 169
        $strKey=Db::name('equipment')->where('mac_address',$MACAddress)->find();
        if (!empty($strKey)){
            $aes=new AESUtil($strKey['key']);
            $key=$aes->decryption($decryKey);
董瑞恩 authored
170
            $this->state($MACAddress,$key,$type);
董瑞恩 authored
171 172 173 174 175 176
        }else{
            $this->apiResponse(301,'MAC地址未认证');
        }
    }

    /**
董瑞恩 authored
177 178
     * @title 设备状态判断
     * @description 解析设备返回值,获取设备状态
董瑞恩 authored
179
     * @author 董瑞恩
董瑞恩 authored
180
     * @url /portal/Aes/state
董瑞恩 authored
181 182
     * @method GET
     *
董瑞恩 authored
183
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
董瑞恩 authored
184
     * @param name:name type:String require:1 default:无 other: desc:设备名称
董瑞恩 authored
185
     * @param name:key type:String require:1 default:无 other: desc:设备返回状态(未加密)
董瑞恩 authored
186
     * @param name:type type:String require:1 default:无 other:1开锁返回指令,2状态返回指令 desc:命令类型
董瑞恩 authored
187
     *
董瑞恩 authored
188 189 190
     * @return state:1成功  0失败
     * @return message:错误类型
     * @return error:错误描述
董瑞恩 authored
191
     */
董瑞恩 authored
192
    public function state($users_id,$name,$key,$type){
董瑞恩 authored
193
        $key=$this->ToArray($key);
董瑞恩 authored
194
        if ($type==1){
董瑞恩 authored
195
            //判断命令是否合法
董瑞恩 authored
196
            if($key[0]=='0xff' && $key[4]=='0xef'){
董瑞恩 authored
197
                $this->open_deal_with($users_id,$name,$key);
董瑞恩 authored
198
            }else{
董瑞恩 authored
199 200
                $data=[
                    'state'=>0,
董瑞恩 authored
201 202
                    'message'=>'返回指令错误',
                    'error' =>''
董瑞恩 authored
203 204
                ];
                $this->apiResponse(200,'success',$data);
董瑞恩 authored
205
            }
董瑞恩 authored
206
        }else if($type==2){
董瑞恩 authored
207
            //判断命令是否合法
董瑞恩 authored
208
            if($key[0]=='0xff' && $key[7]=='0xef'){
董瑞恩 authored
209
                $this->lock_deal_with($users_id,$name,$key);
董瑞恩 authored
210
            }else{
董瑞恩 authored
211 212
                $data=[
                    'state'=>0,
董瑞恩 authored
213 214
                    'message'=>'返回指令错误',
                    'error' =>''
董瑞恩 authored
215 216
                ];
                $this->apiResponse(200,'success',$data);
董瑞恩 authored
217 218 219 220
            }
        }
    }
董瑞恩 authored
221
    //开锁状态处理
董瑞恩 authored
222
    public function open_deal_with($users_id,$name,$key){
董瑞恩 authored
223 224 225
        if ($key[3]=='0x01'){
            //修改数据库设备状态
            try{
董瑞恩 authored
226 227 228
                Db::startTrans();
                //用户状态
                Db::name('users')->where('id',$users_id)->update(['is_use'=>1]);
董瑞恩 authored
229 230
                Db::name('equipment')->where('name',$name)->update(['use'=>1]);
            }catch (\Exception $exception){
董瑞恩 authored
231
                Db::rollback();
董瑞恩 authored
232 233 234 235 236 237
                $data=[
                    'state'=>0,
                    'message'=>'数据库操作错误',
                    'error' => $exception->getMessage()
                ];
                $this->apiResponse(200,'success',$data);
董瑞恩 authored
238
            }
董瑞恩 authored
239
            //生成订单
董瑞恩 authored
240 241
            $order=new OrderController();
            $res=$order->createOrder($users_id,$name);
董瑞恩 authored
242
            if ($res['state']==true){
董瑞恩 authored
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
                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);
            }
董瑞恩 authored
259
        }else if($key[3]=='0x00'){
董瑞恩 authored
260 261
            $data=[
                'state'=>0,
董瑞恩 authored
262 263
                'message'=>'开锁失败',
                'error' =>''
董瑞恩 authored
264 265
            ];
            $this->apiResponse(200,'success',$data);
董瑞恩 authored
266 267 268
        }
    }
    //关锁
董瑞恩 authored
269
    public function lock_deal_with($users_id,$name,$key){
董瑞恩 authored
270 271 272
        //查看数据库中设备状态
        $use=Db::name('equipment')->where('name',$name)->find()['use'];
        if ($use==0){
董瑞恩 authored
273 274
            $data=[
                'state'=>0,
董瑞恩 authored
275 276
                'message'=>'该设备为关闭状态,不能还锁',
                'error' =>''
董瑞恩 authored
277 278
            ];
            $this->apiResponse(200,'success',$data);
董瑞恩 authored
279 280 281 282 283 284 285
        }
        //电量过低,修改数据库状态
        if ($key[5]=='0x01'){
            Db::name('equipment')->where('name',$name)->update(['electricity'=>0]);
        }
        if ($key[6]=='0x01'){
            //修改数据库设备状态
董瑞恩 authored
286 287
            $data=[
                'state'=>0,
董瑞恩 authored
288 289
                'message'=>'未还床',
                'error' =>''
董瑞恩 authored
290 291
            ];
            $this->apiResponse(200,'success',$data);
董瑞恩 authored
292 293
        }else if($key[6]=='0x00'){
            //修改数据库设备状态
董瑞恩 authored
294 295
            $order=new OrderController();
            $order->order($users_id,$name);
董瑞恩 authored
296 297 298
        }
    }
董瑞恩 authored
299
    /**
董瑞恩 authored
300 301
     * @title 设备报修
     * @description 设备报修
董瑞恩 authored
302 303 304 305 306
     * @author 董瑞恩
     * @url /portal/Aes/addFault
     * @method GET
     *
     * @param name:users_id type:String require:1 default:无 other: desc:用户id
董瑞恩 authored
307
     * @param name:eq_name type:String require:1 default:无 other: desc:设备名称
董瑞恩 authored
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
     * @param name:fault_type type:String require:1 default:无 other: desc:错误类型
     * @param name:fault_describe type:String require:1 default:无 other: desc:错误描述
     * @param name:img type:String require:1 default:无 other: desc:图片地址
     *
     * @return state:1成功  0失败
     * @return message:错误类型
     * @return error:错误描述
     */
    public function addFault(){
        $param=$this->request->param();
        $param['create_time']=time();
        try{
            Db::name('fault')->insert($param);
        }catch (\Exception $exception){
            $this->apiResponse(301,$exception->getMessage());
        }
        $this->apiResponse(200,'success');
    }

    /**
     * @title 上传图片
     * @description 上传图片
     * @author 董瑞恩
     * @url /portal/Aes/upload
     * @method GET
     *
     * @param name:file type:file require:1 default:无 other: desc:图片
     *
     * @return state:1成功  0失败
     * @return message:错误类型
     * @return error:错误描述
     */
    public function upload(){
        $file=$this->request->file('file');
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload' . DS . 'portal');
            $fileName = $info->getSaveName();
            $path=str_replace('\\','/',$fileName);
            $data=array();
            $data['filename']=cmf_get_image_preview_url('portal/'.$path);
            $data['name']='portal/'.$path;
            if ($info) {
                $this->apiResponse(200,'success',$data);
            } else {
                $this->apiResponse(301,'error');
            }
        }
    }
董瑞恩 authored
356
董瑞恩 authored
357 358 359 360 361
    //转成数组
    private function ToArray($data){
        $array = array();
        for ($i = 0; $i < strlen($data); $i += 2) {
            $array[]='0x'.substr($data, $i, 2);
董瑞恩 authored
362
        }
董瑞恩 authored
363
        return $array;
董瑞恩 authored
364 365 366
    }

}