InteractsWithApi.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace Dcat\Admin\Traits;
  3. use Dcat\Admin\Support\Helper;
  4. use Illuminate\Http\Request;
  5. /**
  6. * Trait InteractsWithApi.
  7. *
  8. *
  9. * @method mixed handle(Request $request)
  10. * @method mixed valueResult()
  11. */
  12. trait InteractsWithApi
  13. {
  14. use HasAuthorization;
  15. /**
  16. * @var string
  17. */
  18. protected $url;
  19. /**
  20. * @var string
  21. */
  22. protected $method = 'POST';
  23. /**
  24. * @var string
  25. */
  26. protected $uriKey;
  27. /**
  28. * @var array
  29. */
  30. protected $requestSelectors = [];
  31. /**
  32. * @var array
  33. */
  34. protected $requestScripts = [
  35. 'fetching' => [],
  36. 'fetched' => [],
  37. ];
  38. /**
  39. * 获取请求附带参数.
  40. *
  41. * @return array
  42. */
  43. public function parameters(): array
  44. {
  45. return [];
  46. }
  47. /**
  48. * 设置请求地址.
  49. *
  50. * @param string $method
  51. * @param string $url
  52. * @param array $query
  53. *
  54. * @return $this
  55. */
  56. public function request(string $method, string $url, array $query = [])
  57. {
  58. $this->method = $method;
  59. $this->url = admin_url(Helper::urlWithQuery($url, $query));
  60. return $this;
  61. }
  62. /**
  63. * 获取请求地址
  64. *
  65. * @return string
  66. */
  67. public function getRequestUrl()
  68. {
  69. return $this->url ?: route('dcat.api.value');
  70. }
  71. /**
  72. * 获取请求方法.
  73. *
  74. * @return string
  75. */
  76. public function getRequestMethod()
  77. {
  78. return $this->method;
  79. }
  80. /**
  81. * 设置URI标识.
  82. *
  83. * @return string
  84. */
  85. public function getUriKey()
  86. {
  87. return $this->uriKey ?: static::class;
  88. }
  89. /**
  90. * 获取js代码.
  91. *
  92. * @return array
  93. */
  94. public function getRequestScripts()
  95. {
  96. return $this->requestScripts;
  97. }
  98. /**
  99. * 设置点击抓取数据的按钮的css选择器.
  100. *
  101. * @param string|array $selector
  102. *
  103. * @return $this
  104. */
  105. public function click($selector)
  106. {
  107. $this->requestSelectors =
  108. array_merge($this->requestSelectors, (array) $selector);
  109. return $this;
  110. }
  111. /**
  112. * @return array
  113. */
  114. public function getRequestSelectors()
  115. {
  116. return $this->requestSelectors;
  117. }
  118. /**
  119. * 设置抓取数据时执行的js代码.
  120. *
  121. * @param string|\Closure $script
  122. *
  123. * @return $this
  124. */
  125. public function fetching($script)
  126. {
  127. $this->requestScripts['fetching'][] = value($script);
  128. return $this;
  129. }
  130. /**
  131. * 设置抓取完数据后执行的js代码.
  132. *
  133. * @param string|\Closure $script
  134. *
  135. * @return $this
  136. */
  137. public function fetched($script)
  138. {
  139. $this->requestScripts['fetched'][] = value($script);
  140. return $this;
  141. }
  142. /**
  143. * 判断是否使用请求接口功能.
  144. *
  145. * @return bool
  146. */
  147. public function allowBuildRequest()
  148. {
  149. return (
  150. $this->url
  151. || method_exists($this, 'handle')
  152. ) ? true : false;
  153. }
  154. /**
  155. * 构建请求数据js代码.
  156. *
  157. * @return null|string
  158. */
  159. public function buildRequestScript()
  160. {
  161. if (! $this->allowBuildRequest()) {
  162. return;
  163. }
  164. $fetching = implode(';', $this->requestScripts['fetching']);
  165. $fetched = implode(';', $this->requestScripts['fetched']);
  166. return <<<JS
  167. (function () {
  168. var requesting;
  169. function request(data) {
  170. if (requesting) {
  171. return;
  172. }
  173. requesting = 1;
  174. data = $.extend({$this->formatRequestData()}, data || {});
  175. {$fetching};
  176. $.ajax({
  177. url: '{$this->getRequestUrl()}',
  178. dataType: 'json',
  179. method: '{$this->method}',
  180. data: $.extend({_token: Dcat.token}, data),
  181. success: function (response) {
  182. requesting = 0;
  183. {$fetched};
  184. },
  185. error: function (a, b, c) {
  186. requesting = 0;
  187. Dcat.handleAjaxError(a, b, c)
  188. },
  189. });
  190. }
  191. request();
  192. {$this->buildBindingScript()}
  193. })();
  194. JS;
  195. }
  196. /**
  197. * @return string
  198. */
  199. private function formatRequestData()
  200. {
  201. $data = [
  202. '_key' => $this->getUriKey(),
  203. ];
  204. return json_encode(
  205. array_merge($this->parameters(), $data)
  206. );
  207. }
  208. /**
  209. * @return string
  210. */
  211. private function buildBindingScript()
  212. {
  213. $script = '';
  214. foreach ($this->requestSelectors as $v) {
  215. $script .= <<<JS
  216. $('{$v}').on('click', function () {
  217. request($(this).data())
  218. });
  219. JS;
  220. }
  221. return $script;
  222. }
  223. /**
  224. * 合并.
  225. *
  226. * @param static $self
  227. *
  228. * @return $this
  229. */
  230. public function merge($self)
  231. {
  232. $this->url = $self->getRequestUrl();
  233. $this->method = $self->getRequestMethod();
  234. $this->uriKey = $self->getUriKey();
  235. $this->requestSelectors = $self->getRequestSelectors();
  236. $scripts = $self->getRequestScripts();
  237. $this->requestScripts['fetching'] = array_merge($this->requestScripts['fetching'], $scripts['fetching']);
  238. $this->requestScripts['fetched'] = array_merge($this->requestScripts['fetched'], $scripts['fetched']);
  239. return $this;
  240. }
  241. }