Browse Source

修复刷新后菜单请求路径错误

路由守卫中刷新后重新获取菜单的 API 路径
缺少 /upms 前缀,导致代理模式下请求失败。

- router/index.ts: /menu/list → /upms/menu/list

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
tanlie 3 tuần trước cách đây
mục cha
commit
c6542ec6bd
1 tập tin đã thay đổi với 82 bổ sung0 xóa
  1. 82 0
      src/router/index.ts

+ 82 - 0
src/router/index.ts

@@ -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