浏览代码

增加注册中间件功能;
扩展设置表单增加标题设置方法;
修复JSONRESPONSE method if无效问题

jqh 4 年之前
父节点
当前提交
256220ec46

+ 39 - 0
src/Admin.php

@@ -21,6 +21,7 @@ use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Event;
+use Illuminate\Support\Facades\Route;
 use Illuminate\Support\Str;
 
 class Admin
@@ -361,6 +362,44 @@ class Admin
         return Composer::loader();
     }
 
+    /**
+     * 往分组插入中间件.
+     *
+     * @param array $mix
+     */
+    public static function mixMiddlewareGroup(array $mix = [])
+    {
+        $router = app('router');
+
+        $group = $router->getMiddlewareGroups()['admin'] ?? [];
+
+        if ($mix) {
+            $finalGroup = [];
+
+            foreach ($group as $i => $mid) {
+                $next = $i + 1;
+
+                $finalGroup[] = $mid;
+
+                if (! isset($group[$next]) || $group[$next] !== 'admin.permission') {
+                    continue;
+                }
+
+                $finalGroup = array_merge($finalGroup, $mix);
+
+                $mix = [];
+            }
+
+            if ($mix) {
+                $finalGroup = array_merge($finalGroup, $mix);
+            }
+
+            $group = $finalGroup;
+        }
+
+        $router->middlewareGroup('admin', $group);
+    }
+
     /**
      * 获取js配置.
      *

+ 8 - 0
src/Extend/ServiceProvider.php

@@ -347,13 +347,21 @@ abstract class ServiceProvider extends LaravelServiceProvider
         $middleware = (array) config('admin.route.middleware');
 
         $before = $this->middleware['before'] ?? [];
+        $middle = $this->middleware['middle'] ?? [];
         $after = $this->middleware['after'] ?? [];
 
+        $this->mixMiddleware($middle);
+
         config([
             'admin.route.middleware' => array_merge((array) $before, $middleware, (array) $after),
         ]);
     }
 
+    protected function mixMiddleware(array $middle)
+    {
+        Admin::mixMiddlewareGroup($middle);
+    }
+
     /**
      * 配置需要跳过权限认证和登录认证的路由.
      */

+ 9 - 0
src/Extend/Setting.php

@@ -65,6 +65,15 @@ abstract class Setting extends Form implements LazyRenderable
      */
     abstract public function form();
 
+    /**
+     * 弹窗标题.
+     *
+     * @return string
+     */
+    public function title()
+    {
+    }
+
     /**
      * 填充表单数据.
      *

+ 7 - 1
src/Http/Displayers/Extensions/Description.php

@@ -31,11 +31,17 @@ class Description extends AbstractDisplayer
 
         return Modal::make()
             ->lg()
-            ->title(trans('admin.setting').' - '.$this->getKey())
+            ->title($this->getModalTitle($extension))
             ->body($extension->settingForm())
             ->button($label);
     }
 
+    protected function getModalTitle($extension)
+    {
+        return $extension->settingForm()->title()
+            ?: (trans('admin.setting').' - '.str_replace('.', '/', $this->getKey()));
+    }
+
     protected function resolveAction($action)
     {
         $action = new $action();

+ 9 - 1
src/Http/JsonResponse.php

@@ -12,6 +12,12 @@ use Illuminate\Validation\ValidationException;
 /**
  * Class JsonResponse.
  *
+ * @method $this successIf($condition, ?string $message)
+ * @method $this errorIf($condition, ?string $message)
+ * @method $this warningIf($condition, ?string $message)
+ * @method $this infoIf($condition, ?string $message)
+ * @method $this detailIf($condition, ?string $message)
+ * @method $this statusCodeIf($condition, int $code)
  * @method $this redirectIf($condition, ?string $url)
  * @method $this locationIf($condition, ?string $url)
  * @method $this refreshIf($condition)
@@ -374,7 +380,9 @@ class JsonResponse
             if (array_shift($arguments)) {
                 $method = Str::replaceLast('If', '', $method);
 
-                return $this->$method(...$arguments);
+                $condition = value(array_shift($arguments));
+
+                return $condition ? $this->$method(...$arguments) : $this;
             }
         }