You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.0 KiB

3 months ago
3 months ago
2 months ago
3 months ago
4 weeks ago
2 months ago
2 months ago
2 months ago
  1. <template>
  2. <router-view></router-view>
  3. </template>
  4. <script setup>
  5. import { onMounted, ref, watch } from 'vue';
  6. import { useRoute, useRouter } from 'vue-router';
  7. import { storeToRefs } from "pinia";
  8. import { useAdminStore } from "@/store/index.js";
  9. import { hasMenuPermission, permissionMapping } from "@/utils/menuTreePermission.js";
  10. const router = useRouter();
  11. const route = useRoute();
  12. const adminStore = useAdminStore();
  13. const { menuTree } = storeToRefs(adminStore);
  14. const activeTab = ref('');
  15. const hasUser = ref(false);
  16. const hasRole = ref(false);
  17. // 导航方法
  18. const navigateTo = (name) => {
  19. activeTab.value = name;
  20. router.push({ name });
  21. };
  22. // 初始化权限状态
  23. const initPermissions = () => {
  24. if (!menuTree.value || !menuTree.value.length) return;
  25. hasUser.value = hasMenuPermission(menuTree.value, permissionMapping.user_management);
  26. hasRole.value = hasMenuPermission(menuTree.value, permissionMapping.role_management);
  27. console.log('====================================', hasUser.value, hasRole.value)
  28. };
  29. // 默认跳转逻辑
  30. const getDefaultAuditRoute = () => {
  31. initPermissions();
  32. if (hasUser.value) return 'userPermission';
  33. if (hasRole.value) return 'rolePermission';
  34. return 'userPermission';
  35. };
  36. // 监听路由变化更新标签状态
  37. watch(() => route.name, (newName) => {
  38. initPermissions()
  39. if (newName === 'userPermission' || newName === 'rolePermission') {
  40. activeTab.value = newName;
  41. } else if (newName === 'permissions') {
  42. // 每次访问 /permissions 都进行默认跳转
  43. const defaultRoute = getDefaultAuditRoute();
  44. navigateTo(defaultRoute);
  45. }
  46. });
  47. // 初始化逻辑
  48. onMounted(() => {
  49. initPermissions()
  50. if (route.name === 'permissions') {
  51. const defaultRoute = getDefaultAuditRoute();
  52. navigateTo(defaultRoute);
  53. } else {
  54. // 非父路由初始化当前标签状态
  55. if (route.name === 'userPermission' || route.name === 'rolePermission') {
  56. activeTab.value = route.name;
  57. }
  58. }
  59. });
  60. </script>