Parcourir la source

feat(core): 在 ExceptionHandle 中集成 CORS 处理
- 在 composer.json 中添加 think-cors 依赖

runphp il y a 6 mois
Parent
commit
5da816cd4b
2 fichiers modifiés avec 6 ajouts et 1 suppressions
  1. 1 0
      composer.json
  2. 5 1
      src/Exception/ExceptionHandle.php

+ 1 - 0
composer.json

@@ -31,6 +31,7 @@
     "topthink/think-queue": "^3.0.12",
     "topthink/think-view": "^2.0.5",
     "topthink/think-worker": "^5.0.2",
+    "topthink/think-cors": "^1.0.2",
     "opis/closure": "^4.3.1",
     "workerman/crontab": "^1.0.7"
   },

+ 5 - 1
src/Exception/ExceptionHandle.php

@@ -3,6 +3,7 @@ declare(strict_types=1);
 
 namespace SixShop\Core\Exception;
 
+use think\cors\HandleCors;
 use think\db\exception\ModelNotFoundException;
 use think\exception\Handle;
 use think\exception\ValidateException;
@@ -21,7 +22,10 @@ class ExceptionHandle extends Handle
         if ($e instanceof ValidateException) {
             $e = new LogicException(error_response(msg: $e->getMessage(), status: 'invalid_argument', code: 400, httpCode: 200));
         }
-        return parent::render($request, $e);
+        $response = parent::render($request, $e);
+        return $this->app->make(HandleCors::class)->handle($request, function () use ($request, $response) {
+            return $response;
+        });
     }
 
     protected function getDebugMsg(Throwable $exception): array