jqh hace 5 años
padre
commit
be0e17e5b9
Se han modificado 2 ficheros con 27 adiciones y 29 borrados
  1. 3 3
      src/Controllers/ValueController.php
  2. 24 26
      src/Traits/FromApi.php

+ 3 - 3
src/Controllers/ValueController.php

@@ -40,8 +40,8 @@ class ValueController
      */
     protected function resolve(Request $request)
     {
-        if (! $key = $request->has('_key')) {
-            throw new Exception('Invalid action request.');
+        if (! $key = $request->get('_key')) {
+            throw new Exception('Invalid request.');
         }
 
         if (! class_exists($key)) {
@@ -51,7 +51,7 @@ class ValueController
         $instance = app($key);
 
         if (! method_exists($instance, 'handle')) {
-            throw new Exception("The method '{$instance}::handle()' does not exist.");
+            throw new Exception("The method '{$key}::handle()' does not exist.");
         }
 
         return $instance;

+ 24 - 26
src/Traits/FromApi.php

@@ -10,6 +10,7 @@ use Illuminate\Http\Request;
  *
  * @package Dcat\Admin\Traits
  *
+ * @method mixed handle(Request $request)
  * @method mixed result()
  */
 trait FromApi
@@ -26,6 +27,11 @@ trait FromApi
      */
     protected $fromMethod = 'POST';
 
+    /**
+     * @var string
+     */
+    protected $fromUriKey;
+
     /**
      * @var array
      */
@@ -39,23 +45,12 @@ trait FromApi
         'fetched'  => [],
     ];
 
-    /**
-     * 处理请求
-     *
-     * @param \Illuminate\Http\Request $request
-     *
-     * @return mixed
-     */
-    public function handle(Request $request)
-    {
-    }
-
     /**
      * 获取请求附带参数.
      *
      * @return array
      */
-    public function parameters()
+    public function parameters(): array
     {
         return [];
     }
@@ -115,9 +110,9 @@ trait FromApi
      *
      * @return string
      */
-    public function requestUriKey()
+    public function getFromUriKey()
     {
-        return static::class;
+        return $this->fromUriKey ?: static::class;
     }
 
     /**
@@ -182,13 +177,16 @@ trait FromApi
     }
 
     /**
-     * 判断是否允许构建js代码
+     * 判断是否使用请求接口功能.
      *
      * @return bool
      */
-    public function allowBuildRequestScript()
+    public function allowBuildRequest()
     {
-        return $this->fromUrl === null ? false : true;
+        return (
+            $this->fromUrl
+            || method_exists($this, 'handle')
+        ) ? true : false;
     }
 
     /**
@@ -198,7 +196,7 @@ trait FromApi
      */
     public function buildRequestScript()
     {
-        if (! $this->allowBuildRequestScript()) {
+        if (! $this->allowBuildRequest()) {
             return null;
         }
 
@@ -247,7 +245,7 @@ JS;
     private function formatRequestData()
     {
         $data = [
-            '_key' => $this->requestUriKey(),
+            '_key' => $this->getFromUriKey(),
         ];
 
         return json_encode(
@@ -276,18 +274,18 @@ JS;
     /**
      * 合并.
      *
-     * @param static $fetcher
+     * @param static $self
      *
      * @return $this
      */
-    public function merge($fetcher)
+    public function merge($self)
     {
-        $this->fromUrl = $fetcher->getRequestUrl();
-        $this->fromMethod = $fetcher->getRequestMethod();
-
-        $this->fromSelectors = $fetcher->getFromSelectors();
+        $this->fromUrl = $self->getRequestUrl();
+        $this->fromMethod = $self->getRequestMethod();
+        $this->fromUriKey = $self->getFromUriKey();
+        $this->fromSelectors = $self->getFromSelectors();
 
-        $scripts = $fetcher->getRequestScripts();
+        $scripts = $self->getRequestScripts();
 
         $this->fromScripts['fetching'] = array_merge($this->fromScripts['fetching'], $scripts['fetching']);
         $this->fromScripts['fetched'] = array_merge($this->fromScripts['fetched'], $scripts['fetched']);