HasActionHandler.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. /**
  15. * @return Response
  16. */
  17. public function response()
  18. {
  19. if (is_null($this->response)) {
  20. $this->response = new Response();
  21. }
  22. return $this->response;
  23. }
  24. /**
  25. * @return string
  26. */
  27. public function method()
  28. {
  29. return $this->method;
  30. }
  31. /**
  32. * @return array
  33. */
  34. protected function parameters()
  35. {
  36. return [];
  37. }
  38. /**
  39. * Confirm message of action.
  40. *
  41. * @return string|void
  42. */
  43. public function confirm()
  44. {
  45. }
  46. /**
  47. * @return mixed
  48. */
  49. public function makeCalledClass()
  50. {
  51. return str_replace('\\', '_', get_called_class());
  52. }
  53. /**
  54. * @return string
  55. */
  56. public function handlerRoute()
  57. {
  58. return route('dcat.api.action');
  59. }
  60. /**
  61. * @return void
  62. */
  63. protected function addHandlerScript()
  64. {
  65. $data = json_encode($this->parameters());
  66. $script = <<<JS
  67. Dcat.grid.RowAction({
  68. selector: '{$this->selector()}',
  69. event: '{$this->event}',
  70. method: '{$this->method()}',
  71. key: '{$this->getKey()}',
  72. url: '{$this->handlerRoute()}',
  73. data: {$data},
  74. calledClass: '{$this->makeCalledClass()}',
  75. before: {$this->actionScript()},
  76. html: {$this->handleHtmlResponse()},
  77. success: {$this->resolverScript()},
  78. error: {$this->rejectScript()},
  79. });
  80. JS;
  81. Admin::script($script);
  82. Admin::js('@admin/dcat/extra/grid-row-action.js');
  83. }
  84. /**
  85. * @return string
  86. */
  87. protected function actionScript()
  88. {
  89. // 发起请求之前回调,返回false可以中断请求
  90. return <<<'JS'
  91. function (data, target) { }
  92. JS;
  93. }
  94. /**
  95. * @return string
  96. */
  97. protected function resolverScript()
  98. {
  99. // 请求成功回调,返回false可以中断默认的成功处理逻辑
  100. return <<<'JS'
  101. function (target, results) {}
  102. JS;
  103. }
  104. /**
  105. * @return string
  106. */
  107. protected function handleHtmlResponse()
  108. {
  109. return <<<'JS'
  110. function (target, html, data) { // 处理返回的HTML代码
  111. target.html(html);
  112. }
  113. JS;
  114. }
  115. /**
  116. * @return string
  117. */
  118. protected function rejectScript()
  119. {
  120. // // 请求出错回调,返回false可以中断默认的错误处理逻辑
  121. return <<<'JS'
  122. function (target, results) {}
  123. JS;
  124. }
  125. /**
  126. * @return Response
  127. */
  128. public function failedAuthorization()
  129. {
  130. return $this->response()->error(__('admin.deny'));
  131. }
  132. }