DeleteBackupAction.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Services\CleanupService;
  4. use Dcat\Admin\Actions\RowAction;
  5. use Dcat\Admin\Actions\Response;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 删除备份Action
  9. *
  10. * 用于删除备份记录和文件
  11. */
  12. class DeleteBackupAction extends RowAction
  13. {
  14. /**
  15. * 按钮标题
  16. */
  17. protected $title = '删除备份';
  18. /**
  19. * 处理请求
  20. */
  21. public function handle(Request $request)
  22. {
  23. try {
  24. $backupId = $this->getKey();
  25. $deleteFiles = $request->input('delete_files', true);
  26. // 调用服务删除备份
  27. $result = CleanupService::deleteBackup($backupId, $deleteFiles);
  28. if (!$result['success']) {
  29. return $this->response()
  30. ->error('删除失败:' . $result['message']);
  31. }
  32. $data = $result['data'];
  33. return $this->response()
  34. ->success('备份删除成功!')
  35. ->detail("
  36. 备份名称:{$data['backup_name']}<br>
  37. 删除文件数:{$data['deleted_files']}<br>
  38. 释放空间:{$data['freed_space']}
  39. ")
  40. ->refresh();
  41. } catch (\Exception $e) {
  42. return $this->response()
  43. ->error('删除失败:' . $e->getMessage());
  44. }
  45. }
  46. /**
  47. * 确认对话框
  48. */
  49. public function confirm()
  50. {
  51. return [
  52. '确认删除备份?',
  53. '⚠️ 删除后无法恢复,请谨慎操作!',
  54. [
  55. 'delete_files' => [
  56. 'type' => 'checkbox',
  57. 'label' => '同时删除备份文件',
  58. 'checked' => true,
  59. 'help' => '取消勾选则只删除记录,保留文件',
  60. ]
  61. ]
  62. ];
  63. }
  64. /**
  65. * 权限检查
  66. */
  67. public function allowed()
  68. {
  69. $row = $this->row;
  70. return $row->backup_status != 1; // 非进行中状态可以删除
  71. }
  72. /**
  73. * 渲染按钮
  74. */
  75. public function render()
  76. {
  77. return <<<HTML
  78. <a href="javascript:void(0);" class="btn btn-danger btn-xs" data-action="{$this->getHandleRoute()}">
  79. <i class="fa fa-trash"></i> {$this->title}
  80. </a>
  81. HTML;
  82. }
  83. }