CancelTaskAction.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Services\CleanupService;
  4. use Dcat\Admin\Grid\RowAction;
  5. use Dcat\Admin\Actions\Response;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 取消任务Action
  9. *
  10. * 用于取消正在执行或已暂停的清理任务
  11. */
  12. class CancelTaskAction extends RowAction
  13. {
  14. /**
  15. * 按钮标题
  16. */
  17. protected $title = '取消任务';
  18. /**
  19. * 处理请求
  20. */
  21. public function handle(Request $request)
  22. {
  23. try {
  24. $taskId = $this->getKey();
  25. $reason = $request->input('reason', '用户手动取消');
  26. // 调用服务取消任务
  27. $result = CleanupService::cancelTask($taskId, $reason);
  28. if (!$result['success']) {
  29. return $this->response()
  30. ->error('取消失败:' . $result['message']);
  31. }
  32. return $this->response()
  33. ->success('任务取消成功!')
  34. ->detail('任务已取消,已完成的操作不会回滚。')
  35. ->refresh();
  36. } catch (\Exception $e) {
  37. return $this->response()
  38. ->error('取消失败:' . $e->getMessage());
  39. }
  40. }
  41. /**
  42. * 确认对话框
  43. */
  44. public function confirm()
  45. {
  46. return [
  47. '确认取消任务?',
  48. '⚠️ 任务取消后无法恢复,已完成的清理操作不会回滚!',
  49. [
  50. 'reason' => [
  51. 'type' => 'textarea',
  52. 'label' => '取消原因',
  53. 'placeholder' => '请输入取消原因(可选)',
  54. 'rows' => 3,
  55. ]
  56. ]
  57. ];
  58. }
  59. /**
  60. * 权限检查
  61. */
  62. public function allowed()
  63. {
  64. $row = $this->row;
  65. return in_array($row->status, [1, 2, 3, 7]); // 待执行、备份中、执行中、已暂停的任务可以取消
  66. }">
  67. <i class="fa fa-stop"></i> {$this->title}
  68. </a>
  69. HTML;
  70. }
  71. }