import { createRouter,createWebHistory } from 'vue-router'; //all routes const appAllRoutes = []; const importAllRoutes = (r) => { for(let key in r) { appAllRoutes.push(...r[key].default) } } importAllRoutes(import.meta.glob('./modules/*.js',{ eager: true })) const routes = [ ...appAllRoutes, { name: 'login', path: '/login', component: () => import("@/views/Login.vue"), meta: { title: "登录" }, }, { path: '/temppage', component: ()=>import('@/views/transferPage.vue'), name: 'transferPage', hidden: true }, { name: '404', path: '/404', component: () => import("@/views/404.vue"), meta: { title: "404" }, }, { path: '', redirect: { path: '/dashboard' }, }, { path: '/:pathMatch(.*)', redirect: { path: '/404' }, }, ] export const router = createRouter({ history: createWebHistory(import.meta.env.VITE_APP_BASE_URL), routes }); router.beforeEach(async(to, from, next) => { let auth = localStorage.getItem("auth") || false; if (to.path != "/login" && to.path!='/temppage' &&to.path!='/fogetpassword' && !auth) { console.log(1111111) window.location.href = window.location.origin + '/login'; return false; } if (to.path) { //百度统计 if (window._hmt) { window._hmt.push(["_trackPageview", "/#" + to.fullPath]); } next(); } else { next({ path: "/404" }); } }); router.afterEach((to, from, next) => { // 页面标题 document.title = to.matched[to.matched.length-1].meta.title ? `弘则-${to.matched[to.matched.length-1].meta.title}`:'弘则管理后台' window.scrollTo(0, 0); }); export function initRouter(app) { app.use(router) }