feat(ui): 新增登录

This commit is contained in:
廖彦棋
2024-03-06 17:54:38 +08:00
parent ba25b8755e
commit d4a24a0f1d
27 changed files with 287 additions and 21 deletions

View File

@@ -1,7 +1,21 @@
import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from "@/stores/auth";
import CustomLayout from '@/components/CustomLayout.vue'
import menu from './menu'
const whiteListRoutes = [
{
path: "/login",
name: "Login",
component: () => import("@/views/LoginView.vue"),
},
{
path: "/:pathMatch(.*)*",
name: "404",
component: () => import("@/views/NotFound.vue"),
},
];
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
@@ -12,12 +26,28 @@ const router = createRouter({
redirect: () => menu[0].path,
children: menu
},
{
path: "/:pathMatch(.*)*",
name: "404",
component: () => import("@/views/NotFound.vue"),
},
...whiteListRoutes
]
})
const whiteList = whiteListRoutes.map((i) => i.name);
router.beforeEach((to, _, next) => {
const authStore = useAuthStore();
authStore.init()
if (typeof to.name === "string" && whiteList.includes(to.name)) {
if (authStore.token && to.name === "Login") {
next({ path: menu[0].path });
return;
}
next();
return;
}
if (!authStore.token) {
next({ name: "Login" });
return;
}
next();
});
export default router