| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <?php
- namespace Dcat\Admin\Traits;
- use Dcat\Admin\Support\Helper;
- use Illuminate\Http\Request;
- /**
- * Trait InteractsWithApi.
- *
- *
- * @method mixed handle(Request $request)
- * @method mixed valueResult()
- */
- trait InteractsWithApi
- {
- use HasAuthorization;
- /**
- * @var string
- */
- protected $url;
- /**
- * @var string
- */
- protected $method = 'POST';
- /**
- * @var string
- */
- protected $uriKey;
- /**
- * @var array
- */
- protected $requestSelectors = [];
- /**
- * @var array
- */
- protected $requestScripts = [
- 'fetching' => [],
- 'fetched' => [],
- ];
- /**
- * 获取请求附带参数.
- *
- * @return array
- */
- public function parameters(): array
- {
- return [];
- }
- /**
- * 设置请求地址.
- *
- * @param string $method
- * @param string $url
- * @param array $query
- *
- * @return $this
- */
- public function request(string $method, string $url, array $query = [])
- {
- $this->method = $method;
- $this->url = admin_url(Helper::urlWithQuery($url, $query));
- return $this;
- }
- /**
- * 获取请求地址
- *
- * @return string
- */
- public function getRequestUrl()
- {
- return $this->url ?: route('dcat.api.value');
- }
- /**
- * 获取请求方法.
- *
- * @return string
- */
- public function getRequestMethod()
- {
- return $this->method;
- }
- /**
- * 设置URI标识.
- *
- * @return string
- */
- public function getUriKey()
- {
- return $this->uriKey ?: static::class;
- }
- /**
- * 获取js代码.
- *
- * @return array
- */
- public function getRequestScripts()
- {
- return $this->requestScripts;
- }
- /**
- * 设置点击抓取数据的按钮的css选择器.
- *
- * @param string|array $selector
- *
- * @return $this
- */
- public function click($selector)
- {
- $this->requestSelectors =
- array_merge($this->requestSelectors, (array) $selector);
- return $this;
- }
- /**
- * @return array
- */
- public function getRequestSelectors()
- {
- return $this->requestSelectors;
- }
- /**
- * 设置抓取数据时执行的js代码.
- *
- * @param string|\Closure $script
- *
- * @return $this
- */
- public function fetching($script)
- {
- $this->requestScripts['fetching'][] = value($script);
- return $this;
- }
- /**
- * 设置抓取完数据后执行的js代码.
- *
- * @param string|\Closure $script
- *
- * @return $this
- */
- public function fetched($script)
- {
- $this->requestScripts['fetched'][] = value($script);
- return $this;
- }
- /**
- * 判断是否使用请求接口功能.
- *
- * @return bool
- */
- public function allowBuildRequest()
- {
- return (
- $this->url
- || method_exists($this, 'handle')
- ) ? true : false;
- }
- /**
- * 构建请求数据js代码.
- *
- * @return null|string
- */
- public function buildRequestScript()
- {
- if (! $this->allowBuildRequest()) {
- return;
- }
- $fetching = implode(';', $this->requestScripts['fetching']);
- $fetched = implode(';', $this->requestScripts['fetched']);
- return <<<JS
- (function () {
- var requesting;
- function request(data) {
- if (requesting) {
- return;
- }
- requesting = 1;
-
- data = $.extend({$this->formatRequestData()}, data || {});
-
- {$fetching};
-
- $.ajax({
- url: '{$this->getRequestUrl()}',
- dataType: 'json',
- method: '{$this->method}',
- data: $.extend({_token: Dcat.token}, data),
- success: function (response) {
- requesting = 0;
- {$fetched};
- },
- error: function (a, b, c) {
- requesting = 0;
- Dcat.handleAjaxError(a, b, c)
- },
- });
- }
- request();
- {$this->buildBindingScript()}
- })();
- JS;
- }
- /**
- * @return string
- */
- private function formatRequestData()
- {
- $data = [
- '_key' => $this->getUriKey(),
- ];
- return json_encode(
- array_merge($this->parameters(), $data)
- );
- }
- /**
- * @return string
- */
- private function buildBindingScript()
- {
- $script = '';
- foreach ($this->requestSelectors as $v) {
- $script .= <<<JS
- $('{$v}').on('click', function () {
- request($(this).data())
- });
- JS;
- }
- return $script;
- }
- /**
- * 合并.
- *
- * @param static $self
- *
- * @return $this
- */
- public function merge($self)
- {
- $this->url = $self->getRequestUrl();
- $this->method = $self->getRequestMethod();
- $this->uriKey = $self->getUriKey();
- $this->requestSelectors = $self->getRequestSelectors();
- $scripts = $self->getRequestScripts();
- $this->requestScripts['fetching'] = array_merge($this->requestScripts['fetching'], $scripts['fetching']);
- $this->requestScripts['fetched'] = array_merge($this->requestScripts['fetched'], $scripts['fetched']);
- return $this;
- }
- }
|