DashboardController.php 6.0 KB

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