liujie
2026-08-14 92a75a7b157e6fd6423df17cfd32015017bdb8c9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { createRouter, createWebHistory } from 'vue-router'
import { createAdminRoutes, firstPermittedPath, hasPermission } from '@/config/adminMenu'
import { useAuthStore } from '@/stores/auth'
 
const routes = [
  { path: '/login', component: () => import('@/views/LoginView.vue'), meta: { public: true } },
  {
    path: '/',
    component: () => import('@/layouts/AdminLayout.vue'),
    redirect: () => firstPermittedPath(useAuthStore().user?.menuCodes) || '/forbidden',
    children: [
      ...createAdminRoutes(),
      {
        path: 'forbidden',
        name: 'forbidden',
        component: () => import('@/views/NoPermissionView.vue'),
        meta: { title: '无访问权限' },
      },
      {
        path: 'system/roles/new',
        name: 'role-create',
        component: () => import('@/views/RoleDetailView.vue'),
        meta: { title: '新增角色', activeMenu: '/system/roles', permission: 'ROLE_LIST' },
      },
      {
        path: 'system/roles/:id/edit',
        name: 'role-edit',
        component: () => import('@/views/RoleDetailView.vue'),
        meta: { title: '编辑角色', activeMenu: '/system/roles', permission: 'ROLE_LIST' },
      },
      {
        path: 'system/roles/:id',
        name: 'role-detail',
        component: () => import('@/views/RoleDetailView.vue'),
        meta: { title: '角色详情', activeMenu: '/system/roles', permission: 'ROLE_LIST' },
      },
      {
        path: 'operations/scenic-spots/new',
        name: 'scenic-spot-create',
        component: () => import('@/views/ScenicSpotDetailView.vue'),
        meta: { title: '新增景区', activeMenu: '/operations/scenic-spots', permission: 'SCENIC_LIST' },
      },
      {
        path: 'operations/scenic-spots/:id',
        name: 'scenic-spot-detail',
        component: () => import('@/views/ScenicSpotDetailView.vue'),
        meta: { title: '景区详情', activeMenu: '/operations/scenic-spots', permission: 'SCENIC_LIST' },
      },
      {
        path: 'orders/:id',
        name: 'order-detail',
        component: () => import('@/views/OrderDetailView.vue'),
        meta: { title: '订单详情', activeMenu: '/orders', permission: 'ORDER_LIST' },
      },
      {
        path: 'visitors/:id',
        name: 'visitor-detail',
        component: () => import('@/views/MiniUserDetailView.vue'),
        meta: { title: '游客详情', activeMenu: '/visitors', permission: 'VISITOR_LIST' },
      },
      {
        path: 'guide-teams/new',
        name: 'guide-team-create',
        component: () => import('@/views/GuideTeamDetailView.vue'),
        meta: { title: '新增讲解队', activeMenu: '/guide-teams', permission: 'GUIDE_TEAM' },
      },
      {
        path: 'guide-teams/:id/edit',
        name: 'guide-team-edit',
        component: () => import('@/views/GuideTeamDetailView.vue'),
        meta: { title: '编辑讲解队', activeMenu: '/guide-teams', permission: 'GUIDE_TEAM' },
      },
      {
        path: 'guide-teams/:id',
        name: 'guide-team-detail',
        component: () => import('@/views/GuideTeamDetailView.vue'),
        meta: { title: '讲解队详情', activeMenu: '/guide-teams', permission: 'GUIDE_TEAM' },
      },
      {
        path: 'guides/auto-disable',
        name: 'guide-auto-disable',
        component: () => import('@/views/GuideAutoDisableView.vue'),
        meta: { title: '自动禁用设置', activeMenu: '/guides', permission: 'GUIDE_LIST' },
      },
      {
        path: 'guides/:id',
        name: 'guide-detail',
        component: () => import('@/views/GuideUserDetailView.vue'),
        meta: { title: '导游详情', activeMenu: '/guides', permission: 'GUIDE_LIST' },
      },
      { path: 'admin-users', redirect: '/system/employees' },
      { path: 'mini-users', redirect: '/visitors' },
      { path: 'guide-users', redirect: '/guides' },
    ],
  },
]
 
const router = createRouter({ history: createWebHistory(), routes })
 
router.beforeEach(async (to) => {
  const token = localStorage.getItem('admin_token')
  if (!to.meta.public && !token) return '/login'
  if (!token) return true
 
  const authStore = useAuthStore()
  try {
    await authStore.loadUser()
  } catch (error) {
    return false
  }
 
  const firstPath = firstPermittedPath(authStore.user?.menuCodes) || '/forbidden'
  if (to.path === '/login') return firstPath
  if (!hasPermission(authStore.user?.menuCodes, to.meta.permission)) {
    return firstPath && firstPath !== to.path ? firstPath : false
  }
  return true
})
 
export default router