import { createRouter, createWebHistory, type RouteRecordRaw, } from "vue-router"; import Home from "@/views/index.vue"; import Management from "@/views/management.vue"; import Command from "@/views/command.vue"; import ChatBI from "@/views/chatbi.vue"; import Login from "@/views/login.vue"; import { getAccessToken } from "@utils/auth"; import { socialLogin } from "@/api/user"; import * as authUtil from "@/utils/auth"; const routes: RouteRecordRaw[] = [ { path: "/", name: "Home", component: Home, }, { path: "/login", name: "Login", component: Login, }, { path: "/management", name: "Management", component: Management, }, { path: "/command", name: "Command", component: Command, }, { path: "/chatbi", name: "ChatBI", component: ChatBI, }, ]; const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), routes, scrollBehavior(to, from, savePosition) { // 始终滚动到顶部 return { left: 0, top: 0 }; }, }); export const resetRouter = (): void => { const resetWhiteNameList = ["Redirect", "Login", "NoFind", "Root"]; router.getRoutes().forEach((route) => { const { name } = route; if (name && !resetWhiteNameList.includes(name as string)) { router.hasRoute(name) && router.removeRoute(name); } }); }; const whiteList = ["/login", "/social-login", "/auth-redirect"]; router.beforeEach(async (to, from, next) => { if (getAccessToken()) { if (to.path === "/login") { next({ path: "/" }); } else { next(); } } else { if (whiteList.indexOf(to.path) !== -1) { // const code = to.query.code; // if (code) { // const res = await socialLogin( // "20", // typeof code === "string" ? code : "", // "22" // ); // console.log(">>>>>>>>>>>>>>>>>>>>>>>>>", res); // authUtil.setToken(res); // next({ path: "/" }); // } else { // next(); // 正常导航 // } next(); } else { next(`/login`); } } }); export default router;