Browse Source

路由组

afa 5 months ago
parent
commit
433d50d10b
3 changed files with 26 additions and 9 deletions
  1. 10 5
      app/api/middleware/AllowCrossDomain.php
  2. 14 4
      app/api/route/route.php
  3. 2 0
      app/middleware.php

+ 10 - 5
app/api/middleware/AllowCrossDomain.php

@@ -6,10 +6,15 @@ class AllowCrossDomain{
     
     public function handle($request, \Closure $next)
     {
-        header('Access-Control-Allow-Credentials: true');
-        header('Access-Control-Allow-Origin: *');
-        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Token");
-        header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
-        return $next($request);
+        header('Access-Control-Allow-Origin: *'); // 或者指定具体域名  
+        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');  
+        header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');  
+        header('Access-Control-Allow-Credentials: true');  
+    
+        if ($request->method() === 'OPTIONS') {  
+            return response()->code(204);  
+        }  
+    
+        return $next($request);  
     }
 }

+ 14 - 4
app/api/route/route.php

@@ -3,7 +3,17 @@ declare(strict_types=1);
 
 use think\facade\Route;
 
-Route::rule('test','test/index','GET|POST');
-Route::rule('withdraw','transaction/withdraw','GET|POST');
-Route::rule('getTxhashDetail','transaction/getTxhashDetail','GET|POST');
-Route::rule('createAddress','transaction/createAddress','GET|POST');
+
+// 定义 API 路由组
+Route::group('user', function () {
+
+      Route::rule('test','test/index','GET|POST');
+      Route::rule('withdraw','transaction/withdraw','GET|POST');
+      Route::rule('getTxhashDetail','transaction/getTxhashDetail','GET|POST');
+      Route::rule('createAddress','transaction/createAddress','GET|POST');
+
+
+})->middleware(\app\api\middleware\AllowCrossDomain::class);
+
+
+

+ 2 - 0
app/middleware.php

@@ -6,4 +6,6 @@ return [
     app\common\middleware\IpCheck::class,
     //应用结束
     app\common\middleware\EndApp::class,
+
+    \app\api\middleware\AllowCrossDomain::class,
 ];