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

interface

@@ -127,16 +127,16 @@ class AesController extends HomeBaseController{ @@ -127,16 +127,16 @@ class AesController extends HomeBaseController{
127 * @url /portal/Aes/getState 127 * @url /portal/Aes/getState
128 * @method GET 128 * @method GET
129 * 129 *
130 - * @param name:MACAddress type:String require:1 default:无 other: desc:设备MAC地址 130 + * @param name:name type:String require:1 default:无 other: desc:设备名称
131 * 131 *
132 * 132 *
133 * @return lockKey:获取电压、开关状态的指令(数组) 133 * @return lockKey:获取电压、开关状态的指令(数组)
134 */ 134 */
135 135
136 public function getState(){ 136 public function getState(){
137 - $MACAddress=$this->request->param('MACAddress');  
138 - $strKey=Db::name('equipment')->where('mac_address',$MACAddress)->find();  
139 - if (!empty($strKey)){ 137 + $name=$this->request->param('name');
  138 + $equipment=Db::name('equipment')->where('mac_address',$name)->find();
  139 + if (!empty($equipment)){
140 $key="ff05a301ef"; 140 $key="ff05a301ef";
141 $StateKey=$this->ToArray($key); 141 $StateKey=$this->ToArray($key);
142 $this->apiResponse(200,'success',$StateKey); 142 $this->apiResponse(200,'success',$StateKey);
@@ -180,24 +180,26 @@ class AesController extends HomeBaseController{ @@ -180,24 +180,26 @@ class AesController extends HomeBaseController{
180 * @url /portal/Aes/decryption 180 * @url /portal/Aes/decryption
181 * @method GET 181 * @method GET
182 * 182 *
  183 + * @param name:users_id type:String require:1 default:无 other: desc:用户id
183 * @param name:name type:String require:1 default:无 other: desc:设备名称 184 * @param name:name type:String require:1 default:无 other: desc:设备名称
184 * @param name:key type:String require:1 default:无 other: desc:设备返回状态(未加密) 185 * @param name:key type:String require:1 default:无 other: desc:设备返回状态(未加密)
185 * @param name:type type:String require:1 default:无 other:1开锁返回指令,2状态返回指令 desc:命令类型 186 * @param name:type type:String require:1 default:无 other:1开锁返回指令,2状态返回指令 desc:命令类型
186 * 187 *
187 * @return code:1锁已开,0位锁已关,2为数据异常 188 * @return code:1锁已开,0位锁已关,2为数据异常
  189 + * @return name:设备锁名称
188 */ 190 */
189 - public function state($name,$key,$type){ 191 + public function state($users_id,$name,$key,$type){
190 if ($type==1){ 192 if ($type==1){
191 //判断命令是否合法 193 //判断命令是否合法
192 if($key[0]=='0xff' && $key[4]=='0xef'){ 194 if($key[0]=='0xff' && $key[4]=='0xef'){
193 - $this->open_deal_with($name,$key); 195 + $this->open_deal_with($users_id,$name,$key);
194 }else{ 196 }else{
195 $this->apiResponse(2,'返回指令错误'); 197 $this->apiResponse(2,'返回指令错误');
196 } 198 }
197 }else if($type==2){ 199 }else if($type==2){
198 //判断命令是否合法 200 //判断命令是否合法
199 if($key[0]=='0xff' && $key[7]=='0xef'){ 201 if($key[0]=='0xff' && $key[7]=='0xef'){
200 - $this->lock_deal_with($name,$key); 202 + $this->lock_deal_with($users_id,$name,$key);
201 }else{ 203 }else{
202 $this->apiResponse(2,'返回指令错误'); 204 $this->apiResponse(2,'返回指令错误');
203 } 205 }
@@ -205,29 +207,33 @@ class AesController extends HomeBaseController{ @@ -205,29 +207,33 @@ class AesController extends HomeBaseController{
205 } 207 }
206 208
207 //开锁状态处理 209 //开锁状态处理
208 - public function open_deal_with($name,$key){  
209 - $use=Db::name('equipment')->where('name',$name)->find()['use'];  
210 - if ($use==1){  
211 - $this->apiResponse(303,'此锁已被使用');  
212 - } 210 + public function open_deal_with($users_id,$name,$key){
213 if ($key[3]=='0x01'){ 211 if ($key[3]=='0x01'){
214 //修改数据库设备状态 212 //修改数据库设备状态
215 try{ 213 try{
  214 + Db::startTrans();
  215 + //用户状态
  216 + Db::name('users')->where('id',$users_id)->update(['is_use'=>1]);
216 Db::name('equipment')->where('name',$name)->update(['use'=>1]); 217 Db::name('equipment')->where('name',$name)->update(['use'=>1]);
217 }catch (\Exception $exception){ 218 }catch (\Exception $exception){
  219 + Db::rollback();
218 $this->apiResponse(301,'数据库状态更改失败,错误信息:'.$exception->getMessage()); 220 $this->apiResponse(301,'数据库状态更改失败,错误信息:'.$exception->getMessage());
219 } 221 }
220 - $this->apiResponse(200,'锁已开'); 222 + Db::commit();
  223 + //生成订单
  224 +
  225 +
  226 + $this->apiResponse(200,'锁已开',['设备名称'=>$name]);
221 }else if($key[3]=='0x00'){ 227 }else if($key[3]=='0x00'){
222 - $this->apiResponse(302,'锁未开'); 228 + $this->apiResponse(302,'锁未开',['设备名称'=>$name]);
223 } 229 }
224 } 230 }
225 //关锁 231 //关锁
226 - public function lock_deal_with($name,$key){ 232 + public function lock_deal_with($users_id,$name,$key){
227 //查看数据库中设备状态 233 //查看数据库中设备状态
228 $use=Db::name('equipment')->where('name',$name)->find()['use']; 234 $use=Db::name('equipment')->where('name',$name)->find()['use'];
229 if ($use==0){ 235 if ($use==0){
230 - $this->apiResponse(303,'此锁未被使用'); 236 + $this->apiResponse(303,'此锁未打开,还床失败',['设备名称'=>$name]);
231 } 237 }
232 //电量过低,修改数据库状态 238 //电量过低,修改数据库状态
233 if ($key[5]=='0x01'){ 239 if ($key[5]=='0x01'){
@@ -235,15 +241,20 @@ class AesController extends HomeBaseController{ @@ -235,15 +241,20 @@ class AesController extends HomeBaseController{
235 } 241 }
236 if ($key[6]=='0x01'){ 242 if ($key[6]=='0x01'){
237 //修改数据库设备状态 243 //修改数据库设备状态
238 - $this->apiResponse(301,'锁状态:开锁');//开着 244 + $this->apiResponse(301,'锁未关,当前锁状态为:开锁',['设备名称'=>$name]);//开着
239 }else if($key[6]=='0x00'){ 245 }else if($key[6]=='0x00'){
240 //修改数据库设备状态 246 //修改数据库设备状态
241 try{ 247 try{
  248 + Db::startTrans();
  249 + Db::name('users')->where('id',$users_id)->update(['is_use'=>0]);
242 Db::name('equipment')->where('name',$name)->update(['use'=>0]); 250 Db::name('equipment')->where('name',$name)->update(['use'=>0]);
243 }catch (\Exception $exception){ 251 }catch (\Exception $exception){
  252 + Db::rollback();
244 $this->apiResponse(302,'数据库状态更改失败,错误信息:'.$exception->getMessage()); 253 $this->apiResponse(302,'数据库状态更改失败,错误信息:'.$exception->getMessage());
245 } 254 }
246 - $this->apiResponse(200,'关锁成功');//关了 255 + Db::commit();
  256 +
  257 + $this->apiResponse(200,'关锁成功',['设备名称'=>$name]);//关了
247 } 258 }
248 } 259 }
249 260
@@ -38,25 +38,27 @@ class UsersController extends HomeBaseController{ @@ -38,25 +38,27 @@ class UsersController extends HomeBaseController{
38 * @method GET 38 * @method GET
39 * 39 *
40 * @param name:users_id type:String require:1 default:无 other: desc:用户id 40 * @param name:users_id type:String require:1 default:无 other: desc:用户id
  41 + *
  42 + * @return is_use:是否在使用设备
  43 + * @return is_deposit:是否交付押金
  44 + * @return is_order:是否有未支付订单
41 */ 45 */
42 public function lock_check(){ 46 public function lock_check(){
43 $users_id=$this->request->param('users_id'); 47 $users_id=$this->request->param('users_id');
44 //获取提交押金的状态 48 //获取提交押金的状态
45 $users=Db::name('users')->where('id',$users_id)->find(); 49 $users=Db::name('users')->where('id',$users_id)->find();
46 - if ($users['is_deposit']===1){  
47 - if ($users['is_use']==1){  
48 - $this->apiResponse(303,'用户正在使用设备');  
49 - }else{ 50 + $data=[
  51 + 'is_use'=>$users['use'],
  52 + 'is_deposit' => $users['is_deposit']
  53 + ];
50 $order=Db::name('order')->where(['users_id'=>$users_id,'state'=>2])->find(); 54 $order=Db::name('order')->where(['users_id'=>$users_id,'state'=>2])->find();
51 if (empty($order)){ 55 if (empty($order)){
52 - $this->apiResponse(200,'验证通过'); 56 + $data['is_order']=0;
53 }else{ 57 }else{
54 - $this->apiResponse(302,'有未支付订单');  
55 - }  
56 - }  
57 - }else{  
58 - $this->apiResponse(301,'未支付押金'); 58 + $data['is_order']=1;
59 } 59 }
  60 + $this->apiResponse(200,'success',$data);
  61 +
60 } 62 }
61 63
62 /** 64 /**