xieruidong 2 年 前
コミット
3b34bfd3ff
1 ファイル変更21 行追加7 行削除
  1. 21 7
      app/data/controller/api/Auth.php

+ 21 - 7
app/data/controller/api/Auth.php

@@ -42,6 +42,8 @@ abstract class Auth extends Controller
      */
     protected $user;
 
+    protected $noNeedLogin=[];
+
     /**
      * 控制器初始化
      */
@@ -56,9 +58,6 @@ abstract class Auth extends Controller
         // 读取用户数据
         $this->user = $this->getUser();
         $this->uuid = $this->user['id'] ?? '';
-        if (empty($this->uuid)) {
-            $this->error('用户登录失败!', '{-null-}', 401);
-        }
     }
 
     /**
@@ -70,11 +69,18 @@ abstract class Auth extends Controller
         try {
             if (empty($this->uuid)) {
                 $token = $this->request->header('api-token');
-                if (empty($token)) $this->error('登录认证不能为空!');
+                if(!in_array($this->app->request->action(),$this->getNoNeedLogin(),true)) {
+                    if (empty($token)) $this->error('登录认证不能为空!');
+                }
                 [$state, $info, $this->uuid] = UserTokenService::check($this->type, $token);
-                if (empty($state)) $this->error($info, '{-null-}', 401);
+                if(!in_array($this->app->request->action(),$this->getNoNeedLogin(),true)) {
+                    if (empty($state)) $this->error($info, '{-null-}', 401);
+                }
+            }
+            if($this->uuid) {
+                return UserAdminService::get($this->uuid, $this->type);
             }
-            return UserAdminService::get($this->uuid, $this->type);
+            return [];
         } catch (HttpResponseException $exception) {
             throw $exception;
         } catch (\Exception $exception) {
@@ -92,4 +98,12 @@ abstract class Auth extends Controller
             $this->error('账户已被冻结!');
         }
     }
-}
+
+    /**
+     * @return array
+     */
+    public function getNoNeedLogin(): array
+    {
+        return $this->noNeedLogin;
+    }
+}