Browse Source

修复树状表格编辑后跳转回列表url参数异常问题

jqh 5 years ago
parent
commit
1b8449cb67
3 changed files with 41 additions and 1 deletions
  1. 22 0
      src/Admin.php
  2. 14 0
      src/Grid/Concerns/HasTree.php
  3. 5 1
      src/Middleware/Bootstrap.php

+ 22 - 0
src/Admin.php

@@ -443,6 +443,28 @@ class Admin
         return app('admin.context');
     }
 
+    /**
+     * @param array|string $name
+     *
+     * @return void
+     */
+    public static function addIgnoreQueryName($name)
+    {
+        $context = static::context();
+
+        $ignoreQueries = $context->ignoreQueries ?? [];
+
+        $context->ignoreQueries = array_merge($ignoreQueries, (array) $name);
+    }
+
+    /**
+     * @return array
+     */
+    public static function getIgnoreQueryNames()
+    {
+        return static::context()->ignoreQueries ?? [];
+    }
+
     /**
      * 获取js配置.
      *

+ 14 - 0
src/Grid/Concerns/HasTree.php

@@ -54,6 +54,8 @@ trait HasTree
                     $this->getChildrenPageName($this->getParentIdFromRequest())
                 );
             }
+
+            $this->addIgnoreQueries();
         });
 
         $this->collection(function (Collection $collection) {
@@ -71,6 +73,18 @@ trait HasTree
         });
     }
 
+    /**
+     * 设置保存为"前一个页面地址"时需要忽略的参数.
+     */
+    protected function addIgnoreQueries()
+    {
+        Admin::addIgnoreQueryName([
+            $this->getParentIdQueryName(),
+            $this->getTierQueryName(),
+            $this->getChildrenPageName($this->getParentIdFromRequest()),
+        ]);
+    }
+
     /**
      * 禁止树形表格查询.
      *

+ 5 - 1
src/Middleware/Bootstrap.php

@@ -54,7 +54,11 @@ class Bootstrap
             && ! Helper::isAjaxRequest()
             && ! $this->prefetch($request)
         ) {
-            Helper::setPreviousUrl($request->fullUrl());
+            Admin::addIgnoreQueryName(['_token', '_pjax']);
+
+            Helper::setPreviousUrl(
+                Helper::fullUrlWithoutQuery(Admin::getIgnoreQueryNames())
+            );
         }
     }