DashboardController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Module\Admin\AdminControllers;
  3. use App\Module\Admin\Services\AdminService;
  4. use App\Module\Admin\AdminWidgets\SystemStatusWidget;
  5. use App\Module\Admin\AdminWidgets\QuickActionsWidget;
  6. use App\Module\Admin\AdminWidgets\StatisticsWidget;
  7. use Dcat\Admin\Layout\Content;
  8. use Dcat\Admin\Layout\Row;
  9. use Spatie\RouteAttributes\Attributes\Get;
  10. use UCore\DcatAdmin\AdminController;
  11. /**
  12. * 仪表板控制器
  13. */
  14. class DashboardController extends AdminController
  15. {
  16. protected $title = '仪表板';
  17. /**
  18. * 仪表板首页
  19. *
  20. * @param Content $content
  21. * @return Content
  22. */
  23. #[Get('admin/dashboard')]
  24. public function index(Content $content): Content
  25. {
  26. return $content
  27. ->title($this->title)
  28. ->description('系统概览')
  29. ->body($this->buildDashboard());
  30. }
  31. /**
  32. * 构建仪表板内容
  33. *
  34. * @return Row
  35. */
  36. protected function buildDashboard(): Row
  37. {
  38. return new Row(function (Row $row) {
  39. $adminService = new AdminService();
  40. $dashboardData = $adminService->getDashboardData();
  41. // 第一行:统计卡片
  42. $row->column(12, function ($column) use ($dashboardData) {
  43. $column->append(new StatisticsWidget($dashboardData['statistics']));
  44. });
  45. // 第二行:系统状态和快捷操作
  46. $row->column(8, function ($column) use ($dashboardData) {
  47. $column->append(new SystemStatusWidget($dashboardData['system']));
  48. });
  49. $row->column(4, function ($column) use ($dashboardData) {
  50. $column->append(new QuickActionsWidget());
  51. });
  52. // 第三行:系统警报(如果有)
  53. if (!empty($dashboardData['alerts'])) {
  54. $row->column(12, function ($column) use ($dashboardData) {
  55. $column->append($this->buildAlertsWidget($dashboardData['alerts']));
  56. });
  57. }
  58. // 第四行:最近操作
  59. if (!empty($dashboardData['recent_actions'])) {
  60. $row->column(12, function ($column) use ($dashboardData) {
  61. $column->append($this->buildRecentActionsWidget($dashboardData['recent_actions']));
  62. });
  63. }
  64. });
  65. }
  66. /**
  67. * 构建警报小部件
  68. *
  69. * @param array $alerts
  70. * @return string
  71. */
  72. protected function buildAlertsWidget(array $alerts): string
  73. {
  74. $html = '<div class="card">';
  75. $html .= '<div class="card-header"><h4 class="card-title">系统警报</h4></div>';
  76. $html .= '<div class="card-body">';
  77. foreach ($alerts as $alert) {
  78. $type = $alert['type'];
  79. $title = $alert['title'];
  80. $message = $alert['message'];
  81. $timestamp = $alert['timestamp']->format('Y-m-d H:i:s');
  82. $html .= "<div class=\"alert alert-{$type}\">";
  83. $html .= "<strong>{$title}</strong><br>";
  84. $html .= "{$message}<br>";
  85. $html .= "<small class=\"text-muted\">{$timestamp}</small>";
  86. $html .= "</div>";
  87. }
  88. $html .= '</div></div>';
  89. return $html;
  90. }
  91. /**
  92. * 构建最近操作小部件
  93. *
  94. * @param array $actions
  95. * @return string
  96. */
  97. protected function buildRecentActionsWidget(array $actions): string
  98. {
  99. $html = '<div class="card">';
  100. $html .= '<div class="card-header"><h4 class="card-title">最近操作</h4></div>';
  101. $html .= '<div class="card-body">';
  102. $html .= '<div class="table-responsive">';
  103. $html .= '<table class="table table-sm">';
  104. $html .= '<thead>';
  105. $html .= '<tr>';
  106. $html .= '<th>操作</th>';
  107. $html .= '<th>管理员</th>';
  108. $html .= '<th>时间</th>';
  109. $html .= '<th>IP地址</th>';
  110. $html .= '</tr>';
  111. $html .= '</thead>';
  112. $html .= '<tbody>';
  113. foreach ($actions as $action) {
  114. $html .= '<tr>';
  115. $html .= "<td>{$action['description']}</td>";
  116. $html .= "<td>{$action['admin_name']}</td>";
  117. $html .= "<td>{$action['timestamp']}</td>";
  118. $html .= "<td>{$action['ip_address']}</td>";
  119. $html .= '</tr>';
  120. }
  121. $html .= '</tbody>';
  122. $html .= '</table>';
  123. $html .= '</div>';
  124. $html .= '</div></div>';
  125. return $html;
  126. }
  127. /**
  128. * 获取系统状态API
  129. *
  130. * @return \Illuminate\Http\JsonResponse
  131. */
  132. #[Get('admin/api/system-status')]
  133. public function getSystemStatus()
  134. {
  135. try {
  136. $status = $this->adminService->getSystemStatus();
  137. return response()->json([
  138. 'status' => 'success',
  139. 'data' => $status,
  140. ]);
  141. } catch (\Exception $e) {
  142. return response()->json([
  143. 'status' => 'error',
  144. 'message' => $e->getMessage(),
  145. ], 500);
  146. }
  147. }
  148. /**
  149. * 刷新仪表板数据API
  150. *
  151. * @return \Illuminate\Http\JsonResponse
  152. */
  153. #[Get('admin/api/dashboard/refresh')]
  154. public function refreshDashboard()
  155. {
  156. try {
  157. // 清理仪表板缓存
  158. cache()->forget('admin.dashboard.data');
  159. // 获取新数据
  160. $data = $this->adminService->getDashboardData();
  161. return response()->json([
  162. 'status' => 'success',
  163. 'message' => '仪表板数据已刷新',
  164. 'data' => $data,
  165. ]);
  166. } catch (\Exception $e) {
  167. return response()->json([
  168. 'status' => 'error',
  169. 'message' => $e->getMessage(),
  170. ], 500);
  171. }
  172. }
  173. }