|
@@ -4,6 +4,7 @@ namespace Dcat\Admin\Http;
|
|
|
|
|
|
use Dcat\Admin\Exception\AdminException;
|
|
|
use Dcat\Admin\Support\Helper;
|
|
|
+use Illuminate\Contracts\Support\Arrayable;
|
|
|
use Illuminate\Contracts\Validation\Validator;
|
|
|
use Illuminate\Support\MessageBag;
|
|
|
use Illuminate\Support\Str;
|
|
@@ -30,7 +31,7 @@ use Illuminate\Validation\ValidationException;
|
|
|
* @method $this withValidationIf($condition, $errors)
|
|
|
* @method $this withExceptionIf($condition, \Throwable $e)
|
|
|
*/
|
|
|
-class JsonResponse
|
|
|
+class JsonResponse implements Arrayable
|
|
|
{
|
|
|
protected $status = true;
|
|
|
protected $statusCode = 200;
|
|
@@ -39,6 +40,11 @@ class JsonResponse
|
|
|
protected $html;
|
|
|
protected $options = [];
|
|
|
|
|
|
+ public function __construct(array $data = [])
|
|
|
+ {
|
|
|
+ $this->data($data);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 设置请求结果是否成功.
|
|
|
*
|
|
@@ -90,6 +96,8 @@ class JsonResponse
|
|
|
*/
|
|
|
public function success(?string $message)
|
|
|
{
|
|
|
+ $this->status(true);
|
|
|
+
|
|
|
return $this->show('success', $message);
|
|
|
}
|
|
|
|
|
@@ -359,8 +367,7 @@ class JsonResponse
|
|
|
sprintf('[%s] %s', get_class($exception), $exception->getMessage())
|
|
|
);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Flash a piece of data to the session.
|
|
|
*
|
|
@@ -378,13 +385,11 @@ class JsonResponse
|
|
|
|
|
|
return $this;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
|
|
|
/**
|
|
|
- * @return \Illuminate\Http\JsonResponse
|
|
|
+ * @return array
|
|
|
*/
|
|
|
- public function send()
|
|
|
+ public function toArray()
|
|
|
{
|
|
|
$data = ['status' => $this->status, 'data' => $this->data];
|
|
|
|
|
@@ -392,7 +397,15 @@ class JsonResponse
|
|
|
$data['html'] = Helper::render($this->html);
|
|
|
}
|
|
|
|
|
|
- return response()->json($this->options + $data, $this->statusCode);
|
|
|
+ return $data + $this->options;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
+ */
|
|
|
+ public function send()
|
|
|
+ {
|
|
|
+ return response()->json($this->toArray(), $this->statusCode);
|
|
|
}
|
|
|
|
|
|
public function __call($method, $arguments)
|