mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-11-13 12:43:43 +08:00
feat(ui): 新增登录
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user