diff --git a/public/logo.png b/public/logo.png new file mode 100644 index 0000000..70b9ec9 Binary files /dev/null and b/public/logo.png differ diff --git a/src/main.js b/src/main.js index 8787a83..9d7b3b4 100644 --- a/src/main.js +++ b/src/main.js @@ -1,8 +1,6 @@ import './assets/main.css' - import { createApp } from 'vue' import { createPinia } from 'pinia' - import App from './App.vue' import router from './router' import 'amfe-flexible' @@ -11,5 +9,4 @@ const app = createApp(App) app.use(createPinia()) app.use(router) - app.mount('#app') diff --git a/src/router/index.js b/src/router/index.js index 7f32152..74c5af0 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,19 +1,68 @@ import { createRouter, createWebHistory } from 'vue-router' +// 静态路由(公共页面) +const constantRoutes = [ + { + path: '/', + redirect: '/login' + }, + { + path: '/login', + name: 'login', + component: () => import('../views/login/index.vue'), + }, + { + path: '/404', + name: 'NotFound', + component: () => import('../views/error/404.vue'), + }, +]; + const router = createRouter({ - history: createWebHistory(import.meta.env.BASE_URL), - routes: [ - { - path: '/', - name: 'login', - component: () => import('../views/login/index.vue'), + history: createWebHistory(), + routes: constantRoutes, +}) + +// 动态路由(需要权限的页面) +// 注意:这些路由不会在一开始注册,而是在守卫中动态添加 +const asyncRoutes = [ + { + path: '/applyList', + name: 'applyList', + component: () => import('../views/applyList/index.vue'), + meta: { + title: '申请列表', }, - { - path: '/applyList', - name: 'applyList', - component: () => import('../views/applyList/index.vue'), - }, - ], + }, +] + +// 标志位:是否已添加动态路由 +let isRoutesAdded = false +router.beforeEach((to, from, next) => { + const token = localStorage.getItem('token') + if (token) { + // 关键:有 token 但业务路由还没挂载 => 动态添加(仅一次) + if (!isRoutesAdded) { + asyncRoutes.forEach(route => { + router.addRoute(route) + }) + // 必须加载最后面 + router.addRoute({ + path: '/:pathMatch(.*)*', + redirect: '/404' + }); + isRoutesAdded = true + next({ ...to }) + } else { + next() + } + } else { + if (to.path === '/login' || to.path === '/404') { + next() + } else { + next({ path: '/login' }) + } + } }) export default router diff --git a/src/views/error/404.vue b/src/views/error/404.vue new file mode 100644 index 0000000..128ddc2 --- /dev/null +++ b/src/views/error/404.vue @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/src/views/login/index.vue b/src/views/login/index.vue index c5554a1..30dd353 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -1,12 +1,95 @@ +