| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- <?php
- 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;
- 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 = null)
- * @method $this refreshIf($condition)
- * @method $this downloadIf($condition, ?string $url)
- * @method $this scriptIf($condition, ?string $script)
- * @method $this alertIf($condition, bool $alert = true)
- * @method $this htmlIf($condition, $html)
- * @method $this dataIf($condition, array $data)
- * @method $this optionsIf($condition, array $data)
- * @method $this withValidationIf($condition, $errors)
- * @method $this withExceptionIf($condition, \Throwable $e)
- */
- class JsonResponse implements Arrayable
- {
- protected $status = true;
- protected $statusCode = 200;
- protected $exception;
- protected $data = [];
- protected $html;
- protected $options = [];
- public function __construct(array $data = [])
- {
- $this->data($data);
- }
- /**
- * 设置请求结果是否成功.
- *
- * @param bool $status
- *
- * @return $this
- */
- public function status(bool $status)
- {
- $this->status = $status;
- return $this;
- }
- /**
- * 设置 HTTP 状态码.
- *
- * @param int $statusCode
- *
- * @return $this
- */
- public function statusCode(int $statusCode)
- {
- $this->statusCode = $statusCode;
- return $this;
- }
- /**
- * 设置提示信息.
- *
- * @param string $message
- *
- * @return $this
- */
- public function message(?string $message)
- {
- $this->data['message'] = $message;
- return $this;
- }
- /**
- * 显示 成功 提示弹窗.
- *
- * @param string $message
- *
- * @return $this
- */
- public function success(?string $message)
- {
- $this->status(true);
- return $this->show('success', $message);
- }
- /**
- * @param string $message
- *
- * @return $this
- */
- public function info(?string $message)
- {
- return $this->show('info', $message);
- }
- /**
- * @param string $message
- *
- * @return $this
- */
- public function warning(?string $message)
- {
- return $this->show('warning', $message);
- }
- /**
- * 显示 错误 信息弹窗.
- *
- * @param string $message
- * @param bool $alert
- *
- * @return $this
- */
- public function error(?string $message)
- {
- $this->status(false);
- return $this->show('error', $message);
- }
- /**
- * 设置 toastr 显示时长.
- *
- * @param $seconds
- *
- * @return $this
- */
- public function timeout($seconds)
- {
- return $this->data(['timeout' => $seconds]);
- }
- /**
- * 显示确认弹窗.
- *
- * @param bool $alert
- *
- * @return $this
- */
- public function alert(bool $alert = true)
- {
- return $this->data(['alert' => $alert]);
- }
- /**
- * 显示弹窗描述信息.
- *
- * @param string $detail
- *
- * @return $this
- */
- public function detail(?string $detail)
- {
- return $this->data(['detail' => $detail]);
- }
- /**
- * 显示弹窗信息.
- *
- * @param string $type
- * @param string $message
- *
- * @return $this
- */
- protected function show(?string $type, ?string $message = null)
- {
- if ($message) {
- $this->message($message);
- }
- return $this->data(['type' => $type]);
- }
- /**
- * 跳转.
- *
- * @param string $url
- *
- * @return $this
- */
- public function redirect(?string $url)
- {
- return $this->then(['action' => 'redirect', 'value' => admin_url($url)]);
- }
- /**
- * @param string|null $url
- *
- * @return $this
- */
- public function redirectToIntended(?string $url)
- {
- $path = session()->pull('url.intended');
- return $this->redirect($path ?: $url);
- }
- /**
- * Location 跳转.
- *
- * @param string $location 不传则刷新当前页面
- *
- * @return $this
- */
- public function location(?string $location = null)
- {
- return $this->then(['action' => 'location', 'value' => $location ? admin_url($location) : null]);
- }
- /**
- * @param string|null $url
- *
- * @return $this
- */
- public function locationToIntended(?string $url)
- {
- $path = session()->pull('url.intended');
- return $this->location($path ?: $url);
- }
- /**
- * 下载.
- *
- * @param string $url
- *
- * @return $this
- */
- public function download($url)
- {
- return $this->then(['action' => 'download', 'value' => admin_url($url)]);
- }
- /**
- * 刷新页面.
- *
- * @return $this
- */
- public function refresh()
- {
- return $this->then(['action' => 'refresh', 'value' => true]);
- }
- /**
- * 执行JS代码.
- *
- * @param string $script
- *
- * @return $this
- */
- public function script($script)
- {
- return $this->then(['action' => 'script', 'value' => $script]);
- }
- /**
- * @param array $value
- *
- * @return $this
- */
- protected function then(array $value)
- {
- $this->data['then'] = $value;
- return $this;
- }
- /**
- * 设置返回数据.
- *
- * @param array $value
- *
- * @return $this
- */
- public function data(array $value)
- {
- $this->data = array_merge($this->data, $value);
- return $this;
- }
- /**
- * 返回 HTML.
- *
- * @param string $html
- *
- * @return $this
- */
- public function html($html)
- {
- $this->html = $html;
- return $this;
- }
- /**
- * 设置其他字段.
- *
- * @param array $options
- *
- * @return $this
- */
- public function options(array $options)
- {
- $this->options = array_merge($this->options, $options);
- return $this;
- }
- /**
- * 设置字段验证错误信息.
- *
- * @param $errors
- *
- * @return $this
- */
- public function withValidation($errors)
- {
- if ($errors instanceof Validator) {
- $errors = $errors->errors();
- }
- if ($errors instanceof MessageBag) {
- $errors = $errors->getMessages();
- }
- return $this
- ->status(false)
- ->statusCode(422)
- ->options(['errors' => $errors]);
- }
- /**
- * 响应异常.
- *
- * @param \Throwable $exception
- *
- * @return $this
- */
- public function withException(\Throwable $exception)
- {
- if ($exception instanceof ValidationException) {
- return $this->withValidation($exception->errors());
- }
- return $this
- ->status(false)
- ->error(
- sprintf('[%s] %s', get_class($exception), $exception->getMessage())
- );
- }
- /**
- * Flash a piece of data to the session.
- *
- * @param string|array $key
- * @param mixed $value
- * @return $this
- */
- public function with($key, $value = null)
- {
- $key = is_array($key) ? $key : [$key => $value];
- foreach ($key as $k => $v) {
- session()->flash($k, $v);
- }
- return $this;
- }
- /**
- * @return array
- */
- public function toArray()
- {
- $data = ['status' => $this->status, 'data' => $this->data];
- if ($this->html) {
- $data['html'] = Helper::render($this->html);
- }
- return $data + $this->options;
- }
- /**
- * @return \Illuminate\Http\JsonResponse
- */
- public function send()
- {
- return response()->json($this->toArray(), $this->statusCode);
- }
- public function __call($method, $arguments)
- {
- if (Str::endsWith($method, 'If')) {
- if ($arguments) {
- $method = Str::replaceLast('If', '', $method);
- $condition = value(array_shift($arguments));
- return $condition ? $this->$method(...$arguments) : $this;
- }
- }
- throw new AdminException(sprintf('Call to undefined method "%s"', $method));
- }
- /**
- * @param mixed ...$params
- *
- * @return $this
- */
- public static function make(...$params)
- {
- return new static(...$params);
- }
- }
|