2018-06-30 11:36:06 +08:00
|
|
|
import Vue from 'vue'
|
|
|
|
|
import VueRouter from 'vue-router'
|
|
|
|
|
import Cookies from 'js-cookie'
|
|
|
|
|
|
2018-06-30 15:42:04 +08:00
|
|
|
import util from '@/libs/util.js'
|
|
|
|
|
|
2018-06-30 11:36:54 +08:00
|
|
|
// 路由数据
|
2018-06-30 14:29:11 +08:00
|
|
|
import routes from './routes'
|
2018-06-30 11:36:06 +08:00
|
|
|
|
|
|
|
|
Vue.use(VueRouter)
|
|
|
|
|
|
2018-06-30 14:29:11 +08:00
|
|
|
let router = new VueRouter({ routes })
|
2018-06-30 11:36:06 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 路由拦截
|
|
|
|
|
* 权限验证
|
|
|
|
|
*/
|
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
|
|
// 验证当前路由所有的匹配中是否需要有登陆验证的
|
|
|
|
|
if (to.matched.some(r => r.meta.requiresAuth)) {
|
|
|
|
|
// 这里暂时将cookie里是否存有token作为验证是否登陆的条件
|
|
|
|
|
// 请根据自身业务需要修改
|
|
|
|
|
if (Cookies.get('token')) {
|
|
|
|
|
next()
|
|
|
|
|
} else {
|
|
|
|
|
// 没有登陆的时候跳转到登陆界面
|
|
|
|
|
next({
|
|
|
|
|
name: 'login'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 不需要身份校验 直接通过
|
|
|
|
|
next()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.afterEach(to => {
|
|
|
|
|
// 需要的信息
|
|
|
|
|
const app = router.app
|
|
|
|
|
const { name, params, query } = to
|
|
|
|
|
// 多页控制 打开新的页面
|
2018-07-15 22:37:47 +08:00
|
|
|
app.$store.commit('d2adminPageOpenNew', { name, params, query })
|
2018-07-02 09:59:39 +08:00
|
|
|
// 更改标题
|
|
|
|
|
util.title(to.meta.title)
|
2018-06-30 11:36:06 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export default router
|