rkljw 1 nedēļu atpakaļ
vecāks
revīzija
821b538c92
1 mainītis faili ar 32 papildinājumiem un 20 dzēšanām
  1. 32 20
      app/Middleware/Auth/FooMiddleware.php

+ 32 - 20
app/Middleware/Auth/FooMiddleware.php

@@ -46,11 +46,14 @@ class FooMiddleware implements MiddlewareInterface
                 $tokenTime =  $this->JWT->getTokenDynamicCacheTime($header['token'][0]);
                 
                 if($tokenTime==0){
-                    return $this->response->raw(json_encode([
-                        'code' => -1,
-                        'data' => [],
-                        'message' => 'token无效,请重新登录'
-                    ]));
+                    $response = $this->response->withStatus(401)
+                        ->withHeader('Content-Type', 'application/json')
+                        ->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream(json_encode([
+                            'code' => -1,
+                            'data' => [],
+                            'message' => 'token无效,请重新登录'
+                        ])));
+                    return $response;
                 }
 
                 if($header['userurl'] && $header['userurl'][0]){
@@ -61,11 +64,14 @@ class FooMiddleware implements MiddlewareInterface
 
                     $result = $this->websiteServiceClient->getWebsiteId($data);
                     if(!isset($result['data']['id']) || !$result['data']['id']){
-                        return $this->response->raw(json_encode([
-                            'code' => -1,
-                            'data' => [],
-                            'message' => '网站不存在...'
-                        ]));
+                        $response = $this->response->withStatus(404)
+                            ->withHeader('Content-Type', 'application/json')
+                            ->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream(json_encode([
+                                'code' => -1,
+                                'data' => [],
+                                'message' => '网站不存在...'
+                            ])));
+                        return $response;
                     }
                     Context::set("SiteId",$result['data']['id']);
                 }
@@ -78,18 +84,24 @@ class FooMiddleware implements MiddlewareInterface
                 }
             }
 
-            return $this->response->raw(json_encode([
-                'code' => -1,
-                'data' => [],
-                'message' => 'token无效,请重新登录'
-            ]));
+            $response = $this->response->withStatus(401)
+                ->withHeader('Content-Type', 'application/json')
+                ->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream(json_encode([
+                    'code' => -1,
+                    'data' => [],
+                    'message' => 'token无效,请重新登录'
+                ])));
+            return $response;
 
         } catch (\Exception $e) {
-            return $this->response->raw(json_encode([
-                'code' => $e->getCode(),
-                'data' => [],
-                'message' => $e->getMessage()
-            ]));
+            $response = $this->response->withStatus(500)
+                ->withHeader('Content-Type', 'application/json')
+                ->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream(json_encode([
+                    'code' => $e->getCode(),
+                    'data' => [],
+                    'message' => $e->getMessage()
+                ])));
+            return $response;
         }
     }
 }