import Vue from 'vue' import VueRouter from 'vue-router' import index from '../views/Index.vue' import home from "@/components/home/HomePage.vue"; import news from "@/components/hotNews/HotNews.vue"; import navDetail from "@/components/detail/NavDetail.vue"; import notFound from "@/components/notFound/NotFound.vue"; import settings from "@/views/Settings.vue"; import appearanceSettings from "@/components/setting/system/AppearanceSettings.vue"; import informationSettings from "@/components/setting/system/InformationSettings.vue"; import functionSettings from "@/components/setting/system/FunctionSettings.vue"; import systemSettings from "@/components/setting/system/SystemSettings.vue"; import websiteManage from "@/components/setting/website/websiteManage.vue"; import myNav from "@/components/myNav/MyNav.vue"; import personalHomePage from "@/components/setting/home/PersonalHomePage.vue"; import onSiteSetting from "@/components/setting/onsite/OnSiteSetting.vue"; import baseSettings from "@/components/setting/onsite/BaseSettings.vue"; import importExport from "@/components/setting/onsite/ImportExport.vue"; const originalPush = VueRouter.prototype.push VueRouter.prototype.push = function push(location) { return originalPush.call(this, location).catch(err => err) } Vue.use(VueRouter) const routes = [ { path: '/', redirect: '/index' }, { path: '/index', redirect: '/home', name: 'index', component: index, children: [ { path: '/home', name: 'home', component: home }, { path: '/news', name: 'news', component: news },{ path: '/navDetail', name: 'navDetail', component: navDetail }, { path: '/myNav', name: 'myNav', component: myNav } ], }, { path: '/settings', redirect: '/admin', name: '/settings', component: settings, meta:{Authorization:true}, children: [ { path: '/admin', name: '/admin', component: personalHomePage, meta:{Authorization:true}, }, { path: '/system', name: '/system', redirect: '/appearance', component: systemSettings, meta:{Authorization:true}, children:[ { path: '/appearance', name: '/appearance', component: appearanceSettings, meta:{Authorization:true}, }, { path: '/information', name: '/information', component: informationSettings, meta:{Authorization:true}, }, { path: '/function', name: '/function', component: functionSettings, meta:{Authorization:true}, } ] }, { path: '/website', name: '/website', component: websiteManage, meta:{Authorization:true}, }, { path: '/onSite', name: '/onSite', redirect:'/baseSettings', component: onSiteSetting, meta:{Authorization:true}, children:[ { path: '/baseSettings', name: '/baseSettings', component: baseSettings, meta:{Authorization:true}, }, { path: '/importExport', name: '/importExport', component: importExport, meta:{Authorization:true}, }, ] }, ], }, { path: '/404', name: 'NoPage404', component: notFound, hidden: true }, { path: '/:pathMatch(.*)', redirect: '/404', hidden: true } ] const router = new VueRouter({ mode: 'history', routes }) // // 导航守卫:使用 router.beforeEach 注册一个全局前置守卫,判断用户是否登陆 // router.beforeEach((to, from, next) => { // if (to.path === '/home') { // next(); // } else { // let token = localStorage.getItem('Authorization'); // if (token === null || token === '') { // next('/home'); // } else { // next(); // } // } // }); // 全局路由构造函数,判断是否登录和要跳转到页面 router.beforeEach((to, from, next) => { if (to.matched.some(m => m.meta.requiresAuth)) { // 需要登录 if(window.localStorage.Authorization && window.localStorage.isLogin === '1'){ next() } else if (to.path !== '/home') { let token = localStorage.getItem('Authorization'); if (token === 'null' || token === '' || token === undefined){ next({path: '/home'}) // Toast({ message: '检测到您还未登录,请登录后操作!', duration: 1500 }) Vue.prototype.$notify.error({ title: '权限不够', message: "请先登录!", offset: 0 }); } } else { next() } } else { // 不需要登录 next() } }) export default router