|
@@ -0,0 +1,82 @@
|
|
|
|
|
+import { createRouter, createWebHistory } from 'vue-router'
|
|
|
|
|
+import { useUserStore } from '@/store/user'
|
|
|
|
|
+import { http } from '@/utils/request'
|
|
|
|
|
+
|
|
|
|
|
+const router = createRouter({
|
|
|
|
|
+ history: createWebHistory(),
|
|
|
|
|
+ routes: [
|
|
|
|
|
+ {
|
|
|
|
|
+ path: '/login',
|
|
|
|
|
+ name: 'Login',
|
|
|
|
|
+ component: () => import('@/views/login/index.vue'),
|
|
|
|
|
+ meta: { public: true }
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ path: '/',
|
|
|
|
|
+ name: 'Layout',
|
|
|
|
|
+ component: () => import('@/views/layout/index.vue'),
|
|
|
|
|
+ redirect: '/dashboard',
|
|
|
|
|
+ children: [
|
|
|
|
|
+ {
|
|
|
|
|
+ path: '/dashboard',
|
|
|
|
|
+ name: 'Dashboard',
|
|
|
|
|
+ component: () => import('@/views/dashboard/index.vue'),
|
|
|
|
|
+ meta: { title: '仪表盘' }
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ path: '/user',
|
|
|
|
|
+ name: 'User',
|
|
|
|
|
+ component: () => import('@/views/user/index.vue'),
|
|
|
|
|
+ meta: { title: '用户管理' }
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ path: '/system/menu',
|
|
|
|
|
+ name: 'Menu',
|
|
|
|
|
+ component: () => import('@/views/system/menu/index.vue'),
|
|
|
|
|
+ meta: { title: '菜单管理' }
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ path: '/system/role',
|
|
|
|
|
+ name: 'Role',
|
|
|
|
|
+ component: () => import('@/views/system/role/index.vue'),
|
|
|
|
|
+ meta: { title: '角色管理' }
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ path: '/statistics',
|
|
|
|
|
+ name: 'Statistics',
|
|
|
|
|
+ component: () => import('@/views/statistics/index.vue'),
|
|
|
|
|
+ meta: { title: '数据统计' }
|
|
|
|
|
+ }
|
|
|
|
|
+ ]
|
|
|
|
|
+ }
|
|
|
|
|
+ ]
|
|
|
|
|
+})
|
|
|
|
|
+
|
|
|
|
|
+// 路由守卫
|
|
|
|
|
+router.beforeEach(async (to, from, next) => {
|
|
|
|
|
+ const userStore = useUserStore()
|
|
|
|
|
+
|
|
|
|
|
+ if (to.meta.public) {
|
|
|
|
|
+ next()
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!userStore.isLoggedIn) {
|
|
|
|
|
+ next('/login')
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 已登录但菜单为空(刷新后),重新获取菜单
|
|
|
|
|
+ if (userStore.getMenus.length === 0) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const menus = await http.get('/upms/menu/list')
|
|
|
|
|
+ userStore.setMenus(menus)
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error('获取菜单失败:', error)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ next()
|
|
|
|
|
+})
|
|
|
|
|
+
|
|
|
|
|
+export default router
|