import Vue from 'vue'
import Router from 'vue-router'
import { isURL } from '@/utils/validate'
import loginHttp from '@/utils/loginRequest'
Vue.use(Router)
export const pageRoutes = [
{
path: '/404',
component: () => import('@/views/pages/404'),
name: '404',
meta: { title: '404未找到' },
beforeEnter(to, from, next) {
if (/__.*/.test(to.redirectedFrom) {
return next(to.redirectedFrom.replace(/__.*/, '')
}
next()
}
},
{ path: '/login', component: () => import('@/views/pages/login'), name: 'login', meta: { title: '登录' } }
]
export const moduleRoutes = {
path: '/',
component: () => import('@/views/main'),
name: 'main',
redirect: { name: 'home' },
meta: { title: '主入口布局' },
children: [
{ path: '/home', component: () => import('@/views/modules/home'), name: 'home', meta: { title: '首页', isTab: true } }
]
}
export function addDynamicRoute(routeParams, router) {
console.log('addDynamicRoute')
var routeName = routeParams.routeName
var dynamicRoute = window.SITE_CONFIG.dynamicRoutes.filter(item => item.name === routeName)[0]
if (dynamicRoute) {
return router.push({ name: routeName, params: routeParams.params })
}
dynamicRoute = {
path: routeName,
component: () => import(`@/views/modules/${routeParams.path}`),
name: routeName,
meta: {
...window.SITE_CONFIG.contentTabDefault,
menuId: routeParams.menuId,
title: `${routeParams.title}`
}
}
router.addRoutes([
{
...moduleRoutes,
name: `main-dynamic__${dynamicRoute.name}`,
children: [dynamicRoute]
}
])
window.SITE_CONFIG.dynamicRoutes.push(dynamicRoute)
router.push({ name: dynamicRoute.name, params: routeParams.params })
}
const router = new Router({
mode: 'hash',
scrollBehavior: () => ({ y: 0 }),
routes: pageRoutes.concat(moduleRoutes)
})
router.beforeEach((to, from, next) => {
if (window.SITE_CONFIG.dynamicMenuRoutesHasAdded || fnCurrentRouteIsPageRoute(to, pageRoutes) {
return next()
}
loginHttp.get('/menu/nav/' + window.SITE_CONFIG.sysId).then(({ data: res }) => {
console.log(res, '菜单');
if (res.code !== 0) {
Vue.prototype.$message.error(res.msg)
return next({ name: 'login' })
}
window.SITE_CONFIG.menuList = res.data
fnAddDynamicMenuRoutes(window.SITE_CONFIG.menuList)
try {
next({ ...to, replace: true })
} catch (e) {
}
}).catch(() => {
next({ name: 'login' })
})
})
function fnCurrentRouteIsPageRoute(route, pageRoutes = []) {
var temp = []
for (var i = 0; i < pageRoutes.length; i++) {
if (route.path === pageRoutes[i].path) {
return true
}
if (pageRoutes[i].children && pageRoutes[i].children.length >= 1) {
temp = temp.concat(pageRoutes[i].children)
}
}
return temp.length >= 1 ? fnCurrentRouteIsPageRoute(route, temp) : false
}
function fnAddDynamicMenuRoutes(menuList = [], routes = []) {
var temp = []
for (var i = 0; i < menuList.length; i++) {
if (menuList[i].children && menuList[i].children.length >= 1) {
temp = temp.concat(menuList[i].children)
continue
}
var route = {
path: '',
component: null,
name: '',
meta: {
...window.SITE_CONFIG.contentTabDefault,
menuId: menuList[i].id,
title: menuList[i].name
}
}
let URL = (menuList[i].url || '').replace(/{{([^}}]+)?}}/g, (s1, s2) => eval(s2)
if (isURL(URL) {
console.log('本身就是链接')
route.path = route.name = `i-${menuList[i].id}`
route.meta.iframeURL = URL
} else {
console.log('本身是组件')
URL = URL.replace(/^\//, '').replace(/_/g, '/')
route.path = route.name = URL.replace(/\//g, '/')
route.component = () => import(`@/views/modules/${URL}`)
}
routes.push(route)
}
if (temp.length >= 1) {
return fnAddDynamicMenuRoutes(temp, routes)
}
router.addRoutes([
{
...moduleRoutes,
name: 'main-dynamic-menu',
children: routes
},
{ path: '*', redirect: { name: '404' } }
])
window.SITE_CONFIG.dynamicMenuRoutes = routes
window.SITE_CONFIG.dynamicMenuRoutesHasAdded = true
}
export default router