QuickActionsWidget.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Module\Admin\AdminWidgets;
  3. use Dcat\Admin\Widgets\Card;
  4. /**
  5. * 快捷操作小部件
  6. */
  7. class QuickActionsWidget extends Card
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct('快捷操作', $this->buildContent());
  12. }
  13. /**
  14. * 构建内容
  15. *
  16. * @return string
  17. */
  18. protected function buildContent(): string
  19. {
  20. $actions = [
  21. [
  22. 'title' => '清理缓存',
  23. 'icon' => 'fa-broom',
  24. 'color' => 'warning',
  25. 'url' => '/admin/admin-cache',
  26. 'description' => '清理系统缓存',
  27. ],
  28. [
  29. 'title' => '系统信息',
  30. 'icon' => 'fa-info-circle',
  31. 'color' => 'info',
  32. 'url' => '/admin/system-info',
  33. 'description' => '查看系统详细信息',
  34. ],
  35. [
  36. 'title' => '日志管理',
  37. 'icon' => 'fa-file-alt',
  38. 'color' => 'primary',
  39. 'url' => '/admin/logs',
  40. 'description' => '查看和管理系统日志',
  41. ],
  42. [
  43. 'title' => '系统维护',
  44. 'icon' => 'fa-tools',
  45. 'color' => 'danger',
  46. 'url' => '#',
  47. 'description' => '执行系统维护操作',
  48. 'onclick' => 'performMaintenance()',
  49. ],
  50. [
  51. 'title' => '备份系统',
  52. 'icon' => 'fa-archive',
  53. 'color' => 'success',
  54. 'url' => '#',
  55. 'description' => '创建系统备份',
  56. 'onclick' => 'createBackup()',
  57. ],
  58. [
  59. 'title' => '性能监控',
  60. 'icon' => 'fa-chart-line',
  61. 'color' => 'secondary',
  62. 'url' => '/admin/performance',
  63. 'description' => '查看性能监控数据',
  64. ],
  65. ];
  66. $html = '<div class="list-group list-group-flush">';
  67. foreach ($actions as $action) {
  68. $onclick = isset($action['onclick']) ? "onclick=\"{$action['onclick']}\"" : '';
  69. $href = isset($action['onclick']) ? 'javascript:void(0)' : $action['url'];
  70. $html .= '<a href="' . $href . '" class="list-group-item list-group-item-action" ' . $onclick . '>';
  71. $html .= '<div class="d-flex w-100 justify-content-between align-items-center">';
  72. $html .= '<div>';
  73. $html .= "<i class=\"fa {$action['icon']} text-{$action['color']} mr-2\"></i>";
  74. $html .= "<strong>{$action['title']}</strong>";
  75. $html .= "<br><small class=\"text-muted\">{$action['description']}</small>";
  76. $html .= '</div>';
  77. $html .= '<i class="fa fa-chevron-right text-muted"></i>';
  78. $html .= '</div>';
  79. $html .= '</a>';
  80. }
  81. $html .= '</div>';
  82. // 添加JavaScript函数
  83. $html .= $this->getQuickActionsScript();
  84. return $html;
  85. }
  86. /**
  87. * 获取快捷操作JavaScript
  88. *
  89. * @return string
  90. */
  91. protected function getQuickActionsScript(): string
  92. {
  93. return '
  94. <script>
  95. function performMaintenance() {
  96. if (confirm("确定要执行系统维护吗?这可能需要几分钟时间。")) {
  97. Dcat.loading();
  98. $.post("/admin/api/maintenance", {
  99. _token: $("meta[name=csrf-token]").attr("content"),
  100. clear_cache: true,
  101. clean_logs: false,
  102. optimize_database: false
  103. }).done(function(data) {
  104. Dcat.loading(false);
  105. if (data.status === "success") {
  106. Dcat.success("系统维护完成");
  107. setTimeout(function() {
  108. location.reload();
  109. }, 2000);
  110. } else {
  111. Dcat.error(data.message || "维护失败");
  112. }
  113. }).fail(function() {
  114. Dcat.loading(false);
  115. Dcat.error("维护操作失败");
  116. });
  117. }
  118. }
  119. function createBackup() {
  120. if (confirm("确定要创建系统备份吗?")) {
  121. Dcat.loading();
  122. $.post("/admin/api/backup", {
  123. _token: $("meta[name=csrf-token]").attr("content"),
  124. include_database: true,
  125. include_files: false,
  126. include_config: true
  127. }).done(function(data) {
  128. Dcat.loading(false);
  129. if (data.status === "success") {
  130. Dcat.success("备份创建成功");
  131. if (data.download_url) {
  132. window.open(data.download_url, "_blank");
  133. }
  134. } else {
  135. Dcat.error(data.message || "备份失败");
  136. }
  137. }).fail(function() {
  138. Dcat.loading(false);
  139. Dcat.error("备份操作失败");
  140. });
  141. }
  142. }
  143. </script>';
  144. }
  145. }