| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- <?php
- namespace App\Module\Admin\AdminControllers;
- use App\Module\Admin\Enums\ADMIN_ACTION_TYPE;
- use App\Module\Admin\Enums\CACHE_TYPE;
- use App\Module\Admin\Services\AdminService;
- use App\Module\Admin\Services\CacheService;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Widgets\Card;
- use Dcat\Admin\Widgets\Tab;
- use Illuminate\Http\Request;
- use Spatie\RouteAttributes\Attributes\Get;
- use Spatie\RouteAttributes\Attributes\Post;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- /**
- * 缓存管理控制器
- */
- #[Resource('admin-cache', names: 'admin.cache')]
- class CacheController extends AdminController
- {
- protected $title = '缓存管理';
- /**
- * @var CacheService
- */
- protected CacheService $cacheService;
- /**
- * @var AdminService
- */
- protected AdminService $adminService;
- public function __construct()
- {
- $this->cacheService = app('admin.cache');
- $this->adminService = app('admin.service');
- }
- /**
- * 缓存管理首页
- *
- * @param Content $content
- * @return Content
- */
- public function index(Content $content): Content
- {
- return $content
- ->title($this->title)
- ->description('缓存状态管理')
- ->body($this->buildCacheManagement());
- }
- /**
- * 构建缓存管理界面
- *
- * @return Tab
- */
- protected function buildCacheManagement(): Tab
- {
- $tab = new Tab();
-
- // 缓存状态标签页
- $tab->add('缓存状态', $this->buildCacheStatusCard());
-
- // 缓存操作标签页
- $tab->add('缓存操作', $this->buildCacheActionsCard());
-
- // 缓存统计标签页
- $tab->add('缓存统计', $this->buildCacheStatisticsCard());
-
- return $tab;
- }
- /**
- * 构建缓存状态卡片
- *
- * @return Card
- */
- protected function buildCacheStatusCard(): Card
- {
- $status = $this->cacheService->getStatus();
-
- $html = '<div class="row">';
-
- foreach ($status as $typeStatus) {
- $statusClass = $typeStatus['status'] === 'healthy' ? 'success' : 'danger';
- $statusText = $typeStatus['status'] === 'healthy' ? '正常' : '异常';
-
- $html .= '<div class="col-md-4 mb-3">';
- $html .= '<div class="card">';
- $html .= '<div class="card-body">';
- $html .= "<h5 class=\"card-title\">{$typeStatus['label']}</h5>";
- $html .= "<span class=\"badge badge-{$statusClass}\">{$statusText}</span>";
- $html .= "<p class=\"card-text mt-2\">{$typeStatus['description']}</p>";
-
- if (isset($typeStatus['ttl'])) {
- $html .= "<small class=\"text-muted\">TTL: {$typeStatus['ttl']}秒</small><br>";
- }
-
- if (isset($typeStatus['tags'])) {
- $tags = implode(', ', $typeStatus['tags']);
- $html .= "<small class=\"text-muted\">标签: {$tags}</small>";
- }
-
- if (isset($typeStatus['error'])) {
- $html .= "<div class=\"text-danger mt-2\">{$typeStatus['error']}</div>";
- }
-
- $html .= '</div>';
- $html .= '</div>';
- $html .= '</div>';
- }
-
- $html .= '</div>';
-
- return new Card('缓存状态', $html);
- }
- /**
- * 构建缓存操作卡片
- *
- * @return Card
- */
- protected function buildCacheActionsCard(): Card
- {
- $html = '<div class="row">';
-
- // 全部清理按钮
- $html .= '<div class="col-md-12 mb-3">';
- $html .= '<button type="button" class="btn btn-danger" onclick="clearAllCache()">';
- $html .= '<i class="fa fa-trash"></i> 清理所有缓存';
- $html .= '</button>';
- $html .= '</div>';
-
- // 按类型清理
- $html .= '<div class="col-md-12">';
- $html .= '<h5>按类型清理</h5>';
- $html .= '<div class="row">';
-
- foreach (CACHE_TYPE::cases() as $type) {
- $buttonClass = $type->isSafeToClear() ? 'btn-warning' : 'btn-danger';
- $safeText = $type->isSafeToClear() ? '(安全)' : '(谨慎)';
-
- $html .= '<div class="col-md-3 mb-2">';
- $html .= "<button type=\"button\" class=\"btn {$buttonClass} btn-sm\" onclick=\"clearCacheByType('{$type->value}')\">";
- $html .= "<i class=\"fa fa-broom\"></i> {$type->getLabel()} {$safeText}";
- $html .= '</button>';
- $html .= '</div>';
- }
-
- $html .= '</div>';
- $html .= '</div>';
-
- // 预热缓存
- $html .= '<div class="col-md-12 mt-3">';
- $html .= '<h5>缓存预热</h5>';
- $html .= '<button type="button" class="btn btn-primary" onclick="warmupCache()">';
- $html .= '<i class="fa fa-fire"></i> 预热缓存';
- $html .= '</button>';
- $html .= '</div>';
-
- $html .= '</div>';
-
- // 添加JavaScript
- $html .= $this->getCacheManagementScript();
-
- return new Card('缓存操作', $html);
- }
- /**
- * 构建缓存统计卡片
- *
- * @return Card
- */
- protected function buildCacheStatisticsCard(): Card
- {
- $statistics = $this->cacheService->getStatistics();
-
- $html = '<div class="row">';
- $html .= '<div class="col-md-6">';
- $html .= '<table class="table table-bordered">';
- $html .= '<tr><th>缓存驱动</th><td>' . ($statistics['driver'] ?? 'Unknown') . '</td></tr>';
- $html .= '<tr><th>缓存类型总数</th><td>' . ($statistics['total_types'] ?? 0) . '</td></tr>';
- $html .= '<tr><th>关键缓存类型</th><td>' . ($statistics['critical_types'] ?? 0) . '</td></tr>';
- $html .= '<tr><th>安全清理类型</th><td>' . ($statistics['safe_to_clear'] ?? 0) . '</td></tr>';
- $html .= '</table>';
- $html .= '</div>';
- $html .= '</div>';
-
- return new Card('缓存统计', $html);
- }
- /**
- * 获取缓存管理JavaScript
- *
- * @return string
- */
- protected function getCacheManagementScript(): string
- {
- return '
- <script>
- function clearAllCache() {
- if (confirm("确定要清理所有缓存吗?这可能会影响系统性能。")) {
- $.post("/admin/admin-cache/clear-all", {
- _token: $("meta[name=csrf-token]").attr("content")
- }).done(function(data) {
- if (data.status === "success") {
- Dcat.success(data.message);
- location.reload();
- } else {
- Dcat.error(data.message);
- }
- }).fail(function() {
- Dcat.error("操作失败");
- });
- }
- }
-
- function clearCacheByType(type) {
- if (confirm("确定要清理 " + type + " 缓存吗?")) {
- $.post("/admin/admin-cache/clear-type", {
- type: type,
- _token: $("meta[name=csrf-token]").attr("content")
- }).done(function(data) {
- if (data.status === "success") {
- Dcat.success(data.message);
- location.reload();
- } else {
- Dcat.error(data.message);
- }
- }).fail(function() {
- Dcat.error("操作失败");
- });
- }
- }
-
- function warmupCache() {
- if (confirm("确定要预热缓存吗?")) {
- $.post("/admin/admin-cache/warmup", {
- _token: $("meta[name=csrf-token]").attr("content")
- }).done(function(data) {
- if (data.status === "success") {
- Dcat.success(data.message);
- location.reload();
- } else {
- Dcat.error(data.message);
- }
- }).fail(function() {
- Dcat.error("操作失败");
- });
- }
- }
- </script>';
- }
- /**
- * 清理所有缓存
- *
- * @return \Illuminate\Http\JsonResponse
- */
- #[Post('admin-cache/clear-all')]
- public function clearAll()
- {
- try {
- $results = $this->cacheService->clearAll();
-
- // 记录操作日志
- $this->adminService->logAdminAction(
- ADMIN_ACTION_TYPE::CACHE_CLEAR,
- '清理所有缓存',
- ['results' => $results]
- );
-
- return response()->json([
- 'status' => 'success',
- 'message' => '所有缓存清理完成',
- 'data' => $results,
- ]);
- } catch (\Exception $e) {
- return response()->json([
- 'status' => 'error',
- 'message' => $e->getMessage(),
- ], 500);
- }
- }
- /**
- * 按类型清理缓存
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- #[Post('admin-cache/clear-type')]
- public function clearByType(Request $request)
- {
- try {
- $type = CACHE_TYPE::from($request->input('type'));
- $result = $this->cacheService->clearByType($type);
-
- // 记录操作日志
- $this->adminService->logAdminAction(
- ADMIN_ACTION_TYPE::CACHE_CLEAR,
- "清理{$type->getLabel()}缓存",
- ['type' => $type->value, 'result' => $result]
- );
-
- return response()->json([
- 'status' => 'success',
- 'message' => $result['message'],
- 'data' => $result,
- ]);
- } catch (\Exception $e) {
- return response()->json([
- 'status' => 'error',
- 'message' => $e->getMessage(),
- ], 500);
- }
- }
- /**
- * 预热缓存
- *
- * @return \Illuminate\Http\JsonResponse
- */
- #[Post('admin-cache/warmup')]
- public function warmup()
- {
- try {
- $results = $this->cacheService->warmup();
-
- // 记录操作日志
- $this->adminService->logAdminAction(
- ADMIN_ACTION_TYPE::CACHE_CLEAR,
- '预热缓存',
- ['results' => $results]
- );
-
- return response()->json([
- 'status' => 'success',
- 'message' => '缓存预热完成',
- 'data' => $results,
- ]);
- } catch (\Exception $e) {
- return response()->json([
- 'status' => 'error',
- 'message' => $e->getMessage(),
- ], 500);
- }
- }
- }
|