Files
lucia-frontend/src/router/index.js
2023-10-13 17:58:47 +08:00

96 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { createRouter, createWebHistory, createWebHashHistory } from "vue-router";
import AuthContainer from '@/views/AuthContainer.vue';
import MainContainer from '@/views/MainContainer.vue';
import Login from '@/views/Login/index.vue';
import Files from '@/views/Files/index.vue';
import Map from '@/views/Discover/Map/index.vue';
import Conformance from '@/views/Discover/Conformance/index.vue';
import MemberArea from '@/views/MemberArea/index.vue';
import NotFound404 from '@/views/NotFound404.vue';
const routes = [
{
path: '/', // 預設進入路由
redirect: '/files', //重定向
},
{
path: '/',
name: "AuthContainer",
component: AuthContainer,
children: [
{
path: "/login",
name: "Login",
component: Login,
},
]
},
{
path: "/",
name: "MainContainer",
component: MainContainer,
meta: {
title: "MainContainer",
requiresAuth: true
},
children: [
{
path: "/member-area",
name: "MemberArea",
component: MemberArea,
},
{
path: "/files",
name: "Files",
component: Files,
},
{
path: "/discover",
name: "Discover",
children: [
{
path: "/discover/map/:type/:fileId",
name: "Map",
component: Map,
},
{
path: "/discover/conformance/:type/:fileId",
name: "Conformance",
component: Conformance,
},
]
},
]
},
{
path: "/:pathMatch(.*)*",
name: "NotFound404",
component: NotFound404,
},
];
const base_url = import.meta.env.BASE_URL;
const router = createRouter({
history: createWebHistory(base_url), //(/)
// history: createWebHashHistory(base_url), // (/#)
routes
});
// 全域性路由守衛
router.beforeEach((to, from) => {
// to: Route: 即將要進入的目標 路由物件
// from: Route: 當前導航正要離開的路由
let isCookie = document.cookie.split(';').some(c => c.trim().startsWith('luciaToken=')); // 是否登入
// 需要驗證登入的頁面,判斷有無登入,無登入要轉址 login
if(to.meta.requiresAuth) {
if (!isCookie) router.push({ name: 'Login' });
}
// 當路由到 login 時有登入要跳轉至home
if (to.name === 'Login') {
if (isCookie) router.push({ name: 'Files' });
}
});
export default router;