2018-01-14 22:51:12 +08:00
|
|
|
import Vue from 'vue'
|
|
|
|
|
import VueRouter from 'vue-router'
|
2018-01-27 10:25:59 +08:00
|
|
|
import Cookies from 'js-cookie'
|
2018-01-14 22:51:12 +08:00
|
|
|
|
2018-06-06 12:26:44 +08:00
|
|
|
import pathPosix from 'path-posix'
|
2018-06-05 16:21:06 +08:00
|
|
|
import _get from 'lodash.get'
|
2018-06-06 00:33:59 +08:00
|
|
|
|
2018-06-05 16:21:06 +08:00
|
|
|
Vue.use(VueRouter)
|
2018-06-05 15:33:21 +08:00
|
|
|
|
2018-06-06 08:30:23 +08:00
|
|
|
const routesMaker = ({publicPath, namePrefix, req}) => {
|
2018-06-06 16:30:15 +08:00
|
|
|
const win2posix = pathString => pathString.split('\\').join('/')
|
2018-06-05 16:21:06 +08:00
|
|
|
return req.keys().map(req).map(page => {
|
2018-06-06 16:59:50 +08:00
|
|
|
// 每个文件的路径 已经转换为 posix
|
2018-06-06 12:55:28 +08:00
|
|
|
const pagePath = pathPosix.dirname(win2posix(page.default.__file))
|
2018-06-06 12:23:58 +08:00
|
|
|
// 路由中使用的路径
|
2018-06-06 12:55:28 +08:00
|
|
|
const path = pagePath
|
|
|
|
|
.replace(win2posix(publicPath), '')
|
2018-06-06 16:59:50 +08:00
|
|
|
.replace(new RegExp('/page/', 'g'), '/')
|
|
|
|
|
const name = `${namePrefix}${path.split('/').join('-')}`
|
|
|
|
|
const route = {
|
2018-06-05 16:21:06 +08:00
|
|
|
path: `${path}${_get(page, 'router.pathSuffix', '')}`,
|
|
|
|
|
name,
|
|
|
|
|
...page.router,
|
2018-06-05 15:22:20 +08:00
|
|
|
meta: { requiresAuth: true },
|
2018-06-05 16:21:06 +08:00
|
|
|
component: page.default
|
2018-06-05 15:02:34 +08:00
|
|
|
}
|
2018-06-06 16:59:50 +08:00
|
|
|
console.log(JSON.stringify({
|
|
|
|
|
path: route.path,
|
|
|
|
|
name: route.name
|
|
|
|
|
}))
|
|
|
|
|
return route
|
2018-06-05 16:21:06 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const routes = [
|
|
|
|
|
// 首页
|
|
|
|
|
{
|
|
|
|
|
path: '/',
|
|
|
|
|
redirect: { name: 'index' },
|
|
|
|
|
component: resolve => { require(['@/components/core/MainLayout/index.vue'], resolve) },
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
path: 'index',
|
|
|
|
|
name: 'index',
|
|
|
|
|
meta: { requiresAuth: true },
|
|
|
|
|
component: resolve => { require(['@/pages/core/index/index.vue'], resolve) }
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: '/demo/plugins',
|
|
|
|
|
name: 'demo-plugins',
|
|
|
|
|
meta: { requiresAuth: true },
|
|
|
|
|
redirect: { name: 'demo-plugins-index' },
|
|
|
|
|
component: resolve => { require(['@/components/core/MainLayout/index.vue'], resolve) },
|
2018-06-06 08:30:23 +08:00
|
|
|
children: routesMaker({
|
2018-06-05 16:43:16 +08:00
|
|
|
publicPath: 'src/pages/demo/plugins/',
|
|
|
|
|
namePrefix: 'demo-plugins-',
|
|
|
|
|
req: require.context('@/pages/demo/plugins', true, /page\.vue$/)
|
|
|
|
|
})
|
2018-06-05 16:21:06 +08:00
|
|
|
},
|
|
|
|
|
// 登陆
|
|
|
|
|
{
|
|
|
|
|
path: '/login',
|
|
|
|
|
name: 'login',
|
|
|
|
|
component: resolve => { require(['@/pages/core/login/index.vue'], resolve) }
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
console.log(routes)
|
|
|
|
|
|
|
|
|
|
let router = new VueRouter({
|
|
|
|
|
routes
|
2018-01-14 22:51:12 +08:00
|
|
|
})
|
2018-01-27 10:25:59 +08:00
|
|
|
|
|
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
|
|
// 需要身份校验
|
|
|
|
|
if (to.meta.requiresAuth) {
|
2018-03-21 21:59:10 +08:00
|
|
|
// 这里暂时将cookie里是否存有token作为验证是否登陆的条件
|
|
|
|
|
// 请根据自身业务需要修改
|
2018-01-27 10:25:59 +08:00
|
|
|
if (Cookies.get('token')) {
|
|
|
|
|
next()
|
|
|
|
|
} else {
|
|
|
|
|
// 没有登陆的时候跳转到登陆界面
|
|
|
|
|
next({
|
|
|
|
|
name: 'login'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 不需要身份校验 直接通过
|
|
|
|
|
next()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export default router
|