menu-visibility-toggle.blade.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. @php
  2. use App\Models\MenuPermission;
  3. use App\Models\MenuConfig;
  4. use Filament\Facades\Filament;
  5. // 获取当前用户
  6. $user = Filament::getCurrentPanel()->auth()->user();
  7. // 检查是否是管理员
  8. $isAdmin = $user && (
  9. $user->role === 'admin' ||
  10. $user->username === '17689974321'
  11. );
  12. if (!$isAdmin) {
  13. return;
  14. }
  15. // 获取当前路径
  16. $currentPath = request()->path();
  17. $menuKey = null;
  18. // 根据路径映射到菜单键
  19. $menuMappings = [
  20. 'admin/ocr-paper-grading' => 'ocr-paper-grading',
  21. 'admin/exam-analysis' => 'exam-analysis',
  22. 'admin/dashboard' => 'dashboard',
  23. 'admin/exam-history' => 'exam-history',
  24. 'admin/intelligent-exam-generation' => 'intelligent-exam-generation',
  25. 'admin/knowledge-graph' => 'knowledge-graph',
  26. 'admin/student-management' => 'student-management',
  27. 'admin/teacher-management' => 'teacher-management',
  28. ];
  29. // 查找匹配的菜单键
  30. foreach ($menuMappings as $path => $key) {
  31. if (str_contains($currentPath, $path)) {
  32. $menuKey = $key;
  33. break;
  34. }
  35. }
  36. if (!$menuKey) {
  37. return;
  38. }
  39. // 检查该菜单是否纳入管理系统
  40. $menuConfig = MenuConfig::where('menu_key', $menuKey)->first();
  41. if (!$menuConfig || !$menuConfig->is_managed || !$menuConfig->is_active) {
  42. return;
  43. }
  44. // 获取用户ID
  45. $userId = $user->teacher_id ?? $user->id;
  46. // 获取当前菜单可见性
  47. $isVisible = MenuPermission::isMenuVisible($userId, $menuKey);
  48. // 获取菜单显示名称
  49. $menuLabel = $menuConfig->menu_label ?? $menuKey;
  50. @endphp
  51. <div x-data="menuToggle()" class="flex items-center gap-3">
  52. <!-- 菜单状态指示 -->
  53. <div class="flex items-center gap-2 text-sm">
  54. <span class="text-gray-600 dark:text-gray-400">菜单:</span>
  55. <span x-text="isVisible ? '显示' : '隐藏'" class="font-medium"
  56. :class="isVisible ? 'text-success-600' : 'text-danger-600'">
  57. </span>
  58. </div>
  59. <!-- DaisyUI Toggle 开关 -->
  60. <input
  61. type="checkbox"
  62. class="toggle toggle-primary toggle-sm"
  63. :checked="{{ $isVisible ? 'true' : 'false' }}"
  64. @change="toggleMenu('{{ $menuKey }}', '{{ $userId }}', $event.target.checked)"
  65. />
  66. </div>
  67. <script>
  68. function menuToggle() {
  69. return {
  70. isVisible: {{ $isVisible ? 'true' : 'false' }},
  71. async toggleMenu(menuKey, userId, visible) {
  72. try {
  73. const response = await fetch('{{ route('filament.admin.auth.toggle-menu-visibility') }}', {
  74. method: 'POST',
  75. headers: {
  76. 'Content-Type': 'application/json',
  77. 'X-CSRF-TOKEN': '{{ csrf_token() }}',
  78. },
  79. body: JSON.stringify({
  80. menu_key: menuKey,
  81. user_id: userId,
  82. is_visible: visible
  83. })
  84. });
  85. if (response.ok) {
  86. this.isVisible = visible;
  87. // 显示成功通知
  88. if (window.Filament && Filament.notify) {
  89. Filament.notify('success', '菜单已' + (visible ? '显示' : '隐藏'));
  90. }
  91. } else {
  92. throw new Error('操作失败');
  93. }
  94. } catch (error) {
  95. console.error('Error:', error);
  96. // 显示错误通知
  97. if (window.Filament && Filament.notify) {
  98. Filament.notify('danger', '操作失败,请重试');
  99. }
  100. // 恢复原状态
  101. this.isVisible = !visible;
  102. }
  103. }
  104. }
  105. }
  106. </script>