HasActionHandler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Dcat\Admin\Actions;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Traits\HasAuthorization;
  5. trait HasActionHandler
  6. {
  7. use HasAuthorization {
  8. failedAuthorization as parentFailedAuthorization;
  9. }
  10. /**
  11. * @var Response
  12. */
  13. protected $response;
  14. private $confirmString;
  15. private $paramString;
  16. /**
  17. * @return Response
  18. */
  19. public function response()
  20. {
  21. if (is_null($this->response)) {
  22. $this->response = new Response();
  23. }
  24. return $this->response;
  25. }
  26. /**
  27. * @return string
  28. */
  29. public function method()
  30. {
  31. return $this->method;
  32. }
  33. /**
  34. * @return array
  35. */
  36. protected function parameters()
  37. {
  38. return [];
  39. }
  40. /**
  41. * Confirm message of action.
  42. *
  43. * @return string|void
  44. */
  45. public function confirm()
  46. {
  47. }
  48. /**
  49. * @return mixed
  50. */
  51. public function makeCalledClass()
  52. {
  53. return str_replace('\\', '_', get_called_class());
  54. }
  55. /**
  56. * @return string
  57. */
  58. public function handlerRoute()
  59. {
  60. return route(admin_api_route_name('action'));
  61. }
  62. /**
  63. * @return string
  64. */
  65. protected function normalizeConfirmData()
  66. {
  67. if ($this->confirmString !== null) {
  68. return $this->confirmString;
  69. }
  70. $confirm = $this->confirm();
  71. return $this->confirmString = ($confirm ? admin_javascript_json((array) $confirm) : 'false');
  72. }
  73. /**
  74. * @return string
  75. */
  76. protected function normalizeParameters()
  77. {
  78. return $this->paramString ?: ($this->paramString = json_encode($this->parameters()));
  79. }
  80. /**
  81. * @return void
  82. */
  83. protected function addHandlerScript()
  84. {
  85. $script = <<<JS
  86. Dcat.Action({
  87. selector: '{$this->selector()}',
  88. event: '{$this->event}',
  89. method: '{$this->method()}',
  90. key: '{$this->getKey()}',
  91. url: '{$this->handlerRoute()}',
  92. data: {$this->normalizeParameters()},
  93. confirm: {$this->normalizeConfirmData()},
  94. calledClass: '{$this->makeCalledClass()}',
  95. before: {$this->actionScript()},
  96. html: {$this->handleHtmlResponse()},
  97. success: {$this->resolverScript()},
  98. error: {$this->rejectScript()},
  99. });
  100. JS;
  101. Admin::script($script);
  102. Admin::js('@admin/dcat/extra/action.js');
  103. }
  104. /**
  105. * 设置动作发起请求前的回调函数,返回false可以中断请求.
  106. *
  107. * @return string
  108. */
  109. protected function actionScript()
  110. {
  111. // 发起请求之前回调,返回false可以中断请求
  112. return <<<'JS'
  113. function (data, target, action) { }
  114. JS;
  115. }
  116. /**
  117. * 设置请求成功回调,返回false可以中断默认的成功处理逻辑.
  118. *
  119. * @return string
  120. */
  121. protected function resolverScript()
  122. {
  123. // 请求成功回调,返回false可以中断默认的成功处理逻辑
  124. return <<<'JS'
  125. function (target, results) {}
  126. JS;
  127. }
  128. /**
  129. * 处理接口返回的HTML代码.
  130. *
  131. * @return string
  132. */
  133. protected function handleHtmlResponse()
  134. {
  135. return <<<'JS'
  136. function (target, html, data) {
  137. target.html(html);
  138. }
  139. JS;
  140. }
  141. /**
  142. * 设置请求出错回调,返回false可以中断默认的错误处理逻辑.
  143. *
  144. * @return string
  145. */
  146. protected function rejectScript()
  147. {
  148. return <<<'JS'
  149. function (target, results) {}
  150. JS;
  151. }
  152. /**
  153. * @return Response
  154. */
  155. public function failedAuthorization()
  156. {
  157. return $this->response()->error(__('admin.deny'));
  158. }
  159. }