From b1ac2da3eca0e412ce9b2b54920b4d41609c3d25 Mon Sep 17 00:00:00 2001 From: ivamp Date: Tue, 5 Dec 2023 20:56:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.ts | 63 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index 84b5dee..8e5d6a1 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,34 +1,67 @@ // Composables -import { createRouter, createWebHistory } from 'vue-router' +import { createRouter, createWebHistory } from "vue-router"; +import { useUserStore } from "@/store/user"; +import { useConfigStore } from "@/store/config"; const routes = [ { - path: '/', - component: () => import('@/layouts/default/Default.vue'), + path: "/", + component: () => import("@/layouts/default/Default.vue"), children: [ { - path: '', - name: 'Home', + path: "", + name: "Home", + meta: { + auth: true, + }, // route level code-splitting // this generates a separate chunk (Home-[hash].js) for this route // which is lazy-loaded when the route is visited. - component: () => import('@/views/Home.vue'), + component: () => import("@/views/Home.vue"), }, { - path: 'auth/login', - name: 'Login', - // route level code-splitting - // this generates a separate chunk (Home-[hash].js) for this route - // which is lazy-loaded when the route is visited. - component: () => import('@/views/auth/Login.vue'), + path: "auth/login", + name: "Login", + component: () => import("@/views/auth/Login.vue"), }, ], }, -] +]; const router = createRouter({ history: createWebHistory(process.env.BASE_URL), routes, -}) +}); -export default router +router.beforeEach((to, from) => { + + // get route name + console.log(from.name, from.fullPath) + + const userStore = useUserStore(); + const configStore = useConfigStore(); + + // if (to.matched.length === 0) { + // return router.push({ name: "errors.404" }); + // } + + if (to.meta.title) { + document.title = to.meta.title + " - " + configStore.getAppName(); + } else { + document.title = configStore.getAppName(); + } + + if (to.meta.auth == true) { + // validate login state + if (userStore.jwt_token == null) { + router.push({ path: "auth/login" }); + } + } else { + document.title = configStore.getAppName(); + } + + return true; + +}); + +export default router;