Browse Source

即时更新csrf token

jqh 5 years ago
parent
commit
1dcea7f4b0
3 changed files with 26 additions and 8 deletions
  1. 1 1
      resources/views/partials/script.blade.php
  2. 7 5
      src/Grid/Filter.php
  3. 18 2
      src/Middleware/Bootstrap.php

+ 1 - 1
resources/views/partials/script.blade.php

@@ -1,6 +1,6 @@
 <script>
     function LA() {}
-    LA.token = "{{ csrf_token() }}";
+
     LA.lang = {!! json_encode(trans('admin.client') ?: []) !!};
 
     LA.components = {booting:[]};

+ 7 - 5
src/Grid/Filter.php

@@ -127,7 +127,7 @@ class Filter implements Renderable
     /**
      * @var string
      */
-    protected $filterID = 'filter-box';
+    protected $filterID;
 
     /**
      * @var string
@@ -219,7 +219,7 @@ class Filter implements Renderable
      */
     protected function generateFilterId()
     {
-        return 'filter-box-'.Str::random(8);
+        return $this->model->grid()->getName().'-filter-box';
     }
 
     /**
@@ -559,11 +559,13 @@ class Filter implements Renderable
     /**
      * Expand filter container.
      *
+     * @param bool $value
+     *
      * @return $this
      */
-    public function expand()
+    public function expand(bool $value = true)
     {
-        $this->expand = true;
+        $this->expand = $value;
 
         return $this;
     }
@@ -575,7 +577,7 @@ class Filter implements Renderable
      *
      * @return array|Collection|mixed
      */
-    public function execute($toArray = true)
+    public function execute(bool $toArray = true)
     {
         $conditions = array_merge(
             $this->conditions(),

+ 18 - 2
src/Middleware/Bootstrap.php

@@ -8,11 +8,29 @@ use Illuminate\Http\Request;
 class Bootstrap
 {
     public function handle(Request $request, \Closure $next)
+    {
+        $this->includeBootstrapFile();
+        $this->setupScript();
+        $this->fireEvents();
+
+        return $next($request);
+    }
+
+    protected function includeBootstrapFile()
     {
         if (is_file($bootstrap = admin_path('bootstrap.php'))) {
             require $bootstrap;
         }
+    }
 
+    protected function setupScript()
+    {
+        $token = csrf_token();
+        Admin::script("LA.token = \"$token\";");
+    }
+
+    protected function fireEvents()
+    {
         Admin::callBooting();
 
         if (config('admin.cdn')) {
@@ -20,7 +38,5 @@ class Bootstrap
         }
 
         Admin::callBooted();
-
-        return $next($request);
     }
 }