| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- namespace App\Module\Admin\Services;
- use App\Module\Admin\Enums\ADMIN_ACTION_TYPE;
- use App\Module\Admin\Events\AdminActionEvent;
- use App\Module\Admin\Utils\SystemInfo;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Event;
- use Illuminate\Support\Facades\Log;
- /**
- * Admin模块核心服务
- */
- class AdminService
- {
- /**
- * 获取仪表板数据
- *
- * @return array
- */
- public function getDashboardData(): array
- {
- return Cache::remember('admin.dashboard.data', 300, function () {
- $systemInfo = new SystemInfo();
-
- return [
- 'system' => [
- 'server_info' => $systemInfo->getServerInfo(),
- 'php_info' => $systemInfo->getPhpInfo(),
- 'database_info' => $systemInfo->getDatabaseInfo(),
- 'performance' => $systemInfo->getPerformanceMetrics(),
- ],
- 'statistics' => [
- 'users_online' => $this->getOnlineUsersCount(),
- 'total_requests' => $this->getTotalRequestsCount(),
- 'error_rate' => $this->getErrorRate(),
- 'response_time' => $this->getAverageResponseTime(),
- ],
- 'alerts' => $this->getSystemAlerts(),
- 'recent_actions' => $this->getRecentActions(),
- ];
- });
- }
- /**
- * 记录管理员操作
- *
- * @param ADMIN_ACTION_TYPE $actionType
- * @param string $description
- * @param array $data
- * @return void
- */
- public function logAdminAction(ADMIN_ACTION_TYPE $actionType, string $description, array $data = []): void
- {
- $adminUser = Auth::guard('admin')->user();
-
- $logData = [
- 'admin_id' => $adminUser?->id,
- 'admin_name' => $adminUser?->name ?? 'System',
- 'action_type' => $actionType->value,
- 'description' => $description,
- 'data' => $data,
- 'ip_address' => request()->ip(),
- 'user_agent' => request()->userAgent(),
- 'timestamp' => now(),
- ];
- // 触发事件
- Event::dispatch(new AdminActionEvent($logData));
- // 记录日志
- if ($actionType->needsDetailedLog()) {
- Log::channel('admin')->info("Admin Action: {$description}", $logData);
- }
- // 危险操作额外记录
- if ($actionType->isDangerous()) {
- Log::channel('admin')->warning("Dangerous Admin Action: {$description}", $logData);
- }
- }
- /**
- * 获取在线用户数量
- *
- * @return int
- */
- protected function getOnlineUsersCount(): int
- {
- // 这里可以根据实际需求实现
- // 例如:统计最近5分钟内有活动的用户
- return Cache::remember('admin.stats.online_users', 60, function () {
- // 实现逻辑
- return 0;
- });
- }
- /**
- * 获取总请求数
- *
- * @return int
- */
- protected function getTotalRequestsCount(): int
- {
- return Cache::remember('admin.stats.total_requests', 300, function () {
- // 实现逻辑
- return 0;
- });
- }
- /**
- * 获取错误率
- *
- * @return float
- */
- protected function getErrorRate(): float
- {
- return Cache::remember('admin.stats.error_rate', 300, function () {
- // 实现逻辑
- return 0.0;
- });
- }
- /**
- * 获取平均响应时间
- *
- * @return float
- */
- protected function getAverageResponseTime(): float
- {
- return Cache::remember('admin.stats.response_time', 300, function () {
- // 实现逻辑
- return 0.0;
- });
- }
- /**
- * 获取系统警报
- *
- * @return array
- */
- protected function getSystemAlerts(): array
- {
- $alerts = [];
- $systemInfo = new SystemInfo();
- $config = config('admin_module.monitoring.alerts');
- // CPU使用率警报
- $cpuUsage = $systemInfo->getCpuUsage();
- if ($cpuUsage > $config['cpu_threshold']) {
- $alerts[] = [
- 'type' => 'warning',
- 'title' => 'CPU使用率过高',
- 'message' => "当前CPU使用率: {$cpuUsage}%",
- 'timestamp' => now(),
- ];
- }
- // 内存使用率警报
- $memoryUsage = $systemInfo->getMemoryUsage();
- if ($memoryUsage['percentage'] > $config['memory_threshold']) {
- $alerts[] = [
- 'type' => 'warning',
- 'title' => '内存使用率过高',
- 'message' => "当前内存使用率: {$memoryUsage['percentage']}%",
- 'timestamp' => now(),
- ];
- }
- // 磁盘使用率警报
- $diskUsage = $systemInfo->getDiskUsage();
- if ($diskUsage['percentage'] > $config['disk_threshold']) {
- $alerts[] = [
- 'type' => 'danger',
- 'title' => '磁盘空间不足',
- 'message' => "当前磁盘使用率: {$diskUsage['percentage']}%",
- 'timestamp' => now(),
- ];
- }
- return $alerts;
- }
- /**
- * 获取最近的操作记录
- *
- * @param int $limit
- * @return array
- */
- protected function getRecentActions(int $limit = 10): array
- {
- return Cache::remember("admin.recent_actions.{$limit}", 60, function () use ($limit) {
- // 这里应该从数据库获取最近的操作记录
- // 暂时返回空数组
- return [];
- });
- }
- /**
- * 获取系统状态
- *
- * @return array
- */
- public function getSystemStatus(): array
- {
- $systemInfo = new SystemInfo();
-
- return [
- 'status' => 'running',
- 'uptime' => $systemInfo->getUptime(),
- 'load_average' => $systemInfo->getLoadAverage(),
- 'memory_usage' => $systemInfo->getMemoryUsage(),
- 'disk_usage' => $systemInfo->getDiskUsage(),
- 'database_status' => $systemInfo->getDatabaseStatus(),
- 'cache_status' => $this->getCacheStatus(),
- 'queue_status' => $this->getQueueStatus(),
- ];
- }
- /**
- * 获取缓存状态
- *
- * @return array
- */
- protected function getCacheStatus(): array
- {
- try {
- Cache::put('admin.cache.test', 'test', 1);
- $test = Cache::get('admin.cache.test');
- Cache::forget('admin.cache.test');
-
- return [
- 'status' => $test === 'test' ? 'healthy' : 'error',
- 'driver' => config('cache.default'),
- ];
- } catch (\Exception $e) {
- return [
- 'status' => 'error',
- 'error' => $e->getMessage(),
- ];
- }
- }
- /**
- * 获取队列状态
- *
- * @return array
- */
- protected function getQueueStatus(): array
- {
- try {
- return [
- 'status' => 'healthy',
- 'driver' => config('queue.default'),
- 'pending_jobs' => 0, // 这里可以实现获取待处理任务数量的逻辑
- ];
- } catch (\Exception $e) {
- return [
- 'status' => 'error',
- 'error' => $e->getMessage(),
- ];
- }
- }
- /**
- * 执行系统维护
- *
- * @param array $options
- * @return array
- */
- public function performMaintenance(array $options = []): array
- {
- $results = [];
-
- // 清理缓存
- if ($options['clear_cache'] ?? true) {
- $cacheService = app('admin.cache');
- $results['cache'] = $cacheService->clearAll();
- }
-
- // 清理日志
- if ($options['clean_logs'] ?? false) {
- $logService = app('admin.log');
- $results['logs'] = $logService->cleanOldLogs();
- }
-
- // 优化数据库
- if ($options['optimize_database'] ?? false) {
- $results['database'] = $this->optimizeDatabase();
- }
-
- // 记录维护操作
- $this->logAdminAction(
- ADMIN_ACTION_TYPE::MAINTENANCE,
- '执行系统维护',
- ['options' => $options, 'results' => $results]
- );
-
- return $results;
- }
- /**
- * 优化数据库
- *
- * @return array
- */
- protected function optimizeDatabase(): array
- {
- try {
- // 这里可以实现数据库优化逻辑
- return [
- 'status' => 'success',
- 'message' => '数据库优化完成',
- ];
- } catch (\Exception $e) {
- return [
- 'status' => 'error',
- 'message' => $e->getMessage(),
- ];
- }
- }
- }
|