Browse Source

关于find查询为null的完美处理

lizhen_gitee 3 years ago
parent
commit
0cc4346659
2 changed files with 13 additions and 4 deletions
  1. 2 4
      application/api/controller/Baseconfig.php
  2. 11 0
      application/common/controller/Api.php

+ 2 - 4
application/api/controller/Baseconfig.php

@@ -32,11 +32,9 @@ class Baseconfig extends Api
     //启动广告图
     public function start_advert(){
         $info = Db::name('start_advert')->where('is_show',1)->order('id desc')->find();
-        if(empty($info)){
-            $this->success('success',(object)[]);
-        }
+
         $info = info_domain_image($info,['image']);
-        $this->success('success',$info);
+        $this->success_find('success',$info);
     }
 
     //个人资料的一下枚举

+ 11 - 0
application/common/controller/Api.php

@@ -178,6 +178,17 @@ class Api
         }
         $this->result($msg, $data, $code, $type, $header);
     }
+    //find查询出来的结果如果为空数组,强制转换object
+    protected function success_find($msg = '', $data = null, $code = 1, $type = null, array $header = [])
+    {
+        if(empty($msg)){
+            $msg = '操作成功';
+        }
+        if(is_null($data) || $data === []){
+            $data = (object)[];
+        }
+        $this->result($msg, $data, $code, $type, $header);
+    }
 
     /**
      * 操作失败返回的数据